aboutsummaryrefslogtreecommitdiff
path: root/gn3/db_utils.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-04-04 10:07:03 +0300
committerFrederick Muriuki Muriithi2023-04-05 14:51:04 +0300
commit3d873435f0d464864d4d691d6be4db40931fac05 (patch)
tree7d311b19234d46d3a0e75ee6404b64cdfc0ee84b /gn3/db_utils.py
parent87b439265ad44b90742e69a992e2fd66fdc48c49 (diff)
downloadgenenetwork3-3d873435f0d464864d4d691d6be4db40931fac05.tar.gz
Enable use of `database_connection` in scripts without current_app
There is need to run external scripts using the same configurations as the application but without the need to couple the script to the application. In this case, we provide the needed configuration directly in the CLI, and modify the existing `gn3.db_utils.database_connection` function to allow it to work coupled to the app or otherwise.
Diffstat (limited to 'gn3/db_utils.py')
-rw-r--r--gn3/db_utils.py18
1 files changed, 12 insertions, 6 deletions
diff --git a/gn3/db_utils.py b/gn3/db_utils.py
index 862d76c..4827358 100644
--- a/gn3/db_utils.py
+++ b/gn3/db_utils.py
@@ -7,20 +7,21 @@ import xapian
from flask import current_app
-def parse_db_url() -> Tuple:
+def parse_db_url(sql_uri: str) -> Tuple:
"""function to parse SQL_URI env variable note:there\
is a default value for SQL_URI so a tuple result is\
always expected"""
- parsed_db = urlparse(current_app.config["SQL_URI"])
+ parsed_db = urlparse(sql_uri)
return (
parsed_db.hostname, parsed_db.username, parsed_db.password,
parsed_db.path[1:], parsed_db.port)
# This function is deprecated. Use database_connection instead.
-def database_connector() -> mdb.Connection:
+def database_connector(sql_uri: str = "") -> mdb.Connection:
"""function to create db connector"""
- host, user, passwd, db_name, db_port = parse_db_url()
+ host, user, passwd, db_name, db_port = parse_db_url(
+ sql_uri or current_app.config["SQL_URI"])
return mdb.connect(host, user, passwd, db_name, port=(db_port or 3306))
@@ -33,10 +34,15 @@ class Connection(Protocol):
...
+## We need to decouple current_app from this module and function, but since this
+## function is used throughout the code, that will require careful work to update
+## all the code to pass the `sql_uri` argument, and make it a compulsory argument
+## rather than its current optional state.
@contextlib.contextmanager
-def database_connection() -> Iterator[Connection]:
+def database_connection(sql_uri: str = "") -> Iterator[Connection]:
"""Connect to MySQL database."""
- host, user, passwd, db_name, port = parse_db_url()
+ host, user, passwd, db_name, port = parse_db_url(
+ sql_uri or current_app.config["SQL_URI"])
connection = mdb.connect(db=db_name,
user=user,
passwd=passwd or '',