more clear sql builder help
[brisk.git] / sql / builder.sh
index 7e021db..b71624d 100755 (executable)
 #! /bin/bash
-
-#
-#  all this part is from mopshop and we will use it to construct the brisk database
+# set -x
 #
+MATCH_DROP='^DROP.*([^-]...|.[^-]..|..[^M].|...[^F])$|^ALTER TABLE.* DROP .*([^-]...|.[^-]..|..[^M].|...[^F])$|^DELETE .*([^-]...|.[^-]..|..[^M].|...[^F])$|--MB$'
 
-if [ -f $HOME/.db.conf ]; then
-    source $HOME/.db.conf
-else
-    DBHOST=127.0.0.1
-    DBUSER=brisk
-    DBBASE=brisk
-    DBPASS=briskpass
-    PFX="bsk_"
-fi
+DATECUR="$(date +%s)"
 
-if [ -f $HOME/.brisk_install ]; then
-    source $HOME/.brisk_install
-fi
+#  functions
+usage () {
+    echo " USAGE"
+    echo "   $0 <command> [-d|--dryrun] [<-a|--allfiles>|<-p|--devfiles>] [-s|--short] ..."
+    echo "   $0 <-h|--help|help>"
+    echo "   commands are:"
+    echo "       create"
+    echo "       destroy"
+    echo "       clean"
+    echo "       build"
+    echo "       rebuild"
+    echo "       psql"
+    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"
 
+    exit $1
+}
 
 sqlexe () {
     local sht
     sht=$1
-    
-    if [ $sht -eq 1 ];  then 
-        sed "s/#PFX#/$PFX/g" | psql -a -h $DBHOST -U $DBUSER $DBBASE 2>&1 | egrep 'ERROR|^-- MESG' 
+
+    if [ "$SHORT" = "y" ];  then
+        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 -h $DBHOST -U $DBUSER $DBBASE
+        sed "s/#PFX#/$PFX/g;s/#NOW#/$DATECUR/g" | psql -a $pg_args
     fi
 
     return 0
 }
 
 one_or_all() {
-    if [ "$1" = "" ]; then
-        cat sql.d/*.sql
-    else
-        cat "$1"
-    fi
+    local old_ifs
+
+    old_ifs="$IFS"
+    IFS=" "
+    for fil in $(
+        if [ "$1" ]; then
+            echo "$1"
+        elif [ "$TYPE_FILES" = "a" ]; then
+            echo sql.d/[0-9]*
+        elif [ "$TYPE_FILES" = "d" ]; then
+            echo sql.d/[0-9]*.{sql,devel}
+        else
+            echo sql.d/[0-9]*.sql
+            fi); do
+        echo "-- FILE BEG: $fil"
+        cat "$fil"
+        echo "-- FILE END: $fil"
+    done
+    IFS="$old_ifs"
 }
 
 #
-# MAIN
+#  MAIN
 #
-sht=0
 
-if [ "$1" = "-s" ]; then
+CMD=$1
+shift
+
+while [ $# -gt 0 ]; do
+    case $1 in
+        -d|--dryrun)
+            DRY_RUN=y
+            psql () {
+                echo "MOCKPSQL params: $@"
+                cat
+            }
+            ;;
+        -a|--allfiles)
+            TYPE_FILES=a
+            ;;
+        -p|--devfiles)
+            TYPE_FILES=d
+            ;;
+        -s|--short)
+            SHORT=y
+            ;;
+        *)
+            break
+            ;;
+    esac
     shift
-    sht=1
-fi
+done
 
-if [ "$1" = "create" ]; then
-    echo "su root" 
-    su root -c "su postgres -c \"echo \\\"DBUser passwd: $DBPASS\\\" ; createuser -S -D -R -P $DBUSER && createdb -E utf8 -O $DBUSER $DBBASE\"" 
-elif [ "$1" = "destroy" ]; then
-    echo "su root" 
-    su root -c "su postgres -c \"dropdb $DBBASE && dropuser $DBUSER\"" 
-elif [ "$1" = "clean" ]; then
-    ( echo "-- MESG: clean start" ; one_or_all $2 | grep -i '^drop' | tac ; echo "-- MESG: clean end" ;   ) | sqlexe $sht
-elif [ "$1" = "build" ]; then
-    ( echo "-- MESG: build start" ; one_or_all $2 | grep -iv '^drop' ; echo "-- MESG: build end" ;   ) | sqlexe $sht
-elif [ "$1" = "rebuild" ]; then
-    ( echo "-- MESG: clean start" ; one_or_all $2 | grep -i '^drop' | tac ; echo "-- MESG: clean end" ; \
-        echo "-- MESG: build start" ; one_or_all $2 | grep -iv '^drop' ; echo "-- MESG: build end" ;   ) \
-        | sqlexe $sht
-elif [ "$1" = "psql" ]; then
-   shift
-   psql -h $DBHOST -U $DBUSER $DBBASE $@
-elif [ "$1" = "piped" ]; then
-   shift
-   psql -h $DBHOST -U $DBUSER $DBBASE -t -q -A -F '|' $@
-elif [ "$1" = "dump" ]; then
-    if [ $# -eq 1 ]; then
-        pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE
-    else
-        pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE > $2
-    fi
-elif [ "$1" = "dumpall" ]; then
-    if [ $# -eq 1 ]; then
-        pg_dump -h $DBHOST -U $DBUSER $DBBASE
-    else
-        pg_dump -h $DBHOST -U $DBUSER $DBBASE > $2
-    fi
-elif [ "$1" = "add" ]; then
-    cat "$2" | psql -h $DBHOST -U $DBUSER $DBBASE
+if [ -f $HOME/.brisk-db.conf ]; then
+    source $HOME/.brisk-db.conf
+elif [ -f $HOME/.db.conf ]; then
+    source $HOME/.db.conf
 else
-    echo " USAGE"
-    echo "   ./builder create"
-    echo "   ./builder destroy"
-    echo "   ./builder clean"
-    echo "   ./builder build"
-    echo "   ./builder rebuild"
-    echo "   ./builder psql"
-    echo "   ./builder piped"
-    echo "   ./builder add <filesql>"
-    echo "   ./builder dump [dumpfile]"
-    echo "   ./builder dumpall [dumpfile]"
-    echo "   ./builder all"
-    echo "   ./builder help"
+    DBHOST=127.0.0.1
+    DBUSER=brisk
+    DBPORT=5432
+    DBBASE=brisk
+    DBPASS=briskpass
+    PFX="bsk_"
 fi
+
+if [ -f $HOME/.brisk_install ]; then
+    source $HOME/.brisk_install
+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"
+
+
+case $CMD in
+    "create")
+        echo "su root"
+        su root -c "su postgres -c \"echo \\\"DBUser passwd: $DBPASS\\\" ; createuser -S -D -R -P $DBUSER && createdb -E utf8 -O $DBUSER $DBBASE\""
+        ;;
+
+    "destroy")
+        echo "su root"
+        su root -c "su postgres -c \"dropdb $DBBASE && dropuser $DBUSER\""
+        ;;
+    "clean")
+        ( 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 -v "$MATCH_DROP" ; echo "-- MESG: build end" ;   ) | sqlexe
+        ;;
+    "rebuild")
+        ( 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")
+        psql $pg_args $@
+        ;;
+
+    "piped")
+        psql $pg_args -t -q -A -F '|' $@
+        ;;
+    "dump")
+        if [ $# -eq 1 ]; then
+            pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE
+        else
+            pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE > $1
+        fi
+        ;;
+    "dumpall")
+        if [ $# -eq 1 ]; then
+            pg_dump -h $DBHOST -U $DBUSER $DBBASE
+        else
+            pg_dump -h $DBHOST -U $DBUSER $DBBASE > $1
+        fi
+        ;;
+    "add")
+        ( echo "-- MESG: add start" ; cat "$@" | egrep -v "$MATCH_DROP" ; echo "-- MESG: add end" ;   ) | sqlexe
+        ;;
+    "del")
+        ( 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
+        ;;
+    *)
+        usage 1
+        ;;
+esac
+
+exit 0