-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_schema.py
More file actions
74 lines (61 loc) · 2.71 KB
/
Copy pathmigrate_schema.py
File metadata and controls
74 lines (61 loc) · 2.71 KB
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
from app.integrations.google_sheets import GoogleSheetsClient
import os
from dotenv import load_dotenv
load_dotenv()
SPREADSHEET_ID = os.getenv("SPREADSHEET_ID")
CREDENTIALS_FILE = os.getenv("GOOGLE_SERVICE_ACCOUNT_FILE", "credentials/service_account.json")
def migrate():
print(f"Starting Migration for Sheet: {SPREADSHEET_ID}")
client = GoogleSheetsClient(CREDENTIALS_FILE, SPREADSHEET_ID)
client.connect()
# 1. CLIENTES MIGRATION
ws_clients = client._spreadsheet.worksheet("CLIENTES")
headers = ws_clients.row_values(1)
print(f"Current Client Headers: {headers}")
required_client_cols = ["Direccion", "Contador_Ventas"]
# Check for empty duplicates first (the crash cause)
if len(headers) != len(set(headers)):
print("⚠️ Found duplicate/empty headers. Cleaning up...")
# Trim sheet to actual data width
real_width = len([h for h in headers if h.strip()])
ws_clients.resize(rows=ws_clients.row_count, cols=real_width)
print(f"Resized CLIENTES to {real_width} columns.")
headers = ws_clients.row_values(1) # Refresh
# Add missing columns
new_headers = list(headers)
added = False
for req in required_client_cols:
if req not in new_headers:
new_headers.append(req)
added = True
if added:
print(f"Adding new headers: {new_headers}")
# Resize to fit new columns
ws_clients.resize(rows=ws_clients.row_count, cols=len(new_headers))
ws_clients.update_cell(1, len(new_headers)-1, "Direccion")
ws_clients.update_cell(1, len(new_headers), "Contador_Ventas")
print("✅ Added 'Direccion' and 'Contador_Ventas' to CLIENTES.")
else:
print("✓ CLIENTES schema is up to date.")
# 2. PEDIDOS MIGRATION
try:
ws_pedidos = client._spreadsheet.worksheet("PEDIDOS")
p_headers = ws_pedidos.row_values(1)
print(f"Current Order Headers: {p_headers}")
if "Folio_Nota" not in p_headers:
print("Adding 'Folio_Nota' to PEDIDOS...")
ws_pedidos.resize(rows=ws_pedidos.row_count, cols=len(p_headers)+1)
ws_pedidos.update_cell(1, len(p_headers)+1, "Folio_Nota")
print("✅ Added 'Folio_Nota' to PEDIDOS.")
else:
print("✓ PEDIDOS schema is up to date.")
except Exception as e:
print(f"Error migrating PEDIDOS: {e}")
print("\nMigration Complete. Attempting validation...")
try:
recs = ws_clients.get_all_records()
print(f"SUCCESS! Read {len(recs)} clients without error.")
except Exception as e:
print(f"❌ VALIDATION FAILED: {e}")
if __name__ == "__main__":
migrate()