diff --git a/database/migrations/2021_01_29_121118_rename_segments_to_tags.php b/database/migrations/2021_01_29_121118_rename_segments_to_tags.php index e8345dd3..326542e3 100644 --- a/database/migrations/2021_01_29_121118_rename_segments_to_tags.php +++ b/database/migrations/2021_01_29_121118_rename_segments_to_tags.php @@ -16,7 +16,13 @@ public function up() Schema::rename('sendportal_segments', 'sendportal_tags'); Schema::table('sendportal_segment_subscriber', function (Blueprint $table) { - $table->dropForeign(['segment_id']); + $foreignKeys = $this->listTableForeignKeys('sendportal_segment_subscriber'); + + if (in_array('sendportal_segment_subscriber_segment_id_foreign', $foreignKeys)) { + $table->dropForeign('sendportal_segment_subscriber_segment_id_foreign'); + } elseif (in_array('segment_subscriber_segment_id_foreign', $foreignKeys)) { + $table->dropForeign('segment_subscriber_segment_id_foreign'); + } $table->renameColumn('segment_id', 'tag_id'); @@ -27,7 +33,13 @@ public function up() Schema::table('sendportal_campaign_segment', function (Blueprint $table) { - $table->dropForeign(['segment_id']); + $foreignKeys = $this->listTableForeignKeys('sendportal_campaign_segment'); + + if (in_array('sendportal_campaign_segment_segment_id_foreign', $foreignKeys)) { + $table->dropForeign('sendportal_campaign_segment_segment_id_foreign'); + } elseif (in_array('campaign_segment_segment_id_foreign', $foreignKeys)) { + $table->dropForeign('campaign_segment_segment_id_foreign'); + } $table->renameColumn('segment_id', 'tag_id'); @@ -36,4 +48,13 @@ public function up() Schema::rename("sendportal_campaign_segment", "sendportal_campaign_tag"); } + + protected function listTableForeignKeys(string $table): array + { + $conn = Schema::getConnection()->getDoctrineSchemaManager(); + + return array_map(function ($key) { + return $key->getName(); + }, $conn->listTableForeignKeys($table)); + } }