Files
secondary-school-grade-archive/backend/app/services/migrate.py
T
dekun f1ad4273f4 零 Node 部署、超级管理员,并完善本地构建发布文档。
- FastAPI 单进程托管 frontend/dist,systemd 替代 PM2

- 超级管理员 admin、注册开关与用户管理

- README/DEPLOY/USAGE 说明:改代码须本地构建 dist 后 push,服务器 update.sh

- 提交 frontend/dist 与 build-frontend 脚本
2026-06-28 13:19:41 +08:00

36 lines
1.3 KiB
Python

from sqlalchemy import inspect, text
from app.core.config import settings
from app.core.database import engine
def run_migrations() -> None:
"""Apply lightweight schema updates for existing databases."""
inspector = inspect(engine)
if "students" not in inspector.get_table_names():
return
columns = {col["name"] for col in inspector.get_columns("students")}
if "school_level" not in columns:
with engine.begin() as conn:
conn.execute(
text(
"ALTER TABLE students ADD COLUMN school_level VARCHAR(32) "
"NOT NULL DEFAULT 'junior_high'"
)
)
if "users" in inspector.get_table_names():
user_columns = {col["name"] for col in inspector.get_columns("users")}
if "is_superuser" not in user_columns:
with engine.begin() as conn:
conn.execute(
text("ALTER TABLE users ADD COLUMN is_superuser BOOLEAN NOT NULL DEFAULT FALSE")
)
conn.execute(
text(
f"UPDATE users SET is_superuser = TRUE "
f"WHERE username = '{settings.ADMIN_DEFAULT_USERNAME}'"
)
)