From 40f753162ffe1b92048a8400bd438ddc4cb1ea44 Mon Sep 17 00:00:00 2001 From: = Date: Tue, 13 Aug 2024 00:12:22 -0400 Subject: [PATCH] Check for orphan backups on migration --- app/migrations/20240308_multi-backup.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/app/migrations/20240308_multi-backup.py b/app/migrations/20240308_multi-backup.py index 56e01a5c..97ee8ce7 100644 --- a/app/migrations/20240308_multi-backup.py +++ b/app/migrations/20240308_multi-backup.py @@ -13,9 +13,9 @@ from app.classes.shared.file_helpers import FileHelpers logger = logging.getLogger(__name__) -def is_valid_backup(backup, all_servers): +def is_valid_entry(entry, all_servers): try: - return str(backup.server_id) in all_servers + return str(entry.server_id) in all_servers except (TypeError, peewee.DoesNotExist): return False @@ -161,9 +161,14 @@ def migrate(migrator: Migrator, database, **kwargs): row.server_id for row in Servers.select(Servers.server_id).distinct() ] all_backups = Backups.select() + all_schedules = Schedules.select() Console.info("Cleaning up orphan backups for all servers") valid_backups = [ - backup for backup in all_backups if is_valid_backup(backup, all_servers) + backup for backup in all_backups if is_valid_entry(backup, all_servers) + ] + Console.info("Cleaning up orphan schedules for all servers") + valid_schedules = [ + schedule for schedule in all_schedules if is_valid_entry(schedule, all_servers) ] # Copy data from the existing backups table to the new one for backup in valid_backups: @@ -221,13 +226,19 @@ def migrate(migrator: Migrator, database, **kwargs): Console.debug("Migrations: Dropping backup_path from servers table") migrator.drop_columns("servers", ["backup_path"]) - for schedule in Schedules.select(): + for schedule in valid_schedules: action_id = None if schedule.command == "backup_server": Console.info( f"Migrations: Adding backup ID to task with name {schedule.name}" ) - backup = NewBackups.get(NewBackups.server_id == schedule.server_id) + try: + backup = NewBackups.get(NewBackups.server_id == schedule.server_id) + except: + Console.error( + "Could not find backup with selected server ID. Omitting from register." + ) + continue action_id = backup.backup_id NewSchedules.create( schedule_id=schedule.schedule_id,