From 70ad94d66d2f2ec76974e70026007bb0f04a77a3 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 24 Mar 2026 13:11:34 +0000 Subject: [PATCH] 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) --- ...4_000000_add_feature_code_foreign_keys.php | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Migrations/2026_03_24_000000_add_feature_code_foreign_keys.php diff --git a/Migrations/2026_03_24_000000_add_feature_code_foreign_keys.php b/Migrations/2026_03_24_000000_add_feature_code_foreign_keys.php new file mode 100644 index 0000000..9680b1b --- /dev/null +++ b/Migrations/2026_03_24_000000_add_feature_code_foreign_keys.php @@ -0,0 +1,57 @@ +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'); + }); + } +};