1 # frozen_string_literal: true
3 class BackfillNoteDescriptions < ActiveRecord::Migration[7.2]
4 class Note < ApplicationRecord; end
5 class NoteComment < ApplicationRecord; end
7 disable_ddl_transaction!
10 Note.in_batches(:of => 1000) do |notes|
11 note_ids = notes.pluck(:id)
13 sql_query = <<-SQL.squish
14 WITH first_comment AS(
15 SELECT DISTINCT ON (note_id) *
17 WHERE note_id BETWEEN #{note_ids.min} AND #{note_ids.max}
21 SET description = first_comment.body,
22 user_id = first_comment.author_id,
23 user_ip = first_comment.author_ip
25 WHERE first_comment.note_id = notes.id
26 AND first_comment.event = 'opened';
29 ActiveRecord::Base.connection.execute(sql_query)