aboutsummaryrefslogtreecommitdiff
path: root/qc_app/db_utils.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2022-07-18 11:32:44 +0300
committerFrederick Muriuki Muriithi2022-07-19 05:08:17 +0300
commit044184ef28a091519b7632d582387c26bf1543ea (patch)
tree5c3a08605148d5a718cbb0ecd8fc6d9965242250 /qc_app/db_utils.py
parent52024b1f6cde74b51181ce4108cf01611a7ea636 (diff)
downloadgn-uploader-044184ef28a091519b7632d582387c26bf1543ea.tar.gz
Check connections before launching
* qc_app/__init__.py (refactor): Check connection before launching the application * qc_app/check_connections.py (new file): Add code to check connections * qc_app/db_utils.py (refactor): enable passing the database uri as an argument to the connection creation function. * scripts/worker.py (refactor): Use new code to check for redis connection.
Diffstat (limited to 'qc_app/db_utils.py')
-rw-r--r--qc_app/db_utils.py10
1 files changed, 5 insertions, 5 deletions
diff --git a/qc_app/db_utils.py b/qc_app/db_utils.py
index 95b0057..239f45c 100644
--- a/qc_app/db_utils.py
+++ b/qc_app/db_utils.py
@@ -1,20 +1,20 @@
"""module contains all db related stuff"""
-from typing import Tuple
+from typing import Tuple, Optional
from urllib.parse import urlparse
import MySQLdb as mdb
from flask import current_app as app
-def parse_db_url() -> Tuple:
+def parse_db_url(db_url) -> Tuple:
"""
Parse SQL_URI configuration variable.
"""
- parsed_db = urlparse(app.config["SQL_URI"])
+ parsed_db = urlparse(db_url)
return (parsed_db.hostname, parsed_db.username,
parsed_db.password, parsed_db.path[1:])
-def database_connection() -> mdb.Connection:
+def database_connection(db_url: Optional[str] = None) -> mdb.Connection:
"""function to create db connector"""
- host, user, passwd, db_name = parse_db_url()
+ host, user, passwd, db_name = parse_db_url(db_url or app.config["SQL_URI"])
return mdb.connect(host, user, passwd, db_name)