diff options
Diffstat (limited to 'gn_auth/auth/db/mariadb.py')
-rw-r--r-- | gn_auth/auth/db/mariadb.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/gn_auth/auth/db/mariadb.py b/gn_auth/auth/db/mariadb.py index a934fd9..69c1d8e 100644 --- a/gn_auth/auth/db/mariadb.py +++ b/gn_auth/auth/db/mariadb.py @@ -1,12 +1,22 @@ """Connections to MariaDB""" +import logging import traceback import contextlib -from typing import Iterator +from urllib.parse import urlparse +from typing import Tuple, Iterator import MySQLdb as mdb from .protocols import DbConnection +def parse_db_url(sql_uri: str) -> Tuple: + """Parse SQL_URI env variable note:there is a default value for SQL_URI so a + tuple result is always expected""" + parsed_db = urlparse(sql_uri) + return ( + parsed_db.hostname, parsed_db.username, parsed_db.password, + parsed_db.path[1:], parsed_db.port) + @contextlib.contextmanager def database_connection(sql_uri) -> Iterator[DbConnection]: """Connect to MySQL database.""" @@ -18,7 +28,7 @@ def database_connection(sql_uri) -> Iterator[DbConnection]: port=port or 3306) try: yield connection - except Exception as _exc: # TODO: Make the Exception class less general + except mdb.Error as _mdb_err: logging.debug(traceback.format_exc()) connection.rollback() finally: |