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