From 701ae8764efa1384a093d6270daed8fc2122320c Mon Sep 17 00:00:00 2001 From: Renato Atilio Date: Thu, 3 Aug 2023 22:04:35 -0300 Subject: [PATCH] FIX: keep first post edit history when moving/merging (#22966) --- app/models/post_mover.rb | 8 +++++++- app/models/post_revision.rb | 11 +++++++++++ spec/models/post_mover_spec.rb | 24 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/models/post_mover.rb b/app/models/post_mover.rb index fd2d4793e89..cfa1720318c 100644 --- a/app/models/post_mover.rb +++ b/app/models/post_mover.rb @@ -260,7 +260,13 @@ class PostMover PostAction.copy(post, new_post) - attrs_to_update = { reply_count: @reply_count[1] || 0 } + PostRevision.copy(post, new_post) + + attrs_to_update = { + reply_count: @reply_count[1] || 0, + version: post.version, + public_version: post.public_version, + } if new_post.post_number != @move_map[post.post_number] attrs_to_update[:post_number] = @move_map[post.post_number] diff --git a/app/models/post_revision.rb b/app/models/post_revision.rb index 91554852383..6a5033cfc67 100644 --- a/app/models/post_revision.rb +++ b/app/models/post_revision.rb @@ -39,6 +39,17 @@ class PostRevision < ActiveRecord::Base def create_notification PostActionNotifier.after_create_post_revision(self) end + + def self.copy(original_post, target_post) + cols_to_copy = (column_names - %w[id post_id]).join(", ") + + DB.exec <<~SQL + INSERT INTO post_revisions(post_id, #{cols_to_copy}) + SELECT #{target_post.id}, #{cols_to_copy} + FROM post_revisions + WHERE post_id = #{original_post.id} + SQL + end end # == Schema Information diff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rb index 1f5a155399a..28186efd54d 100644 --- a/spec/models/post_mover_spec.rb +++ b/spec/models/post_mover_spec.rb @@ -875,6 +875,18 @@ RSpec.describe PostMover do expect(TopicUser.find_by(topic: destination_topic, user: user).liked).to eq(true) end + it "copies the post revisions from first post to the new post" do + p1.revise(another_user, { raw: "A different raw content" }) + + moved_to = topic.move_posts(user, [p1.id], destination_topic_id: destination_topic.id) + new_post = moved_to.posts.last + + expect(new_post.id).not_to eq(p1.id) + expect(new_post.version).to eq(2) + expect(new_post.public_version).to eq(2) + expect(new_post.post_revisions.size).to eq(1) + end + context "with read state and other stats per user" do def create_topic_user(user, topic, opts = {}) notification_level = opts.delete(:notification_level) || :regular @@ -1223,6 +1235,18 @@ RSpec.describe PostMover do expect(new_topic.first_post.custom_fields).to eq(custom_fields) end + it "preserves the post revisions in the new post" do + p1.revise(another_user, { raw: "A different raw content" }) + + new_topic = topic.move_posts(user, [p1.id], title: "new testing topic name") + new_post = new_topic.posts.where(post_number: 1).first + + expect(new_post.id).not_to eq(p1.id) + expect(new_post.version).to eq(2) + expect(new_post.public_version).to eq(2) + expect(new_post.post_revisions.size).to eq(1) + end + include_examples "moves email related stuff" do let!(:old_post) { p1 } end