enhanced data model and sql builder.sh script to manage complex alter table scenario...
[brisk.git] / sql / builder.sh
1 #! /bin/bash
2
3 #
4 #  all this part is from mopshop and we will use it to construct the brisk database
5 #
6
7 if [ "$1" = "-d" -o "$1" = "--dryrun" ]; then
8     shift
9     DRY_RUN=y
10     psql () {
11         echo "MOCKPSQL params: $@"
12         cat
13     }
14 fi
15
16 if [ -f $HOME/.brisk-db.conf ]; then
17     source $HOME/.brisk-db.conf
18 elif [ -f $HOME/.db.conf ]; then
19     source $HOME/.db.conf
20 else
21     DBHOST=127.0.0.1
22     DBUSER=brisk
23     DBPORT=5432
24     DBBASE=brisk
25     DBPASS=briskpass
26     PFX="bsk_"
27 fi
28
29 if [ -f $HOME/.brisk_install ]; then
30     source $HOME/.brisk_install
31 fi
32
33 pg_args=""
34 test "$DBHOST" != "" && pg_args="$pg_args -h $DBHOST"
35 test "$DBUSER" != "" && pg_args="$pg_args -U $DBUSER"
36 test "$DBPORT" != "" && pg_args="$pg_args -p $DBPORT"
37 test "$DBBASE" != "" && pg_args="$pg_args $DBBASE"
38         
39 sqlexe () {
40     local sht
41     sht=$1
42     
43     if [ $sht -eq 1 ];  then 
44         sed "s/#PFX#/$PFX/g" | psql -a $pg_args 2>&1 | egrep 'ERROR|^-- MESG' 
45     else
46         sed "s/#PFX#/$PFX/g" | psql -a $pg_args
47     fi
48
49     return 0
50 }
51
52 one_or_all() {
53     if [ "$1" = "" ]; then
54         cat sql.d/*.sql
55     else
56         cat "$1"
57     fi
58 }
59
60 #
61 # MAIN
62 #
63 sht=0
64 MATCH_DROP='^drop|^alter table.* drop '
65
66 if [ "$1" = "-s" ]; then
67     shift
68     sht=1
69 fi
70
71 if [ "$1" = "create" ]; then
72     echo "su root" 
73     su root -c "su postgres -c \"echo \\\"DBUser passwd: $DBPASS\\\" ; createuser -S -D -R -P $DBUSER && createdb -E utf8 -O $DBUSER $DBBASE\"" 
74 elif [ "$1" = "destroy" ]; then
75     echo "su root" 
76     su root -c "su postgres -c \"dropdb $DBBASE && dropuser $DBUSER\"" 
77 elif [ "$1" = "clean" ]; then
78     ( echo "-- MESG: clean start" ; one_or_all $2 | egrep -i "$MATCH_DROP" | tac ; echo "-- MESG: clean end" ;   ) | sqlexe $sht
79 elif [ "$1" = "build" ]; then
80     ( echo "-- MESG: build start" ; one_or_all $2 | egrep -iv "$MATCH_DROP" ; echo "-- MESG: build end" ;   ) | sqlexe $sht
81 elif [ "$1" = "rebuild" ]; then
82     ( echo "-- MESG: clean start" ; one_or_all $2 | egrep -i "$MATCH_DROP" | tac ; echo "-- MESG: clean end" ; \
83         echo "-- MESG: build start" ; one_or_all $2 | egrep -iv "$MATCH_DROP" ; echo "-- MESG: build end" ;   ) \
84         | sqlexe $sht
85 elif [ "$1" = "psql" ]; then
86    shift
87    psql $pg_args $@
88 elif [ "$1" = "piped" ]; then
89    shift
90    psql $pg_args -t -q -A -F '|' $@
91 elif [ "$1" = "dump" ]; then
92     if [ $# -eq 1 ]; then
93         pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE
94     else
95         pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE > $2
96     fi
97 elif [ "$1" = "dumpall" ]; then
98     if [ $# -eq 1 ]; then
99         pg_dump -h $DBHOST -U $DBUSER $DBBASE
100     else
101         pg_dump -h $DBHOST -U $DBUSER $DBBASE > $2
102     fi
103 elif [ "$1" = "add" ]; then
104     cat "$2" | sqlexe $sht
105 else
106     echo " USAGE"
107     echo "   ./builder [-d|--dry-run] <command> ..."
108     echo "   commands are:"
109     echo "       create"
110     echo "       destroy"
111     echo "       clean"
112     echo "       build"
113     echo "       rebuild"
114     echo "       psql"
115     echo "       piped"
116     echo "       add <filesql>"
117     echo "       dump [dumpfile]"
118     echo "       dumpall [dumpfile]"
119     echo "       all"
120     echo "       help"
121 fi