dbutils: version can now be left black to auto-upgrade to latest

This commit is contained in:
Hay1tsme 2023-04-15 00:13:04 -04:00
parent baa885f674
commit a416fb09e1
2 changed files with 29 additions and 9 deletions

View File

@ -32,7 +32,7 @@ class Data:
self.arcade = ArcadeData(self.config, self.session) self.arcade = ArcadeData(self.config, self.session)
self.card = CardData(self.config, self.session) self.card = CardData(self.config, self.session)
self.base = BaseData(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_str = "[%(asctime)s] %(levelname)s | Database | %(message)s"
log_fmt = logging.Formatter(log_fmt_str) log_fmt = logging.Formatter(log_fmt_str)
@ -84,8 +84,8 @@ class Data:
f"Could not load database schema from {game_dir} - {e}" f"Could not load database schema from {game_dir} - {e}"
) )
self.logger.info(f"Setting base_schema_ver to {self.schema_ver_latest}") self.logger.info(f"Setting base_schema_ver to {self.current_schema_version}")
self.base.set_schema_ver(self.schema_ver_latest) self.base.set_schema_ver(self.current_schema_version)
self.logger.info( self.logger.info(
f"Setting user auto_incrememnt to {self.config.database.user_table_autoincrement_start}" f"Setting user auto_incrememnt to {self.config.database.user_table_autoincrement_start}"
@ -129,9 +129,28 @@ class Data:
self.create_database() 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) old_ver = self.base.get_schema_ver(game)
sql = "" 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: if old_ver is None:
self.logger.error( self.logger.error(

View File

@ -1,5 +1,6 @@
import yaml import yaml
import argparse import argparse
import logging
from core.config import CoreConfig from core.config import CoreConfig
from core.data import Data from core.data import Data
from os import path, mkdir, access, W_OK from os import path, mkdir, access, W_OK
@ -43,6 +44,7 @@ if __name__ == "__main__":
) )
exit(1) exit(1)
cfg.update({"database": {"loglevel": "info"}}) # Force it to be info if we're doing db work
data = Data(cfg) data = Data(cfg)
@ -54,15 +56,14 @@ if __name__ == "__main__":
elif args.action == "upgrade" or args.action == "rollback": elif args.action == "upgrade" or args.action == "rollback":
if args.version is None: if args.version is None:
data.logger.error("Must set game and version to migrate to") data.logger.warn("No version set, upgrading to latest")
exit(0)
if args.game is None: if args.game is None:
data.logger.info("No game set, upgrading core schema") data.logger.warn("No game set, upgrading core schema")
data.migrate_database("CORE", int(args.version), args.action) data.migrate_database("CORE", int(args.version) if args.version is not None else None, args.action)
else: 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": elif args.action == "autoupgrade":
data.autoupgrade() data.autoupgrade()