#!/usr/bin/env python3
"""Export every table of the staging SQLite DB to JSON-lines files for scripts/load-pg.js.
usage: python3 scripts/export_jsonl.py --db build/papers.db --out build/export
Each
.jsonl: first line {"cols": [...]}, then one JSON array per row.
"""
import argparse, json, os, sqlite3
TABLES = ['conferences', 'taxonomy', 'areas', 'roles', 'benchmarks', 'papers', 'paper_taxonomy', 'work_types',
'paper_benchmarks', 'paper_meta', 'paper_work_types', 'results', 'type_notes', 'type_reviews']
ap = argparse.ArgumentParser(); ap.add_argument("--db", default="build/papers.db"); ap.add_argument("--out", default="build/export")
a = ap.parse_args()
os.makedirs(a.out, exist_ok=True)
db = sqlite3.connect(f"file:{a.db}?mode=ro", uri=True)
for t in TABLES:
cur = db.execute(f"SELECT * FROM {t}")
cols = [d[0] for d in cur.description]
n = 0
with open(os.path.join(a.out, t + ".jsonl"), "w", encoding="utf-8") as f:
f.write(json.dumps({"cols": cols}) + "\n")
for row in cur:
f.write(json.dumps(list(row), ensure_ascii=False) + "\n"); n += 1
print(f"exported {t}: {n}")