Skip to content

Commit

Permalink
PR functional changes addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
EdgesFTW committed Dec 5, 2023
1 parent 0a962b9 commit b42e82f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion logpyle/runalyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 7 additions & 9 deletions logpyle/upgrade_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)

Expand Down
16 changes: 8 additions & 8 deletions test/test_upgrade_db.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import pytest
import sqlite3

from logpyle import upgrade_db
Expand All @@ -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)

0 comments on commit b42e82f

Please sign in to comment.