def __init__(self, session):
root_dir = os.path.abspath(os.path.dirname(os.path.dirname(
os.path.dirname(__file__))))
+
+ self.session = session
+ self.engine = self.session.get_bind()
alembic_cfg_path = os.path.join(root_dir, 'alembic.ini')
self.alembic_cfg = Config(alembic_cfg_path)
- self.alembic_cfg.set_main_option("sqlalchemy.url", str(session.get_bind().url))
- self.session = session
+
+ # TODO: After 0.7.5 alembic has Config.attributes already made, once
+ # we're able to update, please remove this hack!
+ self.alembic_cfg.attributes = {}
+ self.alembic_cfg.attributes["session"] = self.session
+ self.alembic_cfg.set_main_option("qlalchemy.url", str(self.engine.url))
def get_current_revision(self):
context = MigrationContext.configure(self.session.bind)
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()
+ connection = config.attributes["session"].get_bind()
context.configure(
connection=connection,
target_metadata=target_metadata
)
- try:
- with context.begin_transaction():
- context.run_migrations()
- finally:
- connection.close()
+ with context.begin_transaction():
+ context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()