fix: close library upload flow and harden image storage
This commit is contained in:
@@ -17,6 +17,7 @@ def encode(value):
|
||||
class Database:
|
||||
def __init__(self, path):
|
||||
self.lock = threading.RLock()
|
||||
self._transaction_depth = 0
|
||||
self.conn = sqlite3.connect(path, check_same_thread=False)
|
||||
self.conn.row_factory = sqlite3.Row
|
||||
self.conn.executescript("""
|
||||
@@ -68,17 +69,32 @@ class Database:
|
||||
@contextmanager
|
||||
def transaction(self):
|
||||
with self.lock:
|
||||
self.conn.execute("BEGIN IMMEDIATE")
|
||||
nested = self._transaction_depth > 0
|
||||
savepoint = "tx_" + str(self._transaction_depth)
|
||||
self.conn.execute("SAVEPOINT " + savepoint if nested else "BEGIN IMMEDIATE")
|
||||
self._transaction_depth += 1
|
||||
try:
|
||||
yield self.conn
|
||||
self.conn.commit()
|
||||
except Exception:
|
||||
self.conn.rollback()
|
||||
if nested:
|
||||
self.conn.execute("RELEASE SAVEPOINT " + savepoint)
|
||||
else:
|
||||
self.conn.commit()
|
||||
except BaseException:
|
||||
if nested:
|
||||
self.conn.execute("ROLLBACK TO SAVEPOINT " + savepoint)
|
||||
self.conn.execute("RELEASE SAVEPOINT " + savepoint)
|
||||
else:
|
||||
self.conn.rollback()
|
||||
raise
|
||||
finally:
|
||||
self._transaction_depth -= 1
|
||||
|
||||
def execute(self, query, args=()):
|
||||
with self.lock, self.conn:
|
||||
return self.conn.execute(query, args)
|
||||
with self.lock:
|
||||
if self._transaction_depth:
|
||||
return self.conn.execute(query, args)
|
||||
with self.conn:
|
||||
return self.conn.execute(query, args)
|
||||
|
||||
def one(self, query, args=()):
|
||||
with self.lock:
|
||||
|
||||
Reference in New Issue
Block a user