#!/usr/bin/env python3 """Offline import: tab Docker PostgreSQL → WESP SQLite. Usage: TAB_REFERENCE_DATABASE_URL=postgresql://neoton:neoton_secret@localhost:5432/neoton \\ python3 scripts/import_tab_reference_db.py Requires: alembic upgrade head (migration lab_module) applied first. """ from __future__ import annotations import os import sys ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) if ROOT not in sys.path: sys.path.insert(0, ROOT) from app import create_app from config import DevelopmentConfig from app.lab.etl.reference_db_import import DEFAULT_PG_URL, import_from_reference_db def main() -> int: pg_url = os.environ.get("TAB_REFERENCE_DATABASE_URL", DEFAULT_PG_URL) print(f"Source: {pg_url}") os.environ.setdefault("WESP_BACKGROUND_STARTUP", "0") app = create_app(DevelopmentConfig, run_migrations=True) with app.app_context(): stats = import_from_reference_db(pg_url) print("Import complete:") for key, value in stats.__dict__.items(): if key == "errors": continue print(f" {key}: {value}") if stats.errors: for err in stats.errors: print(f" error: {err}", file=sys.stderr) return 1 return 0 if __name__ == "__main__": raise SystemExit(main())