-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreatedb
215 lines (179 loc) · 5.88 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/sh
#set -x
DATABASE_NAME=sau
###########################################################
## 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
if [ -z "$4" ]; then
RestoreCellCatch=false
else
RestoreCellCatch=$4
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"
PSQLWEB="psql -U web -h $DbHost -p $DbPort"
PSQLSAU="psql -U sau -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.
###########################################################
STIME=$(date '+%s')
ExistingDB=$($PSQL -A -t -c "select datname from pg_database where datname = '${DATABASE_NAME}'")
if [ -z "$ExistingDB" ]; then
if ! $PSQL -f $SQLINPUTFILE.sql -L log/$SQLINPUTFILE.log -c "CREATE DATABASE sau"; then
ExitWithError
fi
fi
SQLINPUTFILE=create_user
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
ETIME=$(date '+%s')
echo "Empty SAU db and users created in $(($ETIME - $STIME)) seconds."
if [ -f "data_dump/admin.schema" ]; then
STIME=$(date '+%s')
echo Restoring admin schema. Please enter password for user sau
if ! $RESTORE -d ${DATABASE_NAME} -U sau data_dump/admin.schema; then
ExitWithError
fi
fi
if [ -f "data_dump/web.schema" ]; then
echo Restoring web schema. Please enter password for user sau
if ! $RESTORE -d ${DATABASE_NAME} -U sau data_dump/web.schema; then
ExitWithError
fi
fi
if [ -f "data_dump/geo.schema" ]; then
echo Restoring geo schema. Please enter password for user sau
if ! $RESTORE -d ${DATABASE_NAME} -U sau data_dump/geo.schema; then
ExitWithError
fi
fi
if [ -f "data_dump/feru.schema" ]; then
echo Restoring feru schema. Please enter password for user sau
if ! $RESTORE -d ${DATABASE_NAME} -U sau data_dump/feru.schema; then
ExitWithError
fi
fi
if [ -f "data_dump/expedition.schema" ]; then
echo Restoring expedition schema. Please enter password for user sau
if ! $RESTORE -d ${DATABASE_NAME} -U sau data_dump/expedition.schema; then
ExitWithError
fi
fi
if [ -f "data_dump/fao.schema" ]; then
echo Restoring fao schema. Please enter password for user sau
if ! $RESTORE -d ${DATABASE_NAME} -U sau data_dump/fao.schema; then
ExitWithError
fi
fi
if [ -f "data_dump/web_partition.schema" ]; then
if [ "$RestoreCellCatch" = "true" ]; then
if ! $PSQLSAU -d ${DATABASE_NAME} -c "select * from web_partition.maintain_cell_catch_partition()"; then
ExitWithError
fi
echo Restoring web_partition schema. Please enter password for user sau
if ! $RESTORE -d ${DATABASE_NAME} -U sau data_dump/web_partition.schema; then
ExitWithError
fi
fi
fi
if [ -f "data_dump/allocation.schema" ]; then
echo Restoring allocation schema. Please enter password for user sau
if ! $RESTORE -d ${DATABASE_NAME} -U sau data_dump/allocation.schema; then
ExitWithError
fi
fi
if [ -f "data_dump/distribution.schema" ]; then
echo Restoring distribution schema. Please enter password for user sau
if ! $RESTORE -d ${DATABASE_NAME} -U sau data_dump/distribution.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
echo "select * from web_partition.maintain_cell_catch_partition();" >> rmv.sql
## Adding foreign keys
cat index_web.sql >> rmv.sql
cat foreign_key_web.sql >> rmv.sql
cat index_geo.sql >> rmv.sql
cat foreign_key_geo.sql >> rmv.sql
cat index_allocation.sql >> rmv.sql
cat foreign_key_allocation.sql >> rmv.sql
cat index_feru.sql >> rmv.sql
cat index_expedition.sql >> rmv.sql
cat index_admin.sql >> rmv.sql
cat update_fao_area_key.sql >> rmv.sql
## Adding commands to refresh materialized views
if ! $PSQLSAU -d ${DATABASE_NAME} -f refresh_mv.sql -t >> rmv.sql; then
ExitWithError
fi
if ! $PSQLSAU -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