Merge pull request #6377 from luisramos0/repeat_calculator_migrations

Repeat calculator migrations to clean up existing data
This commit is contained in:
Luis Ramos
2020-11-16 10:46:42 +00:00
committed by GitHub
2 changed files with 64 additions and 0 deletions

View File

@@ -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

View File

@@ -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