2014-11-18 19:07:33 +00:00
set -e
2016-08-05 19:22:17 +00:00
PARSED_DOCKER_HOST = ` echo $DOCKER_HOST | sed 's/tcp:\/\///' | sed 's/:.*//' `
DOCKER_IP = " ${ PARSED_DOCKER_HOST :- 127 .0.0.1 } "
2014-11-18 19:07:33 +00:00
MYSQL_CONFIG_OVERRIDE = " {\"DB_URI\":\"mysql+pymysql://root:password@ $DOCKER_IP /genschema\"} "
2015-04-21 22:01:40 +00:00
PERCONA_CONFIG_OVERRIDE = " {\"DB_URI\":\"mysql+pymysql://root:password@ $DOCKER_IP /genschema\"} "
2014-11-18 19:07:33 +00:00
PGSQL_CONFIG_OVERRIDE = " {\"DB_URI\":\"postgresql://postgres@ $DOCKER_IP /genschema\"} "
2014-10-07 19:29:56 +00:00
up_mysql( ) {
# Run a SQL database on port 3306 inside of Docker.
2018-05-07 13:45:57 +00:00
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD = password -d mysql:5.7
2014-10-07 19:29:56 +00:00
2017-03-19 17:14:18 +00:00
echo 'Sleeping for 25...'
sleep 25
2014-10-07 19:29:56 +00:00
# Add the database to mysql.
docker run --rm --link mysql:mysql mysql sh -c 'echo "create database genschema" | mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -ppassword'
}
down_mysql( ) {
2018-05-07 13:45:57 +00:00
docker kill mysql || true
docker rm -v mysql || true
2014-10-07 19:29:56 +00:00
}
2014-12-22 20:34:17 +00:00
up_mariadb( ) {
# Run a SQL database on port 3306 inside of Docker.
docker run --name mariadb -p 3306:3306 -e MYSQL_ROOT_PASSWORD = password -d mariadb
2017-03-19 17:14:18 +00:00
echo 'Sleeping for 25...'
sleep 25
2014-12-22 20:34:17 +00:00
# Add the database to mysql.
2014-12-23 23:34:02 +00:00
docker run --rm --link mariadb:mariadb mariadb sh -c 'echo "create database genschema" | mysql -h"$MARIADB_PORT_3306_TCP_ADDR" -P"$MARIADB_PORT_3306_TCP_PORT" -uroot -ppassword'
2014-12-22 20:34:17 +00:00
}
down_mariadb( ) {
2018-05-07 13:45:57 +00:00
docker kill mariadb || true
docker rm -v mariadb || true
2014-12-22 20:34:17 +00:00
}
2014-12-23 23:34:02 +00:00
up_percona( ) {
# Run a SQL database on port 3306 inside of Docker.
2015-04-21 22:01:40 +00:00
docker run --name percona -p 3306:3306 -e MYSQL_ROOT_PASSWORD = password -d percona
2014-12-23 23:34:02 +00:00
2017-03-19 17:14:18 +00:00
echo 'Sleeping for 25...'
sleep 25
2014-12-23 23:34:02 +00:00
# Add the daabase to mysql.
2015-04-21 22:01:40 +00:00
docker run --rm --link percona:percona percona sh -c 'echo "create database genschema" | mysql -h $PERCONA_PORT_3306_TCP_ADDR -uroot -ppassword'
2014-12-23 23:34:02 +00:00
}
down_percona( ) {
2018-05-07 13:45:57 +00:00
docker kill percona || true
docker rm -v percona || true
2014-12-23 23:34:02 +00:00
}
2014-10-07 19:29:56 +00:00
up_postgres( ) {
# Run a SQL database on port 5432 inside of Docker.
docker run --name postgres -p 5432:5432 -d postgres
# Sleep for 5s to get SQL get started.
echo 'Sleeping for 5...'
sleep 5
# Add the database to postgres.
docker run --rm --link postgres:postgres postgres sh -c 'echo "create database genschema" | psql -h "$POSTGRES_PORT_5432_TCP_ADDR" -p "$POSTGRES_PORT_5432_TCP_PORT" -U postgres'
}
down_postgres( ) {
2018-05-07 13:45:57 +00:00
docker kill postgres || true
docker rm -v postgres || true
2014-10-07 19:29:56 +00:00
}
gen_migrate( ) {
2016-08-05 19:22:17 +00:00
# Generate a database with the schema as defined by the existing alembic model.
2014-11-18 19:07:33 +00:00
QUAY_OVERRIDE_CONFIG = $1 PYTHONPATH = . alembic upgrade head
2014-10-07 20:09:30 +00:00
2016-08-05 19:22:17 +00:00
2014-10-07 20:09:30 +00:00
# Generate the migration to the current model.
2014-11-18 19:07:33 +00:00
QUAY_OVERRIDE_CONFIG = $1 PYTHONPATH = . alembic revision --autogenerate -m " $2 "
2014-10-07 19:29:56 +00:00
}
test_migrate( ) {
2016-08-05 19:22:17 +00:00
# Generate a database with the schema as defined by the existing alembic model.
echo '> Running upgrade'
2018-05-07 13:45:57 +00:00
TEST_MIGRATE = true QUAY_OVERRIDE_CONFIG = $1 PYTHONPATH = . alembic upgrade head
2014-10-07 19:29:56 +00:00
# Downgrade to verify it works in both directions.
2016-08-05 19:22:17 +00:00
echo '> Running downgrade'
2014-10-07 19:29:56 +00:00
COUNT = ` ls data/migrations/versions/*.py | wc -l | tr -d ' ' `
2018-05-07 13:45:57 +00:00
TEST_MIGRATE = true QUAY_OVERRIDE_CONFIG = $1 PYTHONPATH = . alembic downgrade " - $COUNT "
2014-10-07 19:29:56 +00:00
}
2018-05-07 13:45:57 +00:00
down_mysql
down_postgres
down_mariadb
down_percona
2014-10-07 19:29:56 +00:00
# Test (and generate, if requested) via MySQL.
echo '> Starting MySQL'
up_mysql
if [ ! -z " $@ " ]
then
set +e
echo '> Generating Migration'
2014-11-18 19:07:33 +00:00
gen_migrate $MYSQL_CONFIG_OVERRIDE " $@ "
2014-10-07 19:29:56 +00:00
set -e
fi
echo '> Testing Migration (mysql)'
set +e
2014-11-18 19:07:33 +00:00
test_migrate $MYSQL_CONFIG_OVERRIDE
2014-10-07 19:29:56 +00:00
set -e
down_mysql
2015-10-23 20:24:40 +00:00
# Test via Postgres.
echo '> Starting Postgres'
up_postgres
echo '> Testing Migration (postgres)'
set +e
test_migrate $PGSQL_CONFIG_OVERRIDE
set -e
down_postgres
2014-12-22 20:34:17 +00:00
# Test via MariaDB.
echo '> Starting MariaDB'
up_mariadb
2014-12-23 23:34:02 +00:00
2014-12-22 20:34:17 +00:00
echo '> Testing Migration (mariadb)'
set +e
test_migrate $MYSQL_CONFIG_OVERRIDE
set -e
down_mariadb
2014-12-23 23:34:02 +00:00
# Test via Percona.
echo '> Starting Percona'
up_percona
echo '> Testing Migration (percona)'
set +e
test_migrate $PERCONA_CONFIG_OVERRIDE
set -e
down_percona