Files
ai-exchange/scripts/apply_migrations.py
T
2026-04-24 13:39:01 +08:00

37 lines
802 B
Python

from pathlib import Path
import sys
from sqlalchemy import text
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
from src.db.engine import engine
MIGRATIONS_DIR = Path(__file__).resolve().parents[1] / "migrations"
def iter_sql_files() -> list[Path]:
return sorted(p for p in MIGRATIONS_DIR.glob("*.sql") if p.is_file())
def main() -> None:
files = iter_sql_files()
if not files:
print("No migration files found")
return
with engine.begin() as conn:
for path in files:
sql = path.read_text(encoding="utf-8").strip()
if not sql:
continue
conn.execute(text(sql))
print(f"Applied {path.name}")
if __name__ == "__main__":
main()