diff --git a/db/migrate/20201113163155_repeat_move_all_calculators_outside_the_spree_namespace.rb b/db/migrate/20201113163155_repeat_move_all_calculators_outside_the_spree_namespace.rb new file mode 100644 index 0000000000..60b127245f --- /dev/null +++ b/db/migrate/20201113163155_repeat_move_all_calculators_outside_the_spree_namespace.rb @@ -0,0 +1,39 @@ +# For some unkonwn reason, after removing Spree as a dependency, some spree calculators appeared on live DBs +# Here we repeat the migration +class RepeatMoveAllCalculatorsOutsideTheSpreeNamespace < ActiveRecord::Migration + def up + convert_calculator("DefaultTax") + convert_calculator("FlatPercentItemTotal") + convert_calculator("FlatRate") + convert_calculator("FlexiRate") + convert_calculator("PerItem") + convert_calculator("PriceSack") + end + + def down + revert_calculator("DefaultTax") + revert_calculator("FlatPercentItemTotal") + revert_calculator("FlatRate") + revert_calculator("FlexiRate") + revert_calculator("PerItem") + revert_calculator("PriceSack") + end + + private + + def convert_calculator(calculator_base_name) + update_calculator("Spree::Calculator::" + calculator_base_name, + "Calculator::" + calculator_base_name) + end + + def revert_calculator(calculator_base_name) + update_calculator("Calculator::" + calculator_base_name, + "Spree::Calculator::" + calculator_base_name) + end + + def update_calculator(from, to) + Spree::Calculator.connection.execute( + "UPDATE spree_calculators SET type = '" + to + "' WHERE type = '" + from + "'" + ) + end +end diff --git a/db/migrate/20201113163227_repeat_move_calculators_preferences_outside_spree_namespace.rb b/db/migrate/20201113163227_repeat_move_calculators_preferences_outside_spree_namespace.rb new file mode 100644 index 0000000000..24adad016b --- /dev/null +++ b/db/migrate/20201113163227_repeat_move_calculators_preferences_outside_spree_namespace.rb @@ -0,0 +1,25 @@ +# For some unkonwn reason, after removing Spree as a dependency, some spree calculators appeared on live DBs +# Here we repeat the migration +class RepeatMoveCalculatorsPreferencesOutsideSpreeNamespace < ActiveRecord::Migration + def up + replace_preferences_key("/spree/calculator", "/calculator") + end + + def down + replace_preferences_key("/calculator", "/spree/calculator") + end + + private + + def replace_preferences_key(from_pattern, to_pattern) + updated_pref_key = "replace( pref.key, '" + from_pattern + "', '" + to_pattern + "')" + Spree::Preference.connection.execute( + "UPDATE spree_preferences pref SET key = " + updated_pref_key + " + WHERE pref.key like '" + from_pattern + "%' + AND NOT EXISTS (SELECT 1 FROM spree_preferences existing_pref + WHERE existing_pref.key = " + updated_pref_key + ")" + ) + + Rails.cache.clear + end +end