Skip to content

Commit

Permalink
Use Connection.begin_nested() instead
Browse files Browse the repository at this point in the history
Other libraries that consume this one may have their own transactions
going on when we are called, so use begin_nested() to utilise
savepointing instead, and don't forget to commit!
  • Loading branch information
s-t-e-v-e-n-k committed Sep 27, 2023
1 parent 059b687 commit 91276ae
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions agatesql/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,15 @@ def to_sql(self, connection_or_string, table_name, overwrite=False,
min_col_len=min_col_len, col_len_multiplier=col_len_multiplier)

if create:
with connection.begin():
with connection.begin_nested() as conn:
if overwrite:
sql_table.drop(bind=connection, checkfirst=True)

sql_table.create(bind=connection, checkfirst=create_if_not_exists)
conn.commit()

if insert:
with connection.begin():
with connection.begin_nested() as conn:
insert = sql_table.insert()
for prefix in prefixes:
insert = insert.prefix_with(prefix)
Expand All @@ -295,6 +296,8 @@ def to_sql(self, connection_or_string, table_name, overwrite=False,
connection.execute(insert, [dict(zip(self.column_names, row)) for row in
self.rows[index * chunk_size:end_index]])

conn.commit()

try:
return sql_table
finally:
Expand Down

0 comments on commit 91276ae

Please sign in to comment.