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__)
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}"
)
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,