index : builder | |
Archlinux32 build system | gitolite user |
summaryrefslogtreecommitdiff |
author | Erich Eckner <git@eckner.net> | 2018-09-01 19:43:00 +0200 |
---|---|---|
committer | Erich Eckner <git@eckner.net> | 2018-09-01 19:43:00 +0200 |
commit | 5e08f8967397fb981734eaaf0f179676b9e139a9 (patch) | |
tree | 3d11bd3b34b26782ab19bfb57a6eac5e73f7f855 /lib/mysql-functions | |
parent | 20bdaa1c8b77da6e82364d39daf9ece009bec4e4 (diff) |
-rwxr-xr-x | lib/mysql-functions | 59 |
diff --git a/lib/mysql-functions b/lib/mysql-functions index b6db097..9fe0984 100755 --- a/lib/mysql-functions +++ b/lib/mysql-functions @@ -1178,3 +1178,62 @@ mysql_update_versions_ordering() { mysql_run_query ) } + +# mysql_sort_versions +# sort the table of versions +mysql_sort_versions() { + ( # new shell is intentional + tmp_file=$( + mktemp "tmp.mysql_sort_versions.XXXXXXXXXX" --tmpdir + ) + trap 'rm "${tmp_file}"' EXIT + { + printf 'SELECT ' + printf '`versions`.`%s`,' \ + 'id' \ + 'epoch' \ + 'version' | \ + sed 's/,$//' + printf ' FROM `versions`;\n' + } | \ + mysql_run_query | \ + expand_version 3 | \ + sort -k2n,2 -k3V,3 | \ + cut -f1 | \ + cat -n | \ + awk '{print $1 " " $2}' | \ + tr ' ' '\t' > \ + "${tmp_file}" + max_order=$( + { + printf 'SELECT' + printf ' GREATEST(%s,' \ + "$( + wc -l < "${tmp_file}" + )" + printf 'MAX(`versions`.`order`))' + printf ' FROM `versions`;\n' + } | \ + mysql_run_query + ) + { + printf 'CREATE TEMPORARY TABLE `vs`(' + printf '`id` BIGINT,' + printf '`ord` BIGINT,' + printf 'UNIQUE KEY(`id`),' + printf 'UNIQUE KEY(`ord`)' + printf ');\n' + printf 'LOAD DATA LOCAL INFILE "%s" INTO TABLE `vs`(`ord`,`id`);\n' \ + "${tmp_file}" + printf 'UPDATE `versions`' + printf ' SET `versions`.`order`=`versions`.`order`+1+%s;\n' \ + "${max_order}" + printf 'COMMIT;\n' + printf 'UPDATE `versions`' + printf ' JOIN `vs`' + printf ' ON `vs`.`id`=`versions`.`id`' + printf ' SET `versions`.`order`=`vs`.`ord`;\n' + } | \ + mysql_run_query + ) +} |