From 99b239bb8060edb9fa59cc8a612ca44668c760cc Mon Sep 17 00:00:00 2001 From: rhymes Date: Fri, 4 Sep 2020 21:19:13 +0200 Subject: [PATCH] [deploy] Add unique index on articles canonical_url (#10161) --- app/models/article.rb | 4 ++-- ..._add_unique_index_to_articles_canonical_url.rb | 15 +++++++++++++++ db/schema.rb | 1 + spec/models/article_spec.rb | 2 +- 4 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20200902132326_add_unique_index_to_articles_canonical_url.rb diff --git a/app/models/article.rb b/app/models/article.rb index 367bf7a88..ae723fcc2 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -51,8 +51,8 @@ class Article < ApplicationRecord validates :body_markdown, length: { minimum: 0, allow_nil: false }, uniqueness: { scope: %i[user_id title] } validates :cached_tag_list, length: { maximum: 126 } - validates :canonical_url, url: { allow_blank: true, no_local: true, - schemes: %w[https http] }, uniqueness: { allow_blank: true } + validates :canonical_url, uniqueness: { allow_nil: true } + validates :canonical_url, url: { allow_blank: true, no_local: true, schemes: %w[https http] } validates :feed_source_url, uniqueness: { allow_blank: true } validates :main_image, url: { allow_blank: true, schemes: %w[https http] } validates :main_image_background_hex_color, format: /\A#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\z/ diff --git a/db/migrate/20200902132326_add_unique_index_to_articles_canonical_url.rb b/db/migrate/20200902132326_add_unique_index_to_articles_canonical_url.rb new file mode 100644 index 000000000..52e266202 --- /dev/null +++ b/db/migrate/20200902132326_add_unique_index_to_articles_canonical_url.rb @@ -0,0 +1,15 @@ +class AddUniqueIndexToArticlesCanonicalUrl < ActiveRecord::Migration[6.0] + disable_ddl_transaction! + + def up + unless index_exists?(:articles, :canonical_url, unique: true) + add_index :articles, :canonical_url, unique: true, algorithm: :concurrently + end + end + + def down + if index_exists?(:articles, :canonical_url, unique: true) + remove_index :articles, column: :canonical_url, algorithm: :concurrently + end + end +end diff --git a/db/schema.rb b/db/schema.rb index b1e4ceadb..5c8ec5684 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -154,6 +154,7 @@ ActiveRecord::Schema.define(version: 2020_09_02_204028) do t.string "video_state" t.string "video_thumbnail_url" t.index ["boost_states"], name: "index_articles_on_boost_states", using: :gin + t.index ["canonical_url"], name: "index_articles_on_canonical_url", unique: true t.index ["comment_score"], name: "index_articles_on_comment_score" t.index ["featured_number"], name: "index_articles_on_featured_number" t.index ["feed_source_url"], name: "index_articles_on_feed_source_url" diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index 2fabe6ee1..b7490bd7c 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -37,7 +37,7 @@ RSpec.describe Article, type: :model do it { is_expected.to validate_length_of(:title).is_at_most(128) } it { is_expected.to validate_presence_of(:title) } it { is_expected.to validate_presence_of(:user_id) } - it { is_expected.to validate_uniqueness_of(:canonical_url).allow_blank } + it { is_expected.to validate_uniqueness_of(:canonical_url).allow_nil } it { is_expected.to validate_uniqueness_of(:feed_source_url).allow_blank } it { is_expected.to validate_uniqueness_of(:slug).scoped_to(:user_id) }