From 5c7fc79507970ab49b30ab2f9d3c2c0979dbaa41 Mon Sep 17 00:00:00 2001 From: rhymes Date: Tue, 8 Sep 2020 21:34:34 +0200 Subject: [PATCH] [deploy] Add unique index on articles feed_source_url (#10208) Co-authored-by: Molly Struve --- app/models/article.rb | 3 ++- ...nique_index_to_articles_feed_source_url.rb | 23 +++++++++++++++++++ db/schema.rb | 4 ++-- spec/models/article_spec.rb | 2 +- 4 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20200904151734_add_unique_index_to_articles_feed_source_url.rb diff --git a/app/models/article.rb b/app/models/article.rb index ae723fcc2..673a6c435 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -53,7 +53,8 @@ class Article < ApplicationRecord validates :cached_tag_list, length: { maximum: 126 } 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 :feed_source_url, uniqueness: { allow_nil: true } + validates :feed_source_url, url: { allow_blank: true, no_local: true, schemes: %w[https http] } 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/ validates :slug, presence: { if: :published? }, format: /\A[0-9a-z\-_]*\z/ diff --git a/db/migrate/20200904151734_add_unique_index_to_articles_feed_source_url.rb b/db/migrate/20200904151734_add_unique_index_to_articles_feed_source_url.rb new file mode 100644 index 000000000..68ae45043 --- /dev/null +++ b/db/migrate/20200904151734_add_unique_index_to_articles_feed_source_url.rb @@ -0,0 +1,23 @@ +class AddUniqueIndexToArticlesFeedSourceUrl < ActiveRecord::Migration[6.0] + disable_ddl_transaction! + + def up + # at this point in time the articles table has a non unique index on feed_source_url, + # we must replace that with a unique index + if index_exists?(:articles, :feed_source_url) + remove_index :articles, column: :feed_source_url, algorithm: :concurrently + end + + unless index_exists?(:articles, :feed_source_url, unique: true) + add_index :articles, :feed_source_url, unique: true, algorithm: :concurrently + end + end + + def down + if index_exists?(:articles, :feed_source_url, unique: true) + remove_index :articles, column: :feed_source_url, algorithm: :concurrently + end + + add_index :articles, :feed_source_url, algorithm: :concurrently + end +end diff --git a/db/schema.rb b/db/schema.rb index 5c8ec5684..4113212f3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_09_02_204028) do +ActiveRecord::Schema.define(version: 2020_09_04_151734) do # These are extensions that must be enabled in order to support this database enable_extension "citext" @@ -157,7 +157,7 @@ ActiveRecord::Schema.define(version: 2020_09_02_204028) do 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" + t.index ["feed_source_url"], name: "index_articles_on_feed_source_url", unique: true t.index ["hotness_score"], name: "index_articles_on_hotness_score" t.index ["path"], name: "index_articles_on_path" t.index ["public_reactions_count"], name: "index_articles_on_public_reactions_count", order: :desc diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb index b7490bd7c..073c6473d 100644 --- a/spec/models/article_spec.rb +++ b/spec/models/article_spec.rb @@ -38,7 +38,7 @@ RSpec.describe Article, type: :model do 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_nil } - it { is_expected.to validate_uniqueness_of(:feed_source_url).allow_blank } + it { is_expected.to validate_uniqueness_of(:feed_source_url).allow_nil } it { is_expected.to validate_uniqueness_of(:slug).scoped_to(:user_id) } it { is_expected.not_to allow_value("foo").for(:main_image_background_hex_color) }