Add unique index to comments - part 6 (#8214)

This commit is contained in:
rhymes 2020-06-02 11:30:43 +02:00 committed by GitHub
parent fd639c2c15
commit 948e984ef3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 3 deletions

View file

@ -9,7 +9,6 @@ class Comment < ApplicationRecord
SEARCH_CLASS = Search::FeedContent
BODY_MARKDOWN_SIZE_RANGE = (1..25_000).freeze
BODY_MARKDOWN_UNIQUENESS_SCOPES = %i[user_id ancestry commentable_id commentable_type].freeze
COMMENTABLE_TYPES = %w[Article PodcastEpisode].freeze
TITLE_DELETED = "[deleted]".freeze
TITLE_HIDDEN = "[hidden by post author]".freeze
@ -25,7 +24,7 @@ class Comment < ApplicationRecord
before_validation :evaluate_markdown, if: -> { body_markdown }
validate :permissions, if: :commentable
validates :body_markdown, presence: true, length: { in: BODY_MARKDOWN_SIZE_RANGE }
validates :body_markdown, uniqueness: { scope: BODY_MARKDOWN_UNIQUENESS_SCOPES }
validates :body_markdown, uniqueness: { scope: %i[user_id ancestry commentable_id commentable_type] }
validates :commentable_id, presence: true
validates :commentable_type, inclusion: { in: COMMENTABLE_TYPES }
validates :user_id, presence: true

View file

@ -0,0 +1,13 @@
class AddUniqueIndexesToCommentsBodyMarkdownUserIdAncestryCommentable < ActiveRecord::Migration[5.2]
disable_ddl_transaction!
def change
add_index(
:comments,
%i[body_markdown user_id ancestry commentable_id commentable_type],
unique: true,
algorithm: :concurrently,
name: :index_comments_on_body_markdown_user_id_ancestry_commentable
)
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2020_05_30_084533) do
ActiveRecord::Schema.define(version: 2020_06_01_121243) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -393,6 +393,7 @@ ActiveRecord::Schema.define(version: 2020_05_30_084533) do
t.datetime "updated_at", null: false
t.integer "user_id"
t.index ["ancestry"], name: "index_comments_on_ancestry"
t.index ["body_markdown", "user_id", "ancestry", "commentable_id", "commentable_type"], name: "index_comments_on_body_markdown_user_id_ancestry_commentable", unique: true
t.index ["commentable_id", "commentable_type"], name: "index_comments_on_commentable_id_and_commentable_type"
t.index ["created_at"], name: "index_comments_on_created_at"
t.index ["score"], name: "index_comments_on_score"