make db scripts accept one db at a time

This commit is contained in:
Min RK
2017-09-28 13:28:17 +02:00
parent f733a91d7c
commit 4534499aad
2 changed files with 44 additions and 25 deletions

View File

@@ -2,21 +2,35 @@
# source this file to setup postgres and mysql
# for local testing (as similar as possible to docker)
DOCKER="docker run --rm -d --name"
set -e
export MYSQL_HOST=127.0.0.1
export MYSQL_TCP_PORT=13306
export PGHOST=127.0.0.1
NAME="hub-test-$DB"
DOCKER_RUN="docker run --rm -d --name $NAME"
set -ex
docker rm -f hub-test-mysql hub-test-postgres 2>/dev/null || true
docker rm -f "$NAME" 2>/dev/null || true
$DOCKER hub-test-mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=1 -p 3306:3306 mysql:5.7
$DOCKER hub-test-postgres -p 5432:5432 postgres:9.5
set +x
case "$DB" in
"mysql")
RUN_ARGS="-e MYSQL_ALLOW_EMPTY_PASSWORD=1 -p $MYSQL_TCP_PORT:3306 mysql:5.7"
CHECK="mysql --user root -e \q"
;;
"postgres")
RUN_ARGS="-p 5432:5432 postgres:9.5"
CHECK="psql --user postgres -c \q"
;;
*)
echo '$DB must be mysql or postgres'
exit 1
esac
echo -n 'waiting for postgres'
$DOCKER_RUN $RUN_ARGS
echo -n "waiting for $DB "
for i in {1..60}; do
if psql --user postgres -c '\q' 2>/dev/null; then
if $CHECK; then
echo 'done'
break
else
@@ -24,21 +38,13 @@ for i in {1..60}; do
sleep 1
fi
done
$CHECK
echo -n 'waiting for mysql'
for i in {1..60}; do
if mysql --user root -e '\q' 2>/dev/null; then
echo 'done'
break
else
echo -n '.'
sleep 1
fi
done
echo -e "
Set these environment variables:
export MYSQL_HOST=127.0.0.1
export MYSQL_TCP_PORT=13306
export PGHOST=127.0.0.1
"

View File

@@ -1,14 +1,27 @@
#!/usr/bin/env bash
# initialize jupyterhub databases for testing
set -e
MYSQL="mysql --user root -e "
PSQL="psql --user postgres -c "
case "$DB" in
"mysql")
EXTRA_CREATE='CHARACTER SET utf8 COLLATE utf8_general_ci'
SQL="$MYSQL"
;;
"postgres")
SQL="$PSQL"
;;
*)
echo '$DB must be mysql or postgres'
exit 1
esac
set -x
# drop databases if they don't exist
$MYSQL 'DROP DATABASE jupyterhub_upgrade' 2>/dev/null || true
$PSQL 'DROP DATABASE jupyterhub_upgrade' 2>/dev/null || true
# create the databases
$MYSQL 'CREATE DATABASE jupyterhub_upgrade CHARACTER SET utf8 COLLATE utf8_general_ci;'
$PSQL 'CREATE DATABASE jupyterhub_upgrade;'
$SQL 'DROP DATABASE jupyterhub;' 2>/dev/null || true
$SQL "CREATE DATABASE jupyterhub ${EXTRA_CREATE};"
$SQL 'DROP DATABASE jupyterhub_upgrade;' 2>/dev/null || true
$SQL "CREATE DATABASE jupyterhub_upgrade ${EXTRA_CREATE};"