From: Tom Hughes Date: Sun, 2 Mar 2025 10:54:39 +0000 (+0000) Subject: Merge remote-tracking branch 'upstream/pull/5667' X-Git-Tag: live~200 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/b8b9c3d1e9e412178426e9ba1b612e14e09f2320?hp=7c3c9618eae9e8d8fdb293b8b82fc1d9ef7752d5 Merge remote-tracking branch 'upstream/pull/5667' --- diff --git a/db/migrate/20250212160355_backfill_note_descriptions.rb b/db/migrate/20250212160355_backfill_note_descriptions.rb new file mode 100644 index 000000000..63568592e --- /dev/null +++ b/db/migrate/20250212160355_backfill_note_descriptions.rb @@ -0,0 +1,30 @@ +class BackfillNoteDescriptions < ActiveRecord::Migration[7.2] + class Note < ApplicationRecord; end + class NoteComment < ApplicationRecord; end + + disable_ddl_transaction! + + def up + Note.in_batches(:of => 1000) do |notes| + note_ids = notes.pluck(:id) + + sql_query = <<-SQL.squish + WITH first_comment AS( + SELECT DISTINCT ON (note_id) * + FROM note_comments + WHERE note_id BETWEEN #{note_ids.min} AND #{note_ids.max} + ORDER BY note_id, id + ) + UPDATE notes + SET description = first_comment.body, + user_id = first_comment.author_id, + user_ip = first_comment.author_ip + FROM first_comment + WHERE first_comment.note_id = notes.id + AND first_comment.event = 'opened'; + SQL + + ActiveRecord::Base.connection.execute(sql_query) + end + end +end diff --git a/db/structure.sql b/db/structure.sql index d23c2d748..1af934e02 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -3429,6 +3429,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('23'), ('22'), ('21'), +('20250212160355'), ('20250206202905'), ('20250121191749'), ('20250105154621'),