forked from volatiletech/sqlboiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boil.sh
executable file
·305 lines (265 loc) · 10.9 KB
/
boil.sh
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/bin/sh
DB_USER="sqlboiler_root_user"
DB_NAME="sqlboiler_model_test"
DB_PASS="sqlboiler"
MSSQLPASS="Sqlboiler@1234"
DRIVER_USER="sqlboiler_driver_user"
DRIVER_DB="sqlboiler_driver_test"
# ====================================
# Building
# ====================================
build() {
driver=$1
buildPath=github.com/volatiletech/sqlboiler/v4
case "${driver}" in
all)
set -o xtrace
go build "${buildPath}"
{ set +o xtrace; } 2>/dev/null
drivers="psql mysql mssql"
shift ;;
psql)
drivers="psql"
shift ;;
mysql)
drivers="mysql"
shift ;;
mssql)
drivers="mssql"
shift ;;
*)
set -o xtrace
go build "$@" "${buildPath}"
{ set +o xtrace; } 2>/dev/null
return ;;
esac
for d in $drivers; do
set -o xtrace
go build "$@" ${buildPath}/drivers/sqlboiler-"${d}"
{ set +o xtrace; } 2>/dev/null
done
}
# ====================================
# Generation
# ====================================
gen() {
db=$1
shift
set -o xtrace
./sqlboiler "$@" --wipe "${db}"
}
# ====================================
# Testing
# ====================================
runtest() {
set -o xtrace
go test -v -race "$@" ./models
}
# ====================================
# Test users
# ====================================
test_user() {
driver=$1
case "${driver}" in
psql)
# Can't use createuser because it interactively promtps for a password
set -o xtrace
psql --host localhost --username postgres --command "create user ${DB_USER} with superuser password '${DB_PASS}';"
;;
mysql)
set -o xtrace
mysql --host localhost --execute "create user ${DB_USER} identified by '${DB_PASS}';"
mysql --host localhost --execute "grant all privileges on *.* to ${DB_USER};"
;;
mssql)
set -o xtrace
sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -Q "create login ${DB_USER} with password = '${MSSQLPASS}';"
sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -Q "alter server role sysadmin add member ${DB_USER};"
;;
mssql-docker)
set -o xtrace
docker exec --interactive --tty mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -Q "create login ${DB_USER} with password = '${MSSQLPASS}';"
docker exec --interactive --tty mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -Q "alter server role sysadmin add member ${DB_USER};"
;;
*)
echo "unknown driver"
;;
esac
}
# ====================================
# Test DBs
# ====================================
test_db() {
driver=$1
case "${driver}" in
psql)
set -o xtrace
env PGPASSWORD=${DB_PASS} dropdb --host localhost --username ${DB_USER} --if-exists ${DB_NAME}
env PGPASSWORD=${DB_PASS} createdb --host localhost --owner ${DB_USER} --username ${DB_USER} ${DB_NAME}
env PGPASSWORD=${DB_PASS} psql --host localhost --username ${DB_USER} --file testdata/psql_test_schema.sql ${DB_NAME}
;;
mysql)
set -o xtrace
mysql --host localhost --user ${DB_USER} --password=${DB_PASS} --execute "drop database if exists ${DB_NAME};"
mysql --host localhost --user ${DB_USER} --password=${DB_PASS} --execute "create database ${DB_NAME};"
mysql --host localhost --user ${DB_USER} --password=${DB_PASS} ${DB_NAME} < testdata/mysql_test_schema.sql
;;
mssql)
set -o xtrace
sqlcmd -S localhost -U ${DB_USER} -P ${MSSQLPASS} -Q "drop database if exists ${DB_NAME}";
sqlcmd -S localhost -U ${DB_USER} -P ${MSSQLPASS} -Q "create database ${DB_NAME}";
sqlcmd -S localhost -U ${DB_USER} -P ${MSSQLPASS} -d ${DB_NAME} -i testdata/mssql_test_schema.sql
;;
mssql-docker)
set -o xtrace
docker exec --interactive --tty mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U ${DB_USER} -P ${MSSQLPASS} -Q "drop database if exists ${DB_NAME}";
docker exec --interactive --tty mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U ${DB_USER} -P ${MSSQLPASS} -Q "create database ${DB_NAME}";
docker exec --interactive --tty mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U ${DB_USER} -P ${MSSQLPASS} -d ${DB_NAME} -i testdata/mssql_test_schema.sql
;;
*)
echo "unknown driver"
;;
esac
}
# ====================================
# Driver test
# ====================================
driver_test() {
driver=$1
shift
cd "drivers/sqlboiler-${driver}/driver"
set -o xtrace
go test "$@"
}
# ====================================
# Driver test users
# ====================================
driver_test_user() {
driver=$1
case "${driver}" in
psql)
set -o xtrace
env PGPASSWORD=${DB_PASS} psql --host localhost --username ${DB_USER} --command "create role ${DRIVER_USER} login nocreatedb nocreaterole password '${DB_PASS}';" ${DRIVER_DB}
env PGPASSWORD=${DB_PASS} psql --host localhost --username ${DB_USER} --command "alter database ${DRIVER_DB} owner to ${DRIVER_USER};" ${DRIVER_DB}
;;
mysql)
set -o xtrace
mysql --host localhost --execute "create user ${DRIVER_USER} identified by '${DB_PASS}';"
mysql --host localhost --execute "grant all privileges on ${DRIVER_DB}.* to ${DRIVER_USER};"
;;
mssql)
set -o xtrace
sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -d "${DRIVER_DB}" -Q "create user ${DRIVER_USER} with password = '${MSSQLPASS}'";
sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -d "${DRIVER_DB}" -Q "grant alter, control to ${DRIVER_USER}";
;;
mssql-docker)
set -o xtrace
docker exec --interactive --tty mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -d "${DRIVER_DB}" -Q "create user ${DRIVER_USER} with password = '${MSSQLPASS}'";
docker exec --interactive --tty mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -d "${DRIVER_DB}" -Q "grant alter, control to ${DRIVER_USER}";
;;
*)
echo "unknown driver"
;;
esac
}
# ====================================
# Driver test databases
# ====================================
driver_test_db() {
driver=$1
case "${driver}" in
psql)
set -o xtrace
env PGPASSWORD=${DB_PASS} createdb --host localhost --username ${DB_USER} --owner ${DB_USER} ${DRIVER_DB}
;;
mysql)
set -o xtrace
mysql --host localhost --user ${DB_USER} --password=${DB_PASS} --execute "create database ${DRIVER_DB};"
;;
mssql)
set -o xtrace
sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -Q "create database ${DRIVER_DB};"
sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -d "${DRIVER_DB}" -Q "exec sp_configure 'contained database authentication', 1;"
sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -d "${DRIVER_DB}" -Q "reconfigure"
sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -d "${DRIVER_DB}" -Q "alter database ${DRIVER_DB} set containment = partial;"
;;
mssql-docker)
set -o xtrace
docker exec --interactive --tty mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -Q "create database ${DRIVER_DB};"
docker exec --interactive --tty mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -d "${DRIVER_DB}" -Q "exec sp_configure 'contained database authentication', 1;"
docker exec --interactive --tty mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -d "${DRIVER_DB}" -Q "reconfigure"
docker exec --interactive --tty mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -d "${DRIVER_DB}" -Q "alter database ${DRIVER_DB} set containment = partial;"
;;
*)
echo "unknown driver"
;;
esac
}
# ====================================
# MSSQL stuff
# ====================================
mssql_run() {
if test "attach" = "${1}"; then
args="--interactive --tty"
else
args="--detach"
fi
set -o xtrace
docker run $args --rm \
--env 'ACCEPT_EULA=Y' --env "SA_PASSWORD=${MSSQLPASS}" \
--publish 1433:1433 \
--volume "${PWD}/testdata/mssql_test_schema.sql:/testdata/mssql_test_schema.sql" \
--name mssql \
mcr.microsoft.com/mssql/server:2019-latest
}
mssql_stop() {
set -o xtrace
docker stop mssql
}
mssql_sqlcmd() {
set -o xtrace
docker exec --interactive --tty mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P "${MSSQLPASS}" -Q "$@"
}
# ====================================
# Clean
# ====================================
clean() {
set -o xtrace
rm -f ./sqlboiler
rm -f ./sqlboiler-*
}
# ====================================
# CLI
# ====================================
command=$1
shift
case "${command}" in
build) build "$@" ;;
gen) gen "$@" ;;
test) runtest "$@" ;;
test-user) test_user "$1" ;;
test-db) test_db "$1" ;;
driver-test) driver_test "$@" ;;
driver-test-db) driver_test_db "$1" ;;
driver-test-user) driver_test_user "$1" ;;
mssql-run) mssql_run "$@" ;;
mssql-stop) mssql_stop "$@" ;;
mssql-sqlcmd) mssql_sqlcmd "$@" ;;
clean) clean ;;
*)
echo "./boil.sh command [args]"
echo "Helpers to build, test and develop on sqlboiler"
echo
echo "Commands:"
echo " build [all|driver] - builds the driver (or all drivers if all), or sqlboiler if omitted"
echo " gen <driver> [args] - generates models for driver, passing args along to sqlboiler"
echo " test [args] - runs model tests, passing args to the test binary"
echo " test-user <driver> - creates a user for the tests (superuser)"
echo " test-db <driver> - recreates test datbase for driver using test-user"
echo " driver-test <driver> [args] - runs tests for the driver"
echo " driver-test-db <driver> - create driver db (run before driver-test-user)"
echo " driver-test-user <driver> - creates a user for the driver tests (unprivileged)"
echo " mssql-run [attach] - run mssql docker container, if attach is present will not daemonize"
echo " mssql-stop - stop mssql docker container"
echo " mssql-sqlcmd [args...] - run sql query using sqlcmd in docker container"
esac