Spaces:
Sleeping
Sleeping
File size: 8,687 Bytes
b613c13 | 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | #!/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 ββββββββββββββ
|