From 8afc9bb0c349054727b0f158654eeb6c1dcba7a9 Mon Sep 17 00:00:00 2001 From: rhymes Date: Mon, 6 Jul 2020 18:15:56 +0200 Subject: [PATCH] [deploy] Add unique indexes to follows - part 7 (#8215) --- app/models/follow.rb | 2 +- ...d_unique_index_to_follows_followable_follower.rb | 13 +++++++++++++ db/schema.rb | 3 ++- spec/models/follow_spec.rb | 3 +++ 4 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20200702143618_add_unique_index_to_follows_followable_follower.rb diff --git a/app/models/follow.rb b/app/models/follow.rb index 4cfe43545..826d9fa1c 100644 --- a/app/models/follow.rb +++ b/app/models/follow.rb @@ -34,7 +34,7 @@ class Follow < ApplicationRecord after_create_commit :create_chat_channel before_destroy :modify_chat_channel_status - validates :followable_id, uniqueness: { scope: %i[followable_type follower_id] } + validates :followable_id, uniqueness: { scope: %i[followable_type follower_id follower_type] } validates :subscription_status, inclusion: { in: %w[all_articles none] } def self.need_new_follower_notification_for?(followable_type) diff --git a/db/migrate/20200702143618_add_unique_index_to_follows_followable_follower.rb b/db/migrate/20200702143618_add_unique_index_to_follows_followable_follower.rb new file mode 100644 index 000000000..23042d4a7 --- /dev/null +++ b/db/migrate/20200702143618_add_unique_index_to_follows_followable_follower.rb @@ -0,0 +1,13 @@ +class AddUniqueIndexToFollowsFollowableFollower < ActiveRecord::Migration[6.0] + disable_ddl_transaction! + + def change + add_index( + :follows, + %i[followable_id followable_type follower_id follower_type], + unique: true, + algorithm: :concurrently, + name: :index_follows_on_followable_and_follower + ) + end +end diff --git a/db/schema.rb b/db/schema.rb index 775e0fa1d..dbe66b4ca 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_06_18_212422) do +ActiveRecord::Schema.define(version: 2020_07_02_143618) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -564,6 +564,7 @@ ActiveRecord::Schema.define(version: 2020_06_18_212422) do t.string "subscription_status", default: "all_articles", null: false t.datetime "updated_at" t.index ["created_at"], name: "index_follows_on_created_at" + t.index ["followable_id", "followable_type", "follower_id", "follower_type"], name: "index_follows_on_followable_and_follower", unique: true t.index ["followable_id", "followable_type"], name: "fk_followables" t.index ["follower_id", "follower_type"], name: "fk_follows" end diff --git a/spec/models/follow_spec.rb b/spec/models/follow_spec.rb index 39a2e89f4..1502fbf75 100644 --- a/spec/models/follow_spec.rb +++ b/spec/models/follow_spec.rb @@ -5,7 +5,10 @@ RSpec.describe Follow, type: :model do let(:user_2) { create(:user) } describe "validations" do + subject { user.follow(user_2) } + it { is_expected.to validate_inclusion_of(:subscription_status).in_array(%w[all_articles none]) } + it { is_expected.to validate_uniqueness_of(:followable_id).scoped_to(%i[followable_type follower_id follower_type]) } end it "follows user" do