67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
"""将误写入的 0020_* revision 回退到 0018,以便重新执行 0019_align_legacy_schema。
|
|||
|
|
|
|||
|
|
用法(在 api 目录,DATABASE_URL 已配置):
|
|||
|
|
|
|||
|
|
uv run python scripts/repair_alembic_version_after_withdrawn_0020.py
|
|||
|
|
uv run python scripts/repair_alembic_version_after_withdrawn_0020.py --dry-run
|
|||
|
|
|
|||
|
|
启动时也会自动执行同等逻辑(见 app.core.alembic_startup)。
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
import argparse
|
|||
|
|
import sys
|
|||
|
|
from pathlib import Path
|
|||
|
|
|
|||
|
|
_API_DIR = Path(__file__).resolve().parents[1]
|
|||
|
|
if str(_API_DIR) not in sys.path:
|
|||
|
|
sys.path.insert(0, str(_API_DIR))
|
|||
|
|
|
|||
|
|
from sqlalchemy import create_engine, text
|
|||
|
|
|
|||
|
|
from app.core.alembic_revision_repair import (
|
|||
|
|
_WITHDRAWN_0020_REVISIONS,
|
|||
|
|
try_repair_withdrawn_0020_revision,
|
|||
|
|
)
|
|||
|
|
from app.core.db import _database_url
|
|||
|
|
|
|||
|
|
|
|||
|
|
def main() -> int:
|
|||
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|||
|
|
parser.add_argument(
|
|||
|
|
"--dry-run",
|
|||
|
|
action="store_true",
|
|||
|
|
help="只检查,不写库",
|
|||
|
|
)
|
|||
|
|
args = parser.parse_args()
|
|||
|
|
|
|||
|
|
engine = create_engine(_database_url())
|
|||
|
|
with engine.connect() as conn:
|
|||
|
|
row = conn.execute(text("SELECT version_num FROM alembic_version")).fetchone()
|
|||
|
|
current = row[0] if row else None
|
|||
|
|
print(f"当前 version_num: {current!r}")
|
|||
|
|
|
|||
|
|
if current is None:
|
|||
|
|
print("alembic_version 表为空,无需修复")
|
|||
|
|
return 0
|
|||
|
|
|
|||
|
|
if args.dry_run:
|
|||
|
|
if current in _WITHDRAWN_0020_REVISIONS:
|
|||
|
|
print("[dry-run] 将回退到 0018_users_language_preference")
|
|||
|
|
else:
|
|||
|
|
print("无需修复")
|
|||
|
|
return 0
|
|||
|
|
|
|||
|
|
if try_repair_withdrawn_0020_revision(conn):
|
|||
|
|
conn.commit()
|
|||
|
|
print("已回退到 0018_users_language_preference;请执行: uv run alembic upgrade head")
|
|||
|
|
else:
|
|||
|
|
print("无需修复")
|
|||
|
|
return 0
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
raise SystemExit(main())
|