[deploy] Add unique indexes to follows - part 7 (#8215)

This commit is contained in:
rhymes 2020-07-06 18:15:56 +02:00 committed by GitHub
parent dc4e52252d
commit 8afc9bb0c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 2 deletions

View file

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

View file

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

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

View file

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