-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbeam-docs.sh
60 lines (45 loc) · 2 KB
/
beam-docs.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
#!/bin/sh
set -e
. ${BEAM_DOCS_LIBRARY}
CHINOOK_MYSQL_URL="https://raw.githubusercontent.com/lerocha/chinook-database/master/ChinookDatabase/DataSources/Chinook_MySql.sql"
EXPECTED_SHA256="409d9f34e6ab9f5a3a5dba58d9fa2a6484263e68659b5b7b22bbe97fd7be5353"
HOST=$1
PORT=$2
USER=$3
PASSWORD=$4
DATABASE=$5
run_mysql () {
if [[ ! -z $PASSWORD ]]; then
PASSWORD_ARG="--password $PASSWORD"
else
PASSWORD_ARG=""
fi
mysql -h $HOST -P $PORT --user=$USER $PASSWORD_ARG --protocol=TCP -N "$@"
}
db_exists() {
echo "SHOW DATABASES" | run_mysql | grep -Fx "$1" >/dev/null
}
print_open_statement() {
echo "chinook <- connect defaultConnectInfo {"
echo " connectHost = \"$HOST\", connectPort = $PORT,"
echo " connectUser = \"$USER\", connectPassword = \"$PASSWORD\","
echo " connectDatabase = \"$DATABASE\","
echo " connectOptions = [ Base.CharsetName \"utf8\", Base.Protocol Base.TCP ] }"
echo "autocommit chinook False -- Do not auto commit DDL statements"
}
if db_exists "$DATABASE"; then
print_open_statement
exit 0
fi
if [ ! -f chinook-data/Chinook_MySql.sql ]; then
beam_doc_status "Downloading MySql chinook data ..."
download "chinook-data/Chinook_MySql.sql" "$CHINOOK_MYSQL_URL" "$EXPECTED_SHA256" "tail -c +4 | sed '/CREATE TABLE/,\$!d'"
fi
beam_doc_status "Creating temporary MySql database ${DATABASE}..."
echo "CREATE DATABASE ${DATABASE}" | run_mysql
(echo "START TRANSACTION; SET autocommit=0;"; pv chinook-data/Chinook_MySql.sql; echo "COMMIT;") | run_mysql "${DATABASE}"
echo "ALTER TABLE \`InvoiceLine\` DROP FOREIGN KEY \`FK_InvoiceLineInvoiceId\`" | run_mysql "${DATABASE}"
echo "ALTER TABLE \`Invoice\` MODIFY \`InvoiceId\` INTEGER NOT NULL AUTO_INCREMENT" | run_mysql "${DATABASE}"
echo "ALTER TABLE \`InvoiceLine\` ADD FOREIGN KEY (\`InvoiceId\`) REFERENCES \`Invoice\`(\`InvoiceId\`) ON DELETE NO ACTION ON UPDATE NO ACTION" | run_mysql "${DATABASE}"
beam_doc_status "Success"
print_open_statement