diff --git a/CHANGELOG.md b/CHANGELOG.md index 06f8230ca..0549a80cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. ### Fixed * Fixes a bug with the location update API endpoint throwing an error due to an unexected response value. * Fixes bug where node creation API endpoint was not correctly requiring the `disk_overallocate` key. +* Prevents an exception from being thrown when a database with the same name is created on two different hosts. ### Changed * `allocation_limit` for servers now defaults to a null value, and is not required in PATCH/POST requests when adding diff --git a/app/Http/Requests/Admin/Servers/Databases/StoreServerDatabaseRequest.php b/app/Http/Requests/Admin/Servers/Databases/StoreServerDatabaseRequest.php index 7f08ecd08..ba8b76a83 100644 --- a/app/Http/Requests/Admin/Servers/Databases/StoreServerDatabaseRequest.php +++ b/app/Http/Requests/Admin/Servers/Databases/StoreServerDatabaseRequest.php @@ -2,6 +2,8 @@ namespace Pterodactyl\Http\Requests\Admin\Servers\Databases; +use Illuminate\Validation\Rule; +use Illuminate\Database\Query\Builder; use Pterodactyl\Http\Requests\Admin\AdminFormRequest; class StoreServerDatabaseRequest extends AdminFormRequest @@ -14,7 +16,15 @@ class StoreServerDatabaseRequest extends AdminFormRequest public function rules(): array { return [ - 'database' => 'required|string|min:1|max:24', + 'database' => [ + 'required', + 'string', + 'min:1', + 'max:24', + Rule::unique('databases')->where(function (Builder $query) { + $query->where('database_host_id', $this->input('database_host_id') ?? 0); + }), + ], 'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/', 'database_host_id' => 'required|integer|exists:database_hosts,id', ]; diff --git a/app/Http/Requests/Api/Application/Servers/Databases/StoreServerDatabaseRequest.php b/app/Http/Requests/Api/Application/Servers/Databases/StoreServerDatabaseRequest.php index ee4264a9d..c2dbfe14a 100644 --- a/app/Http/Requests/Api/Application/Servers/Databases/StoreServerDatabaseRequest.php +++ b/app/Http/Requests/Api/Application/Servers/Databases/StoreServerDatabaseRequest.php @@ -2,6 +2,8 @@ namespace Pterodactyl\Http\Requests\Api\Application\Servers\Databases; +use Illuminate\Validation\Rule; +use Illuminate\Database\Query\Builder; use Pterodactyl\Services\Acl\Api\AdminAcl; use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest; @@ -25,7 +27,15 @@ class StoreServerDatabaseRequest extends ApplicationApiRequest public function rules(): array { return [ - 'database' => 'required|string|min:1|max:24', + 'database' => [ + 'required', + 'string', + 'min:1', + 'max:24', + Rule::unique('databases')->where(function (Builder $query) { + $query->where('database_host_id', $this->input('host') ?? 0); + }), + ], 'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/', 'host' => 'required|integer|exists:database_hosts,id', ]; diff --git a/database/migrations/2019_03_02_151321_fix_unique_index_to_account_for_host.php b/database/migrations/2019_03_02_151321_fix_unique_index_to_account_for_host.php new file mode 100644 index 000000000..fe5f85f88 --- /dev/null +++ b/database/migrations/2019_03_02_151321_fix_unique_index_to_account_for_host.php @@ -0,0 +1,40 @@ +dropUnique(['database']); + $table->dropUnique(['username']); + + $table->unique(['database_host_id', 'database']); + $table->unique(['database_host_id', 'username']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('databases', function (Blueprint $table) { + $table->dropUnique(['database_host_id', 'database']); + $table->dropUnique(['database_host_id', 'username']); + + $table->unique(['database']); + $table->unique(['username']); + }); + } +}