A platform for community discussion. Free, open, simple.
Merge pull request #8736 from gschlager/rename_reply_id_column
Gerhard Schlagerab07b945c286
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex 46ff0a334e9..27810f9e718 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -483,7 +483,7 @@ class ApplicationController < ActionController::Basedef post_ids_including_repliespost_ids = params[:post_ids].map(&:to_i)- post_ids |= PostReply.where(post_id: params[:reply_post_ids]).pluck(:reply_id) if params[:reply_post_ids]+ post_ids |= PostReply.where(post_id: params[:reply_post_ids]).pluck(:reply_post_id) if params[:reply_post_ids]post_idsenddiff --git a/app/models/post.rb b/app/models/post.rbindex 5abe6dbd431..db05eb470e4 100644--- a/app/models/post.rb+++ b/app/models/post.rb@@ -817,10 +817,10 @@ class Post < ActiveRecord::BaseWITH RECURSIVE breadcrumb(id, level) AS (SELECT :post_id, 0UNION- SELECT reply_id, level + 1+ SELECT reply_post_id, level + 1FROM post_replies AS rJOIN breadcrumb AS b ON (r.post_id = b.id)- WHERE r.post_id <> r.reply_id+ WHERE r.post_id <> r.reply_post_idAND b.level < :max_reply_level), breadcrumb_with_count AS (SELECT@@ -828,8 +828,8 @@ class Post < ActiveRecord::Baselevel,COUNT(*) AS countFROM post_replies AS r- JOIN breadcrumb AS b ON (r.reply_id = b.id)- WHERE r.reply_id <> r.post_id+ JOIN breadcrumb AS b ON (r.reply_post_id = b.id)+ WHERE r.reply_post_id <> r.post_idGROUP BY id, level)SELECT id, level@@ -1063,7 +1063,7 @@ class Post < ActiveRecord::Basedef create_reply_relationship_with(post)return if post.nil? || self.deleted_at.present?- post_reply = post.post_replies.new(reply_id: id)+ post_reply = post.post_replies.new(reply_post_id: id)if post_reply.saveif Topic.visible_post_types.include?(self.post_type)Post.where(id: post.id).update_all ['reply_count = reply_count + 1']diff --git a/app/models/post_mover.rb b/app/models/post_mover.rbindex facccc8ebd7..4a8148f2971 100644--- a/app/models/post_mover.rb+++ b/app/models/post_mover.rb@@ -250,7 +250,7 @@ class PostMoverFROM (SELECT r.post_id, mp.new_topic_id, COUNT(1) AS moved_reply_countFROM moved_posts mp- JOIN post_replies r ON (mp.old_post_id = r.reply_id)+ JOIN post_replies r ON (mp.old_post_id = r.reply_post_id)GROUP BY r.post_id, mp.new_topic_id) xWHERE x.post_id = p.id AND x.new_topic_id <> p.topic_id@@ -275,7 +275,7 @@ class PostMoverSET post_id = mp.new_post_idFROM moved_posts mpWHERE mp.old_post_id <> mp.new_post_id AND pr.post_id = mp.old_post_id AND- EXISTS (SELECT 1 FROM moved_posts mr WHERE mr.new_post_id = pr.reply_id)+ EXISTS (SELECT 1 FROM moved_posts mr WHERE mr.new_post_id = pr.reply_post_id)SQLend@@ -283,8 +283,8 @@ class PostMoverDB.exec <<~SQLDELETEFROM post_replies pr USING moved_posts mp, posts p, posts r- WHERE (pr.reply_id = mp.old_post_id OR pr.post_id = mp.old_post_id) AND- p.id = pr.post_id AND r.id = pr.reply_id AND p.topic_id <> r.topic_id+ WHERE (pr.reply_post_id = mp.old_post_id OR pr.post_id = mp.old_post_id) AND+ p.id = pr.post_id AND r.id = pr.reply_post_id AND p.topic_id <> r.topic_idSQLenddiff --git a/app/models/post_reply.rb b/app/models/post_reply.rbindex 37654e67537..9cc9be26922 100644--- a/app/models/post_reply.rb+++ b/app/models/post_reply.rb@@ -1,10 +1,14 @@# frozen_string_literal: trueclass PostReply < ActiveRecord::Base+ self.ignored_columns = %w{+ reply_id+ }+belongs_to :post- belongs_to :reply, class_name: 'Post'+ belongs_to :reply, foreign_key: :reply_post_id, class_name: 'Post'- validates_uniqueness_of :reply_id, scope: :post_id+ validates_uniqueness_of :reply_post_id, scope: :post_idvalidate :ensure_same_topicprivate@@ -23,13 +27,13 @@ end## Table name: post_replies#-# post_id :integer-# reply_id :integer-# created_at :datetime not null-# updated_at :datetime not null+# post_id :integer+# created_at :datetime not null+# updated_at :datetime not null+# reply_post_id :integer## Indexes#-# index_post_replies_on_post_id_and_reply_id (post_id,reply_id) UNIQUE-# index_post_replies_on_reply_id (reply_id)+# index_post_replies_on_post_id_and_reply_post_id (post_id,reply_post_id) UNIQUE+# index_post_replies_on_reply_post_id (reply_post_id)#diff --git a/db/migrate/20200116140132_rename_reply_id_column.rb b/db/migrate/20200116140132_rename_reply_id_column.rbnew file mode 100644index 00000000000..c90623aa458--- /dev/null+++ b/db/migrate/20200116140132_rename_reply_id_column.rb@@ -0,0 +1,18 @@+# frozen_string_literal: true++class RenameReplyIdColumn < ActiveRecord::Migration[6.0]+ def up+ Migration::ColumnDropper.mark_readonly(:post_replies, :reply_id)++ add_column :post_replies, :reply_post_id, :integer++ execute <<~SQL+ UPDATE post_replies+ SET reply_post_id = reply_id+ SQL+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/db/migrate/20200117141138_update_post_reply_indexes.rb b/db/migrate/20200117141138_update_post_reply_indexes.rbnew file mode 100644index 00000000000..0ba728edcc7--- /dev/null+++ b/db/migrate/20200117141138_update_post_reply_indexes.rb@@ -0,0 +1,29 @@+# frozen_string_literal: true++class UpdatePostReplyIndexes < ActiveRecord::Migration[6.0]+ # Adding and removing indexes concurrently doesn't work within transaction.+ # It also needs existence checks for indexes in case the migration fails and needs to run again.+ disable_ddl_transaction!++ def up+ if !index_exists?(:post_replies, [:post_id, :reply_post_id])+ add_index :post_replies, [:post_id, :reply_post_id], unique: true, algorithm: :concurrently+ end++ if !index_exists?(:post_replies, [:reply_post_id])+ add_index :post_replies, [:reply_post_id], algorithm: :concurrently+ end++ if index_exists?(:post_replies, [:post_id, :reply_id])+ remove_index :post_replies, column: [:post_id, :reply_id], algorithm: :concurrently+ end++ if index_exists?(:post_replies, [:reply_id])+ remove_index :post_replies, column: [:reply_id], algorithm: :concurrently+ end+ end++ def down+ raise ActiveRecord::IrreversibleMigration+ end+enddiff --git a/lib/email/sender.rb b/lib/email/sender.rbindex 2ae0654eb94..9fab38906b3 100644--- a/lib/email/sender.rb+++ b/lib/email/sender.rb@@ -114,7 +114,7 @@ module Emailreferenced_posts = Post.includes(:incoming_email).joins("INNER JOIN post_replies ON post_replies.post_id = posts.id ")- .where("post_replies.reply_id = ?", post_id)+ .where("post_replies.reply_post_id = ?", post_id).order(id: :desc)referenced_post_message_ids = referenced_posts.map do |referenced_post|diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rbindex 214cb2bbaa1..d53e33915ed 100644--- a/lib/post_destroyer.rb+++ b/lib/post_destroyer.rb@@ -307,10 +307,10 @@ class PostDestroyerenddef remove_associated_replies- post_ids = PostReply.where(reply_id: @post.id).pluck(:post_id)+ post_ids = PostReply.where(reply_post_id: @post.id).pluck(:post_id)if post_ids.present?- PostReply.where(reply_id: @post.id).delete_all+ PostReply.where(reply_post_id: @post.id).delete_allPost.where(id: post_ids).each { |p| p.update_column :reply_count, p.replies.count }endenddiff --git a/lib/tasks/import.rake b/lib/tasks/import.rakeindex d10eea7f031..14212bfb956 100644--- a/lib/tasks/import.rake+++ b/lib/tasks/import.rake@@ -45,7 +45,7 @@ def insert_post_replieslog "Inserting post replies..."DB.exec <<-SQL- INSERT INTO post_replies (post_id, reply_id, created_at, updated_at)+ INSERT INTO post_replies (post_id, reply_post_id, created_at, updated_at)SELECT p2.id, p.id, p.created_at, p.created_atFROM posts pINNER JOIN posts p2 ON p2.post_number = p.reply_to_post_number AND p2.topic_id = p.topic_id@@ -300,7 +300,7 @@ def update_posts# WITH X AS (# SELECT pr.post_id, p.user_id# FROM post_replies pr- # JOIN posts p ON p.id = pr.reply_id+ # JOIN posts p ON p.id = pr.reply_post_id# )# UPDATE posts# SET reply_to_user_id = X.user_iddiff --git a/script/bulk_import/discourse_merger.rb b/script/bulk_import/discourse_merger.rbindex 0bd4f37dfd4..abe45f492d4 100644--- a/script/bulk_import/discourse_merger.rb+++ b/script/bulk_import/discourse_merger.rb@@ -511,7 +511,7 @@ class BulkImport::DiscourseMerger < BulkImport::Baseenddef process_post_reply(post_reply)- post_reply['reply_id'] = post_id_from_imported_id(post_reply['reply_id']) if post_reply['reply_id']+ post_reply['reply_post_id'] = post_id_from_imported_id(post_reply['reply_post_id']) if post_reply['reply_post_id']post_replyenddiff --git a/spec/components/post_destroyer_spec.rb b/spec/components/post_destroyer_spec.rbindex 6de14c80d48..3b370e4dc30 100644--- a/spec/components/post_destroyer_spec.rb+++ b/spec/components/post_destroyer_spec.rb@@ -639,7 +639,7 @@ describe PostDestroyer dodescribe 'with a reply' dofab!(:reply) { Fabricate(:basic_reply, user: coding_horror, topic: post.topic) }- let!(:post_reply) { PostReply.create(post_id: post.id, reply_id: reply.id) }+ let!(:post_reply) { PostReply.create(post_id: post.id, reply_post_id: reply.id) }it 'changes the post count of the topic' dopost.reloaddiff --git a/spec/models/post_mover_spec.rb b/spec/models/post_mover_spec.rbindex 99ea605ba10..7ae87eab6a4 100644--- a/spec/models/post_mover_spec.rb+++ b/spec/models/post_mover_spec.rb@@ -955,10 +955,10 @@ describe PostMover doexpect(topic.highest_post_number).to eq(p4.post_number)# updates replies for posts moved to same topic- expect(PostReply.where(reply_id: p2.id).pluck(:post_id)).to contain_exactly(new_first.id)+ expect(PostReply.where(reply_post_id: p2.id).pluck(:post_id)).to contain_exactly(new_first.id)# leaves replies to the first post of the original topic unchanged- expect(PostReply.where(reply_id: p3.id).pluck(:post_id)).to contain_exactly(p1.id)+ expect(PostReply.where(reply_post_id: p3.id).pluck(:post_id)).to contain_exactly(p1.id)endit "preserves post actions in the new post" dodiff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rbindex 24bc394b549..82990338847 100644--- a/spec/requests/posts_controller_spec.rb+++ b/spec/requests/posts_controller_spec.rb@@ -236,7 +236,7 @@ describe PostsController dodescribe "can delete replies" dobefore do- PostReply.create(post_id: post1.id, reply_id: post2.id)+ PostReply.create(post_id: post1.id, reply_post_id: post2.id)endit "deletes the post and the reply to it" dodiff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 4900b0555d2..9ac9edea019 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -162,7 +162,7 @@ RSpec.describe TopicsController douser = sign_in(moderator)p1 = Fabricate(:post, topic: topic, user: user)p2 = Fabricate(:post, topic: topic, user: user, reply_to_post_number: p1.post_number)- PostReply.create(post_id: p1.id, reply_id: p2.id)+ PostReply.create(post_id: p1.id, reply_post_id: p2.id)post "/t/#{topic.id}/move-posts.json", params: {title: 'new topic title',