Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Rails/UniqueValidationWithoutIndex (part of #11482) #13163

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -604,17 +604,6 @@ Rails/TransactionExitStatement:
Exclude:
- 'app/services/place_proxy_order.rb'

# Offense count: 5
# Configuration parameters: Include.
# Include: app/models/**/*.rb
Rails/UniqueValidationWithoutIndex:
Exclude:
- 'app/models/customer.rb'
- 'app/models/exchange.rb'
- 'app/models/spree/stock_item.rb'
- 'app/models/spree/tax_category.rb'
- 'app/models/spree/zone.rb'

# Offense count: 1
Security/Open:
Exclude:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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],
Comment on lines +5 to +6
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SELECT sender_id, order_cycle_id, receiver_id, incoming, COUNT(*) FROM exchanges GROUP BY sender_id, order_cycle_id, receiver_id, incoming HAVING COUNT(*) >1;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have some offenders here, too:

$ ansible all_prod -u openfoodnetwork -a "psql -h localhost openfoodnetwork ofn_user -c '$sql'"
openfoodnetwork.org.au | CHANGED | rc=0 >>
 sender_id | order_cycle_id | receiver_id | incoming | count 
-----------+----------------+-------------+----------+-------
(0 rows)
openfoodnetwork.org.nz | CHANGED | rc=0 >>
 sender_id | order_cycle_id | receiver_id | incoming | count 
-----------+----------------+-------------+----------+-------
(0 rows)
openfoodnetwork.net | CHANGED | rc=0 >>
 sender_id | order_cycle_id | receiver_id | incoming | count 
-----------+----------------+-------------+----------+-------
(0 rows)
openfoodnetwork.ca | CHANGED | rc=0 >>
 sender_id | order_cycle_id | receiver_id | incoming | count 
-----------+----------------+-------------+----------+-------
(0 rows)
coopcircuits.fr | CHANGED | rc=0 >>
 sender_id | order_cycle_id | receiver_id | incoming | count 
-----------+----------------+-------------+----------+-------
      3322 |          12333 |        3226 | t        |     2
       703 |          12260 |        1855 | t        |     2
      5792 |          31804 |        5792 | f        |     2
       209 |           7681 |         347 | t        |     2
      3949 |          13878 |        1680 | t        |     2
      3468 |          12333 |        3226 | t        |     2
       370 |          10482 |         370 | f        |     2
      3156 |          12260 |        1855 | t        |     2
      1680 |          13878 |        1680 | t        |     2
(9 rows)
openfoodnetwork.de | CHANGED | rc=0 >>
 sender_id | order_cycle_id | receiver_id | incoming | count 
-----------+----------------+-------------+----------+-------
(0 rows)
openfoodnetwork.in | CHANGED | rc=0 >>
 sender_id | order_cycle_id | receiver_id | incoming | count 
-----------+----------------+-------------+----------+-------
(0 rows)
openfoodnetwork.org.uk | CHANGED | rc=0 >>
 sender_id | order_cycle_id | receiver_id | incoming | count 
-----------+----------------+-------------+----------+-------
    204016 |         217768 |      201237 | t        |     2
    201237 |         210286 |      201237 | f        |     2
(2 rows)
openfoodnetwork.be | CHANGED | rc=0 >>
 sender_id | order_cycle_id | receiver_id | incoming | count 
-----------+----------------+-------------+----------+-------
        91 |           3996 |          91 | f        |     2
(1 row)
openfood.hu | CHANGED | rc=0 >>
 sender_id | order_cycle_id | receiver_id | incoming | count 
-----------+----------------+-------------+----------+-------
(0 rows)
app.katuma.org | CHANGED | rc=0 >>
 sender_id | order_cycle_id | receiver_id | incoming | count 
-----------+----------------+-------------+----------+-------
(0 rows)
openfoodnetwork.ie | CHANGED | rc=0 >>
 sender_id | order_cycle_id | receiver_id | incoming | count 
-----------+----------------+-------------+----------+-------
        36 |            704 |          36 | f        |     2
(1 row)

Those numbers are small enough to resolve manually, I think.

Copy link
Member

@mkllnk mkllnk Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have four servers to resolve:

  • coopcircuits.fr
  • openfoodnetwork.org.uk
  • openfoodnetwork.be
  • openfoodnetwork.ie

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a look at openfoodnetwork.ie and the duplicate exchange has been created 100ms after the first one. I'm not sure where the glitch is. Maybe something in the Javascript sending two items? They are exactly the same. So I can delete one.

unique: true, name: :index_exchanges_on_sender_id)
end
end
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions db/migrate/20250215091227_add_unique_index_name_to_zones.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 6 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2025_01_28_031518) do
ActiveRecord::Schema[7.0].define(version: 2025_02_15_091227) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements"
enable_extension "plpgsql"
Expand Down Expand Up @@ -105,7 +105,7 @@
t.boolean "created_manually", default: false
t.index ["bill_address_id"], name: "index_customers_on_bill_address_id"
t.index ["created_manually"], name: "index_customers_on_created_manually"
t.index ["email"], name: "index_customers_on_email"
t.index ["email", "enterprise_id"], name: "index_customers_on_email_and_enterprise_id", unique: true
t.index ["enterprise_id", "code"], name: "index_customers_on_enterprise_id_and_code", unique: true
t.index ["ship_address_id"], name: "index_customers_on_ship_address_id"
t.index ["user_id"], name: "index_customers_on_user_id"
Expand Down Expand Up @@ -268,7 +268,7 @@
t.text "receival_instructions"
t.index ["order_cycle_id"], name: "index_exchanges_on_order_cycle_id"
t.index ["receiver_id"], name: "index_exchanges_on_receiver_id"
t.index ["sender_id"], name: "index_exchanges_on_sender_id"
t.index ["sender_id", "order_cycle_id", "receiver_id", "incoming"], name: "index_exchanges_on_sender_id", unique: true
end

create_table "flipper_features", id: :serial, force: :cascade do |t|
Expand Down Expand Up @@ -832,7 +832,7 @@
t.integer "lock_version", default: 0
t.index ["stock_location_id", "variant_id"], name: "stock_item_by_loc_and_var_id"
t.index ["stock_location_id"], name: "index_spree_stock_items_on_stock_location_id"
t.index ["variant_id"], name: "index_spree_stock_items_on_variant_id", unique: true
t.index ["variant_id", "deleted_at"], name: "index_spree_stock_items_on_variant_id_and_deleted_at", unique: true
end

create_table "spree_stock_locations", id: :serial, force: :cascade do |t|
Expand Down Expand Up @@ -870,6 +870,7 @@
t.datetime "updated_at", precision: nil, null: false
t.boolean "is_default", default: false
t.datetime "deleted_at", precision: nil
t.index ["name", "deleted_at"], name: "index_spree_tax_categories_on_name_and_deleted_at", unique: true
end

create_table "spree_tax_rates", id: :serial, force: :cascade do |t|
Expand Down Expand Up @@ -999,6 +1000,7 @@
t.datetime "updated_at", precision: nil, null: false
t.boolean "default_tax", default: false
t.integer "zone_members_count", default: 0
t.index ["name"], name: "index_spree_zones_on_name", unique: true
end

create_table "stripe_accounts", id: :serial, force: :cascade do |t|
Expand Down
Loading