From d1c7494933b7fa4a216eac2c374176546f4d07b9 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Thu, 15 Dec 2022 12:39:37 -0700 Subject: [PATCH] app: fix DeployServerDatabaseService --- .../Databases/DeployServerDatabaseService.php | 21 ++++++++++--------- .../DeployServerDatabaseServiceTest.php | 11 +++++----- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/app/Services/Databases/DeployServerDatabaseService.php b/app/Services/Databases/DeployServerDatabaseService.php index e22eba51d..ec678e40a 100644 --- a/app/Services/Databases/DeployServerDatabaseService.php +++ b/app/Services/Databases/DeployServerDatabaseService.php @@ -27,21 +27,22 @@ class DeployServerDatabaseService Assert::notEmpty($data['database'] ?? null); Assert::notEmpty($data['remote'] ?? null); - $hosts = DatabaseHost::query()->get()->toBase(); - if ($hosts->isEmpty()) { - throw new NoSuitableDatabaseHostException(); - } else { - $nodeHosts = $hosts->where('node_id', $server->node_id)->toBase(); - - if ($nodeHosts->isEmpty() && !config('pterodactyl.client_features.databases.allow_random')) { + $databaseHostId = $server->node->database_host_id; + if (is_null($databaseHostId)) { + if (!config('pterodactyl.client_features.databases.allow_random')) { throw new NoSuitableDatabaseHostException(); } + + $hosts = DatabaseHost::query()->get()->toBase(); + if ($hosts->isEmpty()) { + throw new NoSuitableDatabaseHostException(); + } + + $databaseHostId = $hosts->random()->id; } return $this->managementService->create($server, [ - 'database_host_id' => $nodeHosts->isEmpty() - ? $hosts->random()->id - : $nodeHosts->random()->id, + 'database_host_id' => $databaseHostId, 'database' => DatabaseManagementService::generateUniqueDatabaseName($data['database'], $server->id), 'remote' => $data['remote'], ]); diff --git a/tests/Integration/Services/Databases/DeployServerDatabaseServiceTest.php b/tests/Integration/Services/Databases/DeployServerDatabaseServiceTest.php index 9dada9405..f1511852b 100644 --- a/tests/Integration/Services/Databases/DeployServerDatabaseServiceTest.php +++ b/tests/Integration/Services/Databases/DeployServerDatabaseServiceTest.php @@ -62,7 +62,7 @@ class DeployServerDatabaseServiceTest extends IntegrationTestCase $server = $this->createServerModel(); $host = DatabaseHost::factory()->create(); - $node = Node::factory()->create(['location_id' => $server->location->id]); + $node = Node::factory()->create(['database_host_id' => $host->id, 'location_id' => $server->location->id]); config()->set('pterodactyl.client_features.databases.allow_random', false); @@ -96,12 +96,13 @@ class DeployServerDatabaseServiceTest extends IntegrationTestCase { $server = $this->createServerModel(); - $node = Node::factory()->create(['location_id' => $server->location->id, 'database_host_id' => DatabaseHost::factory()->create()->id]); - $host = DatabaseHost::factory()->create(); - $server->node->database_host_id = $host->id; + $host1 = DatabaseHost::factory()->create(); + $host2 = DatabaseHost::factory()->create(); + $node = Node::factory()->create(['database_host_id' => $host2->id, 'location_id' => $server->location->id]); + $server->node->database_host_id = $host2->id; $this->managementService->expects('create')->with($server, [ - 'database_host_id' => $host->id, + 'database_host_id' => $host2->id, 'database' => "s{$server->id}_something", 'remote' => '%', ])->andReturns(new Database());