A platform for community discussion. Free, open, simple.
FIX: PG::UniqueViolation for PostTimings in posts:reorder_posts rake task when PostTiming records have no corresponding Post (#35212)
Juan David Martínez Cubillosc9e0ee9649c4
diff --git a/lib/tasks/posts.rake b/lib/tasks/posts.rakeindex 26afa83fc18..349aab8a8d4 100644--- a/lib/tasks/posts.rake+++ b/lib/tasks/posts.rake@@ -427,12 +427,12 @@ task "posts:reorder_posts", [:topic_id] => [:environment] do |_, args|UPDATE#{table} AS xSET- #{column} = p.sort_order * -1+ #{column} = x.#{column} * -1FROMposts AS pINNER JOIN topics t ON t.id = p.topic_id- /*where*/- SQL+ /*where*/+ SQLbuilder.where("p.topic_id = ?", args[:topic_id]) if args[:topic_id]builder.where("p.post_number < 0")@@ -440,14 +440,51 @@ task "posts:reorder_posts", [:topic_id] => [:environment] do |_, args|builder.where("x.#{column} = ABS(p.post_number)")builder.exec- DB.exec <<~SQL+ # Mark orphaned records from the current table as negative so they+ # don't collide (PostTimings) or are mismatched when reordering++ orphan_builder = DB.build <<~SQL+ UPDATE+ #{table} AS x+ SET+ #{column} = x.#{column} * -1+ FROM topics t+ /*where*/+ SQL++ orphan_builder.where("t.id = x.topic_id")+ orphan_builder.where("x.#{column} > 0")+ orphan_builder.where("x.topic_id = ?", args[:topic_id]) if args[:topic_id]+ orphan_builder.where(<<~SQL)+ NOT EXISTS (+ SELECT+ 1+ FROM+ posts p+ WHERE+ p.topic_id = x.topic_id+ AND p.post_number = x.#{column}+ )+ SQL+ orphan_builder.exec++ builder = DB.build <<~SQLUPDATE- #{table}+ #{table} AS xSET- #{column} = #{column} * -1- WHERE- #{column} < 0+ #{column} = p.sort_order+ FROM+ posts AS p+ INNER JOIN topics t ON t.id = p.topic_id+ /*where*/SQL++ builder.where("p.topic_id = ?", args[:topic_id]) if args[:topic_id]+ builder.where("p.post_number < 0")+ builder.where("x.#{column} < 0")+ builder.where("x.topic_id = p.topic_id")+ builder.where("ABS(x.#{column}) = ABS(p.post_number)")+ builder.execendbuilder = DB.build <<~SQLdiff --git a/spec/tasks/posts_spec.rb b/spec/tasks/posts_spec.rbindex 847f4118e98..d5bd2745a1a 100644--- a/spec/tasks/posts_spec.rb+++ b/spec/tasks/posts_spec.rb@@ -76,4 +76,52 @@ RSpec.describe "Post rake tasks" doexpect(post.custom_fields[Post::MISSING_UPLOADS]).to be_nilendend++ describe "posts:reorder_posts" do+ fab!(:topic)++ fab!(:p1) { Fabricate(:post, topic: topic, created_at: 1.day.ago, post_number: 5) }+ fab!(:p2) { Fabricate(:post, topic: topic, created_at: 2.days.ago, post_number: 1) }+ fab!(:p3) { Fabricate(:post, topic: topic, created_at: 3.days.ago, post_number: 3) }++ # PostTimings pointing at existing posts+ fab!(:pt1) do+ PostTiming.create!(topic_id: topic.id, post_number: p1.post_number, user_id: -2, msecs: 111)+ end+ fab!(:pt2) do+ PostTiming.create!(topic_id: topic.id, post_number: p2.post_number, user_id: -2, msecs: 222)+ end+ fab!(:pt3) do+ PostTiming.create!(topic_id: topic.id, post_number: p3.post_number, user_id: -2, msecs: 333)+ end++ # Orphaned PostTiming (no post with this post_number exists in topic)+ # This orphaned PostTiming will cause duplicate key errors if not taken+ # into account when the rake task is run+ fab!(:pt_orphan) do+ PostTiming.create!(topic_id: topic.id, post_number: 2, user_id: -2, msecs: 999)+ end++ it "reorders posts and fixes orphaned PostTimings" do+ invoke_rake_task("posts:reorder_posts")++ expect(topic.posts.order(:created_at).pluck(:post_number)).to eq([1, 2, 3])++ # Orphaned PostTiming should have been negated+ pt_orphan_updated = PostTiming.find_by(topic_id: topic.id, user_id: -2, post_number: -2)+ expect(pt_orphan_updated).to be_present++ p1.reload+ p2.reload+ p3.reload++ pt1_updated = PostTiming.find_by(topic_id: topic.id, user_id: -2, post_number: p1.post_number)+ pt2_updated = PostTiming.find_by(topic_id: topic.id, user_id: -2, post_number: p2.post_number)+ pt3_updated = PostTiming.find_by(topic_id: topic.id, user_id: -2, post_number: p3.post_number)++ expect(pt1_updated).to be_present+ expect(pt2_updated).to be_present+ expect(pt3_updated).to be_present+ end+ endend