Fix db upgrade with SQLAlchemy 2.0

DB schema upgrade is stuck when SQLAlchemy 2.0 is used because all
operations are not executed within a transaction.

Change-Id: Ief7ea81e78344ea32fb2a295f11a5d5a873251a8
This commit is contained in:
Takashi Kajinami 2024-04-19 16:03:48 +09:00
parent fa8b7dc862
commit 50cc264476
1 changed files with 12 additions and 10 deletions

View File

@ -156,17 +156,19 @@ class Connection(base.Connection):
def upgrade(self, nocreate=False):
cfg = self._get_alembic_config()
cfg.conf = self.conf
if nocreate:
command.upgrade(cfg, "head")
else:
engine = enginefacade.writer.get_engine()
ctxt = migration.MigrationContext.configure(engine.connect())
current_version = ctxt.get_current_revision()
if current_version is None:
models.Base.metadata.create_all(engine, checkfirst=False)
command.stamp(cfg, "head")
else:
engine = enginefacade.writer.get_engine()
with engine.begin() as connection:
cfg.attributes['connection'] = connection
if nocreate:
command.upgrade(cfg, "head")
else:
ctxt = migration.MigrationContext.configure(connection)
current_version = ctxt.get_current_revision()
if current_version is None:
models.Base.metadata.create_all(engine, checkfirst=False)
command.stamp(cfg, "head")
else:
command.upgrade(cfg, "head")
def clear(self):
engine = enginefacade.writer.get_engine()