fix: 增强 SQLite 数据库恢复与异常提示
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from collections.abc import Generator
|
||||
from pathlib import Path
|
||||
|
||||
from sqlalchemy import create_engine, event
|
||||
from sqlalchemy import Engine, create_engine, event
|
||||
from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker
|
||||
|
||||
from app.config import get_settings
|
||||
@@ -34,10 +34,41 @@ engine = create_sqlite_engine(get_settings().database_url)
|
||||
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
||||
|
||||
|
||||
def check_database_integrity(target_engine: Engine = engine) -> str:
|
||||
raw_connection = target_engine.raw_connection()
|
||||
try:
|
||||
cursor = raw_connection.driver_connection.cursor()
|
||||
try:
|
||||
cursor.execute("PRAGMA integrity_check")
|
||||
result = cursor.fetchone()
|
||||
finally:
|
||||
cursor.close()
|
||||
finally:
|
||||
raw_connection.close()
|
||||
|
||||
status = result[0] if result else "missing_result"
|
||||
if status != "ok":
|
||||
raise RuntimeError(f"SQLite integrity check failed: {status}")
|
||||
return status
|
||||
|
||||
|
||||
def checkpoint_sqlite_wal(target_engine: Engine = engine) -> bool:
|
||||
if str(target_engine.url) == "sqlite:///:memory:":
|
||||
return False
|
||||
if target_engine.url.get_backend_name() != "sqlite":
|
||||
return False
|
||||
|
||||
with target_engine.begin() as connection:
|
||||
connection.exec_driver_sql("PRAGMA wal_checkpoint(TRUNCATE)")
|
||||
return True
|
||||
|
||||
|
||||
def init_db() -> None:
|
||||
import app.models # noqa: F401
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
check_database_integrity(engine)
|
||||
checkpoint_sqlite_wal(engine)
|
||||
|
||||
|
||||
def get_db_session() -> Generator[Session]:
|
||||
|
||||
Reference in New Issue
Block a user