pagepiper/scripts/db_migrate.py

30 lines
762 B
Python

"""Apply all pending migrations to the pagepiper SQLite database."""
from __future__ import annotations
import sqlite3
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
from app.config import DB_PATH
def migrate(db_path: str = DB_PATH) -> None:
migrations_dir = Path(__file__).parent.parent / "migrations"
conn = sqlite3.connect(db_path)
conn.execute("PRAGMA foreign_keys = ON")
conn.execute("PRAGMA journal_mode = WAL")
for sql_file in sorted(migrations_dir.glob("*.sql")):
print(f"Applying {sql_file.name}...")
conn.executescript(sql_file.read_text())
conn.commit()
conn.close()
print(f"Migrations applied to {db_path}")
if __name__ == "__main__":
migrate()