forked from Rockachopa/Timmy-time-dashboard
fix: handle concurrent table creation race in SQLite (#151)
This commit is contained in:
committed by
GitHub
parent
ae3bb1cc21
commit
8dbce25183
@@ -235,3 +235,22 @@ def test_reorder_promote_later_to_next(client: TestClient, db_session: Session):
|
||||
assert db_session.query(Task).filter(Task.id == task_now.id).first().state == TaskState.NOW
|
||||
assert db_session.query(Task).filter(Task.id == task_later1.id).first().state == TaskState.NEXT
|
||||
assert db_session.query(Task).filter(Task.id == task_later2.id).first().state == TaskState.LATER
|
||||
|
||||
|
||||
def test_create_tables_idempotent_under_concurrency():
|
||||
"""Calling create_tables() when tables already exist must not crash.
|
||||
|
||||
This covers the race where multiple pytest-xdist workers (or app
|
||||
processes) import the calm routes module simultaneously and each
|
||||
calls create_tables() against the same SQLite file.
|
||||
"""
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlalchemy.exc import OperationalError
|
||||
|
||||
from dashboard.models.database import create_tables
|
||||
|
||||
fake_error = OperationalError("CREATE TABLE", {}, Exception("table tasks already exists"))
|
||||
with patch("dashboard.models.database.Base.metadata.create_all", side_effect=fake_error):
|
||||
# Must not raise — the OperationalError is caught and logged
|
||||
create_tables()
|
||||
|
||||
Reference in New Issue
Block a user