From dd02be12fc505305de34b7c055e3bb64d78621c7 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 3 Nov 2024 10:38:22 +0100 Subject: [PATCH] [db] Wrap SQL statements into `connection.begin()` blocks. The latest release of SQLAlchemy 2.x has apparently removed the `autocommit` implicit logic for good. Mutations should be explicitly wrapped into `with ... begin()` blocks, or they will be rolled back when the connection is closed. --- platypush/plugins/db/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/platypush/plugins/db/__init__.py b/platypush/plugins/db/__init__.py index 0e4b84a9a1..b9d9e959e8 100644 --- a/platypush/plugins/db/__init__.py +++ b/platypush/plugins/db/__init__.py @@ -105,7 +105,9 @@ class DbPlugin(Plugin): (see https:///docs.sqlalchemy.org/en/latest/core/engines.html) """ - with self.get_engine(engine, *args, **kwargs).connect() as connection: + with self.get_engine( + engine, *args, **kwargs + ).connect() as connection, connection.begin(): connection.execute(text(statement)) def _get_table(self, table: str, *args, engine=None, **kwargs): @@ -324,7 +326,7 @@ class DbPlugin(Plugin): update_records = [] returned_records = [] - with engine.connect() as connection: + with engine.connect() as connection, connection.begin(): # Upsert case if key_columns: insert_records, update_records = self._get_new_and_existing_records( @@ -462,7 +464,7 @@ class DbPlugin(Plugin): } """ engine = self.get_engine(engine, *args, **kwargs) - with engine.connect() as connection: + with engine.connect() as connection, connection.begin(): table, engine = self._get_table(table, *args, engine=engine, **kwargs) return self._update(connection, table, records, key_columns) @@ -505,7 +507,7 @@ class DbPlugin(Plugin): engine = self.get_engine(engine, *args, **kwargs) - with engine.connect() as connection: + with engine.connect() as connection, connection.begin(): for record in records: table_, engine = self._get_table(table, *args, engine=engine, **kwargs) delete = table_.delete()