Reputation: 107
I am trying to generate a list of SQL statements to execute, but the current code I believe commits them one by one:
for batch in batches: # batch is a list of dictionary objects, batches is a list of lists
for obj in batch: # for dictionary object in list
with engine.connect() as connection:
overwrite_failure = context['retry_failure'] == True and obj['status'] in ['success', 'critical failure']
if overwrite_failure:
# load_deletes returns a string query which is escaped and formatted by .execute()
query_one, query_two = load_deletes(obj, table_name, columns, unique_cols, should_overwrite, identifier)
connection.execute(query_one, obj)
connection.execute(query_two, obj)
else:
# load_updates returns a string query which is escaped and formatted by .execute()
query = load_updates(obj, table_name, columns, unique_cols, should_overwrite)
connection.execute(query, obj)
Upvotes: 0
Views: 311
Reputation: 89
Init the DB connection outside the loops. Also make sure the auto_commit setting is off.
Upvotes: 1