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.

Utilize `ActiveRecord#insert_all` to cut seed loading time (#11352)

Damian Legawiec33d568ced67c
diff --git a/core/app/services/spree/seeds/countries.rb b/core/app/services/spree/seeds/countries.rb
index 360ffea618..1a23f94347 100644
--- a/core/app/services/spree/seeds/countries.rb
+++ b/core/app/services/spree/seeds/countries.rb
@@ -9,17 +9,33 @@ module Spree
def call
ApplicationRecord.transaction do
- Carmen::Country.all.each do |country|
- # Skip the creation of some territories, uninhabited islands and the Antarctic.
- next if EXCLUDED_COUNTRIES.include?(country.alpha_2_code)
+ if Rails::VERSION::MAJOR >= 6
+ new_countries = Carmen::Country.all.map do |country|
+ # Skip the creation of some territories, uninhabited islands and the Antarctic.
+ next if EXCLUDED_COUNTRIES.include?(country.alpha_2_code)
- Spree::Country.where(
- name: country.name,
- iso3: country.alpha_3_code,
- iso: country.alpha_2_code,
- iso_name: country.name.upcase,
- numcode: country.numeric_code
- ).first_or_create!
+ Hash[
+ 'name': country.name,
+ 'iso3': country.alpha_3_code,
+ 'iso': country.alpha_2_code,
+ 'iso_name': country.name.upcase,
+ 'numcode': country.numeric_code,
+ ]
+ end.compact.uniq
+ Spree::Country.insert_all(new_countries)
+ else
+ Carmen::Country.all.each do |country|
+ # Skip the creation of some territories, uninhabited islands and the Antarctic.
+ next if EXCLUDED_COUNTRIES.include?(country.alpha_2_code)
+
+ Spree::Country.where(
+ name: country.name,
+ iso3: country.alpha_3_code,
+ iso: country.alpha_2_code,
+ iso_name: country.name.upcase,
+ numcode: country.numeric_code
+ ).first_or_create!
+ end
end
# Find countries that do not use postal codes (by iso) and set 'zipcode_required' to false for them.
diff --git a/core/app/services/spree/seeds/roles.rb b/core/app/services/spree/seeds/roles.rb
index 723cbad0a5..9d363f31f1 100644
--- a/core/app/services/spree/seeds/roles.rb
+++ b/core/app/services/spree/seeds/roles.rb
@@ -3,8 +3,6 @@ module Spree
class Roles
prepend Spree::ServiceModule::Base
- EXCLUDED_COUNTRIES = ['AQ', 'AX', 'GS', 'UM', 'HM', 'IO', 'EH', 'BV', 'TF'].freeze
-
def call
Spree::Role.where(name: 'admin').first_or_create!
Spree::Role.where(name: 'user').first_or_create!
diff --git a/core/app/services/spree/seeds/states.rb b/core/app/services/spree/seeds/states.rb
index 097acc4e1b..665f95beab 100644
--- a/core/app/services/spree/seeds/states.rb
+++ b/core/app/services/spree/seeds/states.rb
@@ -49,11 +49,22 @@ module Spree
end
def province_level(country, subregion)
- subregion.subregions.each do |province|
- country.states.where(
- name: province.name,
- abbr: province.code
- ).first_or_create!
+ if Rails::VERSION::MAJOR >= 6
+ new_states = subregion.subregions.map do |province|
+ Hash[
+ name: province.name,
+ abbr: province.code,
+ country_id: country.id
+ ]
+ end.compact.uniq
+ Spree::State.insert_all(new_states)
+ else
+ subregion.subregions.each do |province|
+ country.states.where(
+ name: province.name,
+ abbr: province.code
+ ).first_or_create!
+ end
end
end
end