File size: 1,294 Bytes
5e21013
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""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")