Load packages into tag_packages table

Written at: Feb 6, 2015

This script was written for loading packages and related tags and owners into tag_packages table in koji database.

#!/bin/sh

function koji_get_number {
    n=$(echo "$@" | psql koji | sed "s/ \+//" | grep "^[0-9]\+$")
    echo $n
}


setfile=/tmp/koji-setfile.txt
test -e "$setfile" && rm "$setfile"
touch "$setfile"
function find_package_tag_active {
    package_id="$1"
    tag_id="$2"
    active="$3"

    search_key="${package_id},${tag_id},${active}"
    grep "^$search_key$" "$setfile" > /dev/null
    if [ $? == 0 ]; then
        ret="yes"
    else
        echo "$search_key" >> "$setfile"
        ret="no"
    fi

    echo $ret
}


sqlfile=/tmp/koji-sql.txt
test -e $sqlfile && rm $sqlfile
touch $sqlfile
last_pkg_name=
last_tag_name=
last_owner=

echo "BEGIN;" >> "$sqlfile"
cat brew-pkgs.txt | while read pkg_name tag_name owner; do
    if [ "x$last_pkg_name" != "x$pkg_name" ]; then
        package_id=$(koji_get_number "SELECT id FROM package WHERE name = '${pkg_name}'")
        last_pkg_name=$pkg_name
    fi

    if [ "x$last_tag_name" != "x$tag_name" ]; then
        tag_id=$(koji_get_number "SELECT id FROM tag WHERE name = '${tag_name}'")
        last_tag_name=$tag_name
    fi

    if [ "x$last_owner" != "x$owner" ]; then
        user_id=$(koji_get_number "SELECT id FROM users WHERE name = '${owner}'")
        last_owner=$owner
    fi

    creator_id=$user_id
    active="true"

    ret=$(find_package_tag_active "$package_id $tag_id $active")
    if [ "x$ret" == "xyes" ]; then
        active="false"
    fi

    echo "INSERT INTO tag_packages (package_id, tag_id, owner, blocked, creator_id, active) VALUES (${package_id}, ${tag_id}, ${user_id}, false, ${creator_id}, ${active});" >> $sqlfile
done
echo "COMMIT;" >> "$sqlfile"

Following command is used to generate brew-pkgs.txt

filename=/tmp/pkgs.txt
touch $filename
tag_names=
for tag_name in $tag_names; do
    koji list-pkgs --tag=tag_name --quiet | sed 's/ \+/ /g' >> $filename
done