Add uniq index for Repository and Team in TeamRepositories

This commit is contained in:
Urban Rotnik 2019-07-15 17:17:26 +02:00
parent 5456c1e38a
commit 1a77702fa7
4 changed files with 13 additions and 1 deletions

View file

@ -7,4 +7,5 @@ class TeamRepository < ApplicationRecord
belongs_to :repository
validates :permission_level, presence: true
validates :repository, uniqueness: { scope: :team_id }
end

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddUniqueIndexToTeamRepositories < ActiveRecord::Migration[5.2]
def change
add_index :team_repositories, %i(team_id repository_id), unique: true
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: 2019_07_11_125513) do
ActiveRecord::Schema.define(version: 2019_07_15_150326) do
# These are extensions that must be enabled in order to support this database
enable_extension "btree_gist"
@ -725,6 +725,7 @@ ActiveRecord::Schema.define(version: 2019_07_11_125513) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["repository_id"], name: "index_team_repositories_on_repository_id"
t.index ["team_id", "repository_id"], name: "index_team_repositories_on_team_id_and_repository_id", unique: true
t.index ["team_id"], name: "index_team_repositories_on_team_id"
end

View file

@ -13,6 +13,9 @@ describe TeamRepository, type: :model do
describe '#permission_level' do
it { is_expected.to validate_presence_of(:permission_level) }
end
describe '#repository' do
it { expect(team_repository).to validate_uniqueness_of(:repository).scoped_to(:team_id) }
end
end
describe 'Associations' do