-
Notifications
You must be signed in to change notification settings - Fork 0
/
createdb
executable file
·131 lines (108 loc) · 3.53 KB
/
createdb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/sh
#set -x
DATABASE_NAME=fishbase
###########################################################
## Function to call when error is encountered
###########################################################
ExitWithError () {
echo
echo #####
echo Error encountered trying to create ${DATABASE_NAME} db.
echo See ./log/$SQLINPUTFILE.log for more details...
echo #####
SQLINPUTFILE=
exit 1
}
###########################################################
## Process command line parameter(s)
###########################################################
if [ -z "$1" ]; then
DbHost=localhost
else
DbHost=$1
fi
if [ -z "$2" ]; then
DbPort=5432
else
DbPort=$2
fi
if [ -z "$3" ]; then
RestoreThreadCount=8
else
RestoreThreadCount=$3
fi
###########################################################
## Deleting any previous log files
###########################################################
if [ ! -d log ]; then
mkdir log
fi
rm -f log/*.log
PSQL="psql -U postgres -h $DbHost -p $DbPort"
PSQLFISHBASE="psql -U fishbase -h $DbHost -p $DbPort"
RESTORE="pg_restore -h $DbHost -p $DbPort -Fc -a -j $RestoreThreadCount"
###########################################################
## Check if there's already a "${DATABASE_NAME}" database present.
## If not, create the "${DATABASE_NAME}" database and the requisite db users,
## then proceed to invoke the initialize.sql script.
## If yes, proceed to invoke initialize.sql script only.
###########################################################
SQLINPUTFILE=create_user_and_db
if ! $PSQL -f $SQLINPUTFILE.sql -L log/$SQLINPUTFILE.log; then
ExitWithError
fi
SQLINPUTFILE=set_users_search_path
if ! $PSQL -f $SQLINPUTFILE.sql -L log/$SQLINPUTFILE.log; then
ExitWithError
fi
SQLINPUTFILE=initialize
if ! $PSQL -d ${DATABASE_NAME} -f $SQLINPUTFILE.sql -L log/$SQLINPUTFILE.log; then
ExitWithError
fi
##RdsAdmin=$($PSQL -A -t -c "select usename from pg_user where usename = 'rdsadmin'")
##if [ -n "$RdsAdmin" ]; then
## echo Amazon RDS environment detected. Re-configuring postgis environment appropriately...
## SQLINPUTFILE=rds_postgis_setup
## if ! $PSQL -d ${DATABASE_NAME} -f $SQLINPUTFILE.sql -L log/$SQLINPUTFILE.log; then
## ExitWithError
## fi
##fi
STIME=$(date '+%s')
if [ -f data_dump/fbapp.schema ]; then
echo Restoring fbapp schema. Please enter password for user fishbase
if ! $RESTORE -d ${DATABASE_NAME} -U fishbase data_dump/fbapp.schema; then
ExitWithError
fi
fi
if [ -f data_dump/slbapp.schema ]; then
echo Restoring slbapp schema. Please enter password for user fishbase
if ! $RESTORE -d ${DATABASE_NAME} -U fishbase data_dump/slbapp.schema; then
ExitWithError
fi
fi
ETIME=$(date '+%s')
echo "Schema data restored in $(($ETIME - $STIME)) seconds"
STIME=$(date '+%s')
# Clear previous content of rmv.sql or create anew
echo "vacuum analyze;" > rmv.sql
## Adding foreign keys
cat index_admin.sql >> rmv.sql
cat foreign_key_admin.sql >> rmv.sql
## Adding commands to refresh materialized views
if ! $PSQLFISHBASE -d ${DATABASE_NAME} -f refresh_mv.sql -t >> rmv.sql; then
ExitWithError
fi
if ! $PSQLFISHBASE -d ${DATABASE_NAME} -f rmv.sql; then
ExitWithError
fi
ETIME=$(date '+%s')
echo "Vacuuming, Indexing, Foreign Key Creations and Refreshing of Materialized Views completed in $(($ETIME - $STIME)) seconds"
#
# Success: print message and exit with successful return code
#
echo
echo #####
echo Successfully created ${DATABASE_NAME} database!
echo #####
SQLINPUTFILE=
exit 0