reduced log
[brisk.git] / sql / builder.sh
index 83843d3..db1dcd1 100755 (executable)
@@ -1,11 +1,16 @@
 #! /bin/bash
-
+# set -x
 #
+MATCH_DROP='^DROP.*([^-]...|.[^-]..|..[^M].|...[^F])$|^ALTER TABLE.* DROP .*([^-]...|.[^-]..|..[^M].|...[^F])$|^DELETE .*([^-]...|.[^-]..|..[^M].|...[^F])$|--MB$'
+
+DATECUR="$(date +%s)"
+
 #  functions
 usage () {
+    local _v
     echo " USAGE"
-    echo "   ./builder <command> [-d|--dryrun] [-a|--allfiles] [-s|--short] ..."
-    echo "   ./builder <-h|--help|help>"
+    echo "   $0 <command> [-c|--config <override_file>] [-d|--dryrun] [<-a|--allfiles>|<-p|--devfiles>] [-s|--short] ..."
+    echo "   $0 [-c|--config <override_file>] <-h|--help|help>"
     echo "   commands are:"
     echo "       create"
     echo "       destroy"
@@ -16,9 +21,25 @@ usage () {
     echo "       piped"
     echo "       add <filesql> [<filesql2> [..]]"
     echo "       del <filesql> [<filesql2> [..]]"
+    echo "       res <filesql> [<filesql2> [..]]"
     echo "       dump [dumpfile]"
     echo "       dumpall [dumpfile]"
     echo "       all"
+    echo
+    echo "The match rule for clean lines is:"
+    echo "  [$MATCH_DROP]"
+    echo "NOTE: to invert normal 'del' rules add '--MF' (move forward) suffix to each line"
+    echo "      to invert normal 'add' rules add '--MB' (move backward) suffix to each line"
+    echo
+    echo "ENVIRONMENT"
+    for _v in \
+apache_conf card_hand players_n tables_n tables_appr_n tables_auth_n tables_cert_n \
+brisk_auth_conf brisk_debug web_path ftok_path proxy_path legal_path prefix_path \
+brisk_conf usock_path sys_user web_only test_add \
+DBHOST DBUSER DBPORT DBBASE DBPASS PFX ; do
+        echo "    $_v=${!_v}"
+    done
+
     exit $1
 }
 
@@ -27,37 +48,50 @@ sqlexe () {
     sht=$1
 
     if [ "$SHORT" = "y" ];  then
-        sed "s/#PFX#/$PFX/g" | psql -a $pg_args 2>&1 | egrep 'ERROR|^-- MESG'
+        sed "s/#PFX#/$PFX/g;s/#NOW#/$DATECUR/g" | psql -a $pg_args 2>&1 | egrep 'ERROR|^-- MESG|^-- FILE '
     else
-        sed "s/#PFX#/$PFX/g" | psql -a $pg_args
+        sed "s/#PFX#/$PFX/g;s/#NOW#/$DATECUR/g" | psql -a $pg_args
     fi
 
     return 0
 }
 
 one_or_all() {
-    if [ "$ALL_FILES" = "y" ]; then
-        sfx_files='*'
-    else
-        sfx_files='*.sql'
-    fi
-
-    if [ "$1" = "" ]; then
-        cat sql.d/$sfx_files
-    else
-        cat "$1"
-    fi
+    local old_ifs
+
+    old_ifs="$IFS"
+    IFS=" "
+    for fil in $(
+        if [ "$1" ]; then
+            echo "$1"
+        elif [ "$TYPE_FILES" = "a" ]; then
+            find sql.d/ -name '[0-9]*' -type f | sort -n | tr '\n' ' '
+        elif [ "$TYPE_FILES" = "d" ]; then
+            find sql.d/ -name '[0-9]*' | egrep '.*_devel.sql$|^[^_]+.sql$' | sort -n | tr '\n' ' '
+        else
+            find sql.d/ -name '[0-9]*' | egrep '^[^_]+.sql$' | sort -n | tr '\n' ' '
+        fi); do
+        echo "-- FILE BEG: $fil"
+        cat "$fil"
+        echo "-- FILE END: $fil"
+    done
+    IFS="$old_ifs"
 }
 
 #
 #  MAIN
 #
-
-CMD=$1
-shift
+if [ "${1:0:1}" != "-" ]; then
+    CMD=$1
+    shift
+fi
 
 while [ $# -gt 0 ]; do
     case $1 in
+        -c|--config)
+            CONFIG_FILE=$2
+            shift
+            ;;
         -d|--dryrun)
             DRY_RUN=y
             psql () {
@@ -66,11 +100,17 @@ while [ $# -gt 0 ]; do
             }
             ;;
         -a|--allfiles)
-            ALL_FILES=y
+            TYPE_FILES=a
+            ;;
+        -p|--devfiles)
+            TYPE_FILES=d
             ;;
         -s|--short)
             SHORT=y
             ;;
+        -h|--help|help)
+            CMD=help
+            ;;
         *)
             break
             ;;
@@ -95,13 +135,16 @@ if [ -f $HOME/.brisk_install ]; then
     source $HOME/.brisk_install
 fi
 
+if [ -f "$CONFIG_FILE" ]; then
+    source "$CONFIG_FILE"
+fi
+
 pg_args=""
 test "$DBHOST" != "" && pg_args="$pg_args -h $DBHOST"
 test "$DBUSER" != "" && pg_args="$pg_args -U $DBUSER"
 test "$DBPORT" != "" && pg_args="$pg_args -p $DBPORT"
 test "$DBBASE" != "" && pg_args="$pg_args $DBBASE"
 
-MATCH_DROP='^drop|^alter table.* drop '
 
 case $CMD in
     "create")
@@ -114,14 +157,14 @@ case $CMD in
         su root -c "su postgres -c \"dropdb $DBBASE && dropuser $DBUSER\""
         ;;
     "clean")
-        ( echo "-- MESG: clean start" ; one_or_all $2 | egrep -i "$MATCH_DROP" | tac ; echo "-- MESG: clean end" ;   ) | sqlexe
+        ( echo "-- MESG: clean start" ; one_or_all $2 | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; echo "-- MESG: clean end" ;   ) | sqlexe
         ;;
     "build")
-        ( echo "-- MESG: build start" ; one_or_all $2 | egrep -iv "$MATCH_DROP" ; echo "-- MESG: build end" ;   ) | sqlexe
+        ( echo "-- MESG: build start" ; one_or_all $2 | egrep -v "$MATCH_DROP" ; echo "-- MESG: build end" ;   ) | sqlexe
         ;;
     "rebuild")
-        ( echo "-- MESG: clean start" ; one_or_all $2 | egrep -i "$MATCH_DROP" | tac ; echo "-- MESG: clean end" ; \
-            echo "-- MESG: build start" ; one_or_all $2 | egrep -iv "$MATCH_DROP" ; echo "-- MESG: build end" ;   ) \
+        ( echo "-- MESG: clean start" ; one_or_all $2 | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; echo "-- MESG: clean end" ; \
+            echo "-- MESG: build start" ; one_or_all $2 | egrep -v "$MATCH_DROP" ; echo "-- MESG: build end" ;   ) \
             | sqlexe
         ;;
     "psql")
@@ -146,10 +189,13 @@ case $CMD in
         fi
         ;;
     "add")
-        ( echo "-- MESG: add start" ; cat "$@" | egrep -iv "$MATCH_DROP" ; echo "-- MESG: add end" ;   ) | sqlexe
+        ( echo "-- MESG: add start" ; cat "$@" | egrep -v "$MATCH_DROP" ; echo "-- MESG: add end" ;   ) | sqlexe
         ;;
     "del")
-        ( echo "-- MESG: del start" ; cat "$@" | egrep -i  "$MATCH_DROP" | tac ; echo "-- MESG: del end" ;   ) | sqlexe
+        ( echo "-- MESG: del start" ; cat "$@" | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; echo "-- MESG: del end" ;   ) | sqlexe
+        ;;
+    "res")
+        ( echo "-- MESG: res start" ; cat "$@" | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; cat "$@" | egrep -v "$MATCH_DROP" ; echo "-- MESG: del end" ;   ) | sqlexe
         ;;
     "help"|"-h"|"--help")
         usage 0