feat: 统一生产初始化流程并保护系统管理员
This commit is contained in:
@@ -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()
|
||||
Reference in New Issue
Block a user