more clear sql builder help
[brisk.git] / sql / builder.sh
1 #! /bin/bash
2 # set -x
3 #
4 MATCH_DROP='^DROP.*([^-]...|.[^-]..|..[^M].|...[^F])$|^ALTER TABLE.* DROP .*([^-]...|.[^-]..|..[^M].|...[^F])$|^DELETE .*([^-]...|.[^-]..|..[^M].|...[^F])$|--MB$'
5
6 DATECUR="$(date +%s)"
7
8 #  functions
9 usage () {
10     echo " USAGE"
11     echo "   $0 <command> [-d|--dryrun] [<-a|--allfiles>|<-p|--devfiles>] [-s|--short] ..."
12     echo "   $0 <-h|--help|help>"
13     echo "   commands are:"
14     echo "       create"
15     echo "       destroy"
16     echo "       clean"
17     echo "       build"
18     echo "       rebuild"
19     echo "       psql"
20     echo "       piped"
21     echo "       add <filesql> [<filesql2> [..]]"
22     echo "       del <filesql> [<filesql2> [..]]"
23     echo "       res <filesql> [<filesql2> [..]]"
24     echo "       dump [dumpfile]"
25     echo "       dumpall [dumpfile]"
26     echo "       all"
27     echo
28     echo "The match rule for clean lines is:"
29     echo "  [$MATCH_DROP]"
30     echo "NOTE: to invert normal 'del' rules add '--MF' (move forward) suffix to each line"
31     echo "      to invert normal 'add' rules add '--MB' (move backward) suffix to each line"
32
33     exit $1
34 }
35
36 sqlexe () {
37     local sht
38     sht=$1
39
40     if [ "$SHORT" = "y" ];  then
41         sed "s/#PFX#/$PFX/g;s/#NOW#/$DATECUR/g" | psql -a $pg_args 2>&1 | egrep 'ERROR|^-- MESG|^-- FILE '
42     else
43         sed "s/#PFX#/$PFX/g;s/#NOW#/$DATECUR/g" | psql -a $pg_args
44     fi
45
46     return 0
47 }
48
49 one_or_all() {
50     local old_ifs
51
52     old_ifs="$IFS"
53     IFS=" "
54     for fil in $(
55         if [ "$1" ]; then
56             echo "$1"
57         elif [ "$TYPE_FILES" = "a" ]; then
58             echo sql.d/[0-9]*
59         elif [ "$TYPE_FILES" = "d" ]; then
60             echo sql.d/[0-9]*.{sql,devel}
61         else
62             echo sql.d/[0-9]*.sql
63             fi); do
64         echo "-- FILE BEG: $fil"
65         cat "$fil"
66         echo "-- FILE END: $fil"
67     done
68     IFS="$old_ifs"
69 }
70
71 #
72 #  MAIN
73 #
74
75 CMD=$1
76 shift
77
78 while [ $# -gt 0 ]; do
79     case $1 in
80         -d|--dryrun)
81             DRY_RUN=y
82             psql () {
83                 echo "MOCKPSQL params: $@"
84                 cat
85             }
86             ;;
87         -a|--allfiles)
88             TYPE_FILES=a
89             ;;
90         -p|--devfiles)
91             TYPE_FILES=d
92             ;;
93         -s|--short)
94             SHORT=y
95             ;;
96         *)
97             break
98             ;;
99     esac
100     shift
101 done
102
103 if [ -f $HOME/.brisk-db.conf ]; then
104     source $HOME/.brisk-db.conf
105 elif [ -f $HOME/.db.conf ]; then
106     source $HOME/.db.conf
107 else
108     DBHOST=127.0.0.1
109     DBUSER=brisk
110     DBPORT=5432
111     DBBASE=brisk
112     DBPASS=briskpass
113     PFX="bsk_"
114 fi
115
116 if [ -f $HOME/.brisk_install ]; then
117     source $HOME/.brisk_install
118 fi
119
120 pg_args=""
121 test "$DBHOST" != "" && pg_args="$pg_args -h $DBHOST"
122 test "$DBUSER" != "" && pg_args="$pg_args -U $DBUSER"
123 test "$DBPORT" != "" && pg_args="$pg_args -p $DBPORT"
124 test "$DBBASE" != "" && pg_args="$pg_args $DBBASE"
125
126
127 case $CMD in
128     "create")
129         echo "su root"
130         su root -c "su postgres -c \"echo \\\"DBUser passwd: $DBPASS\\\" ; createuser -S -D -R -P $DBUSER && createdb -E utf8 -O $DBUSER $DBBASE\""
131         ;;
132
133     "destroy")
134         echo "su root"
135         su root -c "su postgres -c \"dropdb $DBBASE && dropuser $DBUSER\""
136         ;;
137     "clean")
138         ( echo "-- MESG: clean start" ; one_or_all $2 | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; echo "-- MESG: clean end" ;   ) | sqlexe
139         ;;
140     "build")
141         ( echo "-- MESG: build start" ; one_or_all $2 | egrep -v "$MATCH_DROP" ; echo "-- MESG: build end" ;   ) | sqlexe
142         ;;
143     "rebuild")
144         ( echo "-- MESG: clean start" ; one_or_all $2 | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; echo "-- MESG: clean end" ; \
145             echo "-- MESG: build start" ; one_or_all $2 | egrep -v "$MATCH_DROP" ; echo "-- MESG: build end" ;   ) \
146             | sqlexe
147         ;;
148     "psql")
149         psql $pg_args $@
150         ;;
151
152     "piped")
153         psql $pg_args -t -q -A -F '|' $@
154         ;;
155     "dump")
156         if [ $# -eq 1 ]; then
157             pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE
158         else
159             pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE > $1
160         fi
161         ;;
162     "dumpall")
163         if [ $# -eq 1 ]; then
164             pg_dump -h $DBHOST -U $DBUSER $DBBASE
165         else
166             pg_dump -h $DBHOST -U $DBUSER $DBBASE > $1
167         fi
168         ;;
169     "add")
170         ( echo "-- MESG: add start" ; cat "$@" | egrep -v "$MATCH_DROP" ; echo "-- MESG: add end" ;   ) | sqlexe
171         ;;
172     "del")
173         ( echo "-- MESG: del start" ; cat "$@" | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; echo "-- MESG: del end" ;   ) | sqlexe
174         ;;
175     "res")
176         ( echo "-- MESG: res start" ; cat "$@" | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; cat "$@" | egrep -v "$MATCH_DROP" ; echo "-- MESG: del end" ;   ) | sqlexe
177         ;;
178     "help"|"-h"|"--help")
179         usage 0
180         ;;
181     *)
182         usage 1
183         ;;
184 esac
185
186 exit 0