From 5a85a9d1bbdd7e70e68510fa6c0c51eb273f9b16 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Wed, 25 Oct 2023 19:01:47 +0200 Subject: [PATCH 1/2] Add a rake task to migrate adjustments from legacy promotions Also has a task to migrate back. --- .../migrate_adjustments.rb | 41 ++++++++++ .../migrate_adjustments.rake | 17 +++++ .../migrate_adjustments_spec.rb | 74 +++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 friendly_promotions/lib/solidus_friendly_promotions/migrate_adjustments.rb create mode 100644 friendly_promotions/lib/tasks/solidus_friendly_promotions/migrate_adjustments.rake create mode 100644 friendly_promotions/spec/lib/solidus_friendly_promotions/migrate_adjustments_spec.rb diff --git a/friendly_promotions/lib/solidus_friendly_promotions/migrate_adjustments.rb b/friendly_promotions/lib/solidus_friendly_promotions/migrate_adjustments.rb new file mode 100644 index 00000000..ee6cfcad --- /dev/null +++ b/friendly_promotions/lib/solidus_friendly_promotions/migrate_adjustments.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +module SolidusFriendlyPromotions + class MigrateAdjustments + class << self + def up + sql = <<~SQL + UPDATE spree_adjustments + SET source_id = friendly_promotion_actions.id, + source_type = 'SolidusFriendlyPromotions::PromotionAction' + FROM spree_promotion_actions + INNER JOIN friendly_promotion_actions ON friendly_promotion_actions.original_promotion_action_id = spree_promotion_actions.id + WHERE spree_adjustments.source_id = spree_promotion_actions.id and spree_adjustments.source_type = 'Spree::PromotionAction' + SQL + + execute(sql) + end + + def down + sql = <<~SQL + UPDATE spree_adjustments + SET source_id = spree_promotion_actions.id, + source_type = 'Spree::PromotionAction' + FROM spree_promotion_actions + INNER JOIN friendly_promotion_actions ON friendly_promotion_actions.original_promotion_action_id = spree_promotion_actions.id + WHERE spree_adjustments.source_id = friendly_promotion_actions.id and spree_adjustments.source_type = 'SolidusFriendlyPromotions::PromotionAction' + SQL + + execute(sql) + end + + private + + def execute(sql) + Spree::Adjustment.transaction do + ActiveRecord::Base.connection.execute(sql) + end + end + end + end +end diff --git a/friendly_promotions/lib/tasks/solidus_friendly_promotions/migrate_adjustments.rake b/friendly_promotions/lib/tasks/solidus_friendly_promotions/migrate_adjustments.rake new file mode 100644 index 00000000..d98a9ffd --- /dev/null +++ b/friendly_promotions/lib/tasks/solidus_friendly_promotions/migrate_adjustments.rake @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +namespace :solidus_friendly_promotions do + namespace :migrate_adjustments do + desc "Migrate adjustments with Spree::PromotionAction sources to SolidusFriendlyPromotions::PromotionAction sources" + task up: :environment do + require "solidus_friendly_promotions/migrate_adjustments" + SolidusFriendlyPromotions::MigrateAdjustments.up + end + + desc "Migrate adjustments with SolidusFriendlyPromotions::PromotionAction sources to Spree::PromotionAction sources" + task down: :environment do + require "solidus_friendly_promotions/migrate_adjustments" + SolidusFriendlyPromotions::MigrateAdjustments.down + end + end +end diff --git a/friendly_promotions/spec/lib/solidus_friendly_promotions/migrate_adjustments_spec.rb b/friendly_promotions/spec/lib/solidus_friendly_promotions/migrate_adjustments_spec.rb new file mode 100644 index 00000000..69e45c15 --- /dev/null +++ b/friendly_promotions/spec/lib/solidus_friendly_promotions/migrate_adjustments_spec.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +require "spec_helper" +require "solidus_friendly_promotions/promotion_migrator" +require "solidus_friendly_promotions/promotion_map" +require "solidus_friendly_promotions/migrate_adjustments" + +RSpec.describe SolidusFriendlyPromotions::MigrateAdjustments do + let(:promotion) { create(:promotion, :with_adjustable_action) } + let(:order) { create(:order_with_line_items) } + let(:line_item) { order.line_items.first } + let(:tax_rate) { create(:tax_rate) } + + before do + line_item.adjustments.create!( + source: tax_rate, + amount: -3, + label: "Business tax", + eligible: true, + included: true, + order: order + ) + line_item.adjustments.create!( + source: promotion.actions.first, + amount: -2, + label: "Promotion (Because we like you)", + eligible: true, + order: order + ) + SolidusFriendlyPromotions::PromotionMigrator.new( + SolidusFriendlyPromotions::PROMOTION_MAP + ).call + end + + describe ".up" do + subject { described_class.up } + + it "migrates our adjustment" do + spree_promotion_action = Spree::PromotionAction.first + friendly_promotion_action = SolidusFriendlyPromotions::PromotionAction.first + expect { subject }.to change { + Spree::Adjustment.promotion.first.source + }.from(spree_promotion_action).to(friendly_promotion_action) + end + + it "will not touch tax adjustments" do + expect { subject }.not_to change { + Spree::Adjustment.tax.first.attributes + } + end + end + + describe ".down" do + subject { described_class.down } + + before do + described_class.up + end + + it "migrates our adjustment" do + spree_promotion_action = Spree::PromotionAction.first + friendly_promotion_action = SolidusFriendlyPromotions::PromotionAction.first + expect { subject }.to change { + Spree::Adjustment.promotion.first.source + }.from(friendly_promotion_action).to(spree_promotion_action) + end + + it "will not touch tax adjustments" do + expect { subject }.not_to change { + Spree::Adjustment.tax.first.attributes + } + end + end +end From 481447c97dfa8562f9d46ef4eafa53ea524d5413 Mon Sep 17 00:00:00 2001 From: dave Date: Wed, 25 Oct 2023 20:05:15 -0700 Subject: [PATCH 2/2] Fix sql for mysql MigrateAdjustments --- .../migrate_adjustments.rb | 45 ++++++++++++++----- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/friendly_promotions/lib/solidus_friendly_promotions/migrate_adjustments.rb b/friendly_promotions/lib/solidus_friendly_promotions/migrate_adjustments.rb index ee6cfcad..c27c4916 100644 --- a/friendly_promotions/lib/solidus_friendly_promotions/migrate_adjustments.rb +++ b/friendly_promotions/lib/solidus_friendly_promotions/migrate_adjustments.rb @@ -4,27 +4,48 @@ module SolidusFriendlyPromotions class MigrateAdjustments class << self def up - sql = <<~SQL - UPDATE spree_adjustments - SET source_id = friendly_promotion_actions.id, - source_type = 'SolidusFriendlyPromotions::PromotionAction' + sql = if ActiveRecord::Base.connection_db_config.adapter == "mysql2" + <<~SQL + UPDATE spree_adjustments + INNER JOIN spree_promotion_actions ON spree_adjustments.source_id = spree_promotion_actions.id and spree_adjustments.source_type = 'Spree::PromotionAction' + INNER JOIN friendly_promotion_actions ON friendly_promotion_actions.original_promotion_action_id = spree_promotion_actions.id + SET source_id = friendly_promotion_actions.id, + source_type = 'SolidusFriendlyPromotions::PromotionAction' + SQL + else + <<~SQL + UPDATE spree_adjustments + SET source_id = friendly_promotion_actions.id, + source_type = 'SolidusFriendlyPromotions::PromotionAction' FROM spree_promotion_actions - INNER JOIN friendly_promotion_actions ON friendly_promotion_actions.original_promotion_action_id = spree_promotion_actions.id + INNER JOIN friendly_promotion_actions ON friendly_promotion_actions.original_promotion_action_id = spree_promotion_actions.id WHERE spree_adjustments.source_id = spree_promotion_actions.id and spree_adjustments.source_type = 'Spree::PromotionAction' - SQL + SQL + end execute(sql) end def down - sql = <<~SQL - UPDATE spree_adjustments - SET source_id = spree_promotion_actions.id, - source_type = 'Spree::PromotionAction' + sql = if ActiveRecord::Base.connection_db_config.adapter == "mysql2" + <<~SQL + UPDATE spree_adjustments + INNER JOIN friendly_promotion_actions + INNER JOIN spree_promotion_actions ON spree_adjustments.source_id = friendly_promotion_actions.id and spree_adjustments.source_type = 'SolidusFriendlyPromotions::PromotionAction' + SET source_id = spree_promotion_actions.id, + source_type = 'Spree::PromotionAction' + WHERE friendly_promotion_actions.original_promotion_action_id = spree_promotion_actions.id + SQL + else + <<~SQL + UPDATE spree_adjustments + SET source_id = spree_promotion_actions.id, + source_type = 'Spree::PromotionAction' FROM spree_promotion_actions - INNER JOIN friendly_promotion_actions ON friendly_promotion_actions.original_promotion_action_id = spree_promotion_actions.id + INNER JOIN friendly_promotion_actions ON friendly_promotion_actions.original_promotion_action_id = spree_promotion_actions.id WHERE spree_adjustments.source_id = friendly_promotion_actions.id and spree_adjustments.source_type = 'SolidusFriendlyPromotions::PromotionAction' - SQL + SQL + end execute(sql) end