Created
January 20, 2017 04:37
-
-
Save mmerickel/dcc06bbe749fce05b1a481814201bf9e to your computer and use it in GitHub Desktop.
alembic db:migrate script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from alembic.config import Config | |
from alembic.command import stamp | |
from alembic.command import upgrade | |
from alembic.migration import MigrationContext | |
from myapp.model.meta import metadata | |
from myapp.model.meta import get_engine | |
log = __import__('logging').getLogger(__name__) | |
def get_current_version(engine): | |
conn = engine.connect() | |
try: | |
ctx = MigrationContext.configure(conn) | |
return ctx.get_current_revision() | |
finally: | |
conn.close() | |
def main(cli, args): | |
cfg = Config(args.config_file) | |
engine = get_engine(cli.settings, check_version=False) | |
version = get_current_version(engine) | |
if version is None: | |
log.debug('initializing the database') | |
metadata.create_all(bind=engine) | |
stamp(cfg, 'head') | |
else: | |
log.debug('executing migrations') | |
upgrade(cfg, args.rev) | |
version = get_current_version(engine) | |
log.info('database is now at revision=%s', version) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment