Drop table in finally

Refs https://github.com/simonw/sqlite-utils/pull/653#issuecomment-2864963011
This commit is contained in:
Simon Willison 2025-05-08 20:15:07 -07:00
commit 494075584f

View file

@ -692,8 +692,8 @@ class Database:
def supports_on_conflict(self) -> bool:
# SQLite's upsert is implemented as INSERT INTO ... ON CONFLICT DO ...
if not hasattr(self, "_supports_on_conflict"):
table_name = "t{}".format(secrets.token_hex(16))
try:
table_name = "t{}".format(secrets.token_hex(16))
with self.conn:
self.conn.execute(
"create table {} (id integer primary key, name text)".format(
@ -709,10 +709,11 @@ class Database:
"on conflict do update set name = 'two'"
).format(table_name)
)
self.conn.execute("drop table {}".format(table_name))
self._supports_on_conflict = True
self._supports_on_conflict = True
except Exception:
self._supports_on_conflict = False
finally:
self.conn.execute("drop table {}".format(table_name))
return self._supports_on_conflict
@property