Add a script for generating schema migrations. Should be run from the root quay directory.
This commit is contained in:
parent
e5055763f6
commit
70e0aba257
2 changed files with 30 additions and 2 deletions
|
@ -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
|
||||
|
||||
|
|
21
data/migrations/generate-schema-migration.sh
Executable file
21
data/migrations/generate-schema-migration.sh
Executable file
|
@ -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
|
Reference in a new issue