[deploy] Add unique index on articles feed_source_url (#10208)

Co-authored-by: Molly Struve <mollylbs@gmail.com>
This commit is contained in:
rhymes 2020-09-08 21:34:34 +02:00 committed by GitHub
parent 6eed518404
commit 5c7fc79507
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 4 deletions

View file

@ -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/

View file

@ -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

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_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

View file

@ -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) }