50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
"""
|
|
运行账迁移 CLI。
|
|
|
|
用法:
|
|
python -m agent.runtime_db.migrate --list
|
|
python -m agent.runtime_db.migrate
|
|
|
|
需配置 PG_DSN 或 PG_HOST/PG_DATABASE/PG_USER;目标库必须是 inquiry_robot_runtime(禁止 ytd_runtime)。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import logging
|
|
import sys
|
|
|
|
from agent.config import get_settings
|
|
from agent.runtime_db import apply_migrations, assert_safe_database, list_migration_files
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
|
|
parser = argparse.ArgumentParser(description="inquiry_robot_runtime 迁移")
|
|
parser.add_argument("--list", action="store_true", help="只列出 SQL 文件,不连库")
|
|
args = parser.parse_args(argv)
|
|
|
|
if args.list:
|
|
for name in list_migration_files():
|
|
print(name)
|
|
return 0
|
|
|
|
settings = get_settings()
|
|
dsn = settings.resolve_pg_dsn()
|
|
if not dsn:
|
|
logging.error("未配置 PG_DSN / PG_*,无法迁移")
|
|
return 2
|
|
try:
|
|
db = assert_safe_database(dsn)
|
|
logging.info("目标库 %s", db)
|
|
applied = apply_migrations(settings)
|
|
logging.info("新应用 %s 个迁移: %s", len(applied), applied or "(无)")
|
|
return 0
|
|
except Exception as exc: # noqa: BLE001
|
|
logging.exception("迁移失败: %s", exc)
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|