<p></p>
<p><b>@grekko</b> commented on this pull request.</p>

<hr>

<p>In <a href="https://github.com/openstreetmap/openstreetmap-website/pull/4481#discussion_r1481486368">lib/tasks/migrate_notes.rake</a>:</p>
<pre style='color:#555'>> +  desc "Backfills notes-columns `body`, `author_ip` and `author_id`"
+  task :migrate_notes => :environment do
+    scope = Note.where(:body => nil, :author => nil, :author_ip => nil)
+    total_count = scope.count
+    remaining_count = total_count
+    puts "A total of #{total_count} Note-records have to be migrated."
+
+    # NB: default batch size is 1000
+    scope.find_in_batches do |batch|
+      puts "Processing batch of #{batch.size} records."
+      batch.each do |record|
+        opened_comment = record.comments.unscope(:where => :visible).find_by(:event => "opened")
+        (putc "x" && next) unless opened_comment
+
+        attributes = opened_comment.attributes.slice(*%w[body author_id author_ip]).compact_blank
+        record.update_columns(attributes) # rubocop:disable Rails/SkipsModelValidations
</pre>
<blockquote>
<p dir="auto">I think this needs to remove the comment too, right? Otherwise there will be a body, and then the first comment will duplicate the body.</p>
</blockquote>
<p dir="auto">I am hesitant to perform any destructive operations until the very last step, but I open to change my approach here since it would simplify the code.</p>
<p dir="auto"><code class="notranslate">Note</code>-model methods (<code class="notranslate">#body</code>, <code class="notranslate">#author</code>, <code class="notranslate">#author_ip</code>) have been adjusted so that calls to these return the migrated data once its present and otherwise look it up through the <code class="notranslate">opened</code>-NoteComment-record.</p>
<div class="highlight highlight-source-ruby" dir="auto"><pre class="notranslate"><span class="pl-k">class</span> <span class="pl-v">Note</span> < <span class="pl-v">ApplicationRecord</span>
  <span class="pl-c"># FIXME: notes_refactoring remove this once the backfilling is completed</span>
  <span class="pl-k">def</span> <span class="pl-en">body_migrated?</span>
    <span class="pl-en">attributes</span><span class="pl-kos">[</span><span class="pl-s">"body"</span><span class="pl-kos">]</span><span class="pl-kos">.</span><span class="pl-en">present?</span>
  <span class="pl-k">end</span>

  <span class="pl-c"># FIXME: notes_refactoring remove this once the backfilling is completed</span>
  <span class="pl-k">def</span> <span class="pl-en">author</span>
    <span class="pl-k">return</span> <span class="pl-smi">super</span> <span class="pl-k">if</span> <span class="pl-en">body_migrated?</span>

    <span class="pl-en">opened_note_comment</span>&.<span class="pl-en">author</span>
  <span class="pl-k">end</span>

  <span class="pl-c"># FIXME: notes_refactoring remove this once the backfilling is completed</span>
  <span class="pl-k">def</span> <span class="pl-en">author_ip</span>
    <span class="pl-k">return</span> <span class="pl-smi">super</span> <span class="pl-k">if</span> <span class="pl-en">body_migrated?</span>

    <span class="pl-en">opened_note_comment</span>&.<span class="pl-en">author_ip</span>
  <span class="pl-k">end</span>

  <span class="pl-c"># Return the note body</span>
  <span class="pl-k">def</span> <span class="pl-en">body</span>
    <span class="pl-s1">body</span> <span class="pl-c1">=</span> <span class="pl-en">body_migrated?</span> ? <span class="pl-smi">super</span> : <span class="pl-en">opened_note_comment</span>&.<span class="pl-s1">body</span>&.<span class="pl-en">to_s</span>
    <span class="pl-v">RichText</span><span class="pl-kos">.</span><span class="pl-en">new</span><span class="pl-kos">(</span><span class="pl-s">"text"</span><span class="pl-kos">,</span> <span class="pl-s1">body</span><span class="pl-kos">)</span>
  <span class="pl-k">end</span>
<span class="pl-k">end</span></pre></div>
<blockquote>
<p dir="auto">I also think this needs to have tests, and so I would move all the logic into the app (e.g. Note.migrate_bodies, or a standalone class in lib, or somesuch) to make it easier to test.</p>
</blockquote>
<p dir="auto">👍🏽 will add this.</p>

<p style="font-size:small;-webkit-text-size-adjust:none;color:#666;">—<br />Reply to this email directly, <a href="https://github.com/openstreetmap/openstreetmap-website/pull/4481#discussion_r1481486368">view it on GitHub</a>, or <a href="https://github.com/notifications/unsubscribe-auth/AAK2OLMX3QNXCMHCISWTZEDYSN7ONAVCNFSM6AAAAABB4FLGOWVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMYTQNRXHAZTCMZVGA">unsubscribe</a>.<br />You are receiving this because you are subscribed to this thread.<img src="https://github.com/notifications/beacon/AAK2OLJP7AOP4SJJ2KETX7LYSN7ONA5CNFSM6AAAAABB4FLGOWWGG33NNVSW45C7OR4XAZNRKB2WY3CSMVYXKZLTORJGK5TJMV32UY3PNVWWK3TUL5UWJTTPKTMDM.gif" height="1" width="1" alt="" /><span style="color: transparent; font-size: 0; display: none; visibility: hidden; overflow: hidden; opacity: 0; width: 0; height: 0; max-width: 0; max-height: 0; mso-hide: all">Message ID: <span><openstreetmap/openstreetmap-website/pull/4481/review/1867831350</span><span>@</span><span>github</span><span>.</span><span>com></span></span></p>
<script type="application/ld+json">[
{
"@context": "http://schema.org",
"@type": "EmailMessage",
"potentialAction": {
"@type": "ViewAction",
"target": "https://github.com/openstreetmap/openstreetmap-website/pull/4481#discussion_r1481486368",
"url": "https://github.com/openstreetmap/openstreetmap-website/pull/4481#discussion_r1481486368",
"name": "View Pull Request"
},
"description": "View this Pull Request on GitHub",
"publisher": {
"@type": "Organization",
"name": "GitHub",
"url": "https://github.com"
}
}
]</script>