fix: exclude system and computed fields from model replication

- Remove auto-generated properties (`id`, `created_at` and `updated_at` ) from replicate
- Exclude computed count properties (`additional_servers_count`, `additional_networks_count`) loaded by global scope to prevent errors
This commit is contained in:
peaklabs-dev
2025-01-07 17:18:04 +01:00
parent 1fe4dd722b
commit d53065967e
2 changed files with 84 additions and 12 deletions

View File

@@ -109,7 +109,13 @@ class CloneMe extends Component
$services = $this->environment->services; $services = $this->environment->services;
foreach ($applications as $application) { foreach ($applications as $application) {
$uuid = (string) new Cuid2; $uuid = (string) new Cuid2;
$newApplication = $application->replicate()->fill([ $newApplication = $application->replicate([
'id',
'created_at',
'updated_at',
'additional_servers_count',
'additional_networks_count',
])->fill([
'uuid' => $uuid, 'uuid' => $uuid,
'fqdn' => generateFqdn($this->server, $uuid), 'fqdn' => generateFqdn($this->server, $uuid),
'status' => 'exited', 'status' => 'exited',
@@ -120,14 +126,26 @@ class CloneMe extends Component
$newApplication->save(); $newApplication->save();
$environmentVaribles = $application->environment_variables()->get(); $environmentVaribles = $application->environment_variables()->get();
foreach ($environmentVaribles as $environmentVarible) { foreach ($environmentVaribles as $environmentVarible) {
$newEnvironmentVariable = $environmentVarible->replicate()->fill([ $newEnvironmentVariable = $environmentVarible->replicate([
'id',
'created_at',
'updated_at',
'additional_servers_count',
'additional_networks_count',
])->fill([
'resourceable_id' => $newApplication->id, 'resourceable_id' => $newApplication->id,
]); ]);
$newEnvironmentVariable->save(); $newEnvironmentVariable->save();
} }
$persistentVolumes = $application->persistentStorages()->get(); $persistentVolumes = $application->persistentStorages()->get();
foreach ($persistentVolumes as $volume) { foreach ($persistentVolumes as $volume) {
$newPersistentVolume = $volume->replicate()->fill([ $newPersistentVolume = $volume->replicate([
'id',
'created_at',
'updated_at',
'additional_servers_count',
'additional_networks_count',
])->fill([
'name' => $newApplication->uuid.'-'.str($volume->name)->afterLast('-'), 'name' => $newApplication->uuid.'-'.str($volume->name)->afterLast('-'),
'resource_id' => $newApplication->id, 'resource_id' => $newApplication->id,
]); ]);
@@ -136,7 +154,13 @@ class CloneMe extends Component
} }
foreach ($databases as $database) { foreach ($databases as $database) {
$uuid = (string) new Cuid2; $uuid = (string) new Cuid2;
$newDatabase = $database->replicate()->fill([ $newDatabase = $database->replicate([
'id',
'created_at',
'updated_at',
'additional_servers_count',
'additional_networks_count',
])->fill([
'uuid' => $uuid, 'uuid' => $uuid,
'status' => 'exited', 'status' => 'exited',
'started_at' => null, 'started_at' => null,
@@ -149,13 +173,25 @@ class CloneMe extends Component
$payload = []; $payload = [];
$payload['resourceable_id'] = $newDatabase->id; $payload['resourceable_id'] = $newDatabase->id;
$payload['resourceable_type'] = $newDatabase->getMorphClass(); $payload['resourceable_type'] = $newDatabase->getMorphClass();
$newEnvironmentVariable = $environmentVarible->replicate()->fill($payload); $newEnvironmentVariable = $environmentVarible->replicate([
'id',
'created_at',
'updated_at',
'additional_servers_count',
'additional_networks_count',
])->fill($payload);
$newEnvironmentVariable->save(); $newEnvironmentVariable->save();
} }
} }
foreach ($services as $service) { foreach ($services as $service) {
$uuid = (string) new Cuid2; $uuid = (string) new Cuid2;
$newService = $service->replicate()->fill([ $newService = $service->replicate([
'id',
'created_at',
'updated_at',
'additional_servers_count',
'additional_networks_count',
])->fill([
'uuid' => $uuid, 'uuid' => $uuid,
'environment_id' => $environment->id, 'environment_id' => $environment->id,
'destination_id' => $this->selectedDestination, 'destination_id' => $this->selectedDestination,

View File

@@ -44,7 +44,13 @@ class ResourceOperations extends Component
if ($this->resource->getMorphClass() === \App\Models\Application::class) { if ($this->resource->getMorphClass() === \App\Models\Application::class) {
$name = 'clone-of-'.str($this->resource->name)->limit(20).'-'.$uuid; $name = 'clone-of-'.str($this->resource->name)->limit(20).'-'.$uuid;
$new_resource = $this->resource->replicate()->fill([ $new_resource = $this->resource->replicate([
'id',
'created_at',
'updated_at',
'additional_servers_count',
'additional_networks_count',
])->fill([
'uuid' => $uuid, 'uuid' => $uuid,
'name' => $name, 'name' => $name,
'fqdn' => generateFqdn($server, $uuid), 'fqdn' => generateFqdn($server, $uuid),
@@ -59,7 +65,13 @@ class ResourceOperations extends Component
} }
$environmentVaribles = $this->resource->environment_variables()->get(); $environmentVaribles = $this->resource->environment_variables()->get();
foreach ($environmentVaribles as $environmentVarible) { foreach ($environmentVaribles as $environmentVarible) {
$newEnvironmentVariable = $environmentVarible->replicate()->fill([ $newEnvironmentVariable = $environmentVarible->replicate([
'id',
'created_at',
'updated_at',
'additional_servers_count',
'additional_networks_count',
])->fill([
'resourceable_id' => $new_resource->id, 'resourceable_id' => $new_resource->id,
'resourceable_type' => $new_resource->getMorphClass(), 'resourceable_type' => $new_resource->getMorphClass(),
]); ]);
@@ -71,7 +83,13 @@ class ResourceOperations extends Component
if ($volumeName === $volume->name) { if ($volumeName === $volume->name) {
$volumeName = $new_resource->uuid.'-'.str($volume->name)->afterLast('-'); $volumeName = $new_resource->uuid.'-'.str($volume->name)->afterLast('-');
} }
$newPersistentVolume = $volume->replicate()->fill([ $newPersistentVolume = $volume->replicate([
'id',
'created_at',
'updated_at',
'additional_servers_count',
'additional_networks_count',
])->fill([
'name' => $volumeName, 'name' => $volumeName,
'resource_id' => $new_resource->id, 'resource_id' => $new_resource->id,
]); ]);
@@ -95,7 +113,13 @@ class ResourceOperations extends Component
$this->resource->getMorphClass() === \App\Models\StandaloneClickhouse::class $this->resource->getMorphClass() === \App\Models\StandaloneClickhouse::class
) { ) {
$uuid = (string) new Cuid2; $uuid = (string) new Cuid2;
$new_resource = $this->resource->replicate()->fill([ $new_resource = $this->resource->replicate([
'id',
'created_at',
'updated_at',
'additional_servers_count',
'additional_networks_count',
])->fill([
'uuid' => $uuid, 'uuid' => $uuid,
'name' => $this->resource->name.'-clone-'.$uuid, 'name' => $this->resource->name.'-clone-'.$uuid,
'status' => 'exited', 'status' => 'exited',
@@ -117,7 +141,13 @@ class ResourceOperations extends Component
} elseif ($this->resource->type() === 'standalone-mariadb') { } elseif ($this->resource->type() === 'standalone-mariadb') {
$payload['standalone_mariadb_id'] = $new_resource->id; $payload['standalone_mariadb_id'] = $new_resource->id;
} }
$newEnvironmentVariable = $environmentVarible->replicate()->fill($payload); $newEnvironmentVariable = $environmentVarible->replicate([
'id',
'created_at',
'updated_at',
'additional_servers_count',
'additional_networks_count',
])->fill($payload);
$newEnvironmentVariable->save(); $newEnvironmentVariable->save();
} }
$route = route('project.database.configuration', [ $route = route('project.database.configuration', [
@@ -129,7 +159,13 @@ class ResourceOperations extends Component
return redirect()->to($route); return redirect()->to($route);
} elseif ($this->resource->type() === 'service') { } elseif ($this->resource->type() === 'service') {
$uuid = (string) new Cuid2; $uuid = (string) new Cuid2;
$new_resource = $this->resource->replicate()->fill([ $new_resource = $this->resource->replicate([
'id',
'created_at',
'updated_at',
'additional_servers_count',
'additional_networks_count',
])->fill([
'uuid' => $uuid, 'uuid' => $uuid,
'name' => $this->resource->name.'-clone-'.$uuid, 'name' => $this->resource->name.'-clone-'.$uuid,
'destination_id' => $new_destination->id, 'destination_id' => $new_destination->id,