diff options
author | Christopher Allan Webber <cwebber@dustycloud.org> | 2014-09-22 13:58:13 -0500 |
---|---|---|
committer | Christopher Allan Webber <cwebber@dustycloud.org> | 2014-09-22 13:58:13 -0500 |
commit | a6252cbf211691aa9d81fd535f5af7bcdbbd6620 (patch) | |
tree | ca1131ba7a8ff0437d392c20a70f17ee7470e3dd /mediagoblin/db/migrations/env.py | |
parent | c47f402d7ee42e88ee1086351010dd598da6c0a5 (diff) | |
parent | 6430ae97eca57f4db4bcef54436df6c2abcd21ad (diff) | |
download | mediagoblin-a6252cbf211691aa9d81fd535f5af7bcdbbd6620.tar.lz mediagoblin-a6252cbf211691aa9d81fd535f5af7bcdbbd6620.tar.xz mediagoblin-a6252cbf211691aa9d81fd535f5af7bcdbbd6620.zip |
Merge branch 'merge-python3-port'
Conflicts:
setup.py
Diffstat (limited to 'mediagoblin/db/migrations/env.py')
-rw-r--r-- | mediagoblin/db/migrations/env.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/mediagoblin/db/migrations/env.py b/mediagoblin/db/migrations/env.py new file mode 100644 index 00000000..712b6164 --- /dev/null +++ b/mediagoblin/db/migrations/env.py @@ -0,0 +1,71 @@ +from __future__ import with_statement +from alembic import context +from sqlalchemy import engine_from_config, pool +from logging.config import fileConfig + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +fileConfig(config.config_file_name) + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +target_metadata = None + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure(url=url, target_metadata=target_metadata) + + with context.begin_transaction(): + context.run_migrations() + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + engine = engine_from_config( + config.get_section(config.config_ini_section), + prefix='sqlalchemy.', + poolclass=pool.NullPool) + + connection = engine.connect() + context.configure( + connection=connection, + target_metadata=target_metadata + ) + + try: + with context.begin_transaction(): + context.run_migrations() + finally: + connection.close() + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() + |