utility script to manage sql
[brisk.git] / sql / builder.sh
1 #! /bin/bash
2
3 exit 0
4 #
5 #  all this part is from mopshop and we will use it to construct the brisk database
6 #
7 DBHOST=127.0.0.1
8 DBUSER=mopshop
9 DBBASE=mopshop
10 DBPASS=sozopoco
11 PFX="msh_"
12
13 sqlexe () {
14     local sht
15     sht=$1
16     
17     if [ $sht -eq 1 ];  then 
18         sed "s/#PFX#/$PFX/g" | psql -a -h $DBHOST -U $DBUSER $DBBASE 2>&1 | egrep 'ERROR|^-- MESG' 
19     else
20         sed "s/#PFX#/$PFX/g" | psql -a -h $DBHOST -U $DBUSER $DBBASE
21     fi
22
23     return 0
24 }
25
26 #
27 # MAIN
28 #
29 sht=0
30 if [ -f $HOME/.db.conf ]; then
31     source $HOME/.db.conf
32 fi
33
34 if [ "$1" = "-s" ]; then
35     shift
36     sht=1
37 fi
38
39 if [ "$1" = "create" ]; then
40     echo "su root" 
41     su root -c "su postgres -c \"echo \\\"DBUser passwd: $DBPASS\\\" ; createuser -S -D -R -P $DBUSER && createdb -E utf8 -O $DBUSER $DBBASE\"" 
42 elif [ "$1" = "destroy" ]; then
43     echo "su root" 
44     su root -c "su postgres -c \"dropdb $DBBASE && dropuser $DBUSER\"" 
45 elif [ "$1" = "clean" ]; then
46     ( echo "-- MESG: clean start" ; cat sql.d/*.sql | grep -i '^drop' | tac ; echo "-- MESG: clean end" ;   ) | sqlexe $sht
47 elif [ "$1" = "build" ]; then
48     ( echo "-- MESG: build start" ; cat sql.d/*.sql | grep -iv '^drop' ; echo "-- MESG: build end" ;   ) | sqlexe $sht
49 elif [ "$1" = "rebuild" ]; then
50     ( echo "-- MESG: clean start" ; cat sql.d/*.sql | grep -i '^drop' | tac ; echo "-- MESG: clean end" ; \
51       echo "-- MESG: build start" ; cat sql.d/*.sql | grep -iv '^drop' ; echo "-- MESG: build end" ;   ) \
52         | sqlexe $sht
53 elif [ "$1" = "psql" ]; then
54    psql -h $DBHOST -U $DBUSER $DBBASE
55 elif [ "$1" = "dump" ]; then
56    if [ $# -eq 1 ]; then
57       pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE
58    else
59       pg_dump -a --inserts -h $DBHOST -U $DBUSER $DBBASE > $2
60    fi
61 elif [ "$1" = "dumpall" ]; then
62    if [ $# -eq 1 ]; then
63       pg_dump -h $DBHOST -U $DBUSER $DBBASE
64    else
65       pg_dump -h $DBHOST -U $DBUSER $DBBASE > $2
66    fi
67 elif [ "$1" = "add" ]; then
68    cat "$2" | psql -h $DBHOST -U $DBUSER $DBBASE
69 else
70     echo " USAGE"
71     echo "   ./builder create"
72     echo "   ./builder destroy"
73     echo "   ./builder clean"
74     echo "   ./builder build"
75     echo "   ./builder rebuild"
76     echo "   ./builder psql"
77     echo "   ./builder add <filesql>"
78     echo "   ./builder dump [dumpfile]"
79     echo "   ./builder dumpall [dumpfile]"
80     echo "   ./builder all"
81     echo "   ./builder help"
82 fi