Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mssql): avoid calling .commit() unless a DDL operation is being performed #9658

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions ibis/backends/mssql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@

@contextlib.contextmanager
def begin(self):
with contextlib.closing(self.con.cursor()) as cur:
yield cur

@contextlib.contextmanager
def _ddl_begin(self):
con = self.con
cur = con.cursor()
try:
Expand All @@ -273,22 +278,24 @@
cur.execute(query, *args, **kwargs)
yield cur

@contextlib.contextmanager
def _safe_ddl(self, query, *args, **kwargs):
with contextlib.suppress(AttributeError):
query = query.sql(self.dialect)

with self._ddl_begin() as cur:
cur.execute(query, *args, **kwargs)
yield cur

def raw_sql(self, query: str | sg.Expression, **kwargs: Any) -> Any:
with contextlib.suppress(AttributeError):
query = query.sql(self.dialect)

con = self.con
cursor = con.cursor()

try:
cursor.execute(query, **kwargs)
except Exception:
con.rollback()
cursor.close()
raise
else:
con.commit()
return cursor
cursor.execute(query, **kwargs)
return cursor

Check warning on line 298 in ibis/backends/mssql/__init__.py

View check run for this annotation

Codecov / codecov/patch

ibis/backends/mssql/__init__.py#L297-L298

Added lines #L297 - L298 were not covered by tests

def create_catalog(self, name: str, force: bool = False) -> None:
expr = (
Expand All @@ -309,11 +316,11 @@
if force
else stmt
)
with self._safe_raw_sql(create_stmt):
with self._safe_ddl(create_stmt):
pass

def drop_catalog(self, name: str, force: bool = False) -> None:
with self._safe_raw_sql(
with self._safe_ddl(
sge.Drop(
kind="DATABASE",
this=sg.to_identifier(name, quoted=self.compiler.quoted),
Expand Down Expand Up @@ -584,7 +591,7 @@

this = sg.table(name, catalog=catalog, db=db, quoted=self.compiler.quoted)
raw_this = sg.table(name, catalog=catalog, db=db, quoted=False)
with self._safe_raw_sql(create_stmt) as cur:
with self._safe_ddl(create_stmt) as cur:
if query is not None:
# You can specify that a table is temporary for the sqlglot `Create` but not
# for the subsequent `Insert`, so we need to shove a `#` in
Expand Down Expand Up @@ -666,7 +673,7 @@
data = df.itertuples(index=False)

insert_stmt = self._build_insert_template(name, schema=schema, columns=True)
with self._safe_raw_sql(create_stmt) as cur:
with self._safe_ddl(create_stmt) as cur:
if not df.empty:
cur.executemany(insert_stmt, data)

Expand Down