FIX: Username update should ignore revisions without raw

FIX: Username update should ignore revisions without raw

diff --git a/app/jobs/regular/update_username.rb b/app/jobs/regular/update_username.rb
index f6138c8..a467e89 100644
--- a/app/jobs/regular/update_username.rb
+++ b/app/jobs/regular/update_username.rb
@@ -31,9 +31,11 @@ module Jobs
 
     def update_revisions
       PostRevision.where(post_conditions("post_revisions.post_id"), post_condition_args).find_each do |revision|
-        revision.modifications["raw"].map! { |raw| update_raw(raw) }
-        revision.modifications["cooked"].map! { |cooked| update_cooked(cooked) }
-        revision.save!
+        if revision.modifications.key?("raw") || revision.modifications.key?("cooked")
+          revision.modifications["raw"]&.map! { |raw| update_raw(raw) }
+          revision.modifications["cooked"]&.map! { |cooked| update_cooked(cooked) }
+          revision.save!
+        end
       end
     end
 
diff --git a/spec/services/username_changer_spec.rb b/spec/services/username_changer_spec.rb
index ad0fac1..c02ea46 100644
--- a/spec/services/username_changer_spec.rb
+++ b/spec/services/username_changer_spec.rb
@@ -218,28 +218,30 @@ describe UsernameChanger do
         end
 
         it 'replaces mentions within revisions' do
-          revisions = [{ raw: "Hello Foo" }, { raw: "Hello @foo!" }, { raw: "Hello @foo!!" }]
+          revisions = [{ raw: "Hello Foo" }, { title: "new topic title" }, { raw: "Hello @foo!" }, { raw: "Hello @foo!!" }]
           post = create_post_and_change_username(raw: "Hello @foo", revisions: revisions)
 
           expect(post.raw).to eq("Hello @bar!!")
           expect(post.cooked).to eq(%Q(<p>Hello <a class="mention" href="/u/bar">@bar</a>!!</p>))
 
-          expect(post.revisions.count).to eq(3)
+          expect(post.revisions.count).to eq(4)
 
           expect(post.revisions[0].modifications["raw"][0]).to eq("Hello @bar")
           expect(post.revisions[0].modifications["raw"][1]).to eq("Hello Foo")
           expect(post.revisions[0].modifications["cooked"][0]).to eq(%Q(<p>Hello <a class="mention" href="/u/bar">@bar</a></p>))
           expect(post.revisions[0].modifications["cooked"][1]).to eq(%Q(<p>Hello Foo</p>))
 
-          expect(post.revisions[1].modifications["raw"][0]).to eq("Hello Foo")
-          expect(post.revisions[1].modifications["raw"][1]).to eq("Hello @bar!")
-          expect(post.revisions[1].modifications["cooked"][0]).to eq(%Q(<p>Hello Foo</p>))
-          expect(post.revisions[1].modifications["cooked"][1]).to eq(%Q(<p>Hello <a class="mention" href="/u/bar">@bar</a>!</p>))
+          expect(post.revisions[1].modifications).to include("title")
 
-          expect(post.revisions[2].modifications["raw"][0]).to eq("Hello @bar!")
-          expect(post.revisions[2].modifications["raw"][1]).to eq("Hello @bar!!")
-          expect(post.revisions[2].modifications["cooked"][0]).to eq(%Q(<p>Hello <a class="mention" href="/u/bar">@bar</a>!</p>))
-          expect(post.revisions[2].modifications["cooked"][1]).to eq(%Q(<p>Hello <a class="mention" href="/u/bar">@bar</a>!!</p>))
+          expect(post.revisions[2].modifications["raw"][0]).to eq("Hello Foo")
+          expect(post.revisions[2].modifications["raw"][1]).to eq("Hello @bar!")
+          expect(post.revisions[2].modifications["cooked"][0]).to eq(%Q(<p>Hello Foo</p>))
+          expect(post.revisions[2].modifications["cooked"][1]).to eq(%Q(<p>Hello <a class="mention" href="/u/bar">@bar</a>!</p>))
+
+          expect(post.revisions[3].modifications["raw"][0]).to eq("Hello @bar!")
+          expect(post.revisions[3].modifications["raw"][1]).to eq("Hello @bar!!")
+          expect(post.revisions[3].modifications["cooked"][0]).to eq(%Q(<p>Hello <a class="mention" href="/u/bar">@bar</a>!</p>))
+          expect(post.revisions[3].modifications["cooked"][1]).to eq(%Q(<p>Hello <a class="mention" href="/u/bar">@bar</a>!!</p>))
         end
 
         it 'replaces mentions in posts marked for deletion' do

GitHub sha: c67c2dc6