enhanced data model and sql builder.sh script to manage complex alter table scenario...
[brisk.git] / sql / builder.sh
index 74e009c..5fbd33b 100755 (executable)
@@ -3,24 +3,47 @@
 #
 #  all this part is from mopshop and we will use it to construct the brisk database
 #
-DBHOST=127.0.0.1
-DBUSER=brisk
-DBBASE=brisk
-DBPASS=briskpass
-PFX="bsk_"
+
+if [ "$1" = "-d" -o "$1" = "--dryrun" ]; then
+    shift
+    DRY_RUN=y
+    psql () {
+        echo "MOCKPSQL params: $@"
+        cat
+    }
+fi
+
+if [ -f $HOME/.brisk-db.conf ]; then
+    source $HOME/.brisk-db.conf
+elif [ -f $HOME/.db.conf ]; then
+    source $HOME/.db.conf
+else
+    DBHOST=127.0.0.1
+    DBUSER=brisk
+    DBPORT=5432
+    DBBASE=brisk
+    DBPASS=briskpass
+    PFX="bsk_"
+fi
 
 if [ -f $HOME/.brisk_install ]; then
-   . $HOME/.brisk_install
+    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"
+       
 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' 
+        sed "s/#PFX#/$PFX/g" | psql -a $pg_args 2>&1 | egrep 'ERROR|^-- MESG' 
     else
-        sed "s/#PFX#/$PFX/g" | psql -a -h $DBHOST -U $DBUSER $DBBASE
+        sed "s/#PFX#/$PFX/g" | psql -a $pg_args
     fi
 
     return 0
@@ -38,9 +61,7 @@ one_or_all() {
 # MAIN
 #
 sht=0
-if [ -f $HOME/.db.conf ]; then
-    source $HOME/.db.conf
-fi
+MATCH_DROP='^drop|^alter table.* drop '
 
 if [ "$1" = "-s" ]; then
     shift
@@ -54,43 +75,47 @@ 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
+    ( echo "-- MESG: clean start" ; one_or_all $2 | egrep -i "$MATCH_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
+    ( echo "-- MESG: build start" ; one_or_all $2 | egrep -iv "$MATCH_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" ;   ) \
+    ( 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" ;   ) \
         | sqlexe $sht
 elif [ "$1" = "psql" ]; then
-   psql -h $DBHOST -U $DBUSER $DBBASE $@
+   shift
+   psql $pg_args $@
 elif [ "$1" = "piped" ]; then
-   psql -h $DBHOST -U $DBUSER $DBBASE -t -q -A -F '|' $@
+   shift
+   psql $pg_args -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
+    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
+    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
+    cat "$2" | sqlexe $sht
 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"
+    echo "   ./builder [-d|--dry-run] <command> ..."
+    echo "   commands are:"
+    echo "       create"
+    echo "       destroy"
+    echo "       clean"
+    echo "       build"
+    echo "       rebuild"
+    echo "       psql"
+    echo "       piped"
+    echo "       add <filesql>"
+    echo "       dump [dumpfile]"
+    echo "       dumpall [dumpfile]"
+    echo "       all"
+    echo "       help"
 fi