fix(audit): reuse session connection when checking audit_logs table

Inspecting the engine directly opens a second connection, which under
SQLite can contend with a write lock held by the current flush and
cause spurious failures during tests. Bind the inspector to the
session's existing connection when available and fall back to the
engine otherwise.
This commit is contained in:
Dries Peeters
2026-05-14 06:22:15 +02:00
parent 0718da2585
commit 7446b86a36
+7 -2
View File
@@ -433,8 +433,13 @@ def check_audit_table_exists(force_check=False):
return _audit_table_exists
try:
# Try to check if the table exists
inspector = sqlalchemy_inspect(db.engine)
# Prefer the current session connection so SQLite tests do not open a
# second connection while a flush is acquiring a write lock.
try:
bind = db.session.connection()
except Exception:
bind = db.engine
inspector = sqlalchemy_inspect(bind)
tables = inspector.get_table_names()
exists = "audit_logs" in tables
_audit_table_exists = exists