Add unique index to subscriptions on subscribable and user and project
This commit is contained in:
parent
731946bad2
commit
bde0202693
5 changed files with 34 additions and 3 deletions
|
@ -5,7 +5,7 @@ class Subscription < ActiveRecord::Base
|
|||
|
||||
validates :user, :project, :subscribable, presence: true
|
||||
|
||||
validates :user_id,
|
||||
uniqueness: { scope: [:subscribable_id, :subscribable_type] },
|
||||
validates :project_id,
|
||||
uniqueness: { scope: [:subscribable_id, :subscribable_type, :user_id] },
|
||||
presence: true
|
||||
end
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
class AddUniqueIndexToSubscriptions < ActiveRecord::Migration
|
||||
include Gitlab::Database::MigrationHelpers
|
||||
|
||||
DOWNTIME = true
|
||||
DOWNTIME_REASON = 'This migration requires downtime because it changes a column to not accept null values.'
|
||||
|
||||
disable_ddl_transaction!
|
||||
|
||||
def up
|
||||
add_concurrent_index :subscriptions, [:subscribable_id, :subscribable_type, :user_id, :project_id], { unique: true, name: 'index_subscriptions_on_subscribable_and_user_id_and_project_id' }
|
||||
remove_index :subscriptions, name: 'subscriptions_user_id_and_ref_fields' if index_name_exists?(:subscriptions, 'subscriptions_user_id_and_ref_fields', false)
|
||||
end
|
||||
|
||||
def down
|
||||
add_concurrent_index :subscriptions, [:subscribable_id, :subscribable_type, :user_id], { unique: true, name: 'subscriptions_user_id_and_ref_fields' }
|
||||
remove_index :subscriptions, name: 'index_subscriptions_on_subscribable_and_user_id_and_project_id' if index_name_exists?(:subscriptions, 'index_subscriptions_on_subscribable_and_user_id_and_project_id', false)
|
||||
end
|
||||
end
|
|
@ -1055,7 +1055,7 @@ ActiveRecord::Schema.define(version: 20161109150329) do
|
|||
t.integer "project_id"
|
||||
end
|
||||
|
||||
add_index "subscriptions", ["subscribable_id", "subscribable_type", "user_id"], name: "subscriptions_user_id_and_ref_fields", unique: true, using: :btree
|
||||
add_index "subscriptions", ["subscribable_id", "subscribable_type", "user_id", "project_id"], name: "index_subscriptions_on_subscribable_and_user_id_and_project_id", unique: true, using: :btree
|
||||
|
||||
create_table "taggings", force: :cascade do |t|
|
||||
t.integer "tag_id"
|
||||
|
|
7
spec/factories/subscriptions.rb
Normal file
7
spec/factories/subscriptions.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
FactoryGirl.define do
|
||||
factory :subscription do
|
||||
user
|
||||
project factory: :empty_project
|
||||
subscribable factory: :issue
|
||||
end
|
||||
end
|
|
@ -11,5 +11,11 @@ describe Subscription, models: true do
|
|||
it { is_expected.to validate_presence_of(:project) }
|
||||
it { is_expected.to validate_presence_of(:subscribable) }
|
||||
it { is_expected.to validate_presence_of(:user) }
|
||||
|
||||
it 'validates uniqueness of project_id scoped to subscribable_id, subscribable_type, and user_id' do
|
||||
create(:subscription)
|
||||
|
||||
expect(subject).to validate_uniqueness_of(:project_id).scoped_to([:subscribable_id, :subscribable_type, :user_id])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue