Skip to content

Commit 8253246

Browse files
committed
Prevent an exception when creating databases with the same name on multiple hosts.
closes pterodactyl#1456
1 parent 91c9cbb commit 8253246

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
77
### Fixed
88
* Fixes a bug with the location update API endpoint throwing an error due to an unexected response value.
99
* Fixes bug where node creation API endpoint was not correctly requiring the `disk_overallocate` key.
10+
* Prevents an exception from being thrown when a database with the same name is created on two different hosts.
1011

1112
### Changed
1213
* `allocation_limit` for servers now defaults to a null value, and is not required in PATCH/POST requests when adding

app/Http/Requests/Admin/Servers/Databases/StoreServerDatabaseRequest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Pterodactyl\Http\Requests\Admin\Servers\Databases;
44

5+
use Illuminate\Validation\Rule;
6+
use Illuminate\Database\Query\Builder;
57
use Pterodactyl\Http\Requests\Admin\AdminFormRequest;
68

79
class StoreServerDatabaseRequest extends AdminFormRequest
@@ -14,7 +16,15 @@ class StoreServerDatabaseRequest extends AdminFormRequest
1416
public function rules(): array
1517
{
1618
return [
17-
'database' => 'required|string|min:1|max:24',
19+
'database' => [
20+
'required',
21+
'string',
22+
'min:1',
23+
'max:24',
24+
Rule::unique('databases')->where(function (Builder $query) {
25+
$query->where('database_host_id', $this->input('database_host_id') ?? 0);
26+
}),
27+
],
1828
'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/',
1929
'database_host_id' => 'required|integer|exists:database_hosts,id',
2030
];

app/Http/Requests/Api/Application/Servers/Databases/StoreServerDatabaseRequest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Pterodactyl\Http\Requests\Api\Application\Servers\Databases;
44

5+
use Illuminate\Validation\Rule;
6+
use Illuminate\Database\Query\Builder;
57
use Pterodactyl\Services\Acl\Api\AdminAcl;
68
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
79

@@ -25,7 +27,15 @@ class StoreServerDatabaseRequest extends ApplicationApiRequest
2527
public function rules(): array
2628
{
2729
return [
28-
'database' => 'required|string|min:1|max:24',
30+
'database' => [
31+
'required',
32+
'string',
33+
'min:1',
34+
'max:24',
35+
Rule::unique('databases')->where(function (Builder $query) {
36+
$query->where('database_host_id', $this->input('host') ?? 0);
37+
}),
38+
],
2939
'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/',
3040
'host' => 'required|integer|exists:database_hosts,id',
3141
];
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class FixUniqueIndexToAccountForHost extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('databases', function (Blueprint $table) {
17+
$table->dropUnique(['database']);
18+
$table->dropUnique(['username']);
19+
20+
$table->unique(['database_host_id', 'database']);
21+
$table->unique(['database_host_id', 'username']);
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
Schema::table('databases', function (Blueprint $table) {
33+
$table->dropUnique(['database_host_id', 'database']);
34+
$table->dropUnique(['database_host_id', 'username']);
35+
36+
$table->unique(['database']);
37+
$table->unique(['username']);
38+
});
39+
}
40+
}

0 commit comments

Comments
 (0)