feat: 统一生产初始化流程并保护系统管理员

This commit is contained in:
Cheng Zhou
2026-03-27 17:17:32 +08:00
parent f2f856ea24
commit a67991740e
14 changed files with 492 additions and 10 deletions
+37
View File
@@ -0,0 +1,37 @@
import asyncio
import subprocess
import sys
from pathlib import Path
PROJECT_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(PROJECT_ROOT))
from app.core.config import PROTECTED_ADMIN_EMAIL # noqa: E402
from app.crud.user import ensure_admin_exists # noqa: E402
from app.db.session import SessionLocal # noqa: E402
def run_migrations() -> None:
subprocess.run(
[sys.executable, "-m", "alembic", "upgrade", "head"],
check=True,
cwd=PROJECT_ROOT,
)
async def seed_protected_admin() -> None:
async with SessionLocal() as session:
await ensure_admin_exists(session)
def main() -> None:
print("Running Alembic migrations...")
run_migrations()
print(f"Ensuring protected admin exists: {PROTECTED_ADMIN_EMAIL}")
asyncio.run(seed_protected_admin())
print("Production initialization complete.")
if __name__ == "__main__":
main()