]> git.openstreetmap.org Git - rails.git/commitdiff
Copies description, user_id and user_ip to notes
authorNenad Vujicic <nenadus@gmail.com>
Mon, 20 Jan 2025 11:58:51 +0000 (12:58 +0100)
committerNenad Vujicic <nenadus@gmail.com>
Mon, 17 Feb 2025 09:45:55 +0000 (10:45 +0100)
Adds migration script for copying description, user_id and user_ip from first note's comment to notes table.

db/migrate/20250212160355_backfill_note_descriptions.rb [new file with mode: 0644]
db/structure.sql

diff --git a/db/migrate/20250212160355_backfill_note_descriptions.rb b/db/migrate/20250212160355_backfill_note_descriptions.rb
new file mode 100644 (file)
index 0000000..6356859
--- /dev/null
@@ -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
index d23c2d7487802486884652bc258e904134844831..1af934e0290add3a3b660353e867148affd02a17 100644 (file)
@@ -3429,6 +3429,7 @@ INSERT INTO "schema_migrations" (version) VALUES
 ('23'),
 ('22'),
 ('21'),
+('20250212160355'),
 ('20250206202905'),
 ('20250121191749'),
 ('20250105154621'),