-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
175 lines (141 loc) · 5.8 KB
/
Copy pathmain.py
File metadata and controls
175 lines (141 loc) · 5.8 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
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
from datetime import datetime
from db import get_pg_conn, get_engine_from_details
from config_reader import get_active_configs
from source_loader import load_source, get_connection_details
from target_loader import create_table_if_not_exists, upsert_data, get_target_connection
from audit import log_run
from schema_engine import get_csv_schema, get_db_schema
import mysql.connector
import psycopg2
# =========================
# 🚀 MAIN PIPELINE
# =========================
def run_pipeline(process_name=None):
# ✅ Import INSIDE function (fixes your error)
from schema_validation import validate_or_capture_schema
configs = get_active_configs(process_name)
if not configs:
raise Exception(f"No active configs found for process: {process_name}")
print(f"🚀 Running Process: {process_name}")
configs = sorted(configs, key=lambda x: x.get("run_seq", 1))
for config in configs:
start_time = datetime.now()
# 🔹 Metadata DB (Postgres)
pg_conn = get_pg_conn()
# 🔹 Target connection (dynamic)
target_conn = get_target_connection(config)
try:
print(f"🚀 Running pipeline for {config['source_table_name']}")
# =========================
# 🔹 LOAD SOURCE
# =========================
df = load_source(config)
# =========================
# 🔹 SOURCE CONNECTION
# =========================
source_conn = get_connection_details(config["source_connection_name"])
system = source_conn["system"].upper()
config["source_system"] = system
# ✅ Populate missing metadata fields if available in connection details
if not config.get("source_database"):
config["source_database"] = source_conn.get("database")
if not config.get("source_schema") or config.get("source_schema") == "N/A":
if system in ["CSV", "SQLITE"]:
config["source_schema"] = "N/A"
elif system == "MYSQL":
config["source_schema"] = source_conn.get("database")
else:
config["source_schema"] = "public"
# =========================
# 🔥 SCHEMA EXTRACTION (Generic)
# =========================
if system == "CSV":
source_schema = get_csv_schema(df)
else:
source_engine = get_engine_from_details(source_conn)
source_schema = get_db_schema(
source_engine,
config["source_table_name"],
schema_name=config.get("source_schema") if config.get("source_schema") != "N/A" else None
)
source_engine.dispose()
# =========================
# 🔥 PREPARE TARGET METADATA
# =========================
tgt_details = get_connection_details(config["target_connection_name"])
target_system = tgt_details["system"].upper()
config["target_system"] = target_system
if not config.get("target_database"):
config["target_database"] = tgt_details.get("database")
if not config.get("target_schema") or config.get("target_schema") == "N/A":
if target_system in ["CSV", "SQLITE"]:
config["target_schema"] = "N/A"
elif target_system == "MYSQL":
config["target_schema"] = tgt_details.get("database")
else:
config["target_schema"] = "public"
# =========================
# 🔥 SCHEMA VALIDATION
# =========================
schema_ok = validate_or_capture_schema(pg_conn, config, source_schema)
if not schema_ok:
log_run(
pg_conn,
config,
start_time,
datetime.now(),
0, 0, 0,
"AWAITING APPROVAL",
"Schema drift detected. Alert sent to admin."
)
print(f"⏸️ Pipeline paused for {config['source_table_name']} until schema changes are approved.")
continue
# =========================
# 🔹 TARGET TABLE
# =========================
target_table = f"{config['target_schema']}.{config['target_table_name']}"
pk = config["primary_key"]
# =========================
# 🔹 CREATE TABLE
# =========================
create_table_if_not_exists(target_conn, config, df, pk)
# =========================
# 🔹 UPSERT
# =========================
ins, upd, rej = upsert_data(target_conn, config, df, pk)
# =========================
# 🔹 AUDIT
# =========================
log_run(
pg_conn,
config,
start_time,
datetime.now(),
ins,
upd,
rej,
"SUCCESS",
None
)
print(f"✅ Completed {target_table}")
except Exception as e:
print(f"❌ Failed: {e}")
pg_conn.rollback()
log_run(
pg_conn,
config,
start_time,
datetime.now(),
0, 0, 0,
"FAILED",
str(e)
)
finally:
pg_conn.close()
if target_conn:
target_conn.dispose()
# =========================
# 🔥 ENTRY POINT
# =========================
if __name__ == "__main__":
run_pipeline()