diff options
author | Frederick Muriuki Muriithi | 2024-11-22 12:18:46 -0600 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2024-11-22 12:18:46 -0600 |
commit | 687b8858b1e8eaee727fa573d7e3f670b4d11f89 (patch) | |
tree | 88739c25fd7140ff31d617375fe5dc282790f0d7 /uploader | |
parent | 8a84c4d6762446e5fdf9f9121f539c89419ae6a0 (diff) | |
download | gn-uploader-687b8858b1e8eaee727fa573d7e3f670b4d11f89.tar.gz |
Reuse the code in gn-libs for connecting to the database, rather than
using the replicated local code.
Diffstat (limited to 'uploader')
-rw-r--r-- | uploader/db_utils.py | 40 |
1 files changed, 3 insertions, 37 deletions
diff --git a/uploader/db_utils.py b/uploader/db_utils.py index d31e2c2..d9d521e 100644 --- a/uploader/db_utils.py +++ b/uploader/db_utils.py @@ -1,54 +1,20 @@ """module contains all db related stuff""" -import logging -import traceback -import contextlib -from urllib.parse import urlparse -from typing import Any, Tuple, Iterator, Callable +from typing import Any, Callable import MySQLdb as mdb from redis import Redis -from MySQLdb.cursors import Cursor from flask import current_app as app +from gn_libs.mysqldb import database_connection -def parse_db_url(db_url) -> Tuple: - """ - Parse SQL_URI configuration variable. - """ - parsed_db = urlparse(db_url) - return (parsed_db.hostname, parsed_db.username, - parsed_db.password, parsed_db.path[1:], parsed_db.port) - - -@contextlib.contextmanager -def database_connection(db_url: str) -> Iterator[mdb.Connection]: - """function to create db connector""" - host, user, passwd, db_name, db_port = parse_db_url(db_url) - connection = mdb.connect( - host, user, passwd, db_name, port=(db_port or 3306)) - try: - yield connection - connection.commit() - except mdb.Error as _mdb_err: - logging.error(traceback.format_exc()) - connection.rollback() - finally: - connection.close() def with_db_connection(func: Callable[[mdb.Connection], Any]) -> Any: """Call `func` with a MySQDdb database connection.""" with database_connection(app.config["SQL_URI"]) as conn: return func(conn) + def with_redis_connection(func: Callable[[Redis], Any]) -> Any: """Call `func` with a redis connection.""" redisuri = app.config["REDIS_URL"] with Redis.from_url(redisuri, decode_responses=True) as rconn: return func(rconn) - - -def debug_query(cursor: Cursor): - """Debug the actual query run with MySQLdb""" - for attr in ("_executed", "statement", "_last_executed"): - if hasattr(cursor, attr): - logging.debug("MySQLdb QUERY: %s", getattr(cursor, attr)) - break |