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