Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Add gem-owned Customer/AdminUser tables and rename user_id to customer_id

Łukasz Adamczyk514bbd7b33a4
diff --git a/spree/core/db/migrate/20220201103922_add_first_name_and_last_name_to_spree_users.rb b/spree/core/db/migrate/20220201103922_add_first_name_and_last_name_to_spree_users.rb
index 93372e1252..628f4d21a1 100644
--- a/spree/core/db/migrate/20220201103922_add_first_name_and_last_name_to_spree_users.rb
+++ b/spree/core/db/migrate/20220201103922_add_first_name_and_last_name_to_spree_users.rb
@@ -1,9 +1,11 @@
class AddFirstNameAndLastNameToSpreeUsers < ActiveRecord::Migration[5.2]
def change
- if Spree.user_class.present?
- users_table_name = Spree.user_class.table_name
- add_column users_table_name, :first_name, :string unless column_exists?(users_table_name, :first_name)
- add_column users_table_name, :last_name, :string unless column_exists?(users_table_name, :last_name)
- end
+ return unless Spree.customer_class.present?
+
+ table = Spree.customer_class.table_name
+ return unless table_exists?(table)
+
+ add_column table, :first_name, :string unless column_exists?(table, :first_name)
+ add_column table, :last_name, :string unless column_exists?(table, :last_name)
end
end
diff --git a/spree/core/db/migrate/20221215151408_add_selected_locale_to_spree_users.rb b/spree/core/db/migrate/20221215151408_add_selected_locale_to_spree_users.rb
index 4466733048..15d35ffe08 100644
--- a/spree/core/db/migrate/20221215151408_add_selected_locale_to_spree_users.rb
+++ b/spree/core/db/migrate/20221215151408_add_selected_locale_to_spree_users.rb
@@ -1,8 +1,10 @@
class AddSelectedLocaleToSpreeUsers < ActiveRecord::Migration[6.1]
def change
- if Spree.user_class.present?
- users_table_name = Spree.user_class.table_name
- add_column users_table_name, :selected_locale, :string unless column_exists?(users_table_name, :selected_locale)
- end
+ return unless Spree.customer_class.present?
+
+ table = Spree.customer_class.table_name
+ return unless table_exists?(table)
+
+ add_column table, :selected_locale, :string unless column_exists?(table, :selected_locale)
end
end
diff --git a/spree/core/db/migrate/20250127151258_add_phone_to_spree_users.rb b/spree/core/db/migrate/20250127151258_add_phone_to_spree_users.rb
index f11fe8c1c0..75b2ffc590 100644
--- a/spree/core/db/migrate/20250127151258_add_phone_to_spree_users.rb
+++ b/spree/core/db/migrate/20250127151258_add_phone_to_spree_users.rb
@@ -1,5 +1,7 @@
class AddPhoneToSpreeUsers < ActiveRecord::Migration[6.1]
def change
- add_column Spree.user_class.table_name, :phone, :string, if_not_exists: true
+ return unless Spree.customer_class.present? && table_exists?(Spree.customer_class.table_name)
+
+ add_column Spree.customer_class.table_name, :phone, :string, if_not_exists: true
end
end
diff --git a/spree/core/db/migrate/20250207084000_add_accepts_email_marketing_field_to_spree_users_table.rb b/spree/core/db/migrate/20250207084000_add_accepts_email_marketing_field_to_spree_users_table.rb
index 28caeab0cf..0ac3e97520 100644
--- a/spree/core/db/migrate/20250207084000_add_accepts_email_marketing_field_to_spree_users_table.rb
+++ b/spree/core/db/migrate/20250207084000_add_accepts_email_marketing_field_to_spree_users_table.rb
@@ -1,6 +1,8 @@
class AddAcceptsEmailMarketingFieldToSpreeUsersTable < ActiveRecord::Migration[6.1]
def change
- add_column Spree.user_class.table_name, :accepts_email_marketing, :boolean, default: false, null: false, if_not_exists: true
- add_index Spree.user_class.table_name, :accepts_email_marketing, if_not_exists: true
+ return unless Spree.customer_class.present? && table_exists?(Spree.customer_class.table_name)
+
+ add_column Spree.customer_class.table_name, :accepts_email_marketing, :boolean, default: false, null: false, if_not_exists: true
+ add_index Spree.customer_class.table_name, :accepts_email_marketing, if_not_exists: true
end
end
diff --git a/spree/core/db/migrate/20250605131334_add_missing_fields_to_users.rb b/spree/core/db/migrate/20250605131334_add_missing_fields_to_users.rb
index b7967b3bfb..66b4bbf7a2 100644
--- a/spree/core/db/migrate/20250605131334_add_missing_fields_to_users.rb
+++ b/spree/core/db/migrate/20250605131334_add_missing_fields_to_users.rb
@@ -2,33 +2,37 @@
# so we need to ensure this is setup properly
class AddMissingFieldsToUsers < ActiveRecord::Migration[7.2]
def change
- users_table = Spree.user_class.table_name
+ users_table = Spree.customer_class.table_name
admin_users_table = Spree.admin_user_class.table_name
- change_table users_table do |t|
- t.string :login unless column_exists?(users_table, :login)
+ if table_exists?(users_table)
+ change_table users_table do |t|
+ t.string :login unless column_exists?(users_table, :login)
- if t.respond_to? :jsonb
- t.jsonb :public_metadata unless column_exists?(users_table, :public_metadata)
- t.jsonb :private_metadata unless column_exists?(users_table, :private_metadata)
- else
- t.json :public_metadata unless column_exists?(users_table, :public_metadata)
- t.json :private_metadata unless column_exists?(users_table, :private_metadata)
+ if t.respond_to? :jsonb
+ t.jsonb :public_metadata unless column_exists?(users_table, :public_metadata)
+ t.jsonb :private_metadata unless column_exists?(users_table, :private_metadata)
+ else
+ t.json :public_metadata unless column_exists?(users_table, :public_metadata)
+ t.json :private_metadata unless column_exists?(users_table, :private_metadata)
+ end
end
- end
- add_reference users_table, :bill_address, if_not_exists: true
- add_reference users_table, :ship_address, if_not_exists: true
+ add_reference users_table, :bill_address, if_not_exists: true
+ add_reference users_table, :ship_address, if_not_exists: true
+ end
- change_table admin_users_table do |t|
- t.string :login unless column_exists?(admin_users_table, :login)
+ if table_exists?(admin_users_table)
+ change_table admin_users_table do |t|
+ t.string :login unless column_exists?(admin_users_table, :login)
- if t.respond_to? :jsonb
- t.jsonb :public_metadata unless column_exists?(admin_users_table, :public_metadata)
- t.jsonb :private_metadata unless column_exists?(admin_users_table, :private_metadata)
- else
- t.json :public_metadata unless column_exists?(admin_users_table, :public_metadata)
- t.json :private_metadata unless column_exists?(admin_users_table, :private_metadata)
+ if t.respond_to? :jsonb
+ t.jsonb :public_metadata unless column_exists?(admin_users_table, :public_metadata)
+ t.jsonb :private_metadata unless column_exists?(admin_users_table, :private_metadata)
+ else
+ t.json :public_metadata unless column_exists?(admin_users_table, :public_metadata)
+ t.json :private_metadata unless column_exists?(admin_users_table, :private_metadata)
+ end
end
end
end
diff --git a/spree/core/db/migrate/20260728000000_create_spree_admin_users.rb b/spree/core/db/migrate/20260728000000_create_spree_admin_users.rb
new file mode 100644
index 0000000000..b9c567768b
--- /dev/null
+++ b/spree/core/db/migrate/20260728000000_create_spree_admin_users.rb
@@ -0,0 +1,25 @@
+class CreateSpreeAdminUsers < ActiveRecord::Migration[7.2]
+ def change
+ create_table :spree_admin_users do |t|
+ t.string :email, null: false
+ t.string :password_digest
+ t.string :first_name
+ t.string :last_name
+ t.string :selected_locale
+ t.integer :failed_attempts
+ t.datetime :locked_at
+
+ if t.respond_to?(:jsonb)
+ t.jsonb :public_metadata
+ t.jsonb :private_metadata
+ else
+ t.json :public_metadata
+ t.json :private_metadata
+ end
+
+ t.timestamps
+ end
+
+ add_index :spree_admin_users, :email, unique: true
+ end
+end
diff --git a/spree/core/db/migrate/20260728000001_create_spree_customers.rb b/spree/core/db/migrate/20260728000001_create_spree_customers.rb
new file mode 100644
index 0000000000..84f550e5a1
--- /dev/null
+++ b/spree/core/db/migrate/20260728000001_create_spree_customers.rb
@@ -0,0 +1,32 @@
+class CreateSpreeCustomers < ActiveRecord::Migration[7.2]
+ def change
+ create_table :spree_customers do |t|
+ t.string :email, null: false
+ t.string :password_digest
+ t.string :first_name
+ t.string :last_name
+ t.string :phone
+ t.string :selected_locale
+ t.boolean :accepts_email_marketing
+ t.bigint :bill_address_id
+ t.bigint :ship_address_id
+ t.integer :failed_attempts
+ t.datetime :locked_at
+
+ if t.respond_to?(:jsonb)
+ t.jsonb :public_metadata
+ t.jsonb :private_metadata
+ else
+ t.json :public_metadata
+ t.json :private_metadata
+ end
+
+ t.timestamps
+ end
+
+ add_index :spree_customers, :email, unique: true
+ add_index :spree_customers, :bill_address_id
+ add_index :spree_customers, :ship_address_id
+ add_index :spree_customers, :accepts_email_marketing
+ end
+end
diff --git a/spree/core/db/migrate/20260728000003_rename_user_id_to_customer_id.rb b/spree/core/db/migrate/20260728000003_rename_user_id_to_customer_id.rb
new file mode 100644
index 0000000000..4d08c053c9
--- /dev/null
+++ b/spree/core/db/migrate/20260728000003_rename_user_id_to_customer_id.rb
@@ -0,0 +1,28 @@
+class RenameUserIdToCustomerId < ActiveRecord::Migration[7.2]
+ def change
+ # Drop the lone DB-level FK constraint (Spree convention: no foreign key constraints).
+ reversible do |dir|
+ dir.up do
+ if foreign_key_exists?(:spree_payment_sources, :spree_users, column: :user_id)
+ remove_foreign_key :spree_payment_sources, :spree_users, column: :user_id
+ end
+ end
+ end
+
+ rename_column :spree_orders, :user_id, :customer_id
+ rename_column :spree_addresses, :user_id, :customer_id
+ rename_column :spree_credit_cards, :user_id, :customer_id
+ rename_column :spree_store_credits, :user_id, :customer_id
+ rename_column :spree_wishlists, :user_id, :customer_id
+ rename_column :spree_gift_cards, :user_id, :customer_id
+ rename_column :spree_gateway_customers, :user_id, :customer_id
+ rename_column :spree_payment_sources, :user_id, :customer_id
+ rename_column :spree_newsletter_subscribers, :user_id, :customer_id
+ rename_column :spree_promotion_rule_users, :user_id, :customer_id
+
+ # Polymorphic membership — rename both id and type so
+ # `belongs_to :customer, polymorphic: true` resolves.
+ rename_column :spree_customer_group_users, :user_id, :customer_id
+ rename_column :spree_customer_group_users, :user_type, :customer_type
+ end
+end