|
8 | 8 | from libs.settings import AIRFLOW_DAGRUN_TIMEOUT, AIRFLOW_DEBUG_MODE |
9 | 9 | from libs.utils import update_job_status |
10 | 10 | from openpyxl.worksheet.worksheet import Worksheet |
11 | | -from psycopg2.extras import execute_values |
12 | 11 |
|
13 | 12 | # PostgreSQL connection hook |
14 | 13 | pg_hook = PostgresHook( |
@@ -106,35 +105,28 @@ def update_temp_data_dictionary_table( |
106 | 105 | } |
107 | 106 | ) |
108 | 107 |
|
109 | | - # Insert records into the temporary table using execute_values for fast bulk inserts |
| 108 | + # Insert records into the temporary table |
110 | 109 | if dictionary_records: |
111 | | - conn = pg_hook.get_conn() |
112 | | - cursor = conn.cursor() |
113 | | - try: |
114 | | - execute_values( |
115 | | - cursor, |
116 | | - f""" |
117 | | - INSERT INTO temp_data_dictionary_{scan_report_id} |
118 | | - (table_name, field_name, value, value_description) |
119 | | - VALUES %s |
120 | | - """, |
121 | | - [ |
122 | | - ( |
123 | | - d["table_name"], |
124 | | - d["field_name"], |
125 | | - d["value"], |
126 | | - d["value_description"], |
127 | | - ) |
128 | | - for d in dictionary_records |
129 | | - ], |
130 | | - ) |
131 | | - conn.commit() |
132 | | - except Exception: |
133 | | - conn.rollback() |
134 | | - raise |
135 | | - finally: |
136 | | - cursor.close() |
137 | | - conn.close() |
| 110 | + pg_hook.insert_rows( |
| 111 | + table=f"temp_data_dictionary_{scan_report_id}", |
| 112 | + rows=[ |
| 113 | + ( |
| 114 | + d["table_name"], |
| 115 | + d["field_name"], |
| 116 | + d["value"], |
| 117 | + d["value_description"], |
| 118 | + ) |
| 119 | + for d in dictionary_records |
| 120 | + ], |
| 121 | + target_fields=[ |
| 122 | + "table_name", |
| 123 | + "field_name", |
| 124 | + "value", |
| 125 | + "value_description", |
| 126 | + ], |
| 127 | + fast_executemany=True, |
| 128 | + commit_every=3000, |
| 129 | + ) |
138 | 130 |
|
139 | 131 | logging.info( |
140 | 132 | f"Created temporary data dictionary table with {len(dictionary_records)} records" |
@@ -205,34 +197,25 @@ def create_temp_field_values_table( |
205 | 197 | } |
206 | 198 | ) |
207 | 199 |
|
208 | | - # Using execute_values() for fast bulk inserts (see notes above) |
209 | 200 | if field_values_data: |
210 | | - conn = pg_hook.get_conn() |
211 | | - cursor = conn.cursor() |
212 | | - try: |
213 | | - execute_values( |
214 | | - cursor, |
215 | | - f""" |
216 | | - INSERT INTO temp_field_values_{table_id} |
217 | | - (field_name, value, frequency) |
218 | | - VALUES %s |
219 | | - """, |
220 | | - [ |
221 | | - ( |
222 | | - d["field_name"], |
223 | | - d["value"], |
224 | | - d["frequency"], |
225 | | - ) |
226 | | - for d in field_values_data |
227 | | - ], |
228 | | - ) |
229 | | - conn.commit() |
230 | | - except Exception: |
231 | | - conn.rollback() |
232 | | - raise |
233 | | - finally: |
234 | | - cursor.close() |
235 | | - conn.close() |
| 201 | + pg_hook.insert_rows( |
| 202 | + table=f"temp_field_values_{table_id}", |
| 203 | + rows=[ |
| 204 | + ( |
| 205 | + d["field_name"], |
| 206 | + d["value"], |
| 207 | + d["frequency"], |
| 208 | + ) |
| 209 | + for d in field_values_data |
| 210 | + ], |
| 211 | + target_fields=[ |
| 212 | + "field_name", |
| 213 | + "value", |
| 214 | + "frequency", |
| 215 | + ], |
| 216 | + fast_executemany=True, |
| 217 | + commit_every=3000, |
| 218 | + ) |
236 | 219 |
|
237 | 220 | except Exception as e: |
238 | 221 | logging.error(f"Error creating data dictionary table: {str(e)}") |
|
0 commit comments