Check for orphan backups on migration

This commit is contained in:
= 2024-08-13 00:12:22 -04:00
parent 2b14d514e0
commit 40f753162f

View File

@ -13,9 +13,9 @@ from app.classes.shared.file_helpers import FileHelpers
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def is_valid_backup(backup, all_servers): def is_valid_entry(entry, all_servers):
try: try:
return str(backup.server_id) in all_servers return str(entry.server_id) in all_servers
except (TypeError, peewee.DoesNotExist): except (TypeError, peewee.DoesNotExist):
return False return False
@ -161,9 +161,14 @@ def migrate(migrator: Migrator, database, **kwargs):
row.server_id for row in Servers.select(Servers.server_id).distinct() row.server_id for row in Servers.select(Servers.server_id).distinct()
] ]
all_backups = Backups.select() all_backups = Backups.select()
all_schedules = Schedules.select()
Console.info("Cleaning up orphan backups for all servers") Console.info("Cleaning up orphan backups for all servers")
valid_backups = [ 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 # Copy data from the existing backups table to the new one
for backup in valid_backups: for backup in valid_backups:
@ -221,13 +226,19 @@ def migrate(migrator: Migrator, database, **kwargs):
Console.debug("Migrations: Dropping backup_path from servers table") Console.debug("Migrations: Dropping backup_path from servers table")
migrator.drop_columns("servers", ["backup_path"]) migrator.drop_columns("servers", ["backup_path"])
for schedule in Schedules.select(): for schedule in valid_schedules:
action_id = None action_id = None
if schedule.command == "backup_server": if schedule.command == "backup_server":
Console.info( Console.info(
f"Migrations: Adding backup ID to task with name {schedule.name}" f"Migrations: Adding backup ID to task with name {schedule.name}"
) )
try:
backup = NewBackups.get(NewBackups.server_id == schedule.server_id) 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 action_id = backup.backup_id
NewSchedules.create( NewSchedules.create(
schedule_id=schedule.schedule_id, schedule_id=schedule.schedule_id,