From fa0fc397606079e4de97fc68032d10b1597f30e4 Mon Sep 17 00:00:00 2001 From: Vehikl Date: Thu, 31 Oct 2024 17:02:18 -0400 Subject: [PATCH 01/11] WIP --- ...database_hosts_to_belong_to_many_nodes.php | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 database/migrations/2024_10_31_203540_change_database_hosts_to_belong_to_many_nodes.php diff --git a/database/migrations/2024_10_31_203540_change_database_hosts_to_belong_to_many_nodes.php b/database/migrations/2024_10_31_203540_change_database_hosts_to_belong_to_many_nodes.php new file mode 100644 index 0000000000..3d788dbdb6 --- /dev/null +++ b/database/migrations/2024_10_31_203540_change_database_hosts_to_belong_to_many_nodes.php @@ -0,0 +1,49 @@ +id(); + $table->foreignId('node_id')->references('id')->on('nodes'); + $table->foreignId('database_host_id')->references('id')->on('database_hosts'); + $table->timestamps(); + }); + + $databaseNodes = DB::table('database_hosts')->whereNotNull('node_id')->get(); + $newJoinEntries = $databaseNodes->map(fn ($record) => [ + 'node_id' => $record->node_id, + 'database_host_id' => $record->id, + 'created_at' => now(), + 'updated_at' => now(), + ]); + + DB::table('database_host_node')->insert($newJoinEntries->toArray()); + + Schema::table('database_hosts', function (Blueprint $table) { + // $table->dropForeign() + $table->dropColumn('node_id'); + }); + } + + public function down(): void + { + Schema::table('database_hosts', function (Blueprint $table) { + $table->foreignId('node_id')->nullable()->references('id')->on('nodes'); + }); + + foreach (DB::table('database_host_node')->get() as $record) { + DB::table('database_hosts') + ->where('id', $record->database_host_id) + ->update(['node_id' => $record->node_id]) + ; + } + + Schema::drop('database_host_node'); + } +}; From dfe8c67223f20d991313941b6e8d1b90fd447d0e Mon Sep 17 00:00:00 2001 From: Vehikl Date: Thu, 7 Nov 2024 16:56:58 -0500 Subject: [PATCH 02/11] Update laravel and migrations --- composer.lock | 22 +++++++++++-------- ...2_09_174834_SetupPermissionsPivotTable.php | 3 +++ ...0_09_13_110007_drop_packs_from_servers.php | 5 +---- .../2024_03_12_154408_remove_nests_table.php | 7 +++--- ...24_03_14_055537_remove_locations_table.php | 5 +---- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/composer.lock b/composer.lock index ca8c6f54ab..b4463fb7e8 100644 --- a/composer.lock +++ b/composer.lock @@ -2802,16 +2802,16 @@ }, { "name": "laravel/framework", - "version": "v11.10.0", + "version": "v11.30.0", "source": { "type": "git", "url": "https://github.com/laravel/framework.git", - "reference": "99b4255194912044b75ab72329f8c19e6345720e" + "reference": "dff716442d9c229d716be82ccc9a7de52eb97193" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/framework/zipball/99b4255194912044b75ab72329f8c19e6345720e", - "reference": "99b4255194912044b75ab72329f8c19e6345720e", + "url": "https://api.github.com/repos/laravel/framework/zipball/dff716442d9c229d716be82ccc9a7de52eb97193", + "reference": "dff716442d9c229d716be82ccc9a7de52eb97193", "shasum": "" }, "require": { @@ -2830,7 +2830,7 @@ "fruitcake/php-cors": "^1.3", "guzzlehttp/guzzle": "^7.8", "guzzlehttp/uri-template": "^1.0", - "laravel/prompts": "^0.1.18", + "laravel/prompts": "^0.1.18|^0.2.0|^0.3.0", "laravel/serializable-closure": "^1.3", "league/commonmark": "^2.2.1", "league/flysystem": "^3.8.0", @@ -2864,6 +2864,7 @@ }, "provide": { "psr/container-implementation": "1.1|2.0", + "psr/log-implementation": "1.0|2.0|3.0", "psr/simple-cache-implementation": "1.0|2.0|3.0" }, "replace": { @@ -2872,6 +2873,7 @@ "illuminate/bus": "self.version", "illuminate/cache": "self.version", "illuminate/collections": "self.version", + "illuminate/concurrency": "self.version", "illuminate/conditionable": "self.version", "illuminate/config": "self.version", "illuminate/console": "self.version", @@ -2914,9 +2916,9 @@ "league/flysystem-sftp-v3": "^3.0", "mockery/mockery": "^1.6", "nyholm/psr7": "^1.2", - "orchestra/testbench-core": "^9.0.15", + "orchestra/testbench-core": "^9.5", "pda/pheanstalk": "^5.0", - "phpstan/phpstan": "^1.4.7", + "phpstan/phpstan": "^1.11.5", "phpunit/phpunit": "^10.5|^11.0", "predis/predis": "^2.0.2", "resend/resend-php": "^0.10.0", @@ -2972,6 +2974,8 @@ "src/Illuminate/Events/functions.php", "src/Illuminate/Filesystem/functions.php", "src/Illuminate/Foundation/helpers.php", + "src/Illuminate/Log/functions.php", + "src/Illuminate/Support/functions.php", "src/Illuminate/Support/helpers.php" ], "psr-4": { @@ -3003,7 +3007,7 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2024-06-04T13:45:55+00:00" + "time": "2024-10-30T15:00:34+00:00" }, { "name": "laravel/helpers", @@ -13482,5 +13486,5 @@ "ext-zip": "*" }, "platform-dev": [], - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.3.0" } diff --git a/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php b/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php index ca2cabfd67..2c16118d42 100644 --- a/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php +++ b/database/migrations/2017_02_09_174834_SetupPermissionsPivotTable.php @@ -31,6 +31,9 @@ public function up(): void $table->dropIndex('permissions_server_id_foreign'); $table->dropForeign('permissions_user_id_foreign'); $table->dropIndex('permissions_user_id_foreign'); + } else { + $table->dropForeign(['server_id']); + $table->dropForeign(['user_id']); } $table->dropColumn('server_id'); diff --git a/database/migrations/2020_09_13_110007_drop_packs_from_servers.php b/database/migrations/2020_09_13_110007_drop_packs_from_servers.php index cc2695eddd..39f048b373 100644 --- a/database/migrations/2020_09_13_110007_drop_packs_from_servers.php +++ b/database/migrations/2020_09_13_110007_drop_packs_from_servers.php @@ -12,10 +12,7 @@ public function up(): void { Schema::table('servers', function (Blueprint $table) { - if (Schema::getConnection()->getDriverName() !== 'sqlite') { - $table->dropForeign(['pack_id']); - } - + $table->dropForeign(['pack_id']); $table->dropColumn('pack_id'); }); } diff --git a/database/migrations/2024_03_12_154408_remove_nests_table.php b/database/migrations/2024_03_12_154408_remove_nests_table.php index cc95a5f8e1..42d1315b91 100644 --- a/database/migrations/2024_03_12_154408_remove_nests_table.php +++ b/database/migrations/2024_03_12_154408_remove_nests_table.php @@ -30,14 +30,15 @@ public function up(): void Schema::table('eggs', function (Blueprint $table) { if (Schema::getConnection()->getDriverName() !== 'sqlite') { $table->dropForeign('service_options_nest_id_foreign'); + } else { + $table->dropForeign(['nest_id']); } + $table->dropColumn('nest_id'); }); Schema::table('servers', function (Blueprint $table) { - if (Schema::getConnection()->getDriverName() !== 'sqlite') { - $table->dropForeign('servers_nest_id_foreign'); - } + $table->dropForeign(['nest_id']); $table->dropColumn('nest_id'); }); diff --git a/database/migrations/2024_03_14_055537_remove_locations_table.php b/database/migrations/2024_03_14_055537_remove_locations_table.php index f06ded66fd..bdd0c382ad 100644 --- a/database/migrations/2024_03_14_055537_remove_locations_table.php +++ b/database/migrations/2024_03_14_055537_remove_locations_table.php @@ -27,10 +27,7 @@ public function up(): void } Schema::table('nodes', function (Blueprint $table) { - if (Schema::getConnection()->getDriverName() !== 'sqlite') { - $table->dropForeign('nodes_location_id_foreign'); - } - + $table->dropForeign(['location_id']); $table->dropColumn('location_id'); }); From 3b75b963621247a6cc5b6a5283d81a61e3273e2c Mon Sep 17 00:00:00 2001 From: Vehikl Date: Thu, 7 Nov 2024 16:57:23 -0500 Subject: [PATCH 03/11] WIP --- .../Pages/CreateDatabaseHost.php | 13 +++++++------ app/Models/DatabaseHost.php | 15 ++++++--------- .../Databases/Hosts/HostCreationService.php | 3 ++- ...nge_database_hosts_to_belong_to_many_nodes.php | 2 +- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/app/Filament/Resources/DatabaseHostResource/Pages/CreateDatabaseHost.php b/app/Filament/Resources/DatabaseHostResource/Pages/CreateDatabaseHost.php index 606b9ca82b..957892fc21 100644 --- a/app/Filament/Resources/DatabaseHostResource/Pages/CreateDatabaseHost.php +++ b/app/Filament/Resources/DatabaseHostResource/Pages/CreateDatabaseHost.php @@ -79,12 +79,13 @@ public function form(Form $form): Form ->revealable() ->maxLength(255) ->required(), - Select::make('node_id') - ->searchable() - ->preload() - ->helperText('This setting only defaults to this database host when adding a database to a server on the selected node.') - ->label('Linked Node') - ->relationship('node', 'name'), +// Select::make('node_ids') +// ->multiple() +// ->searchable() +// ->preload() +// ->helperText('This setting only defaults to this database host when adding a database to a server on the selected node.') +// ->label('Linked Node') +// ->relationship('nodes', 'name'), ]), ]); } diff --git a/app/Models/DatabaseHost.php b/app/Models/DatabaseHost.php index 278cee9b4d..49902afbf2 100644 --- a/app/Models/DatabaseHost.php +++ b/app/Models/DatabaseHost.php @@ -2,8 +2,8 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; -use Illuminate\Database\Eloquent\Relations\BelongsTo; /** * @property int $id @@ -39,7 +39,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', ]; /** @@ -51,7 +51,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', + 'node_ids' => 'nullable|array', + 'node_ids.*' => 'required|integer,exists:nodes,id', ]; protected function casts(): array @@ -59,7 +60,6 @@ protected function casts(): array return [ 'id' => 'integer', 'max_databases' => 'integer', - 'node_id' => 'integer', 'password' => 'encrypted', 'created_at' => 'immutable_datetime', 'updated_at' => 'immutable_datetime', @@ -71,12 +71,9 @@ public function getRouteKeyName(): string return 'id'; } - /** - * Gets the node associated with a database host. - */ - public function node(): BelongsTo + public function nodes(): BelongsToMany { - return $this->belongsTo(Node::class); + return $this->belongsToMany(Node::class); } /** diff --git a/app/Services/Databases/Hosts/HostCreationService.php b/app/Services/Databases/Hosts/HostCreationService.php index da61bbe7a8..be3eb0499e 100644 --- a/app/Services/Databases/Hosts/HostCreationService.php +++ b/app/Services/Databases/Hosts/HostCreationService.php @@ -34,9 +34,10 @@ public function handle(array $data): DatabaseHost 'port' => array_get($data, 'port'), 'username' => array_get($data, 'username'), 'max_databases' => array_get($data, 'max_databases'), - 'node_id' => array_get($data, 'node_id'), ]); + $host->nodes()->sync(array_get($data, 'node_ids', [])); + // Confirm access using the provided credentials before saving data. $this->dynamic->set('dynamic', $host); $this->databaseManager->connection('dynamic')->getPdo(); diff --git a/database/migrations/2024_10_31_203540_change_database_hosts_to_belong_to_many_nodes.php b/database/migrations/2024_10_31_203540_change_database_hosts_to_belong_to_many_nodes.php index 3d788dbdb6..73a7b54134 100644 --- a/database/migrations/2024_10_31_203540_change_database_hosts_to_belong_to_many_nodes.php +++ b/database/migrations/2024_10_31_203540_change_database_hosts_to_belong_to_many_nodes.php @@ -26,7 +26,7 @@ public function up(): void DB::table('database_host_node')->insert($newJoinEntries->toArray()); Schema::table('database_hosts', function (Blueprint $table) { - // $table->dropForeign() + $table->dropForeign(['node_id']); $table->dropColumn('node_id'); }); } From 85e72d102b0664053425d9f7089a51090e6b7055 Mon Sep 17 00:00:00 2001 From: Vehikl Date: Thu, 14 Nov 2024 16:10:06 -0500 Subject: [PATCH 04/11] fix tests --- .../Pages/CreateDatabaseHost.php | 14 +++++++------- .../Api/Application/DatabaseHostTransformer.php | 11 +++++------ .../Databases/DatabaseManagementServiceTest.php | 7 ++++--- .../Databases/DeployServerDatabaseServiceTest.php | 9 +++------ 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/app/Filament/Resources/DatabaseHostResource/Pages/CreateDatabaseHost.php b/app/Filament/Resources/DatabaseHostResource/Pages/CreateDatabaseHost.php index 957892fc21..fc8b058163 100644 --- a/app/Filament/Resources/DatabaseHostResource/Pages/CreateDatabaseHost.php +++ b/app/Filament/Resources/DatabaseHostResource/Pages/CreateDatabaseHost.php @@ -79,13 +79,13 @@ public function form(Form $form): Form ->revealable() ->maxLength(255) ->required(), -// Select::make('node_ids') -// ->multiple() -// ->searchable() -// ->preload() -// ->helperText('This setting only defaults to this database host when adding a database to a server on the selected node.') -// ->label('Linked Node') -// ->relationship('nodes', 'name'), + Select::make('node_ids') + ->multiple() + ->searchable() + ->preload() + ->helperText('This setting only defaults to this database host when adding a database to a server on the selected node.') + ->label('Linked Node') + ->relationship('nodes', 'name'), ]), ]); } diff --git a/app/Transformers/Api/Application/DatabaseHostTransformer.php b/app/Transformers/Api/Application/DatabaseHostTransformer.php index 1a47b905a7..5c0988ee64 100644 --- a/app/Transformers/Api/Application/DatabaseHostTransformer.php +++ b/app/Transformers/Api/Application/DatabaseHostTransformer.php @@ -14,7 +14,7 @@ class DatabaseHostTransformer extends BaseTransformer { protected array $availableIncludes = [ 'databases', - 'node', + 'nodes', ]; /** @@ -36,7 +36,6 @@ public function transform(DatabaseHost $model): array 'host' => $model->host, 'port' => $model->port, 'username' => $model->username, - 'node' => $model->node_id, 'created_at' => $model->created_at->toAtomString(), 'updated_at' => $model->updated_at->toAtomString(), ]; @@ -59,18 +58,18 @@ public function includeDatabases(DatabaseHost $model): Collection|NullResource } /** - * Include the node associated with this host. + * Include the nodes associated with this host. * * @throws \App\Exceptions\Transformer\InvalidTransformerLevelException */ - public function includeNode(DatabaseHost $model): Item|NullResource + public function includeNodes(DatabaseHost $model): Item|NullResource { if (!$this->authorize(AdminAcl::RESOURCE_NODES)) { return $this->null(); } - $model->loadMissing('node'); + $model->loadMissing('nodes'); - return $this->item($model->getRelation('node'), $this->makeTransformer(NodeTransformer::class), Node::RESOURCE_NAME); + return $this->item($model->getRelation('nodes'), $this->makeTransformer(NodeTransformer::class), Node::RESOURCE_NAME); } } diff --git a/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php b/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php index 060ed9f10f..1f3b33befb 100644 --- a/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php +++ b/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php @@ -4,6 +4,7 @@ use App\Models\Database; use App\Models\DatabaseHost; +use App\Models\Node; use App\Tests\Integration\IntegrationTestCase; use App\Services\Databases\DatabaseManagementService; use App\Exceptions\Repository\DuplicateDatabaseNameException; @@ -52,7 +53,7 @@ public function testExceptionIsThrownIfClientDatabasesAreNotEnabled(): void public function testDatabaseCannotBeCreatedIfServerHasReachedLimit(): void { $server = $this->createServerModel(['database_limit' => 2]); - $host = DatabaseHost::factory()->create(['node_id' => $server->node_id]); + $host = DatabaseHost::factory()->recycle($server->node)->create(); Database::factory()->times(2)->create(['server_id' => $server->id, 'database_host_id' => $host->id]); @@ -84,8 +85,8 @@ public function testCreatingDatabaseWithIdenticalNameTriggersAnException(): void $server = $this->createServerModel(); $name = DatabaseManagementService::generateUniqueDatabaseName('something', $server->id); - $host = DatabaseHost::factory()->create(['node_id' => $server->node_id]); - $host2 = DatabaseHost::factory()->create(['node_id' => $server->node_id]); + $host = DatabaseHost::factory()->recycle($server->node)->create(); + $host2 = DatabaseHost::factory()->recycle($server->node)->create(); Database::factory()->create([ 'database' => $name, 'database_host_id' => $host->id, diff --git a/tests/Integration/Services/Databases/DeployServerDatabaseServiceTest.php b/tests/Integration/Services/Databases/DeployServerDatabaseServiceTest.php index 062dbe07d0..60986f5766 100644 --- a/tests/Integration/Services/Databases/DeployServerDatabaseServiceTest.php +++ b/tests/Integration/Services/Databases/DeployServerDatabaseServiceTest.php @@ -62,7 +62,7 @@ public function testErrorIsThrownIfNoDatabaseHostsExistOnNode(): void $server = $this->createServerModel(); $node = Node::factory()->create(); - DatabaseHost::factory()->create(['node_id' => $node->id]); + DatabaseHost::factory()->recycle($node)->create(); config()->set('panel.client_features.databases.allow_random', false); @@ -95,10 +95,7 @@ public function testErrorIsThrownIfNoDatabaseHostsExistOnSystem(): void public function testDatabaseHostOnSameNodeIsPreferred(): void { $server = $this->createServerModel(); - - $node = Node::factory()->create(); - DatabaseHost::factory()->create(['node_id' => $node->id]); - $host = DatabaseHost::factory()->create(['node_id' => $server->node_id]); + $host = DatabaseHost::factory()->recycle($server->node)->create(); $this->managementService->expects('create')->with($server, [ 'database_host_id' => $host->id, @@ -124,7 +121,7 @@ public function testDatabaseHostIsSelectedIfNoSuitableHostExistsOnSameNode(): vo $server = $this->createServerModel(); $node = Node::factory()->create(); - $host = DatabaseHost::factory()->create(['node_id' => $node->id]); + $host = DatabaseHost::factory()->recycle($node)->create(); $this->managementService->expects('create')->with($server, [ 'database_host_id' => $host->id, From cdff1b504586c6cc80206191f7ebe48c578c8151 Mon Sep 17 00:00:00 2001 From: Vehikl Date: Thu, 14 Nov 2024 16:31:21 -0500 Subject: [PATCH 05/11] Update composer --- composer.lock | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/composer.lock b/composer.lock index 3c73e9efa9..dd408b59fe 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "6be29c745a7e8809ca315ffb815a196d", + "content-hash": "0dceabf8745c0bcd53261561a010c8f3", "packages": [ { "name": "abdelhamiderrahmouni/filament-monaco-editor", @@ -2894,16 +2894,16 @@ }, { "name": "laravel/framework", - "version": "v11.10.0", + "version": "v11.31.0", "source": { "type": "git", "url": "https://github.com/laravel/framework.git", - "reference": "99b4255194912044b75ab72329f8c19e6345720e" + "reference": "365090ed2c68244e3141cdb5e247cdf3dfba2c40" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/framework/zipball/99b4255194912044b75ab72329f8c19e6345720e", - "reference": "99b4255194912044b75ab72329f8c19e6345720e", + "url": "https://api.github.com/repos/laravel/framework/zipball/365090ed2c68244e3141cdb5e247cdf3dfba2c40", + "reference": "365090ed2c68244e3141cdb5e247cdf3dfba2c40", "shasum": "" }, "require": { @@ -2922,7 +2922,7 @@ "fruitcake/php-cors": "^1.3", "guzzlehttp/guzzle": "^7.8", "guzzlehttp/uri-template": "^1.0", - "laravel/prompts": "^0.1.18", + "laravel/prompts": "^0.1.18|^0.2.0|^0.3.0", "laravel/serializable-closure": "^1.3", "league/commonmark": "^2.2.1", "league/flysystem": "^3.8.0", @@ -2956,6 +2956,7 @@ }, "provide": { "psr/container-implementation": "1.1|2.0", + "psr/log-implementation": "1.0|2.0|3.0", "psr/simple-cache-implementation": "1.0|2.0|3.0" }, "replace": { @@ -2964,6 +2965,7 @@ "illuminate/bus": "self.version", "illuminate/cache": "self.version", "illuminate/collections": "self.version", + "illuminate/concurrency": "self.version", "illuminate/conditionable": "self.version", "illuminate/config": "self.version", "illuminate/console": "self.version", @@ -3006,9 +3008,9 @@ "league/flysystem-sftp-v3": "^3.0", "mockery/mockery": "^1.6", "nyholm/psr7": "^1.2", - "orchestra/testbench-core": "^9.0.15", + "orchestra/testbench-core": "^9.5", "pda/pheanstalk": "^5.0", - "phpstan/phpstan": "^1.4.7", + "phpstan/phpstan": "^1.11.5", "phpunit/phpunit": "^10.5|^11.0", "predis/predis": "^2.0.2", "resend/resend-php": "^0.10.0", @@ -3064,6 +3066,8 @@ "src/Illuminate/Events/functions.php", "src/Illuminate/Filesystem/functions.php", "src/Illuminate/Foundation/helpers.php", + "src/Illuminate/Log/functions.php", + "src/Illuminate/Support/functions.php", "src/Illuminate/Support/helpers.php" ], "psr-4": { @@ -3095,7 +3099,7 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2024-06-04T13:45:55+00:00" + "time": "2024-11-12T15:36:15+00:00" }, { "name": "laravel/helpers", @@ -13505,5 +13509,5 @@ "ext-zip": "*" }, "platform-dev": [], - "plugin-api-version": "2.6.0" -} \ No newline at end of file + "plugin-api-version": "2.3.0" +} From b91d57b8f7f618b26538555c22018edde8bc65c9 Mon Sep 17 00:00:00 2001 From: Vehikl Date: Thu, 14 Nov 2024 16:31:40 -0500 Subject: [PATCH 06/11] Fix transformer --- .../Api/Application/DatabaseHostTransformer.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/Transformers/Api/Application/DatabaseHostTransformer.php b/app/Transformers/Api/Application/DatabaseHostTransformer.php index 0402ff7e95..3ac34087c2 100644 --- a/app/Transformers/Api/Application/DatabaseHostTransformer.php +++ b/app/Transformers/Api/Application/DatabaseHostTransformer.php @@ -13,7 +13,7 @@ class DatabaseHostTransformer extends BaseTransformer { protected array $availableIncludes = [ 'databases', - 'node', + 'nodes', ]; /** @@ -56,16 +56,16 @@ public function includeDatabases(DatabaseHost $model): Collection|NullResource } /** - * Include the node associated with this host. + * Include the nodes associated with this host. */ - public function includeNode(DatabaseHost $model): Item|NullResource + public function includeNodes(DatabaseHost $model): Item|NullResource { if (!$this->authorize(Node::RESOURCE_NAME)) { return $this->null(); } - $model->loadMissing('node'); + $model->loadMissing('nodes'); - return $this->item($model->getRelation('node'), $this->makeTransformer(NodeTransformer::class), Node::RESOURCE_NAME); + return $this->item($model->getRelation('nodes'), $this->makeTransformer(NodeTransformer::class), Node::RESOURCE_NAME); } } From 4a3a0a0772a549e49f20d6753225f49bd8d97334 Mon Sep 17 00:00:00 2001 From: Vehikl Date: Thu, 14 Nov 2024 16:31:59 -0500 Subject: [PATCH 07/11] Fix filament pages --- .../DatabaseHostResource/Pages/CreateDatabaseHost.php | 2 +- .../DatabaseHostResource/Pages/EditDatabaseHost.php | 7 ++++--- .../DatabaseHostResource/Pages/ListDatabaseHosts.php | 3 ++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/Filament/Resources/DatabaseHostResource/Pages/CreateDatabaseHost.php b/app/Filament/Resources/DatabaseHostResource/Pages/CreateDatabaseHost.php index fc8b058163..b18257f05c 100644 --- a/app/Filament/Resources/DatabaseHostResource/Pages/CreateDatabaseHost.php +++ b/app/Filament/Resources/DatabaseHostResource/Pages/CreateDatabaseHost.php @@ -84,7 +84,7 @@ public function form(Form $form): Form ->searchable() ->preload() ->helperText('This setting only defaults to this database host when adding a database to a server on the selected node.') - ->label('Linked Node') + ->label('Linked Nodes') ->relationship('nodes', 'name'), ]), ]); diff --git a/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php b/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php index 5d0176699a..68a5493be8 100644 --- a/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php +++ b/app/Filament/Resources/DatabaseHostResource/Pages/EditDatabaseHost.php @@ -74,12 +74,13 @@ public function form(Form $form): Form ->password() ->revealable() ->maxLength(255), - Select::make('node_id') + Select::make('nodes') + ->multiple() ->searchable() ->preload() ->helperText('This setting only defaults to this database host when adding a database to a server on the selected node.') - ->label('Linked Node') - ->relationship('node', 'name'), + ->label('Linked Nodes') + ->relationship('nodes', 'name'), ]), ]); } diff --git a/app/Filament/Resources/DatabaseHostResource/Pages/ListDatabaseHosts.php b/app/Filament/Resources/DatabaseHostResource/Pages/ListDatabaseHosts.php index c5db9ea855..f9615f3e7e 100644 --- a/app/Filament/Resources/DatabaseHostResource/Pages/ListDatabaseHosts.php +++ b/app/Filament/Resources/DatabaseHostResource/Pages/ListDatabaseHosts.php @@ -36,8 +36,9 @@ public function table(Table $table): Table ->counts('databases') ->icon('tabler-database') ->label('Databases'), - TextColumn::make('node.name') + TextColumn::make('nodes.name') ->icon('tabler-server-2') + ->badge() ->placeholder('No Nodes') ->sortable(), ]) From 18a7cc369781d84b46e73738688da993fdfb02b1 Mon Sep 17 00:00:00 2001 From: Vehikl Date: Thu, 14 Nov 2024 16:58:37 -0500 Subject: [PATCH 08/11] WIP --- app/Models/Node.php | 6 ++++++ .../Databases/DeployServerDatabaseService.php | 12 ++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/app/Models/Node.php b/app/Models/Node.php index ea31ba2fdf..b9d9292833 100644 --- a/app/Models/Node.php +++ b/app/Models/Node.php @@ -5,6 +5,7 @@ use App\Exceptions\Service\HasActiveServersException; use App\Repositories\Daemon\DaemonConfigurationRepository; use Exception; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Notifications\Notifiable; @@ -381,4 +382,9 @@ public function ipAddresses(): array return $ips->all(); }); } + + public function databaseHosts(): BelongsToMany + { + return $this->belongsToMany(DatabaseHost::class); + } } diff --git a/app/Services/Databases/DeployServerDatabaseService.php b/app/Services/Databases/DeployServerDatabaseService.php index d451423793..06a9b9abc8 100644 --- a/app/Services/Databases/DeployServerDatabaseService.php +++ b/app/Services/Databases/DeployServerDatabaseService.php @@ -27,15 +27,15 @@ public function handle(Server $server, array $data): Database Assert::notEmpty($data['database'] ?? null); Assert::notEmpty($data['remote'] ?? null); - $hosts = DatabaseHost::query()->get()->toBase(); + $hosts = DatabaseHost::query()->get(); if ($hosts->isEmpty()) { throw new NoSuitableDatabaseHostException(); - } else { - $nodeHosts = $hosts->where('node_id', $server->node_id)->toBase(); + } - if ($nodeHosts->isEmpty() && !config('panel.client_features.databases.allow_random')) { - throw new NoSuitableDatabaseHostException(); - } + $nodeHosts = $server->node->databaseHosts()->get(); + // TODO: @areyouscared remove allow random feature for database hosts + if ($nodeHosts->isEmpty() && !config('panel.client_features.databases.allow_random')) { + throw new NoSuitableDatabaseHostException(); } return $this->managementService->create($server, [ From 308f706673acca5b1b4b54ce0aedc043a21ef522 Mon Sep 17 00:00:00 2001 From: Vehikl Date: Thu, 21 Nov 2024 15:39:42 -0500 Subject: [PATCH 09/11] Update DatabaseHostTransformer --- app/Transformers/Api/Application/DatabaseHostTransformer.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/Transformers/Api/Application/DatabaseHostTransformer.php b/app/Transformers/Api/Application/DatabaseHostTransformer.php index 3ac34087c2..0a0999df3f 100644 --- a/app/Transformers/Api/Application/DatabaseHostTransformer.php +++ b/app/Transformers/Api/Application/DatabaseHostTransformer.php @@ -35,7 +35,6 @@ public function transform(DatabaseHost $model): array 'host' => $model->host, 'port' => $model->port, 'username' => $model->username, - 'node' => $model->node_id, 'created_at' => $model->created_at->toAtomString(), 'updated_at' => $model->updated_at->toAtomString(), ]; @@ -58,7 +57,7 @@ public function includeDatabases(DatabaseHost $model): Collection|NullResource /** * Include the nodes associated with this host. */ - public function includeNodes(DatabaseHost $model): Item|NullResource + public function includeNodes(DatabaseHost $model): Collection|NullResource { if (!$this->authorize(Node::RESOURCE_NAME)) { return $this->null(); @@ -66,6 +65,6 @@ public function includeNodes(DatabaseHost $model): Item|NullResource $model->loadMissing('nodes'); - return $this->item($model->getRelation('nodes'), $this->makeTransformer(NodeTransformer::class), Node::RESOURCE_NAME); + return $this->collection($model->getRelation('nodes'), $this->makeTransformer(NodeTransformer::class), Node::RESOURCE_NAME); } } From d8d44d00c96066edbd29869c59698cc741934225 Mon Sep 17 00:00:00 2001 From: Vehikl Date: Thu, 21 Nov 2024 15:52:51 -0500 Subject: [PATCH 10/11] fix: tests --- .../Services/Databases/DatabaseManagementServiceTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php b/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php index 1f3b33befb..098003f026 100644 --- a/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php +++ b/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php @@ -10,6 +10,7 @@ use App\Exceptions\Repository\DuplicateDatabaseNameException; use App\Exceptions\Service\Database\TooManyDatabasesException; use App\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException; +use Database\Factories\NodeFactory; class DatabaseManagementServiceTest extends IntegrationTestCase { @@ -118,7 +119,7 @@ public function testServerDatabaseCanBeCreated(): void $server = $this->createServerModel(); $name = DatabaseManagementService::generateUniqueDatabaseName('something', $server->id); - $host = DatabaseHost::factory()->create(['node_id' => $server->node_id]); + $host = DatabaseHost::factory()->recycle($server->node)->create(); $username = null; $secondUsername = null; @@ -139,7 +140,7 @@ public function testServerDatabaseCanBeCreated(): void $this->assertDatabaseHas('databases', ['server_id' => $server->id, 'id' => $response->id]); } - /** +/** * Test that an exception encountered while creating the database leads to the cleanup code * being called and any exceptions encountered while cleaning up go unreported. */ @@ -155,7 +156,7 @@ public function testExceptionEncounteredWhileCreatingDatabaseAttemptsToCleanup() $server = $this->createServerModel(); $name = DatabaseManagementService::generateUniqueDatabaseName('something', $server->id); - $host = DatabaseHost::factory()->create(['node_id' => $server->node_id]); + $host = DatabaseHost::factory()->recycle($server->node)->create(); $this->repository->expects('createDatabase')->with($name)->andThrows(new \BadMethodCallException()); $this->repository->expects('dropDatabase')->with($name); From 161cf5953848bc215338e90227990f5dca7c3750 Mon Sep 17 00:00:00 2001 From: Vehikl Date: Thu, 21 Nov 2024 16:01:34 -0500 Subject: [PATCH 11/11] pint this files pls --- app/Transformers/Api/Application/DatabaseHostTransformer.php | 1 - ...1_203540_change_database_hosts_to_belong_to_many_nodes.php | 3 +-- .../Services/Databases/DatabaseManagementServiceTest.php | 4 +--- 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/app/Transformers/Api/Application/DatabaseHostTransformer.php b/app/Transformers/Api/Application/DatabaseHostTransformer.php index 0a0999df3f..8f3453f6a3 100644 --- a/app/Transformers/Api/Application/DatabaseHostTransformer.php +++ b/app/Transformers/Api/Application/DatabaseHostTransformer.php @@ -5,7 +5,6 @@ use App\Models\Node; use App\Models\Database; use App\Models\DatabaseHost; -use League\Fractal\Resource\Item; use League\Fractal\Resource\Collection; use League\Fractal\Resource\NullResource; diff --git a/database/migrations/2024_10_31_203540_change_database_hosts_to_belong_to_many_nodes.php b/database/migrations/2024_10_31_203540_change_database_hosts_to_belong_to_many_nodes.php index 73a7b54134..c860993c8a 100644 --- a/database/migrations/2024_10_31_203540_change_database_hosts_to_belong_to_many_nodes.php +++ b/database/migrations/2024_10_31_203540_change_database_hosts_to_belong_to_many_nodes.php @@ -40,8 +40,7 @@ public function down(): void foreach (DB::table('database_host_node')->get() as $record) { DB::table('database_hosts') ->where('id', $record->database_host_id) - ->update(['node_id' => $record->node_id]) - ; + ->update(['node_id' => $record->node_id]); } Schema::drop('database_host_node'); diff --git a/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php b/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php index 098003f026..834e6e345e 100644 --- a/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php +++ b/tests/Integration/Services/Databases/DatabaseManagementServiceTest.php @@ -4,13 +4,11 @@ use App\Models\Database; use App\Models\DatabaseHost; -use App\Models\Node; use App\Tests\Integration\IntegrationTestCase; use App\Services\Databases\DatabaseManagementService; use App\Exceptions\Repository\DuplicateDatabaseNameException; use App\Exceptions\Service\Database\TooManyDatabasesException; use App\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException; -use Database\Factories\NodeFactory; class DatabaseManagementServiceTest extends IntegrationTestCase { @@ -140,7 +138,7 @@ public function testServerDatabaseCanBeCreated(): void $this->assertDatabaseHas('databases', ['server_id' => $server->id, 'id' => $response->id]); } -/** + /** * Test that an exception encountered while creating the database leads to the cleanup code * being called and any exceptions encountered while cleaning up go unreported. */