From 38845d7eb004e0d12b327f634898a10bd54785f3 Mon Sep 17 00:00:00 2001 From: ayntk-ai <122374094+ayntk-ai@users.noreply.github.com> Date: Sat, 31 Aug 2024 18:29:19 +0200 Subject: [PATCH] confirm backup and delete all backups of the job --- app/Livewire/Project/Database/BackupEdit.php | 42 ++++++++++++++++++- .../project/database/backup-edit.blade.php | 17 +++++--- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/app/Livewire/Project/Database/BackupEdit.php b/app/Livewire/Project/Database/BackupEdit.php index 59f2f9a39..35ca2c7ec 100644 --- a/app/Livewire/Project/Database/BackupEdit.php +++ b/app/Livewire/Project/Database/BackupEdit.php @@ -5,6 +5,8 @@ namespace App\Livewire\Project\Database; use App\Models\ScheduledDatabaseBackup; use Livewire\Component; use Spatie\Url\Url; +use Illuminate\Support\Facades\Hash; +use Illuminate\Support\Facades\Auth; class BackupEdit extends Component { @@ -12,6 +14,8 @@ class BackupEdit extends Component public $s3s; + public bool $delete_associated_backups = false; + public ?string $status = null; public array $parameters; @@ -46,16 +50,26 @@ class BackupEdit extends Component } } - public function delete() + public function delete($password) { + if (!Hash::check($password, Auth::user()->password)) { + $this->addError('password', 'The provided password is incorrect.'); + return; + } + try { + if ($this->delete_associated_backups) { + $this->deleteAllBackups(); + } + $this->backup->delete(); + if ($this->backup->database->getMorphClass() === 'App\Models\ServiceDatabase') { $previousUrl = url()->previous(); $url = Url::fromString($previousUrl); $url = $url->withoutQueryParameter('selectedBackupId'); $url = $url->withFragment('backups'); - $url = $url->getPath()."#{$url->getFragment()}"; + $url = $url->getPath() . "#{$url->getFragment()}"; return redirect($url); } else { @@ -104,4 +118,28 @@ class BackupEdit extends Component $this->dispatch('error', $e->getMessage()); } } + + public function deleteAllBackups() + { + $executions = $this->backup->executions; + + foreach ($executions as $execution) { + if ($this->backup->database->getMorphClass() === 'App\Models\ServiceDatabase') { + delete_backup_locally($execution->filename, $this->backup->database->service->destination->server); + } else { + delete_backup_locally($execution->filename, $this->backup->database->destination->server); + } + $execution->delete(); + } + } + + public function render() + { + //Add delete backup form S3 storage and delete backup for SFTP... when it is implemented + return view('livewire.project.database.backup-edit', [ + 'checkboxes' => [ + ['id' => 'delete_associated_backups', 'label' => 'All backups associated with this backup job from this database will be permanently deleted from local storage.'] + ] + ]); + } } diff --git a/resources/views/livewire/project/database/backup-edit.blade.php b/resources/views/livewire/project/database/backup-edit.blade.php index 462d696fa..a737ec091 100644 --- a/resources/views/livewire/project/database/backup-edit.blade.php +++ b/resources/views/livewire/project/database/backup-edit.blade.php @@ -8,12 +8,17 @@ @endif @if ($backup->database_id !== 0) - - - Delete - - This will stop the scheduled backup for this database.
Please think again. -
+ @endif