SPB Git

spb/trouve-ka Public

Trouve-KA — moteur de recherche web indépendant, Québec-first. Crawler distribué, index OpenSearch, ranking bilingue, galerie d'images. En prod : www.trouve-ka.com

Python 76.8% TypeScript 15.7% SQL 3.9% Shell 1.4% CSS 1.3% Dockerfile 0.7%
1.7 KB · 47 lines python
Raw Blame History
1# Trouve-KA — exécution des migrations SQL2# Author: Simon-Pierre Boucher3# Contact: contact@spboucher.ai45"""Applique les migrations SQL de infrastructure/migrations dans l'ordre."""67import os8import pathlib9import re1011import asyncpg121314def _find_migrations_dir() -> pathlib.Path:15    """Résout le dossier de migrations : env, cwd (container /app), ou racine du dépôt (editable)."""16    candidates = [17        os.environ.get("TROUVEKA_MIGRATIONS_DIR"),18        pathlib.Path.cwd() / "infrastructure" / "migrations",19        pathlib.Path(__file__).resolve().parents[2] / "infrastructure" / "migrations",20    ]21    for cand in candidates:22        if cand and pathlib.Path(cand).is_dir():23            return pathlib.Path(cand)24    raise FileNotFoundError("dossier infrastructure/migrations introuvable")252627async def run_migrations(database_url: str) -> list[str]:28    """Applique les migrations manquantes. Retourne la liste des fichiers appliqués."""29    migrations_dir = _find_migrations_dir()30    conn = await asyncpg.connect(database_url)31    applied: list[str] = []32    try:33        await conn.execute(34            "CREATE TABLE IF NOT EXISTS schema_migrations ("35            " version INTEGER PRIMARY KEY, applied_at TIMESTAMPTZ NOT NULL DEFAULT now())"36        )37        done = {r["version"] for r in await conn.fetch("SELECT version FROM schema_migrations")}38        for path in sorted(migrations_dir.glob("*.sql")):39            match = re.match(r"^(\d+)_", path.name)40            if not match or int(match.group(1)) in done:41                continue42            await conn.execute(path.read_text(encoding="utf-8"))43            applied.append(path.name)44    finally:45        await conn.close()46    return applied47