Merge pull request #3528 from okriuchykhin/ok_SCI_6036

Prevent creation of duplicated repository cells [SCI-6036]
This commit is contained in:
Alex Kriuchykhin 2021-09-17 16:37:48 +02:00 committed by GitHub
commit 0db9bf46ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 3 deletions

View file

@ -63,11 +63,12 @@ module Api
p.require(%i(id attributes)) p.require(%i(id attributes))
p.require(:attributes).require(:value) p.require(:attributes).require(:value)
end end
@inventory_item.transaction do @inventory_item.with_lock do
inventory_cells_params.each do |cell_params| inventory_cells_params.each do |cell_params|
cell = @inventory_item.repository_cells.find(cell_params[:id]) cell = @inventory_item.repository_cells.find(cell_params[:id])
cell_value = cell_params.dig(:attributes, :value) cell_value = cell_params.dig(:attributes, :value)
next unless cell.value.data_changed?(cell_value) next unless cell.value.data_changed?(cell_value)
cell.value.update_data!(cell_value, current_user) cell.value.update_data!(cell_value, current_user)
item_changed = true item_changed = true
end end

View file

@ -17,7 +17,7 @@ module RepositoryRows
def call def call
return self unless valid? return self unless valid?
ActiveRecord::Base.transaction do @repository_row.with_lock do
# Update invetory row's cells # Update invetory row's cells
params[:repository_cells]&.each do |column_id, value| params[:repository_cells]&.each do |column_id, value|
column = @repository_row.repository.repository_columns.find_by(id: column_id) column = @repository_row.repository.repository_columns.find_by(id: column_id)

View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
class AddUniqueConstraintToRepositoryCell < ActiveRecord::Migration[6.1]
def change
add_index :repository_cells,
%i(repository_row_id repository_column_id),
name: 'index_repository_cells_on_repository_row_and_repository_column',
unique: true
end
end

View file

@ -5210,6 +5210,13 @@ CREATE INDEX index_repository_asset_values_on_last_modified_by_id ON public.repo
CREATE INDEX index_repository_cells_on_repository_column_id ON public.repository_cells USING btree (repository_column_id); CREATE INDEX index_repository_cells_on_repository_column_id ON public.repository_cells USING btree (repository_column_id);
--
-- Name: index_repository_cells_on_repository_row_and_repository_column; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX index_repository_cells_on_repository_row_and_repository_column ON public.repository_cells USING btree (repository_row_id, repository_column_id);
-- --
-- Name: index_repository_cells_on_repository_row_id; Type: INDEX; Schema: public; Owner: - -- Name: index_repository_cells_on_repository_row_id; Type: INDEX; Schema: public; Owner: -
-- --
@ -7489,6 +7496,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20210715125349'), ('20210715125349'),
('20210716124649'), ('20210716124649'),
('20210720112050'), ('20210720112050'),
('20210811103123'); ('20210811103123'),
('20210906132120');