From 68ff95142e6b77f81b0431abadd7c85e1e2fdb76 Mon Sep 17 00:00:00 2001 From: Martin Burchell Date: Mon, 17 Mar 2025 14:10:51 +0000 Subject: [PATCH 1/2] Always commit when executing DDL This fixes a problem seen when preprocessing SystmOne data on the CPFT server. If a 'crate_pk' column was added to a table and an index created on this column, SQL Server would report that this column didn't exist --- cardinal_pythonlib/sqlalchemy/schema.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cardinal_pythonlib/sqlalchemy/schema.py b/cardinal_pythonlib/sqlalchemy/schema.py index f584119..6d0201e 100644 --- a/cardinal_pythonlib/sqlalchemy/schema.py +++ b/cardinal_pythonlib/sqlalchemy/schema.py @@ -417,8 +417,18 @@ def execute_ddl( if sql: ddl = DDL(sql) with engine.connect() as connection: - # DDL doesn't need a COMMIT. connection.execute(ddl) + # DDL may need a commit for some dialects: + # + # Generic (but generic may not be useful) + # https://stackoverflow.com/questions/730621/do-ddl-statements-always-give-you-an-implicit-commit-or-can-you-get-an-implicit + # Oracle - autocommitted? + # https://docs.oracle.com/cd/A97335_02/apps.102/a83723/keyprog6.htm + # but not Postgres? + # https://dba.stackexchange.com/questions/340916/why-must-i-commit-after-the-alter-table-ddl-to-make-changes-visible + # and in SQL Server they are "batched" so not entirely autocommitted + # https://www.mssqltips.com/sqlservertip/4591/ddl-commands-in-transactions-in-sql-server-versus-oracle/ + connection.commit() # ============================================================================= From 9bbb52b68401fe5324c52a8b4a10a61df7ed053a Mon Sep 17 00:00:00 2001 From: Martin Burchell Date: Mon, 17 Mar 2025 15:10:53 +0000 Subject: [PATCH 2/2] Update changelog --- docs/source/changelog.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/source/changelog.rst b/docs/source/changelog.rst index dd95a46..65ad7e3 100644 --- a/docs/source/changelog.rst +++ b/docs/source/changelog.rst @@ -880,3 +880,8 @@ Quick links: - Allow ``db_url`` parameter to ``cardinal_pythonlib.sqlalchemy.alembic_func.create_database_migration_numbered_style``. + +**2.0.4** + +- Fix :func:`cardinal_pythonlib.sqlalchemy.schema.execute_ddl` so that it always + commits. Not all dialects commit automatically.