From 507a802dec76ffddf6e20f235166ddc3031d0cd5 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Wed, 14 Dec 2022 18:53:31 -0700 Subject: [PATCH] database-host: reverse node relationship --- app/Models/DatabaseHost.php | 23 ++++++++----------- .../Application/DatabaseHostTransformer.php | 1 - .../Database/DatabaseAuthorizationTest.php | 2 +- .../DatabaseManagementServiceTest.php | 10 ++++---- .../DeployServerDatabaseServiceTest.php | 8 +++---- 5 files changed, 20 insertions(+), 24 deletions(-) diff --git a/app/Models/DatabaseHost.php b/app/Models/DatabaseHost.php index df56eb7c7..19a46333e 100644 --- a/app/Models/DatabaseHost.php +++ b/app/Models/DatabaseHost.php @@ -3,7 +3,7 @@ namespace Pterodactyl\Models; use Illuminate\Database\Eloquent\Relations\HasMany; -use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; /** * @property int $id @@ -13,7 +13,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; * @property string $username * @property string $password * @property int|null $max_databases - * @property int|null $node_id * @property \Carbon\CarbonImmutable $created_at * @property \Carbon\CarbonImmutable $updated_at */ @@ -41,7 +40,7 @@ class DatabaseHost extends Model * Fields that are mass assignable. */ protected $fillable = [ - 'name', 'host', 'port', 'username', 'password', 'max_databases', 'node_id', + 'name', 'host', 'port', 'username', 'password', 'max_databases', ]; /** @@ -50,7 +49,6 @@ class DatabaseHost extends Model protected $casts = [ 'id' => 'integer', 'max_databases' => 'integer', - 'node_id' => 'integer', ]; /** @@ -62,17 +60,8 @@ class DatabaseHost extends Model 'port' => 'required|numeric|between:1,65535', 'username' => 'required|string|max:32', 'password' => 'nullable|string', - 'node_id' => 'sometimes|nullable|integer|exists:nodes,id', ]; - /** - * Gets the node associated with a database host. - */ - public function node(): BelongsTo - { - return $this->belongsTo(Node::class); - } - /** * Gets the databases associated with this host. */ @@ -80,4 +69,12 @@ class DatabaseHost extends Model { return $this->hasMany(Database::class); } + + /** + * Returns the nodes that a database host is assigned to. + */ + public function nodes(): BelongsToMany + { + return $this->belongsToMany(Node::class); + } } diff --git a/app/Transformers/Api/Application/DatabaseHostTransformer.php b/app/Transformers/Api/Application/DatabaseHostTransformer.php index c69f9dff3..dfe18c7c4 100644 --- a/app/Transformers/Api/Application/DatabaseHostTransformer.php +++ b/app/Transformers/Api/Application/DatabaseHostTransformer.php @@ -31,7 +31,6 @@ class DatabaseHostTransformer extends Transformer 'host' => $model->host, 'port' => $model->port, 'username' => $model->username, - 'node_id' => $model->node_id, 'created_at' => self::formatTimestamp($model->created_at), 'updated_at' => self::formatTimestamp($model->updated_at), ]; diff --git a/tests/Integration/Api/Client/Server/Database/DatabaseAuthorizationTest.php b/tests/Integration/Api/Client/Server/Database/DatabaseAuthorizationTest.php index ba15c595c..b6f3924e0 100644 --- a/tests/Integration/Api/Client/Server/Database/DatabaseAuthorizationTest.php +++ b/tests/Integration/Api/Client/Server/Database/DatabaseAuthorizationTest.php @@ -24,7 +24,7 @@ class DatabaseAuthorizationTest extends ClientApiIntegrationTestCase // And as no access to $server3. $server3 = $this->createServerModel(); - $host = DatabaseHost::factory()->create([]); + $host = DatabaseHost::factory()->create(); // Set the API $user as a subuser of server 2, but with no permissions // to do anything with the databases for that server. diff --git a/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php b/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php index 7e1319b22..1bb599ef9 100644 --- a/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php +++ b/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php @@ -58,7 +58,7 @@ class DatabaseManagementServiceTest extends IntegrationTestCase public function testDatabaseCannotBeCreatedIfServerHasReachedLimit() { $server = $this->createServerModel(['database_limit' => 2]); - $host = DatabaseHost::factory()->create(['node_id' => $server->node_id]); + $host = DatabaseHost::factory()->create(); Database::factory()->times(2)->create(['server_id' => $server->id, 'database_host_id' => $host->id]); @@ -90,8 +90,8 @@ class DatabaseManagementServiceTest extends IntegrationTestCase $server = $this->createServerModel(); $name = DatabaseManagementService::generateUniqueDatabaseName('soemthing', $server->id); - $host = DatabaseHost::factory()->create(['node_id' => $server->node_id]); - $host2 = DatabaseHost::factory()->create(['node_id' => $server->node_id]); + $host = DatabaseHost::factory()->create(); + $host2 = DatabaseHost::factory()->create(); Database::factory()->create([ 'database' => $name, 'database_host_id' => $host->id, @@ -119,7 +119,7 @@ class DatabaseManagementServiceTest extends IntegrationTestCase $server = $this->createServerModel(); $name = DatabaseManagementService::generateUniqueDatabaseName('soemthing', $server->id); - $host = DatabaseHost::factory()->create(['node_id' => $server->node_id]); + $host = DatabaseHost::factory()->create(); $this->repository->expects('createDatabase')->with($name); @@ -177,7 +177,7 @@ class DatabaseManagementServiceTest extends IntegrationTestCase $server = $this->createServerModel(); $name = DatabaseManagementService::generateUniqueDatabaseName('soemthing', $server->id); - $host = DatabaseHost::factory()->create(['node_id' => $server->node_id]); + $host = DatabaseHost::factory()->create(); $this->repository->expects('createDatabase')->with($name)->andThrows(new \BadMethodCallException()); $this->repository->expects('dropDatabase')->with($name); diff --git a/tests/Integration/Services/Databases/DeployServerDatabaseServiceTest.php b/tests/Integration/Services/Databases/DeployServerDatabaseServiceTest.php index ec02c13c7..52b3b6554 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(); $node = Node::factory()->create(['location_id' => $server->location->id]); - DatabaseHost::factory()->create(['node_id' => $node->id]); + DatabaseHost::factory()->create(); config()->set('pterodactyl.client_features.databases.allow_random', false); @@ -97,8 +97,8 @@ class DeployServerDatabaseServiceTest extends IntegrationTestCase $server = $this->createServerModel(); $node = Node::factory()->create(['location_id' => $server->location->id]); - DatabaseHost::factory()->create(['node_id' => $node->id]); - $host = DatabaseHost::factory()->create(['node_id' => $server->node_id]); + DatabaseHost::factory()->create(); + $host = DatabaseHost::factory()->create(); $this->managementService->expects('create')->with($server, [ 'database_host_id' => $host->id, @@ -124,7 +124,7 @@ class DeployServerDatabaseServiceTest extends IntegrationTestCase $server = $this->createServerModel(); $node = Node::factory()->create(['location_id' => $server->location->id]); - $host = DatabaseHost::factory()->create(['node_id' => $node->id]); + $host = DatabaseHost::factory()->create(); $this->managementService->expects('create')->with($server, [ 'database_host_id' => $host->id,