mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-12-29 19:51:01 +08:00
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:
commit
576520659c
6 changed files with 37 additions and 2 deletions
|
@ -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}")
|
||||
|
|
|
@ -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}")
|
||||
|
|
16
db/migrate/20190227125306_add_unique_index_on_user_teams.rb
Normal file
16
db/migrate/20190227125306_add_unique_index_on_user_teams.rb
Normal 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
|
|
@ -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
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue