From 51b3aea49a5f259bc52d1b32f4577dee21245a73 Mon Sep 17 00:00:00 2001 From: Miha Mencin Date: Wed, 27 Feb 2019 14:13:17 +0100 Subject: [PATCH] added uniquness validator and index on user_projects and user_teams relations finxing typo, fixied failing test fixing mistakenly commited file fixing schema to include bigint migration --- app/models/user_project.rb | 1 + app/models/user_team.rb | 1 + ...90227125306_add_unique_index_on_user_teams.rb | 16 ++++++++++++++++ ...27125352_add_unique_index_on_user_projects.rb | 16 ++++++++++++++++ db/schema.rb | 4 +++- .../client_api/user_team_service_spec.rb | 1 - 6 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20190227125306_add_unique_index_on_user_teams.rb create mode 100644 db/migrate/20190227125352_add_unique_index_on_user_projects.rb diff --git a/app/models/user_project.rb b/app/models/user_project.rb index 36d3c4ce0..cbaeb4ac0 100644 --- a/app/models/user_project.rb +++ b/app/models/user_project.rb @@ -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}") diff --git a/app/models/user_team.rb b/app/models/user_team.rb index fdcd89453..77a1c4320 100644 --- a/app/models/user_team.rb +++ b/app/models/user_team.rb @@ -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}") diff --git a/db/migrate/20190227125306_add_unique_index_on_user_teams.rb b/db/migrate/20190227125306_add_unique_index_on_user_teams.rb new file mode 100644 index 000000000..095cc0e8f --- /dev/null +++ b/db/migrate/20190227125306_add_unique_index_on_user_teams.rb @@ -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 diff --git a/db/migrate/20190227125352_add_unique_index_on_user_projects.rb b/db/migrate/20190227125352_add_unique_index_on_user_projects.rb new file mode 100644 index 000000000..4aad180c4 --- /dev/null +++ b/db/migrate/20190227125352_add_unique_index_on_user_projects.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 86ab1c413..bd5623d77 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: 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 diff --git a/spec/services/client_api/user_team_service_spec.rb b/spec/services/client_api/user_team_service_spec.rb index 7ffd02150..2783efbce 100644 --- a/spec/services/client_api/user_team_service_spec.rb +++ b/spec/services/client_api/user_team_service_spec.rb @@ -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,