Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

refactor backup edit view

Andras Bacsai34c7aa122c91
diff --git a/app/Livewire/Project/Database/BackupEdit.php b/app/Livewire/Project/Database/BackupEdit.php
index 9b82c4b11b..1e70eaba97 100644
--- a/app/Livewire/Project/Database/BackupEdit.php
+++ b/app/Livewire/Project/Database/BackupEdit.php
@@ -4,56 +4,87 @@ namespace App\Livewire\Project\Database;
use App\Models\InstanceSettings;
use App\Models\ScheduledDatabaseBackup;
+use Exception;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
+use Livewire\Attributes\Locked;
+use Livewire\Attributes\Rule;
use Livewire\Component;
use Spatie\Url\Url;
class BackupEdit extends Component
{
- public ?ScheduledDatabaseBackup $backup;
+ public ScheduledDatabaseBackup $backup;
+ #[Locked]
public $s3s;
+ #[Locked]
+ public $parameters;
+
+ #[Rule(['required', 'boolean'])]
public bool $delete_associated_backups_locally = false;
+ #[Rule(['required', 'boolean'])]
public bool $delete_associated_backups_s3 = false;
+ #[Rule(['required', 'boolean'])]
public bool $delete_associated_backups_sftp = false;
+ #[Rule(['nullable', 'string'])]
public ?string $status = null;
- public array $parameters;
-
- protected $rules = [
- 'backup.enabled' => 'required|boolean',
- 'backup.frequency' => 'required|string',
- 'backup.number_of_backups_locally' => 'required|integer|min:1',
- 'backup.save_s3' => 'required|boolean',
- 'backup.s3_storage_id' => 'nullable|integer',
- 'backup.databases_to_backup' => 'nullable',
- 'backup.dump_all' => 'required|boolean',
- ];
-
- protected $validationAttributes = [
- 'backup.enabled' => 'Enabled',
- 'backup.frequency' => 'Frequency',
- 'backup.number_of_backups_locally' => 'Number of Backups Locally',
- 'backup.save_s3' => 'Save to S3',
- 'backup.s3_storage_id' => 'S3 Storage',
- 'backup.databases_to_backup' => 'Databases to Backup',
- 'backup.dump_all' => 'Backup All Databases',
- ];
-
- protected $messages = [
- 'backup.s3_storage_id' => 'Select a S3 Storage',
- ];
+ #[Rule(['required', 'boolean'])]
+ public bool $backupEnabled = false;
+
+ #[Rule(['required', 'string'])]
+ public string $frequency = '';
+
+ #[Rule(['required', 'integer', 'min:1'])]
+ public int $numberOfBackupsLocally = 1;
+
+ #[Rule(['required', 'boolean'])]
+ public bool $saveS3 = false;
+
+ #[Rule(['required', 'integer'])]
+ public int $s3StorageId = 1;
+
+ #[Rule(['nullable', 'string'])]
+ public ?string $databasesToBackup = null;
+
+ #[Rule(['required', 'boolean'])]
+ public bool $dumpAll = false;
public function mount()
{
- $this->parameters = get_route_parameters();
- if (is_null(data_get($this->backup, 's3_storage_id'))) {
- data_set($this->backup, 's3_storage_id', 'default');
+ try {
+ $this->parameters = get_route_parameters();
+ $this->syncData();
+ } catch (Exception $e) {
+ return handleError($e, $this);
+ }
+ }
+
+ public function syncData(bool $toModel = false)
+ {
+ if ($toModel) {
+ $this->customValidate();
+ $this->backup->enabled = $this->backupEnabled;
+ $this->backup->frequency = $this->frequency;
+ $this->backup->number_of_backups_locally = $this->numberOfBackupsLocally;
+ $this->backup->save_s3 = $this->saveS3;
+ $this->backup->s3_storage_id = $this->s3StorageId;
+ $this->backup->databases_to_backup = $this->databasesToBackup;
+ $this->backup->dump_all = $this->dumpAll;
+ $this->backup->save();
+ } else {
+ $this->backupEnabled = $this->backup->enabled;
+ $this->frequency = $this->backup->frequency;
+ $this->numberOfBackupsLocally = $this->backup->number_of_backups_locally;
+ $this->saveS3 = $this->backup->save_s3;
+ $this->s3StorageId = $this->backup->s3_storage_id;
+ $this->databasesToBackup = $this->backup->databases_to_backup;
+ $this->dumpAll = $this->backup->dump_all;
}
}
@@ -96,16 +127,14 @@ class BackupEdit extends Component
public function instantSave()
{
try {
- $this->custom_validate();
- $this->backup->save();
- $this->backup->refresh();
+ $this->syncData(true);
$this->dispatch('success', 'Backup updated successfully.');
} catch (\Throwable $e) {
$this->dispatch('error', $e->getMessage());
}
}
- private function custom_validate()
+ private function customValidate()
{
if (! is_numeric($this->backup->s3_storage_id)) {
$this->backup->s3_storage_id = null;
@@ -120,19 +149,14 @@ class BackupEdit extends Component
public function submit()
{
try {
- $this->custom_validate();
- if ($this->backup->databases_to_backup === '' || $this->backup->databases_to_backup === null) {
- $this->backup->databases_to_backup = null;
- }
- $this->backup->save();
- $this->backup->refresh();
- $this->dispatch('success', 'Backup updated successfully');
+ $this->syncData(true);
+ $this->dispatch('success', 'Backup updated successfully.');
} catch (\Throwable $e) {
$this->dispatch('error', $e->getMessage());
}
}
- public function deleteAssociatedBackupsLocally()
+ private function deleteAssociatedBackupsLocally()
{
$executions = $this->backup->executions;
$backupFolder = null;
@@ -152,17 +176,17 @@ class BackupEdit extends Component
$execution->delete();
}
- if ($backupFolder) {
+ if (str($backupFolder)->isNotEmpty()) {
$this->deleteEmptyBackupFolder($backupFolder, $server);
}
}
- public function deleteAssociatedBackupsS3()
+ private function deleteAssociatedBackupsS3()
{
//Add function to delete backups from S3
}
- public function deleteAssociatedBackupsSftp()
+ private function deleteAssociatedBackupsSftp()
{
//Add function to delete backups from SFTP
}
diff --git a/resources/views/livewire/project/database/backup-edit.blade.php b/resources/views/livewire/project/database/backup-edit.blade.php
index c3288a4f99..b91b35802a 100644
--- a/resources/views/livewire/project/database/backup-edit.blade.php
+++ b/resources/views/livewire/project/database/backup-edit.blade.php
@@ -19,12 +19,12 @@
@endif
</div>
<div class="w-48 pb-2">
- <x-forms.checkbox instantSave label="Backup Enabled" id="backup.enabled" />
- <x-forms.checkbox instantSave label="S3 Enabled" id="backup.save_s3" />
+ <x-forms.checkbox instantSave label="Backup Enabled" id="backupEnabled" />
+ <x-forms.checkbox instantSave label="S3 Enabled" id="saveS3" />
</div>
@if ($backup->save_s3)
<div class="pb-6">
- <x-forms.select id="backup.s3_storage_id" label="S3 Storage" required>
+ <x-forms.select id="s3StorageId" label="S3 Storage" required>
<option value="default">Select a S3 storage</option>
@foreach ($s3s as $s3)
<option value="{{ $s3->id }}">{{ $s3->name }}</option>
@@ -37,40 +37,40 @@
<div class="flex gap-2 flex-col ">
@if ($backup->database_type === 'App\Models\StandalonePostgresql' && $backup->database_id !== 0)
<div class="w-48">
- <x-forms.checkbox label="Backup All Databases" id="backup.dump_all" instantSave />
+ <x-forms.checkbox label="Backup All Databases" id="dumpAll" instantSave />
</div>
@if (!$backup->dump_all)
<x-forms.input label="Databases To Backup"
helper="Comma separated list of databases to backup. Empty will include the default one."
- id="backup.databases_to_backup" />
+ id="databasesToBackup" />
@endif
@elseif($backup->database_type === 'App\Models\StandaloneMongodb')
<x-forms.input label="Databases To Include"
helper="A list of databases to backup. You can specify which collection(s) per database to exclude from the backup. Empty will include all databases and collections.<br><br>Example:<br><br>database1:collection1,collection2|database2:collection3,collection4<br><br> database1 will include all collections except collection1 and collection2. <br>database2 will include all collections except collection3 and collection4.<br><br>Another Example:<br><br>database1:collection1|database2<br><br> database1 will include all collections except collection1.<br>database2 will include ALL collections."
- id="backup.databases_to_backup" />
+ id="databasesToBackup" />
@elseif($backup->database_type === 'App\Models\StandaloneMysql')
<div class="w-48">
- <x-forms.checkbox label="Backup All Databases" id="backup.dump_all" instantSave />
+ <x-forms.checkbox label="Backup All Databases" id="dumpAll" instantSave />
</div>
@if (!$backup->dump_all)
<x-forms.input label="Databases To Backup"
helper="Comma separated list of databases to backup. Empty will include the default one."
- id="backup.databases_to_backup" />
+ id="databasesToBackup" />
@endif
@elseif($backup->database_type === 'App\Models\StandaloneMariadb')
<div class="w-48">
- <x-forms.checkbox label="Backup All Databases" id="backup.dump_all" instantSave />
+ <x-forms.checkbox label="Backup All Databases" id="dumpAll" instantSave />
</div>
@if (!$backup->dump_all)
<x-forms.input label="Databases To Backup"
helper="Comma separated list of databases to backup. Empty will include the default one."
- id="backup.databases_to_backup" />
+ id="databasesToBackup" />
@endif
@endif
</div>
<div class="flex gap-2">
- <x-forms.input label="Frequency" id="backup.frequency" />
- <x-forms.input label="Number of backups to keep (locally)" id="backup.number_of_backups_locally" />
+ <x-forms.input label="Frequency" id="frequency" />
+ <x-forms.input label="Number of backups to keep (locally)" id="numberOfBackupsLocally" />
</div>
</div>
</form>