Merge pull request #1532 from biosistemika/mm-sci-3031-remove-duplicated-relations

added uniquness validator and index on user_projects and user_teams r…
This commit is contained in:
Miha Mencin 2019-02-27 19:53:25 +01:00 committed by GitHub
commit 576520659c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 2 deletions

View file

@ -13,6 +13,7 @@ class UserProject < ApplicationRecord
belongs_to :project, inverse_of: :user_projects, touch: true, optional: true
before_destroy :destroy_associations
validates_uniqueness_of :user_id, scope: :project_id
def role_str
I18n.t("user_projects.enums.role.#{role.to_s}")

View file

@ -14,6 +14,7 @@ class UserTeam < ApplicationRecord
before_destroy :destroy_associations
after_create :create_samples_table_state
validates_uniqueness_of :user_id, scope: :team_id
def role_str
I18n.t("user_teams.enums.role.#{role}")

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true
class AddUniqueIndexOnUserTeams < ActiveRecord::Migration[5.1]
def up
# firstly delete the duplicates
execute 'WITH uniq AS
(SELECT DISTINCT ON (user_id, team_id) * FROM user_teams)
DELETE FROM user_teams WHERE user_teams.id NOT IN
(SELECT id FROM uniq)'
add_index :user_teams, %i(user_id team_id), unique: true
end
def down
remove_index :user_teams, column: %i(user_id team_id)
end
end

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true
class AddUniqueIndexOnUserProjects < ActiveRecord::Migration[5.1]
def up
# firstly delete the duplicates
execute 'WITH uniq AS
(SELECT DISTINCT ON (user_id, project_id) * FROM user_projects)
DELETE FROM user_projects WHERE user_projects.id NOT IN
(SELECT id FROM uniq)'
add_index :user_projects, %i(user_id project_id), unique: true
end
def down
remove_index :user_projects, columns: %i(user_id project_id)
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: 20190125123107) do
ActiveRecord::Schema.define(version: 20190227125352) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -796,6 +796,7 @@ ActiveRecord::Schema.define(version: 20190125123107) do
t.bigint "assigned_by_id"
t.index ["assigned_by_id"], name: "index_user_projects_on_assigned_by_id"
t.index ["project_id"], name: "index_user_projects_on_project_id"
t.index ["user_id", "project_id"], name: "index_user_projects_on_user_id_and_project_id", unique: true
t.index ["user_id"], name: "index_user_projects_on_user_id"
end
@ -821,6 +822,7 @@ ActiveRecord::Schema.define(version: 20190125123107) do
t.bigint "assigned_by_id"
t.index ["assigned_by_id"], name: "index_user_teams_on_assigned_by_id"
t.index ["team_id"], name: "index_user_teams_on_team_id"
t.index ["user_id", "team_id"], name: "index_user_teams_on_user_id_and_team_id", unique: true
t.index ["user_id"], name: "index_user_teams_on_user_id"
end

View file

@ -68,7 +68,6 @@ describe ClientApi::UserTeamService do
describe '#update_role!' do
it 'should raise ClientApi::CustomUserTeamError if no role is set' do
create :user_team, team: team_one, user: user_one
ut_service = ClientApi::UserTeamService.new(
user: user_one,
team_id: team_one.id,