#!/usr/bin/env python3 """ ETL: Google Sheets → PostgreSQL Migration Script Maps every Google Sheets table in Brigawan's GAS codebase to PostgreSQL tables as defined in schema.sql and the SQLAlchemy models. Usage: # One-time migration python etl_sheets_to_postgres.py --mode full # Dry run (no writes) python etl_sheets_to_postgres.py --mode dry-run # Verify after migration python etl_sheets_to_postgres.py --mode verify Environment Variables (see .env.example): GOOGLE_SERVICE_ACCOUNT_JSON — Path to GCP service account key GOOGLE_SHEETS_SPREADSHEET_ID — Google Sheets ID DATABASE_URL_SYNC — PostgreSQL connection string """ import argparse import hashlib import json import os import sys import time from datetime import datetime, timezone from typing import Any, Dict, List, Optional, Tuple # ─── Third-party imports (install via: pip install -r requirements.txt) ─────── try: import google.auth from googleapiclient.discovery import build from google.oauth2 import service_account except ImportError: print("ERROR: google-api-python-client not installed.") print("Run: pip install google-api-python-client google-auth-httplib2") sys.exit(1) try: import psycopg2 from psycopg2.extras import execute_values except ImportError: print("ERROR: psycopg2 not installed.") print("Run: pip install psycopg2-binary") sys.exit(1) # ─── Configuration ─────────────────────────────────────────────────────────── SPREADSHEET_ID = os.environ.get("GOOGLE_SHEETS_SPREADSHEET_ID", "") DATABASE_URL = os.environ.get("DATABASE_URL_SYNC", "postgresql://localhost:5432/brigawan") SERVICE_ACCOUNT_FILE = os.environ.get("GOOGLE_SERVICE_ACCOUNT_JSON", "") # Batch size for INSERT operations BATCH_SIZE = 100 # Dry-run mode (no database writes) DRY_RUN = False # ============================================================================= # SHEET → TABLE MAPPING # Every sheet in the GAS codebase mapped to its PostgreSQL table and column transforms # ============================================================================= SHEET_MAP: Dict[str, Dict[str, Any]] = { # ─── CORE TALENT ────────────────────────────────────────────────────── "Data Utama": { "table": "talents", "skip_header": 1, "transform": lambda row: { "talent_id": _clean(row, 1), "name": _clean(row, 2), "email": _clean(row, 3).lower(), "education": _clean(row, 4), "experience": _clean(row, 5), "category": _clean(row, 6), "score": _int(row, 7, 70), "analysis": _clean(row, 8), "location": _clean(row, 9), "skill_tags": _clean(row, 10), "cv_link": _clean(row, 11), "verification_status": _clean(row, 12, "Pending"), "salary_expected": _clean(row, 13), "badges": _json(row, 14), "advisory_snippet": _clean(row, 15), "advisory_full": _clean(row, 16), "k3_dasar": _clean(row, 17), "k3_khusus": _clean(row, 18), "cert_other": _clean(row, 19), "language": _clean(row, 20), "phone": _clean(row, 21), "is_unlocked": _bool(row, 22), "password_hash": _clean(row, 23), "linkedin_link": _clean(row, 24), "current_salary": _clean(row, 25), "portfolio_links": _clean(row, 26), "cert_links": _clean(row, 27), "base_cv_url": _clean(row, 28), "total_experience": _clean(row, 29), "sector_focus": _clean(row, 30), "talent_type": _clean(row, 31), "cert_expiry": _clean(row, 32), "green_score": _int(row, 33, 0), "job_reference_id": _clean(row, 34), "created_at": datetime.now(timezone.utc), "updated_at": datetime.now(timezone.utc), }, }, "Katalog Talenta (B2B)": { "table": "talent_catalog", "skip_header": 1, "transform": lambda row: { "talent_id": _clean(row, 1), "display_name": _clean(row, 2), "sector_focus": _clean(row, 30), "skill_tags": _clean(row, 10), "location": _clean(row, 9), "score": _int(row, 7, 0), "category": _clean(row, 6), "badges": _json(row, 14), "advisory_snippet": _clean(row, 15), "is_active": True, "created_at": datetime.now(timezone.utc), }, }, # ─── B2B / EMPLOYER ────────────────────────────────────────────────── "Companies": { "table": "companies", "skip_header": 1, "transform": lambda row: { "id": _clean(row, 0), "name": _clean(row, 1), "website": _clean(row, 2), "branding": _clean(row, 3), "bidang_usaha": _clean(row, 4), "produk_jasa": _clean(row, 5), "industri_ai": _clean(row, 6), "credit_balance": _int(row, 7, 0), "csr_budget": _float(row, 8, 0.0), "tier": _clean(row, 9, "free"), "billing": _clean(row, 10, "monthly"), "seats_limit": _int(row, 11, 2), "max_jobs": _int(row, 12, 1), "status": _clean(row, 13, "PENDING"), "created_at": _date_or_now(row, 14), }, }, "Clients": { "table": "client_accounts", "skip_header": 1, "transform": lambda row: { "id": _clean(row, 0), "company_id": _clean(row, 1) or None, "email": _clean(row, 2).lower(), "password_hash": _clean(row, 3, "-"), "name": _clean(row, 4), "whatsapp": _clean(row, 5), "jabatan": _clean(row, 6), "role": _clean(row, 7, "Recruiter"), "status": _clean(row, 8, "PENDING"), "auth_key": _clean(row, 9, "-"), "created_at": _date_or_now(row, 10), }, }, "Pending Employers": { "table": "pending_employers", "skip_header": 1, "transform": lambda row: { "entity_name": _clean(row, 0), "branding": _clean(row, 1), "website": _clean(row, 2), "pic_name": _clean(row, 3), "pic_jabatan": _clean(row, 4), "email": _clean(row, 5).lower(), "whatsapp": _clean(row, 6), "password_hash": _clean(row, 7), "status": _clean(row, 8, "PENDING"), "bidang_usaha": _clean(row, 9), "produk_jasa": _clean(row, 10), "industri_ai": _clean(row, 11), }, }, # ─── FINANCE ───────────────────────────────────────────────────────── "Transactions": { "table": "transactions", "skip_header": 1, "transform": lambda row: { "id": _clean(row, 0), "client_id": _clean(row, 1), "amount": _float(row, 2, 0), "type": _clean(row, 3), "status": _clean(row, 4, "Pending"), "snap_token": _clean(row, 5), "created_at": _date_or_now(row, 6), }, }, "Unlocks": { "table": "profile_unlocks", "skip_header": 1, "transform": lambda row: { "company_id": _clean(row, 1), "candidate_id": _clean(row, 2), "is_masked_only": True, "created_at": _date_or_now(row, 3), }, }, "Interviews": { "table": "interviews", "skip_header": 1, "transform": lambda row: { "company_id": _clean(row, 1), "client_id": _clean(row, 2), "candidate_id": _clean(row, 3), "scheduled_time": _date_or_now(row, 4), "status": _clean(row, 5, "Scheduled"), "transcript_text": _clean(row, 6), "ai_summary": _clean(row, 7), "ai_competency_score": _int(row, 8, 0), "ai_performance_analysis": _clean(row, 9), "created_at": _date_or_now(row, 10), }, }, # ─── LPK ECOSYSTEM ──────────────