Skip to content

Commit

Permalink
Split the two discussion_id -> post_id migrations into 4 total
Browse files Browse the repository at this point in the history
  • Loading branch information
dsevillamartin committed Jul 26, 2023
1 parent aa4f010 commit 2726554
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of fof/polls.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;

// Split 2/2 of 2023_07_08_000000_rename_polls_discussion_id_column.php
return [
'up' => 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');
});
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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']);
});
},
];
39 changes: 39 additions & 0 deletions migrations/2023_07_08_000002_add_polls_post_id_foreign.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of fof/polls.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Illuminate\Database\Query\JoinClause;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
use Illuminate\Support\Arr;

// Split 2/2 of 2023_07_08_000001_update_polls_discussion_relation_to_first_post.php
return [
'up' => 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']);
});
},
];

0 comments on commit 2726554

Please sign in to comment.