mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Merge pull request #6377 from luisramos0/repeat_calculator_migrations
Repeat calculator migrations to clean up existing data
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user