-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebuild-db.sh
executable file
·49 lines (41 loc) · 999 Bytes
/
rebuild-db.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
#!/usr/bin/env bash
die() {
echo "ERROR: $*" >&2
exit 1
}
mysql() {
command mysql --database="$DB_NAME" --host="$DB_HOST" --password="$DB_PASS" --user="$DB_USER""$@"
}
if [ -e ".env" ]; then
source ".env"
fi
if ! [ "$DB_NAME" ]; then
die "Database configuration variables not defined"
fi
while getopts dt name; do
case "$name" in
(t)
use_test="1"
;;
(d)
do_drop="1"
;;
(?)
echo "Unknown option"
exit 1
;;
esac
done
if [ "$do_drop" ]; then
mysql <<<"DROP DATABASE IF EXISTS $DB_NAME; CREATE DATABASE $DB_NAME;" ||
die "Failed dropping/creating"
echo "Dropped and created database '$DB_NAME'"
fi
mysql < schema.sql ||
die "Failed loading schema"
echo "Loaded schema for database '$DB_NAME'"
if [ "$use_test" ]; then
mysql < test-data.sql ||
die "Failed adding test data"
echo "Loaded test data for database '$DB_NAME'"
fi