From 04dffbf0468e9f998f74c8a0fe7c7a190f764dbf Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Wed, 7 Jun 2017 09:48:29 +0200 Subject: [PATCH] Add Rake task to rename Bogus gateways Running rake solidus:migrations:rename_gateways:up helps migrating your data to new bogus payment method class names. rake solidus:migrations:rename_gateways:down reverts this. Also includes a migration that invokes that task, so you don't need to care when deploying this change. --- .../20170608074534_rename_bogus_gateways.rb | 13 +++++++ .../lib/solidus/migrations/rename_gateways.rb | 38 +++++++++++++++++++ .../lib/tasks/migrations/rename_gateways.rake | 19 ++++++++++ 3 files changed, 70 insertions(+) create mode 100644 core/db/migrate/20170608074534_rename_bogus_gateways.rb create mode 100644 core/lib/solidus/migrations/rename_gateways.rb create mode 100644 core/lib/tasks/migrations/rename_gateways.rake diff --git a/core/db/migrate/20170608074534_rename_bogus_gateways.rb b/core/db/migrate/20170608074534_rename_bogus_gateways.rb new file mode 100644 index 00000000000..7a3da21eb61 --- /dev/null +++ b/core/db/migrate/20170608074534_rename_bogus_gateways.rb @@ -0,0 +1,13 @@ +class RenameBogusGateways < ActiveRecord::Migration[5.0] + def up + say_with_time 'Renaming bogus gateways into payment methods' do + Rake::Task['solidus:migrations:rename_gateways:up'].invoke + end + end + + def down + say_with_time 'Renaming bogus payment methods into gateways' do + Rake::Task['solidus:migrations:rename_gateways:down'].invoke + end + end +end diff --git a/core/lib/solidus/migrations/rename_gateways.rb b/core/lib/solidus/migrations/rename_gateways.rb new file mode 100644 index 00000000000..fef103cb9e9 --- /dev/null +++ b/core/lib/solidus/migrations/rename_gateways.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +module Solidus + module Migrations + class RenameGateways + DEFAULT_MAPPING = { + 'Spree::Gateway::Bogus' => 'Spree::PaymentMethod::BogusCreditCard', + 'Spree::Gateway::BogusSimple' => 'Spree::PaymentMethod::SimpleBogusCreditCard' + } + + attr_reader :gateway_mapping + + def initialize(gateway_mapping = DEFAULT_MAPPING) + @gateway_mapping = gateway_mapping + end + + def up + gateway_mapping.inject(0) do |count, mapping| + count + update(from: mapping[0], to: mapping[1]) + end + end + + def down + gateway_mapping.inject(0) do |count, mapping| + count + update(from: mapping[1], to: mapping[0]) + end + end + + private + + def update(from:, to:) + ActiveRecord::Base.connection.update <<-SQL.strip_heredoc + UPDATE spree_payment_methods SET type = '#{to}' WHERE type = '#{from}'; + SQL + end + end + end +end diff --git a/core/lib/tasks/migrations/rename_gateways.rake b/core/lib/tasks/migrations/rename_gateways.rake new file mode 100644 index 00000000000..a62bed55a5e --- /dev/null +++ b/core/lib/tasks/migrations/rename_gateways.rake @@ -0,0 +1,19 @@ +require 'solidus/migrations/rename_gateways' + +namespace 'solidus:migrations:rename_gateways' do + task up: :environment do + count = Solidus::Migrations::RenameGateways.new.up + + unless ENV['VERBOSE'] == 'false' || !verbose + puts "Renamed #{count} gateways into payment methods." + end + end + + task down: :environment do + count = Solidus::Migrations::RenameGateways.new.down + + unless ENV['VERBOSE'] == 'false' || !verbose + puts "Renamed #{count} payment methods into gateways." + end + end +end