Skip to content

Commit 14aa95d

Browse files
authored
Add migration script to fix misplaced comment events (#2487)
Finds comment events that are on the wrong board (after a card move) and updates them to the correct board.
1 parent d611b2c commit 14aa95d

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Fix comment events that are on the wrong board after a card move.
2+
#
3+
# See https://github.com/basecamp/fizzy/pull/2486
4+
#
5+
# Usage:
6+
# bin/rails runner script/migrations/20260204-fix-misplaced-comment-events.rb # dry run
7+
# bin/rails runner script/migrations/20260204-fix-misplaced-comment-events.rb --fix # actually fix
8+
#
9+
dry_run = !ARGV.include?("--fix")
10+
11+
puts dry_run ? "DRY RUN - no changes will be made\n\n" : "FIXING misplaced events\n\n"
12+
13+
misplaced_events = Event
14+
.where(eventable_type: "Comment")
15+
.joins("INNER JOIN comments ON comments.id = events.eventable_id")
16+
.joins("INNER JOIN cards ON cards.id = comments.card_id")
17+
.where("events.board_id != cards.board_id")
18+
19+
total = misplaced_events.count
20+
puts "Found #{total} misplaced comment events\n\n"
21+
22+
if total.zero?
23+
puts "Nothing to fix!"
24+
exit
25+
end
26+
27+
fixed = 0
28+
skipped = 0
29+
30+
misplaced_events.find_each.with_index do |event, index|
31+
comment = event.eventable
32+
card = comment&.card
33+
old_board = event.board
34+
new_board = card&.board
35+
36+
puts "[#{index + 1}/#{total}] Event #{event.id}"
37+
38+
if card.nil? || new_board.nil?
39+
puts " Skipping - orphaned data (comment or card deleted)"
40+
skipped += 1
41+
puts
42+
next
43+
end
44+
45+
puts " Card ##{card.number}: #{card.title.truncate(40)}"
46+
puts " Moving from board '#{old_board&.name || 'nil'}' to '#{new_board.name}'"
47+
48+
if dry_run
49+
puts " (skipped - dry run)"
50+
else
51+
event.update!(board: new_board)
52+
fixed += 1
53+
puts " Fixed!"
54+
end
55+
56+
puts
57+
end
58+
59+
puts "Done. #{dry_run ? "Run with --fix to apply changes." : "Fixed #{fixed} events."} (#{skipped} skipped)"

0 commit comments

Comments
 (0)