Skip to content

Commit

Permalink
Merge pull request #115 from mettle/bugfix/fix-fk-migration
Browse files Browse the repository at this point in the history
Fix rename segments to tags migration
  • Loading branch information
JonoB authored Mar 4, 2021
2 parents 4d30043 + 6def232 commit 23cdcc5
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions database/migrations/2021_01_29_121118_rename_segments_to_tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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');

Expand All @@ -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));
}
}

0 comments on commit 23cdcc5

Please # to comment.