|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of fof/polls. |
| 5 | + * |
| 6 | + * Copyright (c) FriendsOfFlarum. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +use Illuminate\Database\Query\JoinClause; |
| 13 | +use Illuminate\Database\Schema\Blueprint; |
| 14 | +use Illuminate\Database\Schema\Builder; |
| 15 | + |
| 16 | +// Split 1/2 of 2023_07_08_000001_update_polls_discussion_relation_to_first_post.php |
| 17 | +return [ |
| 18 | + 'up' => function (Builder $schema) { |
| 19 | + $db = $schema->getConnection(); |
| 20 | + |
| 21 | + if ($db->table('migrations')->where('migration', '2023_07_08_000001_update_polls_discussion_relation_to_first_post')->exists()) { |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + $db->transaction(function () use ($db) { |
| 26 | + $prefix = $db->getTablePrefix(); |
| 27 | + |
| 28 | + // Don't run through this step if no rows exist in the polls table |
| 29 | + if (!$db->table('polls')->exists()) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // Update polls whose discussions have a clear first post ID associated |
| 34 | + $db->table('polls') |
| 35 | + ->join('discussions', function (JoinClause $join) { |
| 36 | + $join->on('polls.post_id', '=', 'discussions.id') |
| 37 | + ->where('discussions.first_post_id', '!=', null); |
| 38 | + }) |
| 39 | + ->update(['polls.post_id' => $db->raw("{$prefix}discussions.first_post_id")]); |
| 40 | + |
| 41 | + // Update polls whose discussions have a null first post ID associated |
| 42 | + $firstPosts = $db->table('posts') |
| 43 | + ->where('number', '=', 1); |
| 44 | + |
| 45 | + $db->table('polls') |
| 46 | + ->join('discussions', function (JoinClause $join) { |
| 47 | + $join->on('polls.post_id', '=', 'discussions.id') |
| 48 | + ->where('discussions.first_post_id', '=', null); |
| 49 | + }) |
| 50 | + ->leftJoinSub($firstPosts, 'first_posts', function (JoinClause $join) { |
| 51 | + $join->on('first_posts.discussion_id', '=', 'discussions.id'); |
| 52 | + }) |
| 53 | + ->update(['polls.post_id' => $db->raw("{$prefix}first_posts.id")]); |
| 54 | + |
| 55 | + // Delete polls that don't have an associated post |
| 56 | + $deletingPolls = $db->table('polls') |
| 57 | + ->where('post_id', 0); |
| 58 | + $count = $deletingPolls->count(); |
| 59 | + |
| 60 | + if ($count > 0) { |
| 61 | + resolve('log')->warning("[fof/polls] deleting {$deletingPolls->count()} polls with no associated post"); |
| 62 | + resolve('log')->warning("[fof/polls] |> #{$deletingPolls->pluck('id')->join(', #')}"); |
| 63 | + } else { |
| 64 | + resolve('log')->info('[fof/polls] no polls to delete in v2 migration'); |
| 65 | + } |
| 66 | + |
| 67 | + $deletingPolls->delete(); |
| 68 | + }); |
| 69 | + }, |
| 70 | + 'down' => function (Builder $schema) { |
| 71 | + $db = $schema->getConnection(); |
| 72 | + |
| 73 | + $db->transaction(function () use ($db) { |
| 74 | + $prefix = $db->getTablePrefix(); |
| 75 | + |
| 76 | + // Don't run through this step if no rows exist in the polls table |
| 77 | + if (!$db->table('polls')->exists()) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + // Go back to using discussion IDs. The discussion ID will always exist since the posts' foreign key cascades on delete. |
| 82 | + $db->table('polls') |
| 83 | + ->join('posts', 'polls.post_id', '=', 'posts.id') |
| 84 | + ->update(['polls.post_id' => $db->raw("{$prefix}posts.discussion_id")]); |
| 85 | + }); |
| 86 | + }, |
| 87 | +]; |
0 commit comments