A script for parallelizing db updates, where possible user/miker/parallel-db-upgrade
authorMike Rylander <mrylander@gmail.com>
Wed, 3 Apr 2013 16:43:20 +0000 (12:43 -0400)
committerMike Rylander <mrylander@gmail.com>
Wed, 3 Apr 2013 16:43:20 +0000 (12:43 -0400)
This script takes a file of SQL commands, one complete command per line,
and applies them in parallel via psql.

Caveats:
 * no multi-line commands
 * no protection against foot-guns in the SQL (check for injections!)
 * probably will cause puppies to be eaten

Signed-off-by: Mike Rylander <mrylander@gmail.com>
Open-ILS/src/support-scripts/parallel_db_upgrade.sh [new file with mode: 0644]

diff --git a/Open-ILS/src/support-scripts/parallel_db_upgrade.sh b/Open-ILS/src/support-scripts/parallel_db_upgrade.sh
new file mode 100644 (file)
index 0000000..21fb1a3
--- /dev/null
@@ -0,0 +1,23 @@
+#!/bin/bash
+#
+# $0 db-name file-of-commands
+
+TEST_PSQL=$(PG_DATABASE=$1 psql -tAc 'select 42' 2>/dev/null);
+
+if [ "_$2" == "_" ]; then
+    echo "Usage: $0 db-name file-of-commands"
+    exit 1;
+fi
+
+if [ -z $TEST_PSQL ]; then
+    echo "Can't connect to postgres"
+    exit 1;
+fi
+
+N=0
+cat $2 | while read LINE; do
+    N=$((N+1))
+    (PG_DATABASE=$1 echo $LINE | psql -tA -f -  2>&1 | tee $0.output.$N &)
+done
+
+