From f7e31097c5a5b0e228c9e80757bfb571216e78ba Mon Sep 17 00:00:00 2001 From: Martin Burchell Date: Thu, 13 Feb 2025 11:35:45 +0000 Subject: [PATCH 1/3] Close connection when retrieving current alembic version CamCOPS upgrade_db was hanging when upgrading the database due to this open connection causing a metadata lock. --- cardinal_pythonlib/sqlalchemy/alembic_func.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cardinal_pythonlib/sqlalchemy/alembic_func.py b/cardinal_pythonlib/sqlalchemy/alembic_func.py index 9906626..1896ce6 100644 --- a/cardinal_pythonlib/sqlalchemy/alembic_func.py +++ b/cardinal_pythonlib/sqlalchemy/alembic_func.py @@ -104,10 +104,10 @@ def get_current_revision( version_table: table name for Alembic versions """ engine = create_engine(database_url, future=True) - conn = engine.connect() - opts = {"version_table": version_table} - mig_context = MigrationContext.configure(conn, opts=opts) - return mig_context.get_current_revision() + with engine.connect() as conn: + opts = {"version_table": version_table} + mig_context = MigrationContext.configure(conn, opts=opts) + return mig_context.get_current_revision() def get_current_and_head_revision( From 87af04ad79ac8e477d2579d064ce5a66d352a723 Mon Sep 17 00:00:00 2001 From: Martin Burchell Date: Thu, 13 Feb 2025 11:42:45 +0000 Subject: [PATCH 2/3] Update changelog --- docs/source/changelog.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/source/changelog.rst b/docs/source/changelog.rst index a52d33b..4cd5b76 100644 --- a/docs/source/changelog.rst +++ b/docs/source/changelog.rst @@ -852,3 +852,10 @@ Quick links: - Bugfix to ``cardinal_pythonlib.sqlalchemy.sqlserver`` functions as they were executing unconditionally, regardless of SQLAlchemy dialect (they should have been conditional to SQL Server). + +**2.0.2** + +- Bugfix to + :func:`cardinal_pythonlib.sqlalchemy.alembic_func.get_current_revision` where + since SQLAlchemy 2.0, the database connection was persisting, resulting in a + metadata lock. From f15923d00bb94618940b9ba83e3cba192a9b1b0c Mon Sep 17 00:00:00 2001 From: Martin Burchell Date: Thu, 13 Feb 2025 15:59:47 +0000 Subject: [PATCH 3/3] Fix view creation in tests following SQLAlchemy change Use updated example from https://github.com/sqlalchemy/sqlalchemy/wiki/Views for SQLA 2.0.38 --- cardinal_pythonlib/sqlalchemy/tests/schema_tests.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/cardinal_pythonlib/sqlalchemy/tests/schema_tests.py b/cardinal_pythonlib/sqlalchemy/tests/schema_tests.py index 0dacfc7..7fd57d9 100644 --- a/cardinal_pythonlib/sqlalchemy/tests/schema_tests.py +++ b/cardinal_pythonlib/sqlalchemy/tests/schema_tests.py @@ -307,12 +307,14 @@ def _attach_view( of "selectable") is created after the table is created, and dropped before the table is dropped, via listeners. """ - t = table(tablename) - - # noinspection PyProtectedMember - t._columns._populate_separate_keys( - col._make_proxy(t) for col in selectable.selected_columns + t = table( + tablename, + *( + Column(c.name, c.type, primary_key=c.primary_key) + for c in selectable.selected_columns + ), ) + t.primary_key.update(c for c in t.c if c.primary_key) event.listen( metadata,