JBAS-944 SQL functions comparison tool
authorBill Erickson <berickxx@gmail.com>
Mon, 26 Oct 2015 20:31:51 +0000 (16:31 -0400)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Tool to generate diffs for a set of SQL functions.  Useful for analyzing
SQL upgrade scripts.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/sql/schema/tools/pg-func-diffs.sh [new file with mode: 0755]

diff --git a/KCLS/sql/schema/tools/pg-func-diffs.sh b/KCLS/sql/schema/tools/pg-func-diffs.sh
new file mode 100755 (executable)
index 0000000..3f93377
--- /dev/null
@@ -0,0 +1,41 @@
+#!/bin/bash
+# author Bill Erickson
+# Takes a list of PG function names as a command line param
+# and compares stock functions to KCLS versions of those same
+# functions too see which differ.  Diffs for functions that 
+# differ are placed into the 'diffs' directory.
+# Comparisons are done by extracting function definitions from
+# live database instances.  Change PSQL commands below to suit.
+# TODO: getopts, database names via command line.  more docs, etc.
+FUNCS=$1
+PSQL_STOCK="psql -x -U evergreen -d evergreen"
+PSQL_KCLS="psql -x -U evergreen -d eg_kcls_25"
+
+
+mkdir -p results
+mkdir -p diffs
+while read func; do
+    echo $func;
+    echo "\\df+ $func;" | $PSQL_STOCK | grep -v " RECORD 1 " | \
+        sed -e 's/^\s*|//g' > "results/$func-stock.txt"
+    echo "\\df+ $func;" | $PSQL_KCLS  | grep -v " RECORD 1 " | \
+        sed -e 's/^\s*|//g' | sed -e 's/\\r$//g' > "results/$func-kcls.txt"
+    # KCLS schema has pesky windows carriage returns
+done < $FUNCS
+
+while read func; do
+    echo $func
+
+    diff --unified=20 -Naur "results/$func-stock.txt" \
+        "results/$func-kcls.txt" > "diffs/$func.txt"
+
+    # file size 0 means no diff; kill the file.
+    size=$(stat -c '%s' "diffs/$func.txt");
+    if [ $size == "0" ]; then
+        rm "diffs/$func.txt";
+    fi;
+
+done < $FUNCS
+
+rm -r results;
+