From 627558c489298f00e36e08ac2cd29aa9872952f5 Mon Sep 17 00:00:00 2001 From: Anthony Samms Date: Thu, 18 Dec 2025 11:45:24 -0500 Subject: [PATCH] add db versioning --- PyTaiko.py | 16 +++++++++++++++- libs/song_hash.py | 25 +++++++++++++++++++------ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/PyTaiko.py b/PyTaiko.py index 3f42132..e736c03 100644 --- a/PyTaiko.py +++ b/PyTaiko.py @@ -17,6 +17,7 @@ from raylib.defines import ( from libs.audio import audio from libs.global_data import PlayerNum from libs.screen import Screen +from libs.song_hash import DB_VERSION from libs.tja import TJAParser from libs.utils import ( force_dedicated_gpu, @@ -118,6 +119,13 @@ def create_song_db(): """Create the scores database if it doesn't exist""" with sqlite3.connect('scores.db') as con: cursor = con.cursor() + + cursor.execute(''' + SELECT name FROM sqlite_master + WHERE type='table' AND name='Scores' + ''') + table_exists = cursor.fetchone() is not None + create_table_query = ''' CREATE TABLE IF NOT EXISTS Scores ( hash TEXT PRIMARY KEY, @@ -134,8 +142,14 @@ def create_song_db(): ); ''' cursor.execute(create_table_query) + + if not table_exists: + cursor.execute(f'PRAGMA user_version = {DB_VERSION}') + logger.info(f"Scores database created successfully with version {DB_VERSION}") + else: + logger.info("Scores database already exists") + con.commit() - logger.info("Scores database created successfully") def update_camera_for_window_size(camera, virtual_width, virtual_height): """Update camera zoom, offset, scale, and rotation to maintain aspect ratio""" diff --git a/libs/song_hash.py b/libs/song_hash.py index 78055cc..e9bfea0 100644 --- a/libs/song_hash.py +++ b/libs/song_hash.py @@ -12,6 +12,7 @@ from libs.utils import global_data from libs.config import get_config logger = logging.getLogger(__name__) +DB_VERSION = 1 def diff_hashes_object_hook(obj): if "diff_hashes" in obj: @@ -25,6 +26,18 @@ class DiffHashesDecoder(json.JSONDecoder): def __init__(self, *args, **kwargs): super().__init__(object_hook=diff_hashes_object_hook, *args, **kwargs) +def get_db_version(): + with sqlite3.connect('scores.db') as con: + cursor = con.cursor() + cursor.execute('PRAGMA user_version') + version = cursor.fetchone()[0] + return version + +def update_db_version(): + with sqlite3.connect('scores.db') as con: + cursor = con.cursor() + cursor.execute(f'PRAGMA user_version = {DB_VERSION}') + def read_tjap3_score(input_file: Path): """Read a TJAPlayer3 score.ini file and return the scores and clears.""" score_ini = configparser.ConfigParser() @@ -70,12 +83,12 @@ def build_song_hashes(output_dir=Path("cache")): if output_path.exists(): with open(output_path, "r", encoding="utf-8") as f: song_hashes = json.load(f, cls=DiffHashesDecoder) - ''' - for hash in song_hashes: - entry = song_hashes[hash][0] - for diff in entry["diff_hashes"]: - db_updates.append((entry["diff_hashes"][diff], entry["title"]["en"], entry["title"].get("ja", ""), int(diff))) - ''' + if get_db_version() != DB_VERSION: + update_db_version() + for hash in song_hashes: + entry = song_hashes[hash][0] + for diff in entry["diff_hashes"]: + db_updates.append((entry["diff_hashes"][diff], entry["title"]["en"], entry["title"].get("ja", ""), int(diff))) if index_path.exists(): with open(index_path, "r", encoding="utf-8") as f: