2023-08-10 13:52:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Livewire\Project\Database;
|
|
|
|
|
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class BackupEdit extends Component
|
|
|
|
{
|
|
|
|
public $backup;
|
2023-08-11 14:13:53 +00:00
|
|
|
public $s3s;
|
2023-08-10 14:28:29 +00:00
|
|
|
public array $parameters;
|
2023-08-10 13:52:54 +00:00
|
|
|
|
|
|
|
protected $rules = [
|
|
|
|
'backup.enabled' => 'required|boolean',
|
|
|
|
'backup.frequency' => 'required|string',
|
|
|
|
'backup.number_of_backups_locally' => 'required|integer|min:1',
|
2023-08-10 15:55:03 +00:00
|
|
|
'backup.save_s3' => 'required|boolean',
|
2023-08-11 14:13:53 +00:00
|
|
|
'backup.s3_storage_id' => 'nullable|integer',
|
2023-08-10 13:52:54 +00:00
|
|
|
];
|
|
|
|
protected $validationAttributes = [
|
|
|
|
'backup.enabled' => 'Enabled',
|
|
|
|
'backup.frequency' => 'Frequency',
|
|
|
|
'backup.number_of_backups_locally' => 'Number of Backups Locally',
|
2023-08-10 15:55:03 +00:00
|
|
|
'backup.save_s3' => 'Save to S3',
|
2023-08-11 14:13:53 +00:00
|
|
|
'backup.s3_storage_id' => 'S3 Storage',
|
|
|
|
];
|
|
|
|
protected $messages = [
|
|
|
|
'backup.s3_storage_id' => 'Select a S3 Storage',
|
2023-08-10 13:52:54 +00:00
|
|
|
];
|
|
|
|
|
2023-08-10 14:28:29 +00:00
|
|
|
public function mount()
|
|
|
|
{
|
|
|
|
$this->parameters = get_route_parameters();
|
2023-08-11 14:13:53 +00:00
|
|
|
if (is_null($this->backup->s3_storage_id)) {
|
|
|
|
$this->backup->s3_storage_id = 'default';
|
|
|
|
}
|
2023-08-10 14:28:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 15:55:03 +00:00
|
|
|
|
2023-08-10 14:25:59 +00:00
|
|
|
public function delete()
|
|
|
|
{
|
2023-08-10 14:28:29 +00:00
|
|
|
// TODO: Delete backup from server and add a confirmation modal
|
2023-08-10 14:25:59 +00:00
|
|
|
$this->backup->delete();
|
2023-08-10 14:28:29 +00:00
|
|
|
redirect()->route('project.database.backups.all', $this->parameters);
|
2023-08-10 14:25:59 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 13:52:54 +00:00
|
|
|
public function instantSave()
|
|
|
|
{
|
2023-08-11 14:13:53 +00:00
|
|
|
try {
|
|
|
|
$this->custom_validate();
|
|
|
|
$this->backup->save();
|
|
|
|
$this->backup->refresh();
|
|
|
|
$this->emit('success', 'Backup updated successfully');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->emit('error', $e->getMessage());
|
|
|
|
}
|
2023-08-10 13:52:54 +00:00
|
|
|
}
|
|
|
|
|
2023-08-11 14:13:53 +00:00
|
|
|
private function custom_validate()
|
2023-08-10 13:52:54 +00:00
|
|
|
{
|
2023-08-11 18:48:52 +00:00
|
|
|
if (!is_numeric($this->backup->s3_storage_id)) {
|
|
|
|
$this->backup->s3_storage_id = null;
|
|
|
|
}
|
2023-08-10 13:52:54 +00:00
|
|
|
$isValid = validate_cron_expression($this->backup->frequency);
|
|
|
|
if (!$isValid) {
|
2023-08-11 14:13:53 +00:00
|
|
|
throw new \Exception('Invalid Cron / Human expression');
|
2023-08-10 13:52:54 +00:00
|
|
|
}
|
|
|
|
$this->validate();
|
2023-08-11 14:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function submit()
|
|
|
|
{
|
|
|
|
ray($this->backup->s3_storage_id);
|
|
|
|
try {
|
|
|
|
$this->custom_validate();
|
|
|
|
$this->backup->save();
|
|
|
|
$this->backup->refresh();
|
|
|
|
$this->emit('success', 'Backup updated successfully');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->emit('error', $e->getMessage());
|
|
|
|
}
|
2023-08-10 13:52:54 +00:00
|
|
|
}
|
|
|
|
}
|