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