From 774aaf4fd899bf7d82e39096ea0670d99d36c669 Mon Sep 17 00:00:00 2001 From: cyrillefr Date: Tue, 18 Feb 2025 14:59:23 +0100 Subject: [PATCH] Fixes Rails/UniqueValidationWithoutIndex (part of #11482) - When you define a uniqueness validation in Active Record model, you also should add a unique index for the column. - Cf. https://docs.rubocop.org/rubocop-rails/cops_rails.html#railsuniquevalidationwithoutindex - Therefore : migration files to match DB structure and Ruby code. --- .rubocop_todo.yml | 5 +---- ...930_add_unique_index_email_entreprise_to_customers.rb | 8 ++++++++ ...add_unique_index_sender_id_and_others_to_exchanges.rb | 9 +++++++++ ...que_index_variant_id_and_deleted_at_to_stock_items.rb | 8 ++++++++ ...unique_index_name_and_deleted_at_to_tax_categories.rb | 7 +++++++ .../20250215091227_add_unique_index_name_to_zones.rb | 7 +++++++ 6 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20250214192930_add_unique_index_email_entreprise_to_customers.rb create mode 100644 db/migrate/20250214200526_add_unique_index_sender_id_and_others_to_exchanges.rb create mode 100644 db/migrate/20250215083320_add_unique_index_variant_id_and_deleted_at_to_stock_items.rb create mode 100644 db/migrate/20250215085838_add_unique_index_name_and_deleted_at_to_tax_categories.rb create mode 100644 db/migrate/20250215091227_add_unique_index_name_to_zones.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f778f90e75..b5f404f525 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -63,7 +63,6 @@ Metrics/ClassLength: - 'app/controllers/spree/admin/payment_methods_controller.rb' - 'app/controllers/spree/admin/payments_controller.rb' - 'app/controllers/spree/admin/products_controller.rb' - - 'app/controllers/spree/admin/users_controller.rb' - 'app/controllers/spree/orders_controller.rb' - 'app/models/enterprise.rb' - 'app/models/invoice/data_presenter.rb' @@ -149,7 +148,7 @@ Metrics/MethodLength: - 'lib/spree/localized_number.rb' - 'lib/tasks/sample_data/product_factory.rb' -# Offense count: 49 +# Offense count: 47 # Configuration parameters: CountComments, Max, CountAsOne. Metrics/ModuleLength: Exclude: @@ -176,12 +175,10 @@ Metrics/ModuleLength: - 'spec/controllers/admin/order_cycles_controller_spec.rb' - 'spec/controllers/api/v0/order_cycles_controller_spec.rb' - 'spec/controllers/api/v0/orders_controller_spec.rb' - - 'spec/controllers/payment_gateways/stripe_controller_spec.rb' - 'spec/controllers/spree/admin/adjustments_controller_spec.rb' - 'spec/controllers/spree/admin/payment_methods_controller_spec.rb' - 'spec/controllers/spree/admin/variants_controller_spec.rb' - 'spec/lib/open_food_network/address_finder_spec.rb' - - 'spec/lib/open_food_network/enterprise_fee_calculator_spec.rb' - 'spec/lib/open_food_network/order_cycle_form_applicator_spec.rb' - 'spec/lib/open_food_network/order_cycle_permissions_spec.rb' - 'spec/lib/open_food_network/permissions_spec.rb' diff --git a/db/migrate/20250214192930_add_unique_index_email_entreprise_to_customers.rb b/db/migrate/20250214192930_add_unique_index_email_entreprise_to_customers.rb new file mode 100644 index 0000000000..c41d4baece --- /dev/null +++ b/db/migrate/20250214192930_add_unique_index_email_entreprise_to_customers.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class AddUniqueIndexEmailEntrepriseToCustomers < ActiveRecord::Migration[7.0] + def change + remove_index :customers, :email, name: :index_customers_on_email + add_index(:customers, [:email, :enterprise_id], unique: true) + end +end diff --git a/db/migrate/20250214200526_add_unique_index_sender_id_and_others_to_exchanges.rb b/db/migrate/20250214200526_add_unique_index_sender_id_and_others_to_exchanges.rb new file mode 100644 index 0000000000..5f3e072129 --- /dev/null +++ b/db/migrate/20250214200526_add_unique_index_sender_id_and_others_to_exchanges.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class AddUniqueIndexSenderIdAndOthersToExchanges < ActiveRecord::Migration[7.0] + def change + remove_index :exchanges, :sender_id, name: :index_exchanges_on_sender_id + add_index(:exchanges, [:sender_id, :order_cycle_id, :receiver_id, :incoming], + unique: true, name: :index_exchanges_on_sender_id) + end +end diff --git a/db/migrate/20250215083320_add_unique_index_variant_id_and_deleted_at_to_stock_items.rb b/db/migrate/20250215083320_add_unique_index_variant_id_and_deleted_at_to_stock_items.rb new file mode 100644 index 0000000000..667643d2dd --- /dev/null +++ b/db/migrate/20250215083320_add_unique_index_variant_id_and_deleted_at_to_stock_items.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class AddUniqueIndexVariantIdAndDeletedAtToStockItems < ActiveRecord::Migration[7.0] + def change + remove_index :spree_stock_items, :variant_id, name: :index_spree_stock_items_on_variant_id + add_index(:spree_stock_items, [:variant_id, :deleted_at], unique: true) + end +end diff --git a/db/migrate/20250215085838_add_unique_index_name_and_deleted_at_to_tax_categories.rb b/db/migrate/20250215085838_add_unique_index_name_and_deleted_at_to_tax_categories.rb new file mode 100644 index 0000000000..06b34970fa --- /dev/null +++ b/db/migrate/20250215085838_add_unique_index_name_and_deleted_at_to_tax_categories.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddUniqueIndexNameAndDeletedAtToTaxCategories < ActiveRecord::Migration[7.0] + def change + add_index(:spree_tax_categories, [:name, :deleted_at], unique: true) + end +end diff --git a/db/migrate/20250215091227_add_unique_index_name_to_zones.rb b/db/migrate/20250215091227_add_unique_index_name_to_zones.rb new file mode 100644 index 0000000000..975d42be52 --- /dev/null +++ b/db/migrate/20250215091227_add_unique_index_name_to_zones.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddUniqueIndexNameToZones < ActiveRecord::Migration[7.0] + def change + add_index(:spree_zones, :name, unique: true) + end +end