diff --git a/data/migrations/env.py b/data/migrations/env.py index d64cf4ee7..5f7bb986d 100644 --- a/data/migrations/env.py +++ b/data/migrations/env.py @@ -1,4 +1,7 @@ from __future__ import with_statement + +import os + from alembic import context from sqlalchemy import engine_from_config, pool from logging.config import fileConfig @@ -12,8 +15,12 @@ from util.morecollections import AttrDict # this is the Alembic Config object, which provides # access to the values within the .ini file in use. +db_uri = unquote(app.config['DB_URI']) +if 'GENMIGRATE' in os.environ: + db_uri = 'mysql+pymysql://root:password@192.168.59.103/genschema' + config = context.config -config.set_main_option('sqlalchemy.url', unquote(app.config['DB_URI'])) +config.set_main_option('sqlalchemy.url', db_uri) # Interpret the config file for Python logging. # This line sets up loggers basically. @@ -57,7 +64,7 @@ def run_migrations_online(): """ - if isinstance(db.obj, SqliteDatabase): + if isinstance(db.obj, SqliteDatabase) and not 'GENMIGRATE' in os.environ: print ('Skipping Sqlite migration!') return diff --git a/data/migrations/generate-schema-migration.sh b/data/migrations/generate-schema-migration.sh new file mode 100755 index 000000000..c7db14b32 --- /dev/null +++ b/data/migrations/generate-schema-migration.sh @@ -0,0 +1,21 @@ +set -e + +# Run a MySQL database on port 3306 inside of Docker. +docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql + +# Sleep for 5s to get MySQL get started. +echo 'Sleeping for 5...' +sleep 5 + +# Add the database to mysql. +docker run --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' + +# Generate a SQLite database with the schema as defined by the existing alembic model. +GENMIGRATE=true PYTHONPATH=. alembic upgrade head + +# Generate the migration to the current model. +GENMIGRATE=true PYTHONPATH=. alembic revision --autogenerate -m "$@" + +# Kill the MySQL instance. +docker kill mysql +docker rm mysql \ No newline at end of file