add run_nofk to AIOSQLite to run without foreign keys

This commit is contained in:
Jack Robison 2019-02-13 17:46:52 -05:00 committed by Lex Berezhny
parent 57eb56b92d
commit f134bc264b

View file

@ -67,6 +67,25 @@ class AIOSQLite:
self.connection.rollback()
raise
def run_nofk(self, fun, *args, **kwargs) -> Awaitable:
return wrap_future(self.executor.submit(self.__run_transaction_no_fk, fun, *args, **kwargs))
def __run_transaction_no_fk(self, fun: Callable[[sqlite3.Connection, Any, Any], Any], *args, **kwargs):
try:
self.connection.execute('pragma foreign_keys=off')
self.connection.commit()
try:
self.connection.execute('begin')
result = fun(self.connection, *args, **kwargs) # type: ignore
self.connection.commit()
return result
except (Exception, OSError): # as e:
self.connection.rollback()
raise
finally:
self.connection.execute('pragma foreign_keys=on')
self.connection.commit()
def constraints_to_sql(constraints, joiner=' AND ', prepend_key=''):
sql, values = [], {}