add override configuration file option to builder.sh
[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> [-c|--config <override_file>] [-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             find sql.d/ -name '[0-9]*' -type f | sort -n | tr '\n' ' '
59         elif [ "$TYPE_FILES" = "d" ]; then
60             find sql.d/ -name '[0-9]*' | egrep '.*_devel.sql$|^[^_]+.sql$' | sort -n | tr '\n' ' '
61         else
62             find sql.d/ -name '[0-9]*' | egrep '^[^_]+.sql$' | sort -n | tr '\n' ' '
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         -c|--config)
81             CONFIG_FILE=$2
82             shift
83             ;;
84         -d|--dryrun)
85             DRY_RUN=y
86             psql () {
87                 echo "MOCKPSQL params: $@"
88                 cat
89             }
90             ;;
91         -a|--allfiles)
92             TYPE_FILES=a
93             ;;
94         -p|--devfiles)
95             TYPE_FILES=d
96             ;;
97         -s|--short)
98             SHORT=y
99             ;;
100         *)
101             break
102             ;;
103     esac
104     shift
105 done
106
107 if [ -f $HOME/.brisk-db.conf ]; then
108     source $HOME/.brisk-db.conf
109 elif [ -f $HOME/.db.conf ]; then
110     source $HOME/.db.conf
111 else
112     DBHOST=127.0.0.1
113     DBUSER=brisk
114     DBPORT=5432
115     DBBASE=brisk
116     DBPASS=briskpass
117     PFX="bsk_"
118 fi
119
120 if [ -f $HOME/.brisk_install ]; then
121     source $HOME/.brisk_install
122 fi
123
124 if [ -f "$CONFIG_FILE" ]; then
125     source "$CONFIG_FILE"
126 fi
127
128 pg_args=""
129 test "$DBHOST" != "" && pg_args="$pg_args -h $DBHOST"
130 test "$DBUSER" != "" && pg_args="$pg_args -U $DBUSER"
131 test "$DBPORT" != "" && pg_args="$pg_args -p $DBPORT"
132 test "$DBBASE" != "" && pg_args="$pg_args $DBBASE"
133
134
135 case $CMD in
136     "create")
137         echo "su root"
138         su root -c "su postgres -c \"echo \\\"DBUser passwd: $DBPASS\\\" ; createuser -S -D -R -P $DBUSER && createdb -E utf8 -O $DBUSER $DBBASE\""
139         ;;
140
141     "destroy")
142         echo "su root"
143         su root -c "su postgres -c \"dropdb $DBBASE && dropuser $DBUSER\""
144         ;;
145     "clean")
146         ( echo "-- MESG: clean start" ; one_or_all $2 | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; echo "-- MESG: clean end" ;   ) | sqlexe
147         ;;
148     "build")
149         ( echo "-- MESG: build start" ; one_or_all $2 | egrep -v "$MATCH_DROP" ; echo "-- MESG: build end" ;   ) | sqlexe
150         ;;
151     "rebuild")
152         ( echo "-- MESG: clean start" ; one_or_all $2 | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; echo "-- MESG: clean end" ; \
153             echo "-- MESG: build start" ; one_or_all $2 | egrep -v "$MATCH_DROP" ; echo "-- MESG: build end" ;   ) \
154             | sqlexe
155         ;;
156     "psql")
157         psql $pg_args $@
158         ;;
159
160     "piped")
161         psql $pg_args -t -q -A -F '|' $@
162         ;;
163     "dump")
164         if [ $# -eq 1 ]; then
165             pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE
166         else
167             pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE > $1
168         fi
169         ;;
170     "dumpall")
171         if [ $# -eq 1 ]; then
172             pg_dump -h $DBHOST -U $DBUSER $DBBASE
173         else
174             pg_dump -h $DBHOST -U $DBUSER $DBBASE > $1
175         fi
176         ;;
177     "add")
178         ( echo "-- MESG: add start" ; cat "$@" | egrep -v "$MATCH_DROP" ; echo "-- MESG: add end" ;   ) | sqlexe
179         ;;
180     "del")
181         ( echo "-- MESG: del start" ; cat "$@" | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; echo "-- MESG: del end" ;   ) | sqlexe
182         ;;
183     "res")
184         ( echo "-- MESG: res start" ; cat "$@" | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; cat "$@" | egrep -v "$MATCH_DROP" ; echo "-- MESG: del end" ;   ) | sqlexe
185         ;;
186     "help"|"-h"|"--help")
187         usage 0
188         ;;
189     *)
190         usage 1
191         ;;
192 esac
193
194 exit 0