refactor(database): enhance application conversion logic and add existence checks for databases and applications
This commit is contained in:
@@ -89,17 +89,17 @@ class Database extends Component
|
|||||||
try {
|
try {
|
||||||
$service = $this->database->service;
|
$service = $this->database->service;
|
||||||
$serviceDatabase = $this->database;
|
$serviceDatabase = $this->database;
|
||||||
|
|
||||||
// Check if application with same name already exists
|
// Check if application with same name already exists
|
||||||
if ($service->applications()->where('name', $serviceDatabase->name)->exists()) {
|
if ($service->applications()->where('name', $serviceDatabase->name)->exists()) {
|
||||||
throw new \Exception('An application with this name already exists.');
|
throw new \Exception('An application with this name already exists.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new parameters removing database_uuid
|
// Create new parameters removing database_uuid
|
||||||
$redirectParams = collect($this->parameters)
|
$redirectParams = collect($this->parameters)
|
||||||
->except('database_uuid')
|
->except('database_uuid')
|
||||||
->all();
|
->all();
|
||||||
|
|
||||||
DB::transaction(function () use ($service, $serviceDatabase) {
|
DB::transaction(function () use ($service, $serviceDatabase) {
|
||||||
$service->applications()->create([
|
$service->applications()->create([
|
||||||
'name' => $serviceDatabase->name,
|
'name' => $serviceDatabase->name,
|
||||||
@@ -113,9 +113,7 @@ class Database extends Component
|
|||||||
]);
|
]);
|
||||||
$serviceDatabase->delete();
|
$serviceDatabase->delete();
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->dispatch('success', 'Database converted to Application. Hasta la vista, database!');
|
|
||||||
|
|
||||||
return redirect()->route('project.service.configuration', $redirectParams);
|
return redirect()->route('project.service.configuration', $redirectParams);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
return handleError($e, $this);
|
return handleError($e, $this);
|
||||||
|
|||||||
@@ -79,24 +79,31 @@ class ServiceApplicationView extends Component
|
|||||||
try {
|
try {
|
||||||
$service = $this->application->service;
|
$service = $this->application->service;
|
||||||
$serviceApplication = $this->application;
|
$serviceApplication = $this->application;
|
||||||
DB::beginTransaction();
|
|
||||||
$service->databases()->create([
|
|
||||||
'name' => $serviceApplication->name,
|
|
||||||
'human_name' => $serviceApplication->human_name,
|
|
||||||
'description' => $serviceApplication->description,
|
|
||||||
'exclude_from_status' => $serviceApplication->exclude_from_status,
|
|
||||||
'is_log_drain_enabled' => $serviceApplication->is_log_drain_enabled,
|
|
||||||
'image' => $serviceApplication->image,
|
|
||||||
'service_id' => $service->id,
|
|
||||||
'is_migrated' => true,
|
|
||||||
]);
|
|
||||||
$serviceApplication->delete();
|
|
||||||
DB::commit();
|
|
||||||
|
|
||||||
return redirect()->route('project.service.configuration', $this->parameters);
|
// Check if database with same name already exists
|
||||||
|
if ($service->databases()->where('name', $serviceApplication->name)->exists()) {
|
||||||
|
throw new \Exception('A database with this name already exists.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$redirectParams = collect($this->parameters)
|
||||||
|
->except('database_uuid')
|
||||||
|
->all();
|
||||||
|
DB::transaction(function () use ($service, $serviceApplication) {
|
||||||
|
$service->databases()->create([
|
||||||
|
'name' => $serviceApplication->name,
|
||||||
|
'human_name' => $serviceApplication->human_name,
|
||||||
|
'description' => $serviceApplication->description,
|
||||||
|
'exclude_from_status' => $serviceApplication->exclude_from_status,
|
||||||
|
'is_log_drain_enabled' => $serviceApplication->is_log_drain_enabled,
|
||||||
|
'image' => $serviceApplication->image,
|
||||||
|
'service_id' => $service->id,
|
||||||
|
'is_migrated' => true,
|
||||||
|
]);
|
||||||
|
$serviceApplication->delete();
|
||||||
|
});
|
||||||
|
|
||||||
|
return redirect()->route('project.service.configuration', $redirectParams);
|
||||||
} catch (\Throwable $e) {
|
} catch (\Throwable $e) {
|
||||||
DB::rollBack();
|
|
||||||
|
|
||||||
return handleError($e, $this);
|
return handleError($e, $this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user