-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add foreign key constraints for all projectids (#1655)
Our database schema is currently a mess. With no foreign keys defined, it is easy to accidentally forget to update a relationship field properly when an entity is created or deleted. Virtually all modern relational databases support foreign key constraints, and there is no reason for us to not add them. In a first step towards #1442, this PR adds foreign key constraints to all of the table columns which reference a project's ID. This should be a relatively low risk operation for production systems, but it creates a bit of a headache when writing tests, because creating any model which references a project necessarily requires a project model to be created as well.
- Loading branch information
1 parent
1b508b7
commit e9272a7
Showing
9 changed files
with
267 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
database/migrations/2023_07_20_185727_project_foreign_keys.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration { | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
echo 'Adding projectid foreign key to authtoken table...' . PHP_EOL; | ||
Schema::table('authtoken', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
|
||
echo 'Adding projectid foreign key to blockbuild table...' . PHP_EOL; | ||
Schema::table('blockbuild', function (Blueprint $table) { | ||
$table->integer('projectid')->nullable(false)->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
|
||
echo 'Adding projectid foreign key to build table...' . PHP_EOL; | ||
Schema::table('build', function (Blueprint $table) { | ||
$table->integer('projectid')->nullable(false)->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
|
||
echo 'Adding projectid foreign key to build_filters table...' . PHP_EOL; | ||
Schema::table('build_filters', function (Blueprint $table) { | ||
$table->integer('projectid')->nullable(false)->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
|
||
echo 'Adding projectid foreign key to buildgroup table...' . PHP_EOL; | ||
Schema::table('buildgroup', function (Blueprint $table) { | ||
$table->integer('projectid')->nullable(false)->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
|
||
echo 'Adding projectid foreign key to coveragefilepriority table...' . PHP_EOL; | ||
Schema::table('coveragefilepriority', function (Blueprint $table) { | ||
$table->integer('projectid')->nullable(false)->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
|
||
echo 'Adding projectid foreign key to labelemail table...' . PHP_EOL; | ||
Schema::table('labelemail', function (Blueprint $table) { | ||
$table->integer('projectid')->nullable(false)->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
|
||
echo 'Adding projectid foreign key to measurement table...' . PHP_EOL; | ||
Schema::table('measurement', function (Blueprint $table) { | ||
$table->integer('projectid')->nullable(false)->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
|
||
echo 'Adding projectid foreign key to overview_components table...' . PHP_EOL; | ||
Schema::table('overview_components', function (Blueprint $table) { | ||
$table->integer('projectid')->nullable(false)->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
|
||
echo 'Adding projectid foreign key to project2repositories table...' . PHP_EOL; | ||
Schema::table('project2repositories', function (Blueprint $table) { | ||
$table->integer('projectid')->nullable(false)->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
|
||
echo 'Adding projectid foreign key to subproject table...' . PHP_EOL; | ||
Schema::table('subproject', function (Blueprint $table) { | ||
$table->integer('projectid')->nullable(false)->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
|
||
echo 'Adding projectid foreign key to subprojectgroup table...' . PHP_EOL; | ||
Schema::table('subprojectgroup', function (Blueprint $table) { | ||
$table->integer('projectid')->nullable(false)->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
|
||
echo 'Adding projectid foreign key to test table...' . PHP_EOL; | ||
Schema::table('test', function (Blueprint $table) { | ||
$table->integer('projectid')->nullable(false)->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
|
||
echo 'Adding projectid foreign key to user2project table...' . PHP_EOL; | ||
Schema::table('user2project', function (Blueprint $table) { | ||
$table->integer('projectid')->nullable(false)->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
|
||
echo 'Adding projectid foreign key to user2repository table...' . PHP_EOL; | ||
Schema::table('user2repository', function (Blueprint $table) { | ||
$table->integer('projectid')->nullable(false)->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
|
||
echo 'Adding projectid foreign key to userstatistics table...' . PHP_EOL; | ||
Schema::table('userstatistics', function (Blueprint $table) { | ||
$table->integer('projectid')->nullable(false)->change(); | ||
$table->foreign('projectid')->references('id')->on('project')->cascadeOnDelete(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::table('authtoken', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
|
||
Schema::table('blockbuild', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
|
||
Schema::table('build', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
|
||
Schema::table('build_filters', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
|
||
Schema::table('buildgroup', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
|
||
Schema::table('coveragefilepriority', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
|
||
Schema::table('labelemail', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
|
||
Schema::table('measurement', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
|
||
Schema::table('overview_components', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
|
||
Schema::table('project2repositories', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
|
||
Schema::table('subproject', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
|
||
Schema::table('subprojectgroup', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
|
||
Schema::table('test', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
|
||
Schema::table('user2project', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
|
||
Schema::table('user2repository', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
|
||
Schema::table('userstatistics', function (Blueprint $table) { | ||
$table->integer('projectid')->change(); | ||
$table->dropForeign(['projectid']); | ||
}); | ||
} | ||
}; |
Oops, something went wrong.