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.

Keep existing downloads attached across the digital asset rename

Damian Legawiec9e06f237ce1e
diff --git a/spree/core/db/migrate/20260810000001_rename_digitals_to_digital_assets.rb b/spree/core/db/migrate/20260810000001_rename_digitals_to_digital_assets.rb
index 2960dbf898..06878d80d1 100644
--- a/spree/core/db/migrate/20260810000001_rename_digitals_to_digital_assets.rb
+++ b/spree/core/db/migrate/20260810000001_rename_digitals_to_digital_assets.rb
@@ -3,6 +3,11 @@ class RenameDigitalsToDigitalAssets < ActiveRecord::Migration[8.1]
rename_table :spree_digitals, :spree_digital_assets
rename_column :spree_digital_links, :digital_id, :digital_asset_id
+ # Active Storage keys attachments by the record's class name, so the files
+ # on existing rows are stored under 'Spree::Digital'. Rewrite them to the
+ # new class name or every already-uploaded download would look unattached.
+ rewrite_attachment_record_type('Spree::Digital', 'Spree::DigitalAsset')
+
# Null means "use the store's download settings". Per-asset values let one
# catalog mix evergreen re-downloadable files with time-limited ones.
add_column :spree_digital_assets, :authorized_clicks, :integer
@@ -13,7 +18,21 @@ class RenameDigitalsToDigitalAssets < ActiveRecord::Migration[8.1]
remove_column :spree_digital_assets, :authorized_days
remove_column :spree_digital_assets, :authorized_clicks
+ rewrite_attachment_record_type('Spree::DigitalAsset', 'Spree::Digital')
+
rename_column :spree_digital_links, :digital_asset_id, :digital_id
rename_table :spree_digital_assets, :spree_digitals
end
+
+ private
+
+ def rewrite_attachment_record_type(from, to)
+ return unless table_exists?(:active_storage_attachments)
+
+ execute(<<~SQL.squish)
+ UPDATE active_storage_attachments
+ SET record_type = #{quote(to)}
+ WHERE record_type = #{quote(from)}
+ SQL
+ end
end