From f9caab321d56342a5cc330611ed18c38c643ed28 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 6 Dec 2024 12:47:00 -0600 Subject: Move the commit() call to the success path The `finally` clause gets run whether or not the try succeeds. It only makes sense to run the commit on success, so we move it into the try, just after yielding the connection. --- gn_libs/mysqldb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gn_libs/mysqldb.py b/gn_libs/mysqldb.py index 11302a2..be21550 100644 --- a/gn_libs/mysqldb.py +++ b/gn_libs/mysqldb.py @@ -87,11 +87,11 @@ def database_connection(sql_uri: str, logger: logging.Logger = _logger) -> Itera connection = mdb.connect(**parse_db_url(sql_uri)) try: yield connection + connection.commit() except mdb.Error as _mbde: logger.error("DB error encountered", exc_info=True) connection.rollback() finally: - connection.commit() connection.close() -- cgit v1.2.3