From b42e82f31423a098e2b5ee78d0f347fe94bbea4d Mon Sep 17 00:00:00 2001 From: Mike Montano Date: Tue, 5 Dec 2023 11:30:30 -0600 Subject: [PATCH] PR functional changes addressed --- logpyle/runalyzer.py | 2 +- logpyle/upgrade_db.py | 16 +++++++--------- test/test_upgrade_db.py | 16 ++++++++-------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/logpyle/runalyzer.py b/logpyle/runalyzer.py index 9f2971b..724dc36 100644 --- a/logpyle/runalyzer.py +++ b/logpyle/runalyzer.py @@ -465,7 +465,7 @@ def my_sprintf(format: str, arg: str) -> str: def is_gathered(conn: sqlite3.Connection) -> bool: """ - The function checks whether a connection to an existing + Returns whether a connection to an existing database has been gathered. Parameters diff --git a/logpyle/upgrade_db.py b/logpyle/upgrade_db.py index 120e604..61e9b28 100644 --- a/logpyle/upgrade_db.py +++ b/logpyle/upgrade_db.py @@ -3,9 +3,12 @@ -------------------------------- .. autofunction:: upgrade_db """ +import logging import shutil import sqlite3 +logger = logging.getLogger(__name__) + def upgrade_conn(conn: sqlite3.Connection) -> sqlite3.Connection: from logpyle.runalyzer import is_gathered @@ -64,18 +67,13 @@ def upgrade_db( dbfile: str, suffix: str, overwrite: bool ) -> None: """ - - The function first connects to the original database . If the + Upgrade a database file to the most recent format. If the `overwrite` parameter is True, it simply modifies the existing database and uses the same file name for the upgraded database. Otherwise, a new database is created with a separate filename by appending the given suffix to the original file's base name using `filename + suffix + "." + file_ext`. - Next, the function prints a message indicating whether it is - overwriting or creating a new database and then proceeds to - upgrade the database schema version to 3. - Parameters ---------- dbfile @@ -97,7 +95,7 @@ def upgrade_db( # simply perform modifications on old connection new_conn_name = dbfile new_conn = old_conn - print(f"Overwriting Database: {new_conn_name}") + logger.info(f"Overwriting Database: {new_conn_name}") else: # seperate the filename and the extention @@ -109,9 +107,9 @@ def upgrade_db( new_conn = sqlite3.connect(new_conn_name) - print(f"Creating new Database: {new_conn_name}, a clone of {dbfile}") + logger.info(f"Creating new Database: {new_conn_name}, a clone of {dbfile}") - print(f"Upgrading {new_conn_name} to schema version 3") + logger.info(f"Upgrading {new_conn_name} to schema version 3") new_conn = upgrade_conn(new_conn) diff --git a/test/test_upgrade_db.py b/test/test_upgrade_db.py index 7445348..75634f4 100644 --- a/test/test_upgrade_db.py +++ b/test/test_upgrade_db.py @@ -1,3 +1,5 @@ +import os +import pytest import sqlite3 from logpyle import upgrade_db @@ -6,27 +8,25 @@ def test_upgrade_v2_v3(): path = ".github/workflows/log_gathered_v2.sqlite" suffix = "_pytest_upgrade" - # print(path.rsplit(".", 1)) - # assert False filename, file_ext = path.rsplit(".", 1) # ensure it is V2 conn = sqlite3.connect(filename + "." + file_ext) - try: + with pytest.raises(sqlite3.OperationalError): # should throw an exception because logging # should not exist in a V2 database print(list(conn.execute("select * from logging"))) - raise AssertionError(f"{filename} is a v3 database") - except sqlite3.OperationalError: - pass # v2 should not have a logging table conn.close() upgrade_db.upgrade_db(path, suffix, False) # ensure it is V3 - conn = sqlite3.connect(filename + suffix + "." + file_ext) + upgraded_name = filename + suffix + "." + file_ext + conn = sqlite3.connect(upgraded_name) try: print(list(conn.execute("select * from logging"))) except sqlite3.OperationalError: - raise AssertionError(f"{filename} is not a v3 database") + os.remove(upgraded_name) + raise AssertionError(f"{upgraded_name} is not a v3 database") conn.close() + os.remove(upgraded_name)