File size: 1,939 Bytes
769bf77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Seed reference_ranges table in Supabase from data/reference_ranges.json

Usage:
    cd labcard-backend
    python scripts/seed_reference_ranges.py

Requires SUPABASE_URL and SUPABASE_SERVICE_KEY in .env
"""
from __future__ import annotations

import json
import sys
from pathlib import Path

# Allow running from repo root
sys.path.insert(0, str(Path(__file__).parent.parent))

from dotenv import load_dotenv
load_dotenv(".env")

from app.config import get_settings
from app.db.supabase import get_supabase


def main() -> None:
    settings = get_settings()
    if not settings.has_supabase:
        print("❌  SUPABASE_URL / SUPABASE_SERVICE_KEY not set in .env")
        sys.exit(1)

    db = get_supabase()
    if db is None:
        print("❌  Supabase client failed to initialise")
        sys.exit(1)

    data_file = Path(__file__).parent.parent / "data" / "reference_ranges.json"
    payload   = json.loads(data_file.read_text())
    ranges    = payload["ranges"]

    rows = []
    for r in ranges:
        rows.append({
            "biomarker":      r["name"],
            "std_name":       r.get("std", r["name"]),
            "gender":         r.get("gender", "A"),
            "age_min":        r.get("age_min", 0),
            "age_max":        r.get("age_max", 120),
            "normal_low":     r.get("low"),
            "normal_high":    r.get("high"),
            "critical_low":   r.get("critical_low"),
            "critical_high":  r.get("critical_high"),
            "unit":           r.get("unit", ""),
            "category":       r.get("category", "Other"),
            "notes":          r.get("notes", ""),
        })

    # Upsert — safe to re-run
    result = db.table("reference_ranges").upsert(rows, on_conflict="biomarker,gender").execute()
    print(f"✅  Seeded {len(rows)} reference ranges")
    print(f"    Supabase response rows: {len(result.data)}")


if __name__ == "__main__":
    main()