Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Commit

Permalink
Raise an exception when trying to query the DB without needed env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
locriandev committed Jan 31, 2023
1 parent f97c289 commit da7bfdd
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions doozerlib/dblib.py
Expand Up @@ -135,21 +135,25 @@ def select(self, expr, limit=100):
"""

if not self.mysql_db_env_var_setup:
self.runtime.logger.error('No queries can be made without DB env vars setup!')
raise RuntimeError

exeresult = []
if self.mysql_db_env_var_setup:
db_connection = mysql_connector.connect(host=self.host,
user=self.db_user,
password=self.pwd,
database=self.db)
cursor = db_connection.cursor()
try:
cursor.execute("{} LIMIT {}".format(expr, limit))
exeresult = cursor.fetchall()
self.runtime.logger.info(exeresult)
except Exception as e:
self.runtime.logger.error("Error executing command in database. Exception is [{}].".format(e))
finally:
cursor.close()
db_connection = mysql_connector.connect(host=self.host,
user=self.db_user,
password=self.pwd,
database=self.db)
cursor = db_connection.cursor()
try:
cursor.execute("{} LIMIT {}".format(expr, limit))
exeresult = cursor.fetchall()
self.runtime.logger.info(exeresult)
except Exception as e:
self.runtime.logger.error("Error executing command in database. Exception is [{}].".format(e))
finally:
cursor.close()

return exeresult

def check_database_exists(self):
Expand Down

0 comments on commit da7bfdd

Please sign in to comment.