From e8e1ee3011ccdb4e95260877cfc0d7372cb75604 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Mon, 6 Sep 2021 16:21:01 +0200 Subject: [PATCH] Prevent creation of duplicated repository cells [SCI-6036] --- app/controllers/api/v1/inventory_items_controller.rb | 3 ++- .../repository_rows/update_repository_row_service.rb | 2 +- ...6132120_add_unique_constraint_to_repository_cell.rb | 10 ++++++++++ db/structure.sql | 10 +++++++++- 4 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20210906132120_add_unique_constraint_to_repository_cell.rb diff --git a/app/controllers/api/v1/inventory_items_controller.rb b/app/controllers/api/v1/inventory_items_controller.rb index 97e4a785c..5e85ec8ec 100644 --- a/app/controllers/api/v1/inventory_items_controller.rb +++ b/app/controllers/api/v1/inventory_items_controller.rb @@ -63,11 +63,12 @@ module Api p.require(%i(id attributes)) p.require(:attributes).require(:value) end - @inventory_item.transaction do + @inventory_item.with_lock do inventory_cells_params.each do |cell_params| cell = @inventory_item.repository_cells.find(cell_params[:id]) cell_value = cell_params.dig(:attributes, :value) next unless cell.value.data_changed?(cell_value) + cell.value.update_data!(cell_value, current_user) item_changed = true end diff --git a/app/services/repository_rows/update_repository_row_service.rb b/app/services/repository_rows/update_repository_row_service.rb index 5354aafd0..317dacd40 100644 --- a/app/services/repository_rows/update_repository_row_service.rb +++ b/app/services/repository_rows/update_repository_row_service.rb @@ -17,7 +17,7 @@ module RepositoryRows def call return self unless valid? - ActiveRecord::Base.transaction do + @repository_row.with_lock do # Update invetory row's cells params[:repository_cells]&.each do |column_id, value| column = @repository_row.repository.repository_columns.find_by(id: column_id) diff --git a/db/migrate/20210906132120_add_unique_constraint_to_repository_cell.rb b/db/migrate/20210906132120_add_unique_constraint_to_repository_cell.rb new file mode 100644 index 000000000..64533aa8d --- /dev/null +++ b/db/migrate/20210906132120_add_unique_constraint_to_repository_cell.rb @@ -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 diff --git a/db/structure.sql b/db/structure.sql index 6db6edefc..f157da574 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -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); +-- +-- 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: - -- @@ -7489,6 +7496,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20210715125349'), ('20210716124649'), ('20210720112050'), -('20210811103123'); +('20210811103123'), +('20210906132120');