From 27265541d6d37557284d9c38cffd105ddaccd90d Mon Sep 17 00:00:00 2001 From: David Sevilla Martin Date: Wed, 26 Jul 2023 12:22:31 -0400 Subject: [PATCH] Split the two discussion_id -> post_id migrations into 4 total --- ...0000_rename_polls_discussion_id_column.php | 3 +- ...00_rename_polls_discussion_id_column_1.php | 32 +++++++++++++++ ...olls_discussion_relation_to_first_post.php | 9 +---- ...07_08_000002_add_polls_post_id_foreign.php | 39 +++++++++++++++++++ 4 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 migrations/2023_07_08_000000_rename_polls_discussion_id_column_1.php create mode 100644 migrations/2023_07_08_000002_add_polls_post_id_foreign.php diff --git a/migrations/2023_07_08_000000_rename_polls_discussion_id_column.php b/migrations/2023_07_08_000000_rename_polls_discussion_id_column.php index e48c3b91..6f59dffa 100644 --- a/migrations/2023_07_08_000000_rename_polls_discussion_id_column.php +++ b/migrations/2023_07_08_000000_rename_polls_discussion_id_column.php @@ -12,16 +12,15 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Builder; +// Split 1/2 of 2023_07_08_000000_rename_polls_discussion_id_column.php return [ 'up' => function (Builder $schema) { $schema->table('polls', function (Blueprint $table) { $table->dropForeign(['discussion_id']); - $table->renameColumn('discussion_id', 'post_id'); }); }, 'down' => function (Builder $schema) { $schema->table('polls', function (Blueprint $table) { - $table->renameColumn('post_id', 'discussion_id'); $table->foreign('discussion_id')->references('id')->on('discussions')->onDelete('cascade'); }); }, diff --git a/migrations/2023_07_08_000000_rename_polls_discussion_id_column_1.php b/migrations/2023_07_08_000000_rename_polls_discussion_id_column_1.php new file mode 100644 index 00000000..5dd87eb4 --- /dev/null +++ b/migrations/2023_07_08_000000_rename_polls_discussion_id_column_1.php @@ -0,0 +1,32 @@ + function (Builder $schema) { + // Do not run this migration if the column was already renamed before the split + if ($schema->hasColumn('polls', 'post_id')) { + return; + } + + $schema->table('polls', function (Blueprint $table) { + $table->renameColumn('discussion_id', 'post_id'); + }); + }, + 'down' => function (Builder $schema) { + $schema->table('polls', function (Blueprint $table) { + $table->renameColumn('post_id', 'discussion_id'); + }); + }, +]; diff --git a/migrations/2023_07_08_000001_update_polls_discussion_relation_to_first_post.php b/migrations/2023_07_08_000001_update_polls_discussion_relation_to_first_post.php index 36d6b4f4..7b9cd77c 100644 --- a/migrations/2023_07_08_000001_update_polls_discussion_relation_to_first_post.php +++ b/migrations/2023_07_08_000001_update_polls_discussion_relation_to_first_post.php @@ -13,6 +13,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Builder; +// Split 1/2 of 2023_07_08_000001_update_polls_discussion_relation_to_first_post.php return [ 'up' => function (Builder $schema) { $db = $schema->getConnection(); @@ -61,10 +62,6 @@ $deletingPolls->delete(); }); - - $schema->table('polls', function (Blueprint $table) { - $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); - }); }, 'down' => function (Builder $schema) { $db = $schema->getConnection(); @@ -82,9 +79,5 @@ ->join('posts', 'polls.post_id', '=', 'posts.id') ->update(['polls.post_id' => $db->raw("{$prefix}posts.discussion_id")]); }); - - $schema->table('polls', function (Blueprint $table) { - $table->dropForeign(['post_id']); - }); }, ]; diff --git a/migrations/2023_07_08_000002_add_polls_post_id_foreign.php b/migrations/2023_07_08_000002_add_polls_post_id_foreign.php new file mode 100644 index 00000000..104374aa --- /dev/null +++ b/migrations/2023_07_08_000002_add_polls_post_id_foreign.php @@ -0,0 +1,39 @@ + function (Builder $schema) { + // Do not run this migration if the foreign key was already added before the split + $db = $schema->getConnection(); + $foreignKeys = $db->getDoctrineSchemaManager()->listTableForeignKeys("{$db->getTablePrefix()}polls"); + + foreach ($foreignKeys as $foreignKey) { + if (in_array('post_id', $foreignKey->getLocalColumns())) { + return; + } + } + + $schema->table('polls', function (Blueprint $table) { + $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); + }); + }, + 'down' => function (Builder $schema) { + $schema->table('polls', function (Blueprint $table) { + $table->dropForeign(['post_id']); + }); + }, +];