44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
"""数据库种子脚本(非运行态)—— 在迁移后、启动应用前执行。
|
||
|
|
|
||
|
|
用法:uv run python scripts/db/seed.py
|
||
|
|
职责:灌入平台/园区/培训的基础种子数据(角色、权限、用户、默认园区、培训事件课程等)。
|
||
|
|
全部幂等(按标记行判断,重跑安全)。禁止在应用启动时调用本逻辑。
|
||
|
|
"""
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import asyncio
|
||
|
|
import logging
|
||
|
|
import sys
|
||
|
|
import os
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent.parent))
|
||
|
|
|
||
|
|
from app.infrastructure.repositories import Database
|
||
|
|
from app.infrastructure.seed import seed_data as platform_seed
|
||
|
|
from app import config
|
||
|
|
|
||
|
|
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(name)s: %(message)s")
|
||
|
|
log = logging.getLogger("db.seed")
|
||
|
|
|
||
|
|
|
||
|
|
async def run() -> None:
|
||
|
|
db = Database()
|
||
|
|
try:
|
||
|
|
# 平台种子(角色/权限/用户/政务/OPC/投资人/工作台等,幂等)
|
||
|
|
await platform_seed(db.session)
|
||
|
|
await db.session.commit()
|
||
|
|
log.info("平台种子完成:%s", config.DATABASE_URL)
|
||
|
|
# TODO(P2/P3 后):园区默认园区+企业、培训事件课程 经 serverdata/seed/*.json 灌入(当前由各自子应用迁移后接入)
|
||
|
|
except Exception as e: # noqa: BLE001
|
||
|
|
await db.session.rollback()
|
||
|
|
log.error("种子失败:%s", e)
|
||
|
|
raise
|
||
|
|
finally:
|
||
|
|
await db.close()
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
asyncio.run(run())
|