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
Fix: users-to-customers upgrade aborting when there is nothing to copy (#14842)
Damian Legawiec1ed5964f17e8
diff --git a/docs/developer/upgrades/5.6-to-6.0.mdx b/docs/developer/upgrades/5.6-to-6.0.mdxindex acd7cd7547..bef963c75c 100644--- a/docs/developer/upgrades/5.6-to-6.0.mdx+++ b/docs/developer/upgrades/5.6-to-6.0.mdx@@ -194,7 +194,7 @@ bundle exec rake spree:upgrade:migrate_users_to_customersCopies your legacy Devise customer table (default `spree_users`) into `spree_customers`, keeping the same ids, and carries existing passwords over so customers and staff can keep signing in. The source table is left in place as a safety net.<Warning>- Passwords carry over only if your application **never configured a Devise pepper**. The task stops if it finds one. Because Devise is no longer loaded in 6.0, the task usually cannot check for itself — it stops until you confirm no pepper was used by re-running it with `CONFIRM_NO_PEPPER=true`. If you did use a pepper, single sign-on, or a non-bcrypt password format, skip this step and keep your own customer model by setting `Spree.customer_class`.+ Passwords carry over only if your application **never configured a Devise pepper**. The task stops if it finds one. Because Devise is no longer loaded in 6.0, the task usually cannot check for itself — it stops until you confirm no pepper was used by re-running it with `CONFIRM_NO_PEPPER=true`. When there are no passwords to carry over, for example an empty `spree_users` table, the task skips this check. If you did use a pepper, single sign-on, or a non-bcrypt password format, skip this step and keep your own customer model by setting `Spree.customer_class`.</Warning>### Name countries and states by ISO codediff --git a/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml b/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.ymlindex a3f4c42fa9..19ca45eac2 100644--- a/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml+++ b/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml@@ -165,7 +165,9 @@ steps:that has_secure_password reads directly only when unpeppered; the taskaborts if Devise.pepper is set. Because Devise is gone in 6.0 a formerpepper can't be introspected, so when Devise is absent the task aborts until- you confirm none was used with CONFIRM_NO_PEPPER=true. Peppered / SSO /+ you confirm none was used with CONFIRM_NO_PEPPER=true. The check is skipped+ when there are no passwords to copy (e.g. an empty spree_users table), so+ new projects pass without the flag. Peppered / SSO /non-bcrypt installs keep a custom customer_class instead (see the 5.6-to-6.0upgrade guide) and skip this step.diff --git a/spree/core/lib/tasks/migrate_users_to_customers.rake b/spree/core/lib/tasks/migrate_users_to_customers.rakeindex fda040faed..b2a19ebd86 100644--- a/spree/core/lib/tasks/migrate_users_to_customers.rake+++ b/spree/core/lib/tasks/migrate_users_to_customers.rake@@ -16,7 +16,8 @@ namespace :spree doconfigured — Devise's +encrypted_password+ is a plain bcrypt digest, theexact format +has_secure_password+ reads. The task aborts if a pepper isdetected; when Devise is absent (so a former pepper can't be introspected)- it aborts until you confirm none was used with CONFIRM_NO_PEPPER=true.+ it aborts until you confirm none was used with CONFIRM_NO_PEPPER=true. The+ check is skipped when there are no passwords to copy.Source rows with a blank email, or whose email is already taken by adifferent customer, abort the task with their ids — reconcile them or passSKIP_INVALID_ROWS=true to skip and continue. The legacy source table is@@ -38,10 +39,29 @@ namespace :spree donextend+ customer_model = Spree.customer_class+ admin_model = Spree.admin_user_class++ # Read the legacy table by name only — the Devise-era Spree::User /+ # Spree::LegacyUser models are gone in 6.0. Inherit from Spree.base_class+ # so the source shares the customer table's connection (the id-exclusion+ # subquery below spans both).+ source = Class.new(Spree.base_class) { self.table_name = source_table }++ pending = source.where.not(id: customer_model.select(:id))+ admin_pending = if admin_model.column_names.include?('encrypted_password')+ admin_model.where(password_digest: [nil, '']).where.not(encrypted_password: [nil, ''])+ else+ admin_model.none+ end+# Peppered digests can't be verified by has_secure_password. Refuse rather# than silently produce accounts nobody can sign into. Once Devise is gone# a former pepper can't be introspected, so require an explicit all-clear.- if defined?(Devise) && Devise.respond_to?(:pepper) && Devise.pepper.present?+ # With no digest to copy (e.g. an empty legacy table) there is nothing to protect.+ if pending.none? && admin_pending.none?+ puts " No customer or admin passwords to copy — skipping the Devise pepper check."+ elsif defined?(Devise) && Devise.respond_to?(:pepper) && Devise.pepper.present?abort " Devise.pepper is configured — bcrypt digests can't be copied into password_digest. " \"Use the custom-model path (keep the table, alias password_digest) or force password resets."elsif !defined?(Devise) && ENV['CONFIRM_NO_PEPPER'] != 'true'@@ -51,15 +71,6 @@ namespace :spree do"used no pepper, or use the custom-model path / force password resets."end- customer_model = Spree.customer_class- admin_model = Spree.admin_user_class-- # Read the legacy table by name only — the Devise-era Spree::User /- # Spree::LegacyUser models are gone in 6.0. Inherit from Spree.base_class- # so the source shares the customer table's connection (the id-exclusion- # subquery below spans both).- source = Class.new(Spree.base_class) { self.table_name = source_table }-# Copy only columns both tables share; map the digest column separately.direct_columns = customer_model.column_names & source.column_namescopy_digest_from_encrypted = source.column_names.include?('encrypted_password') &&@@ -75,7 +86,6 @@ namespace :spree do# the whole batch on the unique-email index.# Either kind aborts the task with the offending ids unless the operator# 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)# Compare case-insensitively to match Spree::Customer's uniqueness# (case_sensitive: false) — emails are stored case-preserved, so a legacy@@ -140,12 +150,7 @@ namespace :spree do# Admins stay in spree_admin_users (in place) — backfill the digest from# the legacy column when present.- admin_backfilled = 0- if admin_model.column_names.include?('encrypted_password')- admin_backfilled = admin_model.where(password_digest: [nil, '']).- where.not(encrypted_password: [nil, '']).- update_all('password_digest = encrypted_password')- end+ admin_backfilled = admin_pending.update_all('password_digest = encrypted_password')putsputs " Customers copied: #{copied} (skipped #{invalid_ids.size} invalid row(s): " \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 aae3264b72..78aac6d7a9 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@@ -187,14 +187,31 @@ describe 'spree:upgrade:migrate_users_to_customers' dodescribe 'guards' doit 'aborts when a Devise pepper is configured' dostub_const('Devise', Class.new { def self.pepper; 'peppered'; end })+ legacy_users.create!(id: 900_001, email: '[email protected]', encrypted_password: 'alice_digest')expect { subject.invoke }.to raise_error(SystemExit)end- it 'aborts when Devise is absent and no-pepper is unconfirmed' do- ENV.delete('CONFIRM_NO_PEPPER')+ context 'when Devise is absent and no-pepper is unconfirmed' do+ before { ENV.delete('CONFIRM_NO_PEPPER') }- expect { subject.invoke }.to raise_error(SystemExit)+ it 'aborts when there are customers to copy' do+ legacy_users.create!(id: 900_001, email: '[email protected]', encrypted_password: 'alice_digest')++ expect { subject.invoke }.to raise_error(SystemExit)+ expect(Spree.customer_class.exists?(900_001)).to be(false)+ end++ it 'aborts when there are admin digests to backfill' do+ create(:admin_user).update_columns(encrypted_password: 'legacy_admin_digest', password_digest: nil)++ expect { subject.invoke }.to raise_error(SystemExit)+ end++ it 'succeeds without copying anything when there is nothing to copy' do+ expect { subject.invoke }.not_to raise_error+ expect(Spree.customer_class.count).to eq(0)+ endendit 'no-ops when the source table is absent' do