For some reason the alembic connection wasn't seeing any tables that existed however the existing connection works well and we shouldn't be creating a brand new one when we have one ready to use. I've used a little bit of a hack due to our old version of alembic. After 0.7.5 of alembic it offers a `Config.attributes` dictionary which is designed to allow you to pass your connection/engine/whatever to the env.py config script so you're not required to create new ones. As we're on an older version I just create a dictionary with the same name and use it as otherwise documented. It seems this is the suggested fix for #5395 and it fixes #5398 too.
This commit is contained in:
parent
8de5a9319a
commit
4c77f3d563
@ -40,10 +40,17 @@ class AlembicMigrationManager(object):
|
|||||||
def __init__(self, session):
|
def __init__(self, session):
|
||||||
root_dir = os.path.abspath(os.path.dirname(os.path.dirname(
|
root_dir = os.path.abspath(os.path.dirname(os.path.dirname(
|
||||||
os.path.dirname(__file__))))
|
os.path.dirname(__file__))))
|
||||||
|
|
||||||
|
self.session = session
|
||||||
|
self.engine = self.session.get_bind()
|
||||||
alembic_cfg_path = os.path.join(root_dir, 'alembic.ini')
|
alembic_cfg_path = os.path.join(root_dir, 'alembic.ini')
|
||||||
self.alembic_cfg = Config(alembic_cfg_path)
|
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):
|
def get_current_revision(self):
|
||||||
context = MigrationContext.configure(self.session.bind)
|
context = MigrationContext.configure(self.session.bind)
|
||||||
|
@ -47,22 +47,14 @@ def run_migrations_online():
|
|||||||
and associate a connection with the context.
|
and associate a connection with the context.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
engine = engine_from_config(
|
connection = config.attributes["session"].get_bind()
|
||||||
config.get_section(config.config_ini_section),
|
|
||||||
prefix='sqlalchemy.',
|
|
||||||
poolclass=pool.NullPool)
|
|
||||||
|
|
||||||
connection = engine.connect()
|
|
||||||
context.configure(
|
context.configure(
|
||||||
connection=connection,
|
connection=connection,
|
||||||
target_metadata=target_metadata
|
target_metadata=target_metadata
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
with context.begin_transaction():
|
||||||
with context.begin_transaction():
|
context.run_migrations()
|
||||||
context.run_migrations()
|
|
||||||
finally:
|
|
||||||
connection.close()
|
|
||||||
|
|
||||||
if context.is_offline_mode():
|
if context.is_offline_mode():
|
||||||
run_migrations_offline()
|
run_migrations_offline()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user