fix: add FK constraints on feature_code columns to entitlement_features
Add foreign key constraints from usage_alert_history.feature_code, entitlement_boosts.feature_code, and entitlement_usage_records.feature_code to entitlement_features.code to prevent orphaned records. Uses cascadeOnUpdate (code renames propagate) and restrictOnDelete (cannot delete a feature that has usage/alert/boost records). Fixes #12 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
8b05b8a76d
commit
70ad94d66d
1 changed files with 57 additions and 0 deletions
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Add foreign key constraints on feature_code columns to prevent orphaned records.
|
||||
*
|
||||
* Fixes #12: feature_code in usage_alert_history not constrained to entitlement_features.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('entitlement_usage_alert_history', function (Blueprint $table) {
|
||||
$table->foreign('feature_code', 'usage_alert_feature_code_fk')
|
||||
->references('code')
|
||||
->on('entitlement_features')
|
||||
->cascadeOnUpdate()
|
||||
->restrictOnDelete();
|
||||
});
|
||||
|
||||
Schema::table('entitlement_boosts', function (Blueprint $table) {
|
||||
$table->foreign('feature_code', 'boosts_feature_code_fk')
|
||||
->references('code')
|
||||
->on('entitlement_features')
|
||||
->cascadeOnUpdate()
|
||||
->restrictOnDelete();
|
||||
});
|
||||
|
||||
Schema::table('entitlement_usage_records', function (Blueprint $table) {
|
||||
$table->foreign('feature_code', 'usage_records_feature_code_fk')
|
||||
->references('code')
|
||||
->on('entitlement_features')
|
||||
->cascadeOnUpdate()
|
||||
->restrictOnDelete();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('entitlement_usage_alert_history', function (Blueprint $table) {
|
||||
$table->dropForeign('usage_alert_feature_code_fk');
|
||||
});
|
||||
|
||||
Schema::table('entitlement_boosts', function (Blueprint $table) {
|
||||
$table->dropForeign('boosts_feature_code_fk');
|
||||
});
|
||||
|
||||
Schema::table('entitlement_usage_records', function (Blueprint $table) {
|
||||
$table->dropForeign('usage_records_feature_code_fk');
|
||||
});
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Reference in a new issue