Skip to content

Commit

Permalink
[db] Wrap SQL statements into connection.begin() blocks.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
blacklight committed Nov 3, 2024
1 parent 1cb42f8 commit dd02be1
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions platypush/plugins/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ def execute(self, statement, *args, engine=None, **kwargs):
(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):
Expand Down Expand Up @@ -324,7 +326,7 @@ def insert(
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(
Expand Down Expand Up @@ -462,7 +464,7 @@ def update(self, table, records, key_columns, engine=None, *args, **kwargs):
}
"""
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)

Expand Down Expand Up @@ -505,7 +507,7 @@ def delete(self, table, records, engine=None, *args, **kwargs):

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()
Expand Down

0 comments on commit dd02be1

Please sign in to comment.