From e19ba01834625d96835b3adf385a1defc48f4166 Mon Sep 17 00:00:00 2001 From: "Matteo Nastasi (mop)" Date: Thu, 12 Sep 2013 18:29:04 +0200 Subject: [PATCH] builder.sh sql script arguments management consolidated --- sql/builder.sh | 211 +++++++++++++++++++++++++++++-------------------- 1 file changed, 124 insertions(+), 87 deletions(-) diff --git a/sql/builder.sh b/sql/builder.sh index 5fbd33b..4acc242 100755 --- a/sql/builder.sh +++ b/sql/builder.sh @@ -1,17 +1,81 @@ #! /bin/bash # -# all this part is from mopshop and we will use it to construct the brisk database +# functions +usage () { + echo " USAGE" + echo " ./builder [-d|--dryrun] [-a|--allfiles] [-s|--short] ..." + echo " ./builder <-h|--help|help>" + echo " commands are:" + echo " create" + echo " destroy" + echo " clean" + echo " build" + echo " rebuild" + echo " psql" + echo " piped" + echo " add " + echo " dump [dumpfile]" + echo " dumpall [dumpfile]" + echo " all" + exit $1 +} + +sqlexe () { + local sht + sht=$1 + + if [ "$SHORT" = "y" ]; then + sed "s/#PFX#/$PFX/g" | psql -a $pg_args 2>&1 | egrep 'ERROR|^-- MESG' + else + sed "s/#PFX#/$PFX/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 +} + +# +# MAIN # -if [ "$1" = "-d" -o "$1" = "--dryrun" ]; then +CMD=$1 +shift + +while [ $# -gt 0 ]; do + case $1 in + -d|--dryrun) + DRY_RUN=y + psql () { + echo "MOCKPSQL params: $@" + cat + } + ;; + -a|--allfiles) + ALL_FILES=y + ;; + -s|--short) + SHORT=y + ;; + *) + break + ;; + esac shift - DRY_RUN=y - psql () { - echo "MOCKPSQL params: $@" - cat - } -fi +done if [ -f $HOME/.brisk-db.conf ]; then source $HOME/.brisk-db.conf @@ -35,87 +99,60 @@ 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 $pg_args 2>&1 | egrep 'ERROR|^-- MESG' - else - sed "s/#PFX#/$PFX/g" | psql -a $pg_args - fi - return 0 -} +MATCH_DROP='^drop|^alter table.* drop ' -one_or_all() { - if [ "$1" = "" ]; then - cat sql.d/*.sql - else - cat "$1" - fi -} +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\"" + ;; -# -# MAIN -# -sht=0 -MATCH_DROP='^drop|^alter table.* drop ' + "destroy") + echo "su root" + 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 + ;; + "build") + ( echo "-- MESG: build start" ; one_or_all $2 | egrep -iv "$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" ; ) \ + | sqlexe + ;; + "psql") + psql $pg_args $@ + ;; -if [ "$1" = "-s" ]; then - shift - sht=1 -fi + "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") + cat "$1" | sqlexe + ;; + "help"|"-h"|"--help") + usage 0 + ;; + *) + usage 1 + ;; +esac -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 | egrep -i "$MATCH_DROP" | tac ; echo "-- MESG: clean end" ; ) | sqlexe $sht -elif [ "$1" = "build" ]; then - ( 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 | 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 - shift - psql $pg_args $@ -elif [ "$1" = "piped" ]; then - 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 -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" | sqlexe $sht -else - echo " USAGE" - echo " ./builder [-d|--dry-run] ..." - echo " commands are:" - echo " create" - echo " destroy" - echo " clean" - echo " build" - echo " rebuild" - echo " psql" - echo " piped" - echo " add " - echo " dump [dumpfile]" - echo " dumpall [dumpfile]" - echo " all" - echo " help" -fi +exit 0 \ No newline at end of file -- 2.17.1