From 52a6e9ae595d947085fc6f3cd740065d2826544a Mon Sep 17 00:00:00 2001 From: = Date: Mon, 16 Sep 2024 14:54:43 -0400 Subject: [PATCH] Add status file for migrations failure --- app/classes/shared/helpers.py | 2 ++ app/migrations/20240308_multi-backup.py | 22 ++++++++++++++++++++++ main.py | 22 +++++++++++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/classes/shared/helpers.py b/app/classes/shared/helpers.py index e827d5b2..89de0d5b 100644 --- a/app/classes/shared/helpers.py +++ b/app/classes/shared/helpers.py @@ -79,6 +79,7 @@ class Helpers: self.translation = Translation(self) self.update_available = False + self.migration_notifications = [] self.ignored_names = ["crafty_managed.txt", "db_stats"] self.crafty_starting = False self.minimum_password_length = 8 @@ -619,6 +620,7 @@ class Helpers: data = [] response = requests.get("https://craftycontrol.com/notify", timeout=2) data = json.loads(response.content) + data.extend(self.migration_notifications) if self.update_available: data.append(self.update_available) return data diff --git a/app/migrations/20240308_multi-backup.py b/app/migrations/20240308_multi-backup.py index 97ee8ce7..4c7f8279 100644 --- a/app/migrations/20240308_multi-backup.py +++ b/app/migrations/20240308_multi-backup.py @@ -1,4 +1,5 @@ import os +import json import datetime import uuid import peewee @@ -166,10 +167,15 @@ def migrate(migrator: Migrator, database, **kwargs): valid_backups = [ backup for backup in all_backups if is_valid_entry(backup, all_servers) ] + if len(valid_backups) < len(all_backups): + backup_migration_status = False + print("Orphan backup found") Console.info("Cleaning up orphan schedules for all servers") valid_schedules = [ schedule for schedule in all_schedules if is_valid_entry(schedule, all_servers) ] + if len(valid_schedules) > len(all_schedules): + schedule_migration_status = False # Copy data from the existing backups table to the new one for backup in valid_backups: Console.info(f"Trying to get server for backup migration {backup.server_id}") @@ -266,6 +272,22 @@ def migrate(migrator: Migrator, database, **kwargs): # Rename the new table to backups migrator.rename_table("new_schedules", "schedules") + with open("status/20240308_multi-backup.json", "w", encoding="utf-8") as file: + file.write( + json.dumps( + { + "backup_migration": { + "status": backup_migration_status, + "pid": uuid.uuid4(), + }, + "schedule_migration": { + "status": schedule_migration_status, + "pid": uuid.uuid4(), + }, + } + ) + ) + def rollback(migrator: Migrator, database, **kwargs): """ diff --git a/main.py b/main.py index a62a8d27..69cc0043 100644 --- a/main.py +++ b/main.py @@ -115,6 +115,23 @@ def controller_setup(): controller.clear_support_status() +def get_migration_notifications(): + migration_notifications = [] + for file in os.listdir( + os.path.join(APPLICATION_PATH, "app", "migrations", "status") + ): + if os.path.isfile(file): + with open( + os.path.join(APPLICATION_PATH, "app", "migrations", "status", file), + encoding="utf-8", + ) as status_file: + status_json = json.load(status_file) + for item in status_json: + if not status_json[item].get("status"): + migration_notifications.append(item) + return migration_notifications + + def tasks_starter(): """ Method starts stats recording, app scheduler, and @@ -350,6 +367,9 @@ if __name__ == "__main__": helper.db_path, pragmas={"journal_mode": "wal", "cache_size": -1024 * 10} ) database_proxy.initialize(database) + Helpers.ensure_dir_exists( + os.path.join(APPLICATION_PATH, "app", "migrations", "status") + ) migration_manager = MigrationManager(database, helper) migration_manager.up() # Automatically runs migrations @@ -408,7 +428,7 @@ if __name__ == "__main__": controller.set_project_root(APPLICATION_PATH) tasks_manager = TasksManager(helper, controller, file_helper) import3 = Import3(helper, controller) - + helper.migration_notifications = get_migration_notifications() # Check to see if client config.json version is different than the # Master config.json in helpers.py Console.info("Checking for remote changes to config.json")