fix missing bold for your account item
[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     local _v
11     echo " USAGE"
12     echo "   $0 <command> [-c|--config <override_file>] [-d|--dryrun] [<-a|--allfiles>|<-p|--devfiles>] [-s|--short] ..."
13     echo "   $0 [-c|--config <override_file>] <-h|--help|help>"
14     echo "   commands are:"
15     echo "       create"
16     echo "       destroy"
17     echo "       clean"
18     echo "       build"
19     echo "       rebuild"
20     echo "       psql"
21     echo "       piped"
22     echo "       add <filesql> [<filesql2> [..]]"
23     echo "       del <filesql> [<filesql2> [..]]"
24     echo "       res <filesql> [<filesql2> [..]]"
25     echo "       dump [dumpfile]"
26     echo "       dumpall [dumpfile]"
27     echo "       all"
28     echo
29     echo "The match rule for clean lines is:"
30     echo "  [$MATCH_DROP]"
31     echo "NOTE: to invert normal 'del' rules add '--MF' (move forward) suffix to each line"
32     echo "      to invert normal 'add' rules add '--MB' (move backward) suffix to each line"
33     echo
34     echo "ENVIRONMENT"
35     for _v in \
36 apache_conf card_hand players_n tables_n tables_appr_n tables_auth_n tables_cert_n \
37 brisk_auth_conf brisk_debug web_path ftok_path proxy_path legal_path prefix_path \
38 brisk_conf usock_path sys_user web_only test_add \
39 DBHOST DBUSER DBPORT DBBASE DBPASS PFX ; do
40         echo "    $_v=${!_v}"
41     done
42
43     exit $1
44 }
45
46 sqlexe () {
47     local sht
48     sht=$1
49
50     if [ "$SHORT" = "y" ];  then
51         sed "s/#PFX#/$PFX/g;s/#NOW#/$DATECUR/g" | psql -a $pg_args 2>&1 | egrep 'ERROR|^-- MESG|^-- FILE '
52     else
53         sed "s/#PFX#/$PFX/g;s/#NOW#/$DATECUR/g" | psql -a $pg_args
54     fi
55
56     return 0
57 }
58
59 one_or_all() {
60     local old_ifs
61
62     old_ifs="$IFS"
63     IFS=" "
64     for fil in $(
65         if [ "$1" ]; then
66             echo "$1"
67         elif [ "$TYPE_FILES" = "a" ]; then
68             find sql.d/ -name '[0-9]*' -type f | sort -n | tr '\n' ' '
69         elif [ "$TYPE_FILES" = "d" ]; then
70             find sql.d/ -name '[0-9]*' | egrep '.*_devel.sql$|^[^_]+.sql$' | sort -n | tr '\n' ' '
71         else
72             find sql.d/ -name '[0-9]*' | egrep '^[^_]+.sql$' | sort -n | tr '\n' ' '
73         fi); do
74         echo "-- FILE BEG: $fil"
75         cat "$fil"
76         echo "-- FILE END: $fil"
77     done
78     IFS="$old_ifs"
79 }
80
81 #
82 #  MAIN
83 #
84 if [ "${1:0:1}" != "-" ]; then
85     CMD=$1
86     shift
87 fi
88
89 while [ $# -gt 0 ]; do
90     case $1 in
91         -c|--config)
92             CONFIG_FILE=$2
93             shift
94             ;;
95         -d|--dryrun)
96             DRY_RUN=y
97             psql () {
98                 echo "MOCKPSQL params: $@"
99                 cat
100             }
101             ;;
102         -a|--allfiles)
103             TYPE_FILES=a
104             ;;
105         -p|--devfiles)
106             TYPE_FILES=d
107             ;;
108         -s|--short)
109             SHORT=y
110             ;;
111         -h|--help|help)
112             CMD=help
113             ;;
114         *)
115             break
116             ;;
117     esac
118     shift
119 done
120
121 if [ -f $HOME/.brisk-db.conf ]; then
122     source $HOME/.brisk-db.conf
123 elif [ -f $HOME/.db.conf ]; then
124     source $HOME/.db.conf
125 else
126     DBHOST=127.0.0.1
127     DBUSER=brisk
128     DBPORT=5432
129     DBBASE=brisk
130     DBPASS=briskpass
131     PFX="bsk_"
132 fi
133
134 if [ -f $HOME/.brisk_install ]; then
135     source $HOME/.brisk_install
136 fi
137
138 if [ -f "$CONFIG_FILE" ]; then
139     source "$CONFIG_FILE"
140 fi
141
142 pg_args=""
143 test "$DBHOST" != "" && pg_args="$pg_args -h $DBHOST"
144 test "$DBUSER" != "" && pg_args="$pg_args -U $DBUSER"
145 test "$DBPORT" != "" && pg_args="$pg_args -p $DBPORT"
146 test "$DBBASE" != "" && pg_args="$pg_args $DBBASE"
147
148
149 case $CMD in
150     "create")
151         echo "su root"
152         su root -c "su postgres -c \"echo \\\"DBUser passwd: $DBPASS\\\" ; createuser -S -D -R -P $DBUSER && createdb -E utf8 -O $DBUSER $DBBASE\""
153         ;;
154
155     "destroy")
156         echo "su root"
157         su root -c "su postgres -c \"dropdb $DBBASE && dropuser $DBUSER\""
158         ;;
159     "clean")
160         ( echo "-- MESG: clean start" ; one_or_all $2 | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; echo "-- MESG: clean end" ;   ) | sqlexe
161         ;;
162     "build")
163         ( echo "-- MESG: build start" ; one_or_all $2 | egrep -v "$MATCH_DROP" ; echo "-- MESG: build end" ;   ) | sqlexe
164         ;;
165     "rebuild")
166         ( echo "-- MESG: clean start" ; one_or_all $2 | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; echo "-- MESG: clean end" ; \
167             echo "-- MESG: build start" ; one_or_all $2 | egrep -v "$MATCH_DROP" ; echo "-- MESG: build end" ;   ) \
168             | sqlexe
169         ;;
170     "psql")
171         psql $pg_args $@
172         ;;
173
174     "piped")
175         psql $pg_args -t -q -A -F '|' $@
176         ;;
177     "dump")
178         if [ $# -eq 1 ]; then
179             pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE
180         else
181             pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE > $1
182         fi
183         ;;
184     "dumpall")
185         if [ $# -eq 1 ]; then
186             pg_dump -h $DBHOST -U $DBUSER $DBBASE
187         else
188             pg_dump -h $DBHOST -U $DBUSER $DBBASE > $1
189         fi
190         ;;
191     "add")
192         ( echo "-- MESG: add start" ; cat "$@" | egrep -v "$MATCH_DROP" ; echo "-- MESG: add end" ;   ) | sqlexe
193         ;;
194     "del")
195         ( echo "-- MESG: del start" ; cat "$@" | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; echo "-- MESG: del end" ;   ) | sqlexe
196         ;;
197     "res")
198         ( echo "-- MESG: res start" ; cat "$@" | egrep "$MATCH_DROP|^-- MESG|^-- FILE " | tac ; cat "$@" | egrep -v "$MATCH_DROP" ; echo "-- MESG: del end" ;   ) | sqlexe
199         ;;
200     "help"|"-h"|"--help")
201         usage 0
202         ;;
203     *)
204         usage 1
205         ;;
206 esac
207
208 exit 0