Fix comparison pipeline event and transaction handling
This commit is contained in:
+19
-1
@@ -10,12 +10,29 @@ import sqlite3
|
||||
import threading
|
||||
import time
|
||||
import os
|
||||
from contextlib import contextmanager
|
||||
|
||||
DB_PATH = "/tmp/contracts.db"
|
||||
_db_session_key = None
|
||||
_local = threading.local()
|
||||
|
||||
|
||||
@contextmanager
|
||||
def transaction():
|
||||
"""Run DB writes atomically on the current thread connection."""
|
||||
conn = get_conn()
|
||||
conn.execute("BEGIN IMMEDIATE")
|
||||
_local.in_transaction = True
|
||||
try:
|
||||
yield conn
|
||||
conn.commit()
|
||||
except Exception:
|
||||
conn.rollback()
|
||||
raise
|
||||
finally:
|
||||
_local.in_transaction = False
|
||||
|
||||
|
||||
def init_db():
|
||||
"""Создать/пересоздать БД + схему. Вызывается при старте и после cleanup."""
|
||||
global _db_session_key
|
||||
@@ -171,7 +188,8 @@ def execute(sql, params=None):
|
||||
conn = get_conn()
|
||||
sql = _pg_to_sqlite(sql)
|
||||
cur = conn.execute(sql, params or [])
|
||||
conn.commit()
|
||||
if not getattr(_local, "in_transaction", False):
|
||||
conn.commit()
|
||||
return cur.rowcount
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user