Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorErich Eckner <git@eckner.net>2018-09-01 19:43:00 +0200
committerErich Eckner <git@eckner.net>2018-09-01 19:43:00 +0200
commit5e08f8967397fb981734eaaf0f179676b9e139a9 (patch)
tree3d11bd3b34b26782ab19bfb57a6eac5e73f7f855 /lib
parent20bdaa1c8b77da6e82364d39daf9ece009bec4e4 (diff)
lib/mysql-functions: mysql_sort_versions new
Diffstat (limited to 'lib')
-rwxr-xr-xlib/mysql-functions59
1 files changed, 59 insertions, 0 deletions
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
+ )
+}