-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestPostgresMigrations.sh
More file actions
288 lines (223 loc) · 7.44 KB
/
testPostgresMigrations.sh
File metadata and controls
288 lines (223 loc) · 7.44 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash
# This script is coded under the assumtion that it's only used as a development
# tool, only on development boxes. As such, some parts of it will likely not
# play nice in test or production build environments.
# Passing in a Ruby version value only makes sense in RVM-enabled envs.
unset USE_RUBY_VERSION
[ -n "$1" ] && USE_RUBY_VERSION="$1"
COMP_DBS="./compare_databases.rb"
DAO_SQL="common/build/libs/dao.sql"
#SQL_GEN="./generatePostgresSql.sh"
SQL_GEN="./generateDaoSql.sh"
MIN_RUBY_VERSION="2.1.9"
DB_RAKEFILE="sql/Rakefile"
EXPECTED_PSQL="/usr/local/bin/psql"
SL_DB_ROLE="Administrator"
DEFAULT_DB_ROLE="postgres"
FRONTEND_DEV_CONFIG="'/redline-main/product/Documents/Bluestorm Front End Developer Readme.docx'"
# Note on 'rake' gem: Not all Ruby env managers/install approaches automatically
# install 'rake', and although this script uses RVM if it's available (and RVM
# does include 'rake' as part of a Ruby install), we do not assume RVM is
# available, nor that the Ruby install to this machine included 'rake'.
GEM_DEPENDENCIES="rake"
GEM_DEPENDENCIES="$GEM_DEPENDENCIES pg"
GEM_DEPENDENCIES="$GEM_DEPENDENCIES multi_json"
GEM_DEPENDENCIES="$GEM_DEPENDENCIES i18n"
GEM_DEPENDENCIES="$GEM_DEPENDENCIES activesupport"
GEM_DEPENDENCIES="$GEM_DEPENDENCIES builder"
GEM_DEPENDENCIES="$GEM_DEPENDENCIES activemodel"
GEM_DEPENDENCIES="$GEM_DEPENDENCIES tzinfo"
GEM_DEPENDENCIES="$GEM_DEPENDENCIES arel"
GEM_DEPENDENCIES="$GEM_DEPENDENCIES activerecord"
GEM_DEPENDENCIES="$GEM_DEPENDENCIES open4"
if ! [ -f "$COMP_DBS" ] || ! [ -r "$COMP_DBS" ]
then
echo
echo "Either can't find Ruby script '$COMP_DBS' or it's not readable. Exiting."
echo "Expected '$COMP_DBS' to be in dir '$PWD'."
exit 1
fi
if ! [ -f "$SQL_GEN" ] || ! [ -x "$SQL_GEN" ]
then
echo
echo "Either can't find script '$SQL_GEN' or it's not executable. Exiting."
echo "Expected to find '$SQL_GEN' below dir '$PWD'."
exit 1
fi
echo
echo "Launching $SQL_GEN"
echo
if ! $SQL_GEN
then
echo
echo "'$SQL_GEN' failed. Exiting."
echo "Try running './testAll.sh' before '${0##*/}'."
exit 1
fi
if ! [ -f "$DAO_SQL" ]
then
echo
echo "Requied file '$DAO_SQL' does not exist. Exiting."
echo "Expected to find '$DAO_SQL' below dir '$PWD'."
echo "'$SQL_GEN' generates file '$DAO_SQL'."
exit 1
fi
if ! [ -r "$DAO_SQL" ] || ! chmod u+r "$DAO_SQL"
then
echo
echo "Requied file '$DAO_SQL' is not readable. Exiting."
exit 1
fi
if ! [ -f "$DB_RAKEFILE" ]
then
echo
echo "Requied file '$DB_RAKEFILE' does not exist. Exiting."
echo "Expected to find '$DB_RAKEFILE' below dir '$PWD'."
exit 1
fi
if ! [ -r "$DB_RAKEFILE" ] || ! chmod u+r "$DAO_SQL"
then
echo
echo "Requied file '$DB_RAKEFILE' is not readable. Exiting."
exit 1
fi
echo
if ! [ -f "$HOME/.rvm/scripts/rvm" ]
then
if [ -n "$USE_RUBY_VERSION" ]
then
echo "Passing in a Ruby version only makes sense in RVM-enabled envs."
echo
fi
echo "RVM not found. If a Ruby env exists on this host, but it does not"
echo "successfully run '$COMP_DBS', see ${FRONTEND_DEV_CONFIG}"
echo "for RVM-based Ruby install instructions."
else
echo "Configuring Ruby env with RVM."
source "$HOME/.rvm/scripts/rvm"
if [ -z "$USE_RUBY_VERSION" ]
then
RUBY_VERSIONS=$( rvm list |
grep -Eo 'ruby-.* ' |
sed -r 's| .*$||' |
cut -d '-' -f2 )
unset RUBY_VNS
for V in $RUBY_VERSIONS
do
RUBY_VNS="$RUBY_VNS $V"
done
# Assumptions about the format of Ruby version numbers are likely
# fragile, but they do the trick for now. It'll be obvious when they no
# longer do, and they can be addressed at that time.
MAX_RUBY_VERSION=$( echo "$RUBY_VNS" |
tr ' ' '\n' |
sort -br |
tr '\n' ' ' |
cut -d ' ' -f1 )
echo
echo "Setting Ruby to latest of installed versions: $(
echo $RUBY_VERSIONS | tr '\n' ' ' )"
USE_RUBY_VERSION="$MAX_RUBY_VERSION"
unset RUBY_VERSIONS RUBY_VNS
fi
if ! rvm use $USE_RUBY_VERSION
then
echo
echo "RVM failed to set Ruby version. Exiting."
exit 1
fi
fi
# We test for Ruby independent of, and after, attempting to use RVM, so that
# non-RVM hosts that have a functioning Ruby env can also use this script.
if ! which ruby >/dev/null
then
echo
echo "Can't find 'ruby'. '${0##*/}' is mostly a Ruby script wrapper. Exiting."
echo "See the $FRONTEND_DEV_CONFIG for Ruby install instructions."
exit 1
fi
# Assumptions about the format of Ruby version numbers are likely fragile, but
# they do the trick for now. It'll be obvious when they no longer do, and they
# can be addressed at that time.
RV=$( ruby -v | cut -d ' ' -f2 | sed -r 's|[a-zA-Z].*$||' | tr -d '.' )
if [ "$RV" -lt "$( echo $MIN_RUBY_VERSION | tr -d '.' )" ]
then
RV=$( ruby -v | cut -d ' ' -f2 | sed -r 's|[a-zA-Z].*$||' )
echo
echo "Min required Ruby: $MIN_RUBY_VERSION Actual: ${RV} Exiting."
exit 1
fi
echo
echo "Verifying Ruby gem dependencies are installed."
for G in $GEM_DEPENDENCIES
do
if ! gem list "$G" -i --local &>/dev/null
then
echo
echo "Required Ruby gem '$G' is not available. Exiting."
echo "See $FRONTEND_DEV_CONFIG for Ruby install instructions."
exit 1
fi
done
echo
echo "Confirming 'psql' is available and Postgres DB user role '$SL_DB_ROLE' exists."
ACTUAL_PSQL=$( which psql )
if [ -z "$ACTUAL_PSQL" ]
then
echo
echo "Can't find required 'psql'. Exiting."
echo "See $FRONTEND_DEV_CONFIG for Postgres install instructions."
exit 1
fi
if [ "$EXPECTED_PSQL" != "$ACTUAL_PSQL" ]
then
echo
echo "'$COMP_DBS' requires: '$EXPECTED_PSQL'; actual: '$ACTUAL_PSQL'. Exiting."
echo "See the Ruby section of $FRONTEND_DEV_CONFIG"
echo "for how to set a soft link to the actual location in the dir that"
echo "'$COMP_DBS' expects."
exit 1
fi
PATH_SEPARATOR=":"
if uname | grep -E "(CYGWIN|Windows|WINDOWS)+" &>/dev/null
then
PATH_SEPARATOR=";"
echo
echo "Setting PGHOST to 'localhost' for Postgres client C library use in Cygwin env."
# Without this MANY direct 'psql' calls from within Ruby scripts,
# and numerous direct Postgres DB accesses from the 'pg' gem,
# would need updated to not by default try to use a Unix socket to
# connect to the local Postgres DB (at least when running in Cygwin).
export PGHOST=localhost
fi
if ! psql -U $DEFAULT_DB_ROLE \
-c "SELECT rolname FROM pg_roles WHERE rolname='$SL_DB_ROLE'" |
grep -o "$SL_DB_ROLE" &>/dev/null
then
echo
echo \
"Required DB role '$SL_DB_ROLE' does not seem to exist. Trying to create it."
if ! psql -U $DEFAULT_DB_ROLE \
-c "CREATE USER $SL_DB_ROLE WITH SUPERUSER;"
then
echo "Failed to created DB role '$SL_DB_ROLE'. Exiting."
exit 1
fi
echo "Role '$SL_DB_ROLE' created."
fi
unset RUBYLIB
# Make SL Ruby gems available to COMP_DBS, which needs at least one of them.
for D in $( find ${PWD%/*/*}/rubygems/ -maxdepth 2 -type d -name lib )
do
RUBYLIB="$RUBYLIB$D$PATH_SEPARATOR"
done
echo
echo "PATH=$PATH"
export PATH
echo
echo "RUBYLIB=$RUBYLIB"
export RUBYLIB
echo
echo "Launching $COMP_DBS"
echo
ruby "$COMP_DBS" "$DAO_SQL" "$DB_RAKEFILE"