From 3dfca4e4bd799f56c09f70bb9d63a8fb24b8e1c9 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Mon, 13 Jan 2025 17:31:55 +0100 Subject: [PATCH] fix: backup job deletion - delete all backups from s3 and local --- app/Livewire/Project/Database/BackupEdit.php | 32 +++++++++++++++---- .../Project/Database/BackupExecutions.php | 8 +++-- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/Livewire/Project/Database/BackupEdit.php b/app/Livewire/Project/Database/BackupEdit.php index 19b7987db..853b918b8 100644 --- a/app/Livewire/Project/Database/BackupEdit.php +++ b/app/Livewire/Project/Database/BackupEdit.php @@ -123,13 +123,29 @@ class BackupEdit extends Component } try { - if ($this->delete_associated_backups_locally) { - $filenames = $this->backup->executions->pluck('filename')->filter()->all(); - deleteBackupsLocally($filenames, $this->backup->server); + $server = null; + if ($this->backup->database instanceof \App\Models\ServiceDatabase) { + $server = $this->backup->database->service->destination->server; + } elseif ($this->backup->database->destination && $this->backup->database->destination->server) { + $server = $this->backup->database->destination->server; } - if ($this->delete_associated_backups_s3 && $this->backup->s3) { - $filenames = $this->backup->executions->pluck('filename')->filter()->all(); - deleteBackupsS3($filenames, $this->backup->s3); + + $filenames = $this->backup->executions() + ->whereNotNull('filename') + ->where('filename', '!=', '') + ->where('scheduled_database_backup_id', $this->backup->id) + ->pluck('filename') + ->filter() + ->all(); + + if (! empty($filenames)) { + if ($this->delete_associated_backups_locally && $server) { + deleteBackupsLocally($filenames, $server); + } + + if ($this->delete_associated_backups_s3 && $this->backup->s3) { + deleteBackupsS3($filenames, $this->backup->s3); + } } $this->backup->delete(); @@ -145,7 +161,9 @@ class BackupEdit extends Component } else { return redirect()->route('project.database.backup.index', $this->parameters); } - } catch (\Throwable $e) { + } catch (\Exception $e) { + $this->dispatch('error', 'Failed to delete backup: '.$e->getMessage()); + return handleError($e, $this); } } diff --git a/app/Livewire/Project/Database/BackupExecutions.php b/app/Livewire/Project/Database/BackupExecutions.php index 526239eea..7eef1a539 100644 --- a/app/Livewire/Project/Database/BackupExecutions.php +++ b/app/Livewire/Project/Database/BackupExecutions.php @@ -62,10 +62,12 @@ class BackupExecutions extends Component : $execution->scheduledDatabaseBackup->database->destination->server; try { - deleteBackupsLocally($execution->filename, $server); + if ($execution->filename) { + deleteBackupsLocally($execution->filename, $server); - if ($this->delete_backup_s3 && $execution->scheduledDatabaseBackup->s3) { - deleteBackupsS3($execution->filename, $execution->scheduledDatabaseBackup->s3); + if ($this->delete_backup_s3 && $execution->scheduledDatabaseBackup->s3) { + deleteBackupsS3($execution->filename, $execution->scheduledDatabaseBackup->s3); + } } $execution->delete();