1
0

fix: handle concurrent table creation race in SQLite (#151)

This commit is contained in:
Alexander Whitestone
2026-03-08 13:27:11 -04:00
committed by GitHub
parent ae3bb1cc21
commit 8dbce25183
5 changed files with 44 additions and 8 deletions

View File

@@ -1,9 +1,18 @@
import logging
from pathlib import Path
from sqlalchemy import create_engine
from sqlalchemy.exc import OperationalError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session, sessionmaker
logger = logging.getLogger(__name__)
SQLALCHEMY_DATABASE_URL = "sqlite:///./data/timmy_calm.db"
# Ensure the data directory exists before creating the engine
Path("./data").mkdir(parents=True, exist_ok=True)
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
@@ -12,8 +21,11 @@ Base = declarative_base()
def create_tables():
"""Create all tables defined by models that have imported Base."""
Base.metadata.create_all(bind=engine)
"""Create all tables idempotently (safe under pytest-xdist concurrency)."""
try:
Base.metadata.create_all(bind=engine)
except OperationalError as exc:
logger.debug("Table creation skipped (already exists): %s", exc)
def get_db():