fix issue where assigning multiple allocations to a server would only assign the first selection

This commit is contained in:
Sky Mulley
2026-10-09 14:41:44 +01:00
parent efca702654
commit 9a4dc74ba6
2 changed files with 16 additions and 4 deletions
+5 -4
View File
@@ -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'])) {
@@ -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 */