JBAS-255 schema exporter script
authorBill Erickson <berickxx@gmail.com>
Thu, 8 Jan 2015 16:12:20 +0000 (08:12 -0800)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/sql/dev-data/tools/export-db-schema.sh [new file with mode: 0755]

diff --git a/KCLS/sql/dev-data/tools/export-db-schema.sh b/KCLS/sql/dev-data/tools/export-db-schema.sh
new file mode 100755 (executable)
index 0000000..5b3342c
--- /dev/null
@@ -0,0 +1,53 @@
+#!/bin/bash
+# -------------------------------------------------------------------
+# KCLS database schema exporter.
+# Author: Bill Erickson
+#
+# Exports the full 'evergreen' database schema.
+#
+# Usage:
+# 1. Log on to the database server of choice
+# 2. ./export-db-schema.sh
+#
+# This will create one file in the working directory.
+# -------------------------------------------------------------------
+
+pg_dump --schema-only -U evergreen evergreen > tmp-schema.sql
+
+echo "
+
+-- Generated $(date +'%F %T') on $(hostname)
+
+-- These custom users may already exist... errors are OK.
+
+CREATE USER bbonner;
+CREATE USER biblio;
+CREATE USER kclsreporter;
+CREATE USER kclsreporter2;
+
+BEGIN;
+
+\set ON_ERROR_STOP on                                                         
+
+" > tmp-schema2.sql
+
+cat tmp-schema.sql >> tmp-schema2.sql
+
+echo -e "\nCOMMIT;\n" >> tmp-schema2.sql
+
+# we have to comment out the pg_prewarm stuff, since 
+# we're not installing that extension on new databases.
+
+line1=$(grep -n -m1 pg_prewarm tmp-schema2.sql)
+pos1=$(echo "$line1" | cut -d':' -f1)
+pos1=$((($pos1 - 1)))
+sed "${pos1}i/*" tmp-schema2.sql > tmp-schema3.sql
+
+line2=$(grep -n  pg_prewarm tmp-schema3.sql | tail -n1)
+pos2=$(echo "$line2" | cut -d':' -f1)
+pos2=$((($pos2 + 1)))
+
+sed "${pos2}i*/" tmp-schema3.sql > kcls-eg-db-schema.sql
+
+rm tmp-schema*.sql
+