From f0087b207563b9981efbbf6862e5f45d3647d684 Mon Sep 17 00:00:00 2001 From: mauricius Date: Thu, 4 Mar 2021 16:09:47 +0100 Subject: [PATCH 1/2] Fix rename segments to tags migration --- ...1_01_29_121118_rename_segments_to_tags.php | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) 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..bfbe3c1d 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'); + } else if (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'); + } else if (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)); + } } From 6def232dc0da2e793ebd35bfacfcfd476e9d0c43 Mon Sep 17 00:00:00 2001 From: mauricius Date: Thu, 4 Mar 2021 15:11:05 +0000 Subject: [PATCH 2/2] Apply php-cs-fixer changes --- .../2021_01_29_121118_rename_segments_to_tags.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 bfbe3c1d..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 @@ -20,7 +20,7 @@ public function up() if (in_array('sendportal_segment_subscriber_segment_id_foreign', $foreignKeys)) { $table->dropForeign('sendportal_segment_subscriber_segment_id_foreign'); - } else if (in_array('segment_subscriber_segment_id_foreign', $foreignKeys)) { + } elseif (in_array('segment_subscriber_segment_id_foreign', $foreignKeys)) { $table->dropForeign('segment_subscriber_segment_id_foreign'); } @@ -37,7 +37,7 @@ public function up() if (in_array('sendportal_campaign_segment_segment_id_foreign', $foreignKeys)) { $table->dropForeign('sendportal_campaign_segment_segment_id_foreign'); - } else if (in_array('campaign_segment_segment_id_foreign', $foreignKeys)) { + } elseif (in_array('campaign_segment_segment_id_foreign', $foreignKeys)) { $table->dropForeign('campaign_segment_segment_id_foreign'); } @@ -53,7 +53,7 @@ protected function listTableForeignKeys(string $table): array { $conn = Schema::getConnection()->getDoctrineSchemaManager(); - return array_map(function($key) { + return array_map(function ($key) { return $key->getName(); }, $conn->listTableForeignKeys($table)); }