diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d9ff8e42..1283a19e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,8 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. ### Added * Added ability to search the following API endpoints: list users, list servers, and list locations. * Add support for finding a user by external ID using `/api/application/users/external/` or by passing it as the search term when listing all users. - +* Added a unique key to the servers table to data integrity issues where an allocation would be assigned to more than one server at once. + ### Changed * PHP 7.2 is now the minimum required version for this software. * Egg variable default values are no longer validated aganist the ruleset when configuring them. Validation of those rules will only occur when editing or creating a server. diff --git a/app/Models/Server.php b/app/Models/Server.php index 5bc10a3aa..f10e52b1d 100644 --- a/app/Models/Server.php +++ b/app/Models/Server.php @@ -83,7 +83,7 @@ class Server extends Model implements CleansAttributes, ValidableContract 'io' => 'numeric|between:10,1000', 'cpu' => 'numeric|min:0', 'disk' => 'numeric|min:0', - 'allocation_id' => 'exists:allocations,id', + 'allocation_id' => 'bail|unique:servers|exists:allocations,id', 'nest_id' => 'exists:nests,id', 'egg_id' => 'exists:eggs,id', 'pack_id' => 'nullable|numeric|min:0', diff --git a/database/migrations/2018_02_17_134254_ensure_unique_allocation_id_on_servers_table.php b/database/migrations/2018_02_17_134254_ensure_unique_allocation_id_on_servers_table.php new file mode 100644 index 000000000..fcf6b4fe3 --- /dev/null +++ b/database/migrations/2018_02_17_134254_ensure_unique_allocation_id_on_servers_table.php @@ -0,0 +1,32 @@ +unique(['allocation_id']); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('servers', function (Blueprint $table) { + $table->dropUnique(['allocation_id']); + }); + } +}