diff --git a/torba/client/basedatabase.py b/torba/client/basedatabase.py index 4e2d1ee51..ccbffb7e5 100644 --- a/torba/client/basedatabase.py +++ b/torba/client/basedatabase.py @@ -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 = [], {}