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.

Improve the migration path to 5.2 (#13167)

Damian Legawieca413cfa384ec
diff --git a/core/app/models/spree/property.rb b/core/app/models/spree/property.rb
index b46964d785..84ab12571f 100644
--- a/core/app/models/spree/property.rb
+++ b/core/app/models/spree/property.rb
@@ -50,6 +50,23 @@ module Spree
end
end
+ # Returns the metafield type for the property kind
+ # @return [String] eg. 'Spree::Metafields::ShortText'
+ def kind_to_metafield_type
+ case kind
+ when 'short_text'
+ 'Spree::Metafields::ShortText'
+ when 'long_text'
+ 'Spree::Metafields::LongText'
+ when 'number'
+ 'Spree::Metafields::Number'
+ when 'rich_text'
+ 'Spree::Metafields::RichText'
+ else
+ 'Spree::Metafields::ShortText'
+ end
+ end
+
private
def touch_all_products
diff --git a/core/lib/tasks/core.rake b/core/lib/tasks/core.rake
index cf1f08e1e8..7b6f1b2b73 100644
--- a/core/lib/tasks/core.rake
+++ b/core/lib/tasks/core.rake
@@ -247,6 +247,27 @@ use rake db:load_file[/absolute/path/to/sample/filename.rb]}
end
end
+ desc 'Migrate product properties to metafields'
+ task migrate_product_properties_to_metafields: :environment do |_t, _args|
+ Spree::Property.find_each do |property|
+ metafield_definition = Spree::MetafieldDefinition.find_or_initialize_by(
+ namespace: 'properties',
+ key: property.name,
+ resource_type: 'Spree::Product'
+ )
+ metafield_definition.assign_attributes(
+ metafield_type: property.kind_to_metafield_type,
+ display_on: property.display_on
+ )
+ metafield_definition.save!
+
+ property.product_properties.includes(:product).find_each do |product_property|
+ product_property.product.set_metafield("properties.#{property.name}", product_property.value)
+ puts "Migrated #{property.name} to product #{product_property.product.id}"
+ end
+ end
+ end
+
desc 'Migrate product details sections to PDP 2.0 with blocks and metafields'
task migrate_product_details_pages: :environment do |_t, _args|
Spree::PageSections::ProductDetails.find_each do |section|
diff --git a/core/spec/models/spree/property_spec.rb b/core/spec/models/spree/property_spec.rb
index e940ce6ad7..129248f01d 100644
--- a/core/spec/models/spree/property_spec.rb
+++ b/core/spec/models/spree/property_spec.rb
@@ -176,4 +176,36 @@ describe Spree::Property, type: :model do
end
end
end
+
+ describe '#kind_to_metafield_type' do
+ let(:property) { create(:property, kind: 'short_text') }
+
+ it 'returns the correct metafield type' do
+ expect(property.kind_to_metafield_type).to eq('Spree::Metafields::ShortText')
+ end
+
+ context 'when the property kind is long_text' do
+ let(:property) { create(:property, kind: 'long_text') }
+
+ it 'returns the correct metafield type' do
+ expect(property.kind_to_metafield_type).to eq('Spree::Metafields::LongText')
+ end
+ end
+
+ context 'when the property kind is number' do
+ let(:property) { create(:property, kind: 'number') }
+
+ it 'returns the correct metafield type' do
+ expect(property.kind_to_metafield_type).to eq('Spree::Metafields::Number')
+ end
+ end
+
+ context 'when the property kind is rich_text' do
+ let(:property) { create(:property, kind: 'rich_text') }
+
+ it 'returns the correct metafield type' do
+ expect(property.kind_to_metafield_type).to eq('Spree::Metafields::RichText')
+ end
+ end
+ end
end
diff --git a/docs/developer/upgrades/5.1-to-5.2.mdx b/docs/developer/upgrades/5.1-to-5.2.mdx
index db6e5e341d..e06e014fbb 100644
--- a/docs/developer/upgrades/5.1-to-5.2.mdx
+++ b/docs/developer/upgrades/5.1-to-5.2.mdx
@@ -70,4 +70,24 @@ description: This guide covers upgrading a Spree 5.1 application to Spree 5.2.
This works great with our default storefront theme. If you're using a custom theme, you will need to adjust your templates.
</Accordion>
+
+ <Accordion title="5. Migrate store policies">
+ Spree 5.2 introduces more flexible way to manage store policies (eg. terms of service, privacy policy, returns policy, shipping policy).
+
+ Previously they were just attributes (rich text fields) on the Store model. Now they are stored in the new Policy model.
+
+ To migrate to the new system, you will need to run the following command:
+
+ ```bash
+ bin/rake db:migrate_policies
+ ```
+
+ This will migrate your current data to the new format.
+
+ <Warning>
+ If you're using a custom theme, you will need to adjust [app/views/spree/checkout/_footer.html.erb](https://github.com/spree/spree/blob/main/storefront/app/views/spree/checkout/_footer.html.erb) template to be able to manage checkout footer links which was also expanded (now you can link to Pages, Policies, URLs and more).
+
+ `current_store.policies` won't work anymore, you will need to use `current_store.links` instead.
+ </Warning>
+ </Accordion>
</AccordionGroup>