Sort sql.qrc when generating, emphasize script
authorShane Synan <digitalcircuit36939@gmail.com>
Fri, 6 Jan 2017 20:07:20 +0000 (14:07 -0600)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 13 Apr 2017 19:39:15 +0000 (21:39 +0200)
Modify the update SQL resource script to version-sort output and
batch append it to the sql.qrc file.  This improves readability in
'git diff' output, and might slightly improve performance, too.

Move UPDATE_SQL_RESOURCE.sh to SQL/updateSQLResource.sh, making it
easier to find, right next to the other SQL management script.  This
required a few changes to the logic.

Document the new behavior in upgradeSchema.sh, keep the manual
instructions in case the script doesn't work.

Comments, comments everywhere.

(I really wish I would've known about this script earlier...)

Resolves GH-268.

src/core/SQL/updateSQLResource.sh [new file with mode: 0755]
src/core/SQL/upgradeSchema.sh
src/core/UPDATE_SQL_RESOURCE.sh [deleted file]

diff --git a/src/core/SQL/updateSQLResource.sh b/src/core/SQL/updateSQLResource.sh
new file mode 100755 (executable)
index 0000000..e5c99e4
--- /dev/null
@@ -0,0 +1,49 @@
+#!/bin/bash
+# Copyright (C) 2005-2016 by the Quassel Project - devel@quassel-irc.org
+# Licensed under GNU General Public License version 2, or (at your option)
+# version 3.
+#
+# Call this script whenever you move or rename the SQL files inside this
+# folder.  It'll regenerate 'sql.qrc' for you.  If it fails, you can manually
+# edit the file, too; just follow the pattern already in there.
+#
+# NOTE: In most cases you must upgrade the database schema version to make a
+# change, which includes adding upgrade scripts and modifying existing setup
+# queries.  See 'upgradeSchema.sh' for details.
+
+set -u # Bash - fail on undefined variable
+
+# Path to the directory containing this script, resolving any symlinks, etc
+SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
+
+# Name of the current directory
+SQL_DIR_NAME="${SCRIPT_DIR##*/}"
+# See https://stackoverflow.com/questions/1371261/get-current-directory-name-without-full-path-in-bash-script/1371283#1371283
+
+# SQL resource file (one directory up from this script)
+SQL_RES_FILE="$SCRIPT_DIR/../sql.qrc"
+
+# Add the Qt resource header, overwriting the existing file
+cat > "$SQL_RES_FILE" <<EOF
+<!DOCTYPE RCC><RCC version="1.0">
+<qresource>
+EOF
+
+# In a subshell, change to the SQL directory so find output remains consistent,
+# then...
+# Find all files with a .sql ending...
+# | ...sort the results by version
+# | ...modify the beginning to change '.[...]' into '    <file>./SQL[...]' (add the directory path back)
+#   ...and modify the end to add '[...]</file>'
+# | ...append to the resource file.
+(cd "$SCRIPT_DIR" ; find . -name "*.sql" | sort -t/ -k2,2 -k3n | sed -e "s/^./    <file>.\/$SQL_DIR_NAME/g" -e "s/$/<\/file>/g" >> "$SQL_RES_FILE" )
+# Newer versions of sort support a "--version-sort" flag to naturally sort.
+# Unfortunately, some supported platforms don't yet have that version.
+# For now, "-t/ -k2,2 -k3n" does -almost- the same thing (there's a difference
+# when sorting periods versus underscores).
+
+# Add the Qt resource footer
+cat >> "$SQL_RES_FILE" <<EOF
+</qresource>
+</RCC>
+EOF
index 21b22b2..2f30a11 100755 (executable)
 # > Modify 'SQLite/##/migrate_read_ircserver.sql' to select from new column
 # > Modify 'PostgreSQL/##/migrate_write_ircserver.sql' to insert to new column
 #
-# 5.  Add the new SQL queries to 'src/core/sql.qrc', update all existing files
+# 5.  Update the SQL resource file; re-run CMake if needed
 #
-# You may need to re-run CMake to detect these changes.
+# The easy way: run "updateSQLResource.sh" in this directory.
+#
+# The manual way:
+# Add the new SQL queries to 'src/core/sql.qrc', update all existing files.
 #
 # [Example] Modifying the 'ircserver' table to add column 'test'
 # > Add the new upgrade script...
diff --git a/src/core/UPDATE_SQL_RESOURCE.sh b/src/core/UPDATE_SQL_RESOURCE.sh
deleted file mode 100755 (executable)
index 17d10f3..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-#!/bin/bash
-
-cat > sql.qrc <<EOF
-<!DOCTYPE RCC><RCC version="1.0">
-<qresource>
-EOF
-find . -name \*.sql -exec echo "    <file>{}</file>" \; >> sql.qrc
-cat >> sql.qrc <<EOF
-</qresource>
-</RCC>
-EOF