Cleaned up and easier debugging for SQL statements. A lot less redundant information.
This commit is contained in:
193
app/db_sql.py
193
app/db_sql.py
@@ -72,15 +72,73 @@ def sql_insert(sql:str|None=None, data:dict|None=None, table_name:str|None=None,
|
||||
try:
|
||||
result_insert = db.execute(sql_insert, data)
|
||||
trans.commit()
|
||||
except Exception as e:
|
||||
except IntegrityError as e: # Specifically want to capture duplicate entry attempts
|
||||
# http://sqlalche.me/e/14/gkpj
|
||||
# Need a check for this: sqlalchemy.exc.IntegrityError: (MySQLdb._exceptions.IntegrityError) (1062, "Duplicate entry 'z-yyyy-xxxx-wwww for key 'PRIMARY'"
|
||||
trans.rollback()
|
||||
log.exception('*** An exception happened. ***')
|
||||
log.exception(repr(e))
|
||||
log.exception('***')
|
||||
log.exception(str(e))
|
||||
log.exception('^^^ exception ^^^')
|
||||
log.error('This is likely because there was an attempt to create a duplicate entry. Returning None')
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning None * ** *** ****')
|
||||
log.error('^^^^ ^^^ ^^ ^ ### END ### Exception ^ ^^ ^^^ ^^^^')
|
||||
|
||||
log.error('**** *** ** * ### BEGIN ### Exception Details: * ** *** ****')
|
||||
log.error('**** *** ** * SQL Statement: * ** *** ****')
|
||||
log.error(e.statement)
|
||||
log.error('**** *** ** * SQL Parameters: * ** *** ****')
|
||||
log.error(e.params)
|
||||
log.error('**** *** ** * SQL Origin Message: * ** *** ****')
|
||||
log.error(e.orig)
|
||||
log.error('**** *** ** * ### END ### Exception Details: * ** *** ****')
|
||||
|
||||
return None
|
||||
except OperationalError as e: # Likely an unknown field or related
|
||||
trans.rollback()
|
||||
log.error('This is likely because there was an unknown field or similar included. Returning False')
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
log.error('^^^^ ^^^ ^^ ^ ### END ### Exception ^ ^^ ^^^ ^^^^')
|
||||
|
||||
log.error('**** *** ** * ### BEGIN ### Exception Details: * ** *** ****')
|
||||
log.error('**** *** ** * SQL Statement: * ** *** ****')
|
||||
log.error(e.statement)
|
||||
log.error('**** *** ** * SQL Parameters: * ** *** ****')
|
||||
log.error(e.params)
|
||||
log.error('**** *** ** * SQL Origin Message: * ** *** ****')
|
||||
log.error(e.orig)
|
||||
log.error('**** *** ** * ### END ### Exception Details: * ** *** ****')
|
||||
|
||||
return False
|
||||
except Exception as e:
|
||||
trans.rollback()
|
||||
log.error('This is likely because there was an attempt to create a duplicate entry. Returning None')
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
log.error('^^^^ ^^^ ^^ ^ ### END ### Exception ^ ^^ ^^^ ^^^^')
|
||||
|
||||
log.error('**** *** ** * ### BEGIN ### Exception Details: * ** *** ****')
|
||||
log.error('**** *** ** * SQL Statement: * ** *** ****')
|
||||
log.error(e.statement)
|
||||
log.error('**** *** ** * SQL Parameters: * ** *** ****')
|
||||
log.error(e.params)
|
||||
log.error('**** *** ** * SQL Origin Message: * ** *** ****')
|
||||
log.error(e.orig)
|
||||
log.error('**** *** ** * ### END ### Exception Details: * ** *** ****')
|
||||
|
||||
# print('**** *** ** * ### ### * ** *** ****')
|
||||
# log.error(e.code)
|
||||
# log.error('**** *** ** * ### ### * ** *** ****')
|
||||
# log.error(e.statement)
|
||||
# log.error('**** *** ** * ### ### * ** *** ****')
|
||||
# log.error(e.params)
|
||||
# log.error('**** *** ** * ### ### * ** *** ****')
|
||||
# log.error(e.orig)
|
||||
# log.error('**** *** ** * ### ### * ** *** ****')
|
||||
# log.error(e.ismulti)
|
||||
# log.error('**** *** ** * ### ### * ** *** ****')
|
||||
# log.error(e.hide_parameters)
|
||||
# log.error('**** *** ** * ### ### * ** *** ****')
|
||||
# log.error(e.detail)
|
||||
# log.error('**** *** ** * ### ### * ** *** ****')
|
||||
# log.error(e.connection_invalidated)
|
||||
# log.error('**** *** ** * ### ### * ** *** ****')
|
||||
|
||||
return False
|
||||
else:
|
||||
log.debug(result_insert)
|
||||
@@ -158,13 +216,44 @@ def sql_update(sql:str|None=None, data:dict|None=None, table_name:str|None=None,
|
||||
log.info('Trying to execute the SQL UPDATE query...')
|
||||
result_update = db.execute(sql_update, data)
|
||||
trans.commit()
|
||||
except IntegrityError as e: # Specifically want to capture duplicate entry attempts
|
||||
# http://sqlalche.me/e/14/gkpj
|
||||
# Need a check for this: sqlalchemy.exc.IntegrityError: (MySQLdb._exceptions.IntegrityError) (1062, "Duplicate entry 'z-yyyy-xxxx-wwww for key 'PRIMARY'"
|
||||
trans.rollback()
|
||||
log.error('This is likely because there was an attempt to create a duplicate entry. Returning None')
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning None * ** *** ****')
|
||||
log.error('^^^^ ^^^ ^^ ^ ### END ### Exception ^ ^^ ^^^ ^^^^')
|
||||
|
||||
log.error('**** *** ** * ### BEGIN ### Exception Details: * ** *** ****')
|
||||
log.error('**** *** ** * SQL Statement: * ** *** ****')
|
||||
log.error(e.statement)
|
||||
log.error('**** *** ** * SQL Parameters: * ** *** ****')
|
||||
log.error(e.params)
|
||||
log.error('**** *** ** * SQL Origin Message: * ** *** ****')
|
||||
log.error(e.orig)
|
||||
log.error('**** *** ** * ### END ### Exception Details: * ** *** ****')
|
||||
|
||||
return None
|
||||
except OperationalError as e: # Likely an unknown field or related
|
||||
trans.rollback()
|
||||
log.error('This is likely because there was an unknown field or similar included. Returning False')
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
log.error('^^^^ ^^^ ^^ ^ ### END ### Exception ^ ^^ ^^^ ^^^^')
|
||||
|
||||
log.error('**** *** ** * ### BEGIN ### Exception Details: * ** *** ****')
|
||||
log.error('**** *** ** * SQL Statement: * ** *** ****')
|
||||
log.error(e.statement)
|
||||
log.error('**** *** ** * SQL Parameters: * ** *** ****')
|
||||
log.error(e.params)
|
||||
log.error('**** *** ** * SQL Origin Message: * ** *** ****')
|
||||
log.error(e.orig)
|
||||
log.error('**** *** ** * ### END ### Exception Details: * ** *** ****')
|
||||
|
||||
return False
|
||||
except Exception as e:
|
||||
trans.rollback()
|
||||
log.exception('*** An exception happened. ***')
|
||||
log.exception(repr(e))
|
||||
log.exception('***')
|
||||
log.exception(str(e))
|
||||
log.exception('^^^ exception ^^^')
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
log.error('^^^^ ^^^ ^^ ^ ### END ### Exception ^ ^^ ^^^ ^^^^')
|
||||
return False
|
||||
else:
|
||||
log.debug(result_update)
|
||||
@@ -250,13 +339,44 @@ def sql_insert_or_update(
|
||||
log.debug(data)
|
||||
result_insert_or_update = db.execute(sql_insert_or_update, data)
|
||||
trans.commit()
|
||||
except IntegrityError as e: # Specifically want to capture duplicate entry attempts
|
||||
# http://sqlalche.me/e/14/gkpj
|
||||
# Need a check for this: sqlalchemy.exc.IntegrityError: (MySQLdb._exceptions.IntegrityError) (1062, "Duplicate entry 'z-yyyy-xxxx-wwww for key 'PRIMARY'"
|
||||
trans.rollback()
|
||||
log.error('This is likely because there was an attempt to create a duplicate entry. Returning None')
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning None * ** *** ****')
|
||||
log.error('^^^^ ^^^ ^^ ^ ### END ### Exception ^ ^^ ^^^ ^^^^')
|
||||
|
||||
log.error('**** *** ** * ### BEGIN ### Exception Details: * ** *** ****')
|
||||
log.error('**** *** ** * SQL Statement: * ** *** ****')
|
||||
log.error(e.statement)
|
||||
log.error('**** *** ** * SQL Parameters: * ** *** ****')
|
||||
log.error(e.params)
|
||||
log.error('**** *** ** * SQL Origin Message: * ** *** ****')
|
||||
log.error(e.orig)
|
||||
log.error('**** *** ** * ### END ### Exception Details: * ** *** ****')
|
||||
|
||||
return None
|
||||
except OperationalError as e: # Likely an unknown field or related
|
||||
trans.rollback()
|
||||
log.error('This is likely because there was an unknown field or similar included. Returning False')
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
log.error('^^^^ ^^^ ^^ ^ ### END ### Exception ^ ^^ ^^^ ^^^^')
|
||||
|
||||
log.error('**** *** ** * ### BEGIN ### Exception Details: * ** *** ****')
|
||||
log.error('**** *** ** * SQL Statement: * ** *** ****')
|
||||
log.error(e.statement)
|
||||
log.error('**** *** ** * SQL Parameters: * ** *** ****')
|
||||
log.error(e.params)
|
||||
log.error('**** *** ** * SQL Origin Message: * ** *** ****')
|
||||
log.error(e.orig)
|
||||
log.error('**** *** ** * ### END ### Exception Details: * ** *** ****')
|
||||
|
||||
return False
|
||||
except Exception as e:
|
||||
trans.rollback()
|
||||
log.exception('*** An exception happened. ***')
|
||||
log.exception(repr(e))
|
||||
log.exception('***')
|
||||
log.exception(str(e))
|
||||
log.exception('^^^ exception ^^^')
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
log.error('^^^^ ^^^ ^^ ^ ### END ### Exception ^ ^^ ^^^ ^^^^')
|
||||
return False
|
||||
else:
|
||||
log.debug(result_insert_or_update)
|
||||
@@ -444,21 +564,16 @@ def sql_select(
|
||||
log.warning('2x Executing with SQL statement only...')
|
||||
result = db.execute(sql)
|
||||
except Exception as e:
|
||||
log.warning('2x A *second* exception happened. Returning False.')
|
||||
log.exception(repr(e))
|
||||
log.exception('***')
|
||||
log.exception(str(e))
|
||||
log.exception('^^^ exception ^^^')
|
||||
log.exception('**** *** ** * ### BEGIN ### (2x) Second Exception Happened: Returning False * ** *** ****')
|
||||
log.error('^^^^ ^^^ ^^ ^ ### END ### Exception ^ ^^ ^^^ ^^^^')
|
||||
return False # Not successful
|
||||
else:
|
||||
log.info('Successfully executed the SQL on the second try.')
|
||||
pass
|
||||
except Exception as e:
|
||||
log.info('An exception happened. Returning False.')
|
||||
log.exception(repr(e))
|
||||
log.exception('***')
|
||||
log.exception(str(e))
|
||||
log.exception('^^^ exception ^^^')
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
log.error('^^^^ ^^^ ^^ ^ ### END ### Exception ^ ^^ ^^^ ^^^^')
|
||||
return False # Not successful
|
||||
else:
|
||||
log.info('Successfully executed the SQL on the first try.')
|
||||
@@ -613,12 +728,13 @@ def sql_delete(
|
||||
log.debug(dir(result))
|
||||
log.debug(vars(result))
|
||||
except OperationalError as e:
|
||||
log.warning('*** An exception happened: OperationalError ***')
|
||||
log.warning('* This is likely a "MySQL server has gone away" error. Going to try again... *')
|
||||
log.warning(repr(e))
|
||||
log.warning('***')
|
||||
log.warning(str(e))
|
||||
log.warning('^^^ exception ^^^')
|
||||
log.warning('This is likely a "MySQL server has gone away" error. Going to try again...')
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: OperationalError MySQL server connection? * ** *** ****')
|
||||
log.error('^^^^ ^^^ ^^ ^ ### END ### Exception ^ ^^ ^^^ ^^^^')
|
||||
# log.warning(repr(e))
|
||||
# log.warning('***')
|
||||
# log.warning(str(e))
|
||||
# log.warning('^^^ exception ^^^')
|
||||
|
||||
log.warning('Trying to recreate the pool...')
|
||||
log.debug('############## ############')
|
||||
@@ -643,21 +759,16 @@ def sql_delete(
|
||||
result = db.execute(sql)
|
||||
log.debug(result)
|
||||
except Exception as e:
|
||||
log.warning('2x A *second* exception happened. Returning False.')
|
||||
log.exception(repr(e))
|
||||
log.exception('***')
|
||||
log.exception(str(e))
|
||||
log.exception('^^^ exception ^^^')
|
||||
log.warning('Tried again an exception was raised again. Not going to try again.')
|
||||
log.exception('**** *** ** * ### BEGIN ### (2x) Second Exception Happened: * ** *** ****')
|
||||
log.error('^^^^ ^^^ ^^ ^ ### END ### Exception ^ ^^ ^^^ ^^^^')
|
||||
return False # Not successful
|
||||
else:
|
||||
log.info('Successfully executed the SQL on the second try.')
|
||||
pass
|
||||
except Exception as e:
|
||||
log.info('An exception happened. Returning False.')
|
||||
log.exception(repr(e))
|
||||
log.exception('***')
|
||||
log.exception(str(e))
|
||||
log.exception('^^^ exception ^^^')
|
||||
log.exception('**** *** ** * ### BEGIN ### Exception Happened: Returning False * ** *** ****')
|
||||
log.error('^^^^ ^^^ ^^ ^ ### END ### Exception ^ ^^ ^^^ ^^^^')
|
||||
return False # Not successful
|
||||
else:
|
||||
log.info('Successfully executed the SQL on the first try.')
|
||||
|
||||
Reference in New Issue
Block a user