about summary refs log tree commit diff
path: root/gn3/db_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'gn3/db_utils.py')
-rw-r--r--gn3/db_utils.py23
1 files changed, 2 insertions, 21 deletions
diff --git a/gn3/db_utils.py b/gn3/db_utils.py
index 7004590..5ce5f00 100644
--- a/gn3/db_utils.py
+++ b/gn3/db_utils.py
@@ -7,6 +7,8 @@ from typing import Any, Iterator, Protocol, Callable
 import xapian
 import MySQLdb as mdb
 
+from gn_libs.mysqldb import Connection, database_connection
+
 
 LOGGER = logging.getLogger(__file__)
 
@@ -68,27 +70,6 @@ def parse_db_url(sql_uri: str) -> dict:
     }
 
 
-# pylint: disable=missing-class-docstring, missing-function-docstring, too-few-public-methods
-class Connection(Protocol):
-    """Type Annotation for MySQLdb's connection object"""
-    def cursor(self, *args, **kwargs) -> Any:
-        """A cursor in which queries may be performed"""
-
-
-@contextlib.contextmanager
-def database_connection(sql_uri: str, logger: logging.Logger = LOGGER) -> Iterator[Connection]:
-    """Connect to MySQL database."""
-    connection = mdb.connect(**parse_db_url(sql_uri))
-    try:
-        yield connection
-    except mdb.Error as _mbde:
-        logger.error("DB error encountered", exc_info=True)
-        connection.rollback()
-    finally:
-        connection.commit()
-        connection.close()
-
-
 @contextlib.contextmanager
 def xapian_database(path):
     """Open xapian database read-only."""