mirror of
https://github.com/tiennm99/coolify.git
synced 2026-10-13 16:14:42 +00:00
refactor(models): extract defaultStandaloneDockerAttributes method on Server
Extract duplicated inline StandaloneDocker attribute arrays in the Server boot lifecycle into a dedicated method, eliminating repetition between the root-server (id=0) and normal-server paths. Also harden the shared_environment_variables migration by wrapping DDL statements in DB::transaction() and using DROP CONSTRAINT IF EXISTS to make the migration safely re-runnable. Add unit test covering the extracted method to verify uuid is always present in bootstrap attributes.
This commit is contained in:
1 parent
4f6e1f7e42
commit
466eb8504e
3 files changed
+61
-24
No files matched your search
+21
-12
@@ -155,12 +155,7 @@ class Server extends BaseModel
|
||||
'server_id' => $server->id,
|
||||
])->save();
|
||||
} else {
|
||||
(new StandaloneDocker)->forceFill([
|
||||
'id' => 0,
|
||||
'name' => 'coolify',
|
||||
'network' => 'coolify',
|
||||
'server_id' => $server->id,
|
||||
])->saveQuietly();
|
||||
(new StandaloneDocker)->forceFill($server->defaultStandaloneDockerAttributes(id: 0))->saveQuietly();
|
||||
}
|
||||
} else {
|
||||
if ($server->isSwarm()) {
|
||||
@@ -171,12 +166,7 @@ class Server extends BaseModel
|
||||
]);
|
||||
} else {
|
||||
$standaloneDocker = new StandaloneDocker;
|
||||
$standaloneDocker->forceFill([
|
||||
'name' => 'coolify',
|
||||
'uuid' => (string) new Cuid2,
|
||||
'network' => 'coolify',
|
||||
'server_id' => $server->id,
|
||||
]);
|
||||
$standaloneDocker->forceFill($server->defaultStandaloneDockerAttributes());
|
||||
$standaloneDocker->saveQuietly();
|
||||
}
|
||||
}
|
||||
@@ -1043,6 +1033,25 @@ $schema://$host {
|
||||
return $this->belongsTo(Team::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{id?: int, name: string, uuid: string, network: string, server_id: int}
|
||||
*/
|
||||
public function defaultStandaloneDockerAttributes(?int $id = null): array
|
||||
{
|
||||
$attributes = [
|
||||
'name' => 'coolify',
|
||||
'uuid' => (string) new Cuid2,
|
||||
'network' => 'coolify',
|
||||
'server_id' => $this->id,
|
||||
];
|
||||
|
||||
if (! is_null($id)) {
|
||||
$attributes['id'] = $id;
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
public function environment_variables()
|
||||
{
|
||||
return $this->hasMany(SharedEnvironmentVariable::class)->where('type', 'server');
|
||||
|
||||
Reference in new issue
Block a user