From a416fb09e1ea3ff53e9d80f1f8af405f4890ea2b Mon Sep 17 00:00:00 2001 From: Kevin Trocolli Date: Sat, 15 Apr 2023 00:13:04 -0400 Subject: [PATCH] dbutils: version can now be left black to auto-upgrade to latest --- core/data/database.py | 27 +++++++++++++++++++++++---- dbutils.py | 11 ++++++----- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/core/data/database.py b/core/data/database.py index 07fe79e..c52e61c 100644 --- a/core/data/database.py +++ b/core/data/database.py @@ -32,7 +32,7 @@ class Data: self.arcade = ArcadeData(self.config, self.session) self.card = CardData(self.config, self.session) self.base = BaseData(self.config, self.session) - self.schema_ver_latest = 4 + self.current_schema_version = 4 log_fmt_str = "[%(asctime)s] %(levelname)s | Database | %(message)s" log_fmt = logging.Formatter(log_fmt_str) @@ -84,8 +84,8 @@ class Data: f"Could not load database schema from {game_dir} - {e}" ) - self.logger.info(f"Setting base_schema_ver to {self.schema_ver_latest}") - self.base.set_schema_ver(self.schema_ver_latest) + self.logger.info(f"Setting base_schema_ver to {self.current_schema_version}") + self.base.set_schema_ver(self.current_schema_version) self.logger.info( f"Setting user auto_incrememnt to {self.config.database.user_table_autoincrement_start}" @@ -129,9 +129,28 @@ class Data: self.create_database() - def migrate_database(self, game: str, version: int, action: str) -> None: + def migrate_database(self, game: str, version: Optional[int], action: str) -> None: old_ver = self.base.get_schema_ver(game) sql = "" + if version is None: + if not game == "CORE": + titles = Utils.get_all_titles() + + for folder, mod in titles.items(): + for game_ in mod.game_codes: + if not game_ == game: continue + + if hasattr(mod, "current_schema_version"): + version = mod.current_schema_version + + else: + self.logger.warn(f"current_schema_version not found for {folder}") + + else: + version = self.current_schema_version + + if version is None: + self.logger.warn(f"Could not determine latest version for {game}, please specify --version") if old_ver is None: self.logger.error( diff --git a/dbutils.py b/dbutils.py index 176c67e..14af98e 100644 --- a/dbutils.py +++ b/dbutils.py @@ -1,5 +1,6 @@ import yaml import argparse +import logging from core.config import CoreConfig from core.data import Data from os import path, mkdir, access, W_OK @@ -43,6 +44,7 @@ if __name__ == "__main__": ) exit(1) + cfg.update({"database": {"loglevel": "info"}}) # Force it to be info if we're doing db work data = Data(cfg) @@ -54,15 +56,14 @@ if __name__ == "__main__": elif args.action == "upgrade" or args.action == "rollback": if args.version is None: - data.logger.error("Must set game and version to migrate to") - exit(0) + data.logger.warn("No version set, upgrading to latest") if args.game is None: - data.logger.info("No game set, upgrading core schema") - data.migrate_database("CORE", int(args.version), args.action) + data.logger.warn("No game set, upgrading core schema") + data.migrate_database("CORE", int(args.version) if args.version is not None else None, args.action) else: - data.migrate_database(args.game, int(args.version), args.action) + data.migrate_database(args.game, int(args.version) if args.version is not None else None, args.action) elif args.action == "autoupgrade": data.autoupgrade()