]> git.openstreetmap.org Git - rails.git/blob - db/migrate/20250212160355_backfill_note_descriptions.rb
Add frozen_string_literal comments to ruby files
[rails.git] / db / migrate / 20250212160355_backfill_note_descriptions.rb
1 # frozen_string_literal: true
2
3 class BackfillNoteDescriptions < ActiveRecord::Migration[7.2]
4   class Note < ApplicationRecord; end
5   class NoteComment < ApplicationRecord; end
6
7   disable_ddl_transaction!
8
9   def up
10     Note.in_batches(:of => 1000) do |notes|
11       note_ids = notes.pluck(:id)
12
13       sql_query = <<-SQL.squish
14         WITH first_comment AS(
15           SELECT DISTINCT ON (note_id) *
16           FROM note_comments
17           WHERE note_id BETWEEN #{note_ids.min} AND #{note_ids.max}
18           ORDER BY note_id, id
19         )
20         UPDATE notes
21         SET description = first_comment.body,
22             user_id = first_comment.author_id,
23             user_ip = first_comment.author_ip
24         FROM first_comment
25         WHERE first_comment.note_id = notes.id
26           AND first_comment.event = 'opened';
27       SQL
28
29       ActiveRecord::Base.connection.execute(sql_query)
30     end
31   end
32 end