from core.errors.logger import logger from core.models.manage.session_management import init_session, get_session, close_session, create_ALL_tables, create_ONLY_db_version_table, get_path from core.models.manage.version_check import check_database_compatibility, insert_new_version, get_custom_message from core.Constants import Constants # generic import os import sys # ============================================================================ # INITIALIZE DATABASE # ============================================================================ # (engine, Session, _session all initialized from session_management) init_session() session = get_session() create_ONLY_db_version_table() # ============================================================================ # COMPARE DB VERISONS # ============================================================================ compatability_dict = check_database_compatibility(session) reason = compatability_dict.get("reason", "error") is_compatable = compatability_dict.get("result", False) logger.info(f"[DB MANAGEMENT] The result of the check is {is_compatable} and the reason is {reason}") # ============================================================================ # IF THE VERSIONS MATCH # ============================================================================ if is_compatable: made_tables = create_ALL_tables() # SCREEN FAILED TABLES if not made_tables: logger.error(f"[DB MANAGEMENT] Critical Failure with starting the models of the database.") close_session() sys.exit() # ASSUME TABLES CREATED logger.info("[DB MANAGEMENT] Tables successfully made or initialized if pre-existing") # original reason was he had no database, now tag it that he's got the new version, if reason == "no_database": logger.info(f"[DB MANAGEMENT] User had no database to begin with, so after we created the database, we are about to insert the new version in the DB metadata table for next time.") # UPDATE DATA did_it_insert = insert_new_version(session, Constants.DB_VERSION_THIS_APP_WANTS) if not did_it_insert: logger.error(f"[DB MANAGEMENT] Critical Failure with updating/inserting the new DB version into the database.") logger.info(f"[DB MANAGEMENT] Launcher is now passing it off to launch the main GUI window...") from main_ui import start_ui force_sync = False start_ui(force_sync) # ============================================================================ # but IF the versions do NOT match # ============================================================================ else: logger.error(f"[DB MANAGEMENT] The App is expecting a different DB version than the real database. The reason is {reason}") db_version_you_have = compatability_dict.get("old_db_version", "Error getting the Version") # WHAT IS THE REASON? custom_error = get_custom_message(reason, compatability_dict) # shut down db connection close_session() # ============================================================================ # THEN DISPLAY CHOICES AND RECOVERY UI # ============================================================================ from v2.ui.popups.Database_version import show_recovery_dialog choice = show_recovery_dialog({"message": custom_error, "db_version": db_version_you_have, "status": "version_mismatch"}) logger.info(f"[DB MANAGEMENT] From the database error options, the user picked {choice}") system_path = get_path() database_path = system_path / "storage.db" if choice == 1: if os.path.exists(database_path): os.remove(database_path) logger.info(f"[DB MANAGEMENT] Deleted the DB file at {database_path}") logger.info(f"[DB MANAGEMENT] Closing the app gracefully, for them to reboot..") sys.exit() elif choice == 2: logger.info(f"[DB MANAGEMENT] User opted to move the DB file.") from v2.ui.popups.pick_folder_to_move import launch_file_picker launch_file_picker(database_path) sys.exit() else: logger.error(f"[DB MANAGEMENT] User opted to close the app WITHOUT wiping the database, even though they need to.") sys.exit()