Add locking and unique index to repository row assigning [SCI-6591] (#3921)

This commit is contained in:
Alex Kriuchykhin 2022-04-13 10:54:23 +02:00 committed by GitHub
parent 1338d4524f
commit f7f03b19c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 16 deletions

View file

@ -30,6 +30,7 @@ module RepositoryRows
if params[:downstream] == 'true'
@my_module.downstream_modules.each do |downstream_module|
next unless can_assign_my_module_repository_rows?(@user, downstream_module)
unassign_repository_rows_from_my_module(downstream_module)
assign_repository_rows_to_my_module(downstream_module)
end
@ -85,21 +86,23 @@ module RepositoryRows
return [] unless params[:rows_to_assign]
unassigned_rows = @repository.repository_rows
.active
.joins("LEFT OUTER JOIN my_module_repository_rows "\
"ON repository_rows.id = my_module_repository_rows.repository_row_id "\
"AND my_module_repository_rows.my_module_id = #{my_module.id.to_i}")
.where(my_module_repository_rows: { id: nil })
.where(id: @params[:rows_to_assign])
my_module.with_lock do
unassigned_rows = @repository.repository_rows
.active
.joins("LEFT OUTER JOIN my_module_repository_rows "\
"ON repository_rows.id = my_module_repository_rows.repository_row_id "\
"AND my_module_repository_rows.my_module_id = #{my_module.id.to_i}")
.where(my_module_repository_rows: { id: nil })
.where(id: @params[:rows_to_assign])
return [] unless unassigned_rows.any?
return [] unless unassigned_rows.any?
unassigned_rows.find_each do |repository_row|
MyModuleRepositoryRow.create!(my_module: my_module,
repository_row: repository_row,
assigned_by: @user)
assigned_names << repository_row.name
unassigned_rows.find_each do |repository_row|
MyModuleRepositoryRow.create!(my_module: my_module,
repository_row: repository_row,
assigned_by: @user)
assigned_names << repository_row.name
end
end
return [] if assigned_names.blank?

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
class AddUniqueIndexToMyModuleRepositoryRows < ActiveRecord::Migration[6.1]
def change
remove_index :my_module_repository_rows,
%i(my_module_id repository_row_id),
name: 'index_my_module_ids_repository_row_ids'
add_index :my_module_repository_rows,
%i(my_module_id repository_row_id),
name: 'index_my_module_ids_repository_row_ids',
unique: true
end
end

View file

@ -5093,7 +5093,7 @@ CREATE INDEX index_my_module_groups_on_experiment_id ON public.my_module_groups
-- Name: index_my_module_ids_repository_row_ids; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_my_module_ids_repository_row_ids ON public.my_module_repository_rows USING btree (my_module_id, repository_row_id);
CREATE UNIQUE INDEX index_my_module_ids_repository_row_ids ON public.my_module_repository_rows USING btree (my_module_id, repository_row_id);
--
@ -8366,9 +8366,8 @@ INSERT INTO "schema_migrations" (version) VALUES
('20220203122802'),
('20220217104635'),
('20220224153705'),
('20220307120010'),
('20220310105144'),
('20220321122111'),
('20220325101011'),
('20220328164215');