Skip to content

Commit

Permalink
chore: avoid explicit commit if we are in autocommit mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Sep 9, 2024
1 parent 837e3d2 commit a4530d1
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions ibis/backends/mysql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,19 @@ def drop_database(self, name: str, force: bool = False) -> None:
def begin(self):
con = self.con
cur = con.cursor()

if not con.autocommit_mode:
con.begin()

Check warning on line 260 in ibis/backends/mysql/__init__.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/mysql/__init__.py#L260

Added line #L260 was not covered by tests

try:
yield cur
except Exception:
con.rollback()
if not con.autocommit_mode:
con.rollback()

Check warning on line 266 in ibis/backends/mysql/__init__.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/mysql/__init__.py#L266

Added line #L266 was not covered by tests
raise
else:
con.commit()
if not con.autocommit_mode:
con.commit()

Check warning on line 270 in ibis/backends/mysql/__init__.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/mysql/__init__.py#L270

Added line #L270 was not covered by tests
finally:
cur.close()

Expand All @@ -279,14 +285,19 @@ def raw_sql(self, query: str | sg.Expression, **kwargs: Any) -> Any:
con = self.con
cursor = con.cursor()

if not con.autocommit_mode:
con.begin()

Check warning on line 289 in ibis/backends/mysql/__init__.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/mysql/__init__.py#L289

Added line #L289 was not covered by tests

try:
cursor.execute(query, **kwargs)
except Exception:
con.rollback()
if not con.autocommit_mode:
con.rollback()

Check warning on line 295 in ibis/backends/mysql/__init__.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/mysql/__init__.py#L295

Added line #L295 was not covered by tests
cursor.close()
raise
else:
con.commit()
if not con.autocommit_mode:
con.commit()

Check warning on line 300 in ibis/backends/mysql/__init__.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/mysql/__init__.py#L300

Added line #L300 was not covered by tests
return cursor

# TODO: disable positional arguments
Expand Down

0 comments on commit a4530d1

Please sign in to comment.