From 9a4dc74ba631e8f06f8d42bb3676789db4f97ba8 Mon Sep 17 00:00:00 2001 From: Sky Mulley Date: Fri, 9 Oct 2026 14:41:44 +0100 Subject: [PATCH] fix issue where assigning multiple allocations to a server would only assign the first selection --- app/Actions/Servers/UpdateServerBuild.php | 9 +++++---- .../Actions/Servers/UpdateServerBuildTest.php | 11 +++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/Actions/Servers/UpdateServerBuild.php b/app/Actions/Servers/UpdateServerBuild.php index 3afb685e1..a2fb467fb 100644 --- a/app/Actions/Servers/UpdateServerBuild.php +++ b/app/Actions/Servers/UpdateServerBuild.php @@ -79,12 +79,13 @@ final readonly class UpdateServerBuild implements UpdatesServerBuild if (! empty($data['add_allocations'])) { // Assign only currently unassigned allocations on this server's node. - $query = Allocation::query()->where('node_id', $server->node_id) - ->whereIn('id', $data['add_allocations'])->whereNull('server_id'); + $assignable = Allocation::query()->where('node_id', $server->node_id) + ->whereIn('id', $data['add_allocations'])->whereNull('server_id') + ->orderBy('id')->lockForUpdate()->get(['id']); + Allocation::query()->whereKey($assignable->modelKeys())->update(['server_id' => $server->id, 'notes' => null]); // If the default allocation is removed below, use the first newly assigned // allocation as its replacement. - $freshlyAllocated = $query->first(['id'])?->id; - $query->update(['server_id' => $server->id, 'notes' => null]); + $freshlyAllocated = $assignable->first()?->id; } if (! empty($data['remove_allocations'])) { diff --git a/tests/Integration/Actions/Servers/UpdateServerBuildTest.php b/tests/Integration/Actions/Servers/UpdateServerBuildTest.php index 13609b13e..01b340f70 100644 --- a/tests/Integration/Actions/Servers/UpdateServerBuildTest.php +++ b/tests/Integration/Actions/Servers/UpdateServerBuildTest.php @@ -99,6 +99,17 @@ test('no exception is thrown if only removing allocation', function () { $this->daemonServerRepository->assertSynced(); $this->assertDatabaseHas('allocations', ['id' => $allocation->id, 'server_id' => null]); }); +test('multiple allocations can be added at once', function () { + $server = $this->createServerModel(); + /** @var Allocation[] $allocations */ + $allocations = Allocation::factory()->times(3)->create(['node_id' => $server->node_id]); + $response = getService()->update($server, ['add_allocations' => $allocations->pluck('id')->all()]); + $this->daemonServerRepository->assertSynced(); + expect($response->allocations)->toHaveCount(4); + foreach ($allocations as $allocation) { + $this->assertDatabaseHas('allocations', ['id' => $allocation->id, 'server_id' => $server->id]); + } +}); test('allocation in both add and remove is added', function () { $server = $this->createServerModel(); /** @var Allocation $allocation */