Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Match customer email case-insensitivity in the migration preflight
Łukasz Adamczyk43289e74903e
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rbindex fa976e1849..f68bbbb88a 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb@@ -70,8 +70,8 @@ RSpec.describe Spree::Api::V3::Admin::OrdersController, type: :controller doendcontext 'with q[search] (full-text search)' do- # Factories auto-attach a user and a `before_create :link_by_email`- # callback resets `email` from `user.email`. Pass `user: nil` so our+ # Factories auto-attach a customer and a `before_create :link_by_email`+ # callback resets `email` from `customer.email`. Pass `customer: nil` so our# explicit email sticks for the email-match assertion.let!(:matching_order) docreate(:order, store: store, state: 'cart', customer: nil, email: '[email protected]')diff --git a/spree/core/lib/tasks/migrate_users_to_customers.rake b/spree/core/lib/tasks/migrate_users_to_customers.rakeindex fd14747da4..ed324a11e6 100644--- a/spree/core/lib/tasks/migrate_users_to_customers.rake+++ b/spree/core/lib/tasks/migrate_users_to_customers.rake@@ -77,8 +77,12 @@ namespace :spree do# passes SKIP_INVALID_ROWS=true to skip them and continue.pending = source.where.not(id: customer_model.select(:id))blank_email_ids = pending.where("email IS NULL OR email = ''").pluck(:id)- conflict_ids = pending.where("email IS NOT NULL AND email <> ''").- where(email: customer_model.select(:email)).pluck(:id)+ # Compare case-insensitively to match Spree::Customer's uniqueness+ # (case_sensitive: false) — emails are stored case-preserved, so a legacy+ # `Alice@` conflicting with an existing `alice@` must be caught here too.+ existing_emails = customer_model.where.not(email: [nil, '']).pluck(:email).map(&:downcase).to_set+ conflict_ids = pending.where("email IS NOT NULL AND email <> ''").pluck(:id, :email).+ filter_map { |id, email| id if existing_emails.include?(email.downcase) }invalid_ids = (blank_email_ids + conflict_ids).uniqif invalid_ids.any? && ENV['SKIP_INVALID_ROWS'] != 'true'diff --git a/spree/core/spec/lib/tasks/migrate_users_to_customers_spec.rb b/spree/core/spec/lib/tasks/migrate_users_to_customers_spec.rbindex 6cc71525c6..2386a54dc9 100644--- a/spree/core/spec/lib/tasks/migrate_users_to_customers_spec.rb+++ b/spree/core/spec/lib/tasks/migrate_users_to_customers_spec.rb@@ -91,6 +91,13 @@ describe 'spree:upgrade:migrate_users_to_customers' doexpect { subject.invoke }.to raise_error(SystemExit)end+ it 'treats email conflicts case-insensitively' do+ legacy_users.create!(id: 900_011, email: '[email protected]', encrypted_password: 'x')+ create(:user, email: '[email protected]')++ expect { subject.invoke }.to raise_error(SystemExit)+ end+it 'skips invalid rows and copies the rest with SKIP_INVALID_ROWS=true' dolegacy_users.create!(id: 900_010, email: '', encrypted_password: 'ghost_digest')ENV['SKIP_INVALID_ROWS'] = 'true'