add ability to get backup now and get all schedule backup

This commit is contained in:
DanielHemmati
2025-04-25 15:49:14 +02:00
parent 2a06a392d5
commit 81180af27d

View File

@@ -9,6 +9,7 @@ use App\Actions\Database\StopDatabase;
use App\Actions\Database\StopDatabaseProxy; use App\Actions\Database\StopDatabaseProxy;
use App\Enums\NewDatabaseTypes; use App\Enums\NewDatabaseTypes;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Jobs\DatabaseBackupJob;
use App\Jobs\DeleteResourceJob; use App\Jobs\DeleteResourceJob;
use App\Models\Project; use App\Models\Project;
use App\Models\ScheduledDatabaseBackup; use App\Models\ScheduledDatabaseBackup;
@@ -80,12 +81,11 @@ class DatabasesController extends Controller
$databases = $databases->merge($project->databases()); $databases = $databases->merge($project->databases());
} }
$backupConfig = ScheduledDatabaseBackup::with('latest_log')->get(); $databases = $databases->map(function ($database) {
$databases = $databases->map(function ($database) use ($backupConfig) { $backupConfig = ScheduledDatabaseBackup::with('latest_log')->where('database_id', $database->id)->get();
$databaseBackupConfig = $backupConfig->where('database_id', $database->id)->first();
if ($databaseBackupConfig) { if ($backupConfig) {
$database->backup_configs = $databaseBackupConfig; $database->backup_configs = $backupConfig;
} else { } else {
$database->backup_configs = null; $database->backup_configs = null;
} }
@@ -98,7 +98,7 @@ class DatabasesController extends Controller
#[OA\Get( #[OA\Get(
summary: 'Get', summary: 'Get',
description: 'Get database by UUID.', description: 'Get backups details by database UUID.',
path: '/databases/{uuid}/backups', path: '/databases/{uuid}/backups',
operationId: 'get-database-backups-by-uuid', operationId: 'get-database-backups-by-uuid',
security: [ security: [
@@ -291,6 +291,7 @@ class DatabasesController extends Controller
// WIP // WIP
'save_s3' => ['type' => 'boolean', 'description' => 'Weather data is saved in s3 or not'], 'save_s3' => ['type' => 'boolean', 'description' => 'Weather data is saved in s3 or not'],
's3_storage_id' => ['type' => 'integer', 'description' => 'S3 storage id'], 's3_storage_id' => ['type' => 'integer', 'description' => 'S3 storage id'],
'backup_now' => ['type' => 'boolean', 'description' => 'Weather to take a backup now or not'],
'enabled' => ['type' => 'boolean', 'description' => 'Weather the backup is enabled or not'], 'enabled' => ['type' => 'boolean', 'description' => 'Weather the backup is enabled or not'],
'databases_to_backup' => ['type' => 'string', 'description' => 'Comma separated list of databases to backup'], 'databases_to_backup' => ['type' => 'string', 'description' => 'Comma separated list of databases to backup'],
'dump_all' => ['type' => 'boolean', 'description' => 'Weather all databases are dumped or not'], 'dump_all' => ['type' => 'boolean', 'description' => 'Weather all databases are dumped or not'],
@@ -352,6 +353,7 @@ class DatabasesController extends Controller
'limits_cpuset' => 'string|nullable', 'limits_cpuset' => 'string|nullable',
'limits_cpu_shares' => 'numeric', 'limits_cpu_shares' => 'numeric',
'save_s3' => 'boolean', 'save_s3' => 'boolean',
'backup_now' => 'boolean|nullable',
'enabled' => 'boolean', 'enabled' => 'boolean',
'dump_all' => 'boolean', 'dump_all' => 'boolean',
's3_storage_id' => 'integer|min:1|exists:s3_storages,id|nullable', 's3_storage_id' => 'integer|min:1|exists:s3_storages,id|nullable',
@@ -573,7 +575,7 @@ class DatabasesController extends Controller
} }
break; break;
} }
$extraFields = array_diff(array_keys($request->all()), $allowedFields, $allowedBackupConfigsFields); $extraFields = array_diff(array_keys($request->all()), $allowedFields, $allowedBackupConfigsFields, ['backup_now']);
if ($validator->fails() || ! empty($extraFields)) { if ($validator->fails() || ! empty($extraFields)) {
$errors = $validator->errors(); $errors = $validator->errors();
if (! empty($extraFields)) { if (! empty($extraFields)) {
@@ -620,10 +622,18 @@ class DatabasesController extends Controller
's3_storage_id' => $backupPayload['s3_storage_id'] ?? 1, 's3_storage_id' => $backupPayload['s3_storage_id'] ?? 1,
...$backupPayload, ...$backupPayload,
]); ]);
if ($request->backup_now) {
DatabaseBackupJob::dispatch($backupConfig);
}
} }
if ($backupPayload && $backupConfig) { if ($backupPayload && $backupConfig) {
$backupConfig->update($backupPayload); $backupConfig->update($backupPayload);
if ($request->backup_now) {
DatabaseBackupJob::dispatch($backupConfig);
}
} }
if ($whatToDoWithDatabaseProxy === 'start') { if ($whatToDoWithDatabaseProxy === 'start') {