49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
from sqlalchemy import create_engine, text
|
|
from sqlalchemy.orm import sessionmaker, DeclarativeBase
|
|
import os
|
|
|
|
# 使用绝对路径
|
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
DATABASE_URL = f"sqlite:///{os.path.join(BASE_DIR, 'data', 'app.db')}"
|
|
|
|
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
def ensure_schema_compatibility() -> None:
|
|
"""Best-effort lightweight migrations for SQLite deployments."""
|
|
if engine.dialect.name != "sqlite":
|
|
return
|
|
|
|
with engine.begin() as conn:
|
|
columns = {
|
|
row[1]
|
|
for row in conn.execute(text("PRAGMA table_info(assistants)"))
|
|
}
|
|
if "voice_output_enabled" not in columns:
|
|
conn.execute(
|
|
text(
|
|
"ALTER TABLE assistants "
|
|
"ADD COLUMN voice_output_enabled BOOLEAN DEFAULT 1"
|
|
)
|
|
)
|
|
conn.execute(
|
|
text(
|
|
"UPDATE assistants "
|
|
"SET voice_output_enabled = 1 "
|
|
"WHERE voice_output_enabled IS NULL"
|
|
)
|
|
)
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|