Skip to content

Commit

Permalink
fix: database rollback integrity error
Browse files Browse the repository at this point in the history
  • Loading branch information
Sieboldianus committed Aug 22, 2019
1 parent 0d12c9e commit 969b482
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lbsntransform/classes/submit_data.py
Expand Up @@ -609,8 +609,8 @@ def submitBatch(self, insert_sql):
or this: https://stackoverflow.com/questions/8134602/
psycopg2-insert-multiple-rows-with-one-query
"""
self.db_cursor.execute("SAVEPOINT submit_recordBatch")
tsuccessful = False
self.db_cursor.execute("SAVEPOINT submit_recordBatch")
while not tsuccessful:
try:
self.db_cursor.execute(insert_sql)
Expand All @@ -624,15 +624,22 @@ def submitBatch(self, insert_sql):
print(
f'TransactionIntegrityError, inserting language "'
f'{missingLanguage}" first.. ')
# self.db_cursor.rollback()
self.db_cursor.execute(
"ROLLBACK TO SAVEPOINT submit_recordBatch")
insert_language_sql = '''
INSERT INTO data."language"
(language_short,language_name,language_name_de)
(language_short, language_name, language_name_de)
VALUES (%s,NULL,NULL);
'''
# submit sql to db
self.db_cursor.execute(
insert_language_sql, (missingLanguage,))
# commit changes so they're available when
# try is executed again
self.commit_changes()
# recreate SAVEPOINT after language insert
self.db_cursor.execute("SAVEPOINT submit_recordBatch")
else:
sys.exit(f'{e}')
except psycopg2.DataError as e:
Expand All @@ -651,7 +658,7 @@ def submitBatch(self, insert_sql):
except psycopg2.errors.DiskFull as e:
input("Disk space full. Clean files and continue..")
else:
# self.count_affected += self.dbCursor.rowcount # monitoring
# executed if the try clause does not raise an exception
self.db_cursor.execute("RELEASE SAVEPOINT submit_recordBatch")
tsuccessful = True

Expand Down

0 comments on commit 969b482

Please sign in to comment.