"""Apply infra/db/migrations/002_billing.sql to the live Supabase project.""" import os import re import subprocess import sys def read_env(path: str) -> dict[str, str]: env: dict[str, str] = {} with open(path) as f: for line in f: line = line.strip() if not line or line.startswith("#"): continue m = re.match(r'^([A-Z_][A-Z0-9_]*)=(.*)$', line) if m: env[m.group(1)] = m.group(2).strip('"\'') return env root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) env = read_env(os.path.join(root, ".env")) db_url = env.get("POSTGRES_URL_NON_POOLING", "") if not db_url: print("ERROR: POSTGRES_URL_NON_POOLING not found in .env") sys.exit(1) sql_file = os.path.join(root, "supabase", "migrations", "002_billing.sql") print(f"Applying: {sql_file}") result = subprocess.run( ["psql", db_url, "-f", sql_file, "--set=ON_ERROR_STOP=1"], capture_output=True, text=True, ) if result.stdout: print(result.stdout[:4000]) if result.stderr: print(result.stderr[:2000], file=sys.stderr) if result.returncode != 0: print(f"\nFailed with exit code {result.returncode}") sys.exit(result.returncode) print("\n✓ Migration 002_billing applied successfully")