31 lines
880 B
Python
31 lines
880 B
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
"""数据库迁移脚本(非运行态)—— Alembic 封装。
|
||
|
|
|
||
|
|
用法:uv run python scripts/db/migrate.py # alembic upgrade head
|
||
|
|
uv run python scripts/db/migrate.py downgrade # 回退一个版本
|
||
|
|
在启动应用【之前】执行;禁止在应用启动时迁移/建表。
|
||
|
|
"""
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import subprocess
|
||
|
|
import sys
|
||
|
|
import os
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
ROOT = Path(__file__).resolve().parent.parent.parent
|
||
|
|
os.chdir(ROOT)
|
||
|
|
|
||
|
|
|
||
|
|
def main() -> None:
|
||
|
|
cmd = sys.argv[1] if len(sys.argv) > 1 else "upgrade"
|
||
|
|
if cmd == "downgrade":
|
||
|
|
args = ["python", "-m", "alembic", "downgrade", "-1"]
|
||
|
|
else:
|
||
|
|
args = ["python", "-m", "alembic", "upgrade", "head"]
|
||
|
|
print(f"执行迁移: {' '.join(args)}")
|
||
|
|
raise SystemExit(subprocess.call(args, cwd=str(ROOT)))
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|