Skip to content

Commit e9ad64e

Browse files
Merge pull request #26 from RudolfCardinal/alembic-metadata-lock-fix
Close connection after retrieving the current alembic version
2 parents c97edc5 + f15923d commit e9ad64e

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

cardinal_pythonlib/sqlalchemy/alembic_func.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ def get_current_revision(
104104
version_table: table name for Alembic versions
105105
"""
106106
engine = create_engine(database_url, future=True)
107-
conn = engine.connect()
108-
opts = {"version_table": version_table}
109-
mig_context = MigrationContext.configure(conn, opts=opts)
110-
return mig_context.get_current_revision()
107+
with engine.connect() as conn:
108+
opts = {"version_table": version_table}
109+
mig_context = MigrationContext.configure(conn, opts=opts)
110+
return mig_context.get_current_revision()
111111

112112

113113
def get_current_and_head_revision(

cardinal_pythonlib/sqlalchemy/tests/schema_tests.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,14 @@ def _attach_view(
307307
of "selectable") is created after the table is created, and dropped before
308308
the table is dropped, via listeners.
309309
"""
310-
t = table(tablename)
311-
312-
# noinspection PyProtectedMember
313-
t._columns._populate_separate_keys(
314-
col._make_proxy(t) for col in selectable.selected_columns
310+
t = table(
311+
tablename,
312+
*(
313+
Column(c.name, c.type, primary_key=c.primary_key)
314+
for c in selectable.selected_columns
315+
),
315316
)
317+
t.primary_key.update(c for c in t.c if c.primary_key)
316318

317319
event.listen(
318320
metadata,

docs/source/changelog.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,3 +852,10 @@ Quick links:
852852
- Bugfix to ``cardinal_pythonlib.sqlalchemy.sqlserver`` functions as they
853853
were executing unconditionally, regardless of SQLAlchemy dialect (they should
854854
have been conditional to SQL Server).
855+
856+
**2.0.2**
857+
858+
- Bugfix to
859+
:func:`cardinal_pythonlib.sqlalchemy.alembic_func.get_current_revision` where
860+
since SQLAlchemy 2.0, the database connection was persisting, resulting in a
861+
metadata lock.

0 commit comments

Comments
 (0)