Skip to content

Commit

Permalink
Merge pull request #48 from CityOfPhiladelphia/oracle-temp-table-fix
Browse files Browse the repository at this point in the history
dont fail if temp table already exists for oracle load
  • Loading branch information
floptical authored Oct 15, 2024
2 parents 3205f49 + 524febc commit 2992903
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions databridge_etl_tools/oracle/oracle.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,17 @@ def load(self):
cursor.execute(tmp_tbl_stmt)
cursor.execute('COMMIT')
tmp_table_made = True

except Exception as e:
# Sometimes the table already exists for some reason... truncate it if so.
if 'already exists' in str(e) or 'name is already used by an existing object' in str(e):
print('Temp table already exists? truncating..')
cursor.execute(f'TRUNCATE TABLE {temp_table_name}')
cursor.execute('COMMIT')
tmp_table_made = False
else:
raise e

try:
# Replace empty strings with None (which corresponds to NULL in databases)
#rows = etl.convert(rows, {field: lambda v: 'NULL' if v == '' else v for field in etl.header(rows)})

Expand Down Expand Up @@ -423,4 +433,12 @@ def load(self):
if tmp_table_made or 'name is already used by an existing object' in str(e):
cursor.execute(f'DROP TABLE {temp_table_name}')
cursor.execute('COMMIT')
raise e
raise e

# Failsafe try to drop the temp table no matter what, and don't fail if it doesn't exist
try:
cursor.execute(f'DROP TABLE {temp_table_name}')
cursor.execute('COMMIT')
except Exception as e:
if 'table or view does not exist' in str(e):
pass

0 comments on commit 2992903

Please sign in to comment.