Add migration to fix calculator preferences

This commit is contained in:
Luis Ramos
2020-07-21 15:37:16 +01:00
parent 6cc91d457e
commit 1b31b727c7
2 changed files with 24 additions and 1 deletions

View File

@@ -0,0 +1,23 @@
# As we moved the calculators outside the Spree namespace in migration MoveAllCalculatorsOutsideTheSpreeNamespace
# We need to move their preferences too (currency, value, etc), otherwise they are not used
class MoveCalculatorsPreferencesOutsideSpreeNamespace < 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 + ")"
)
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20200702112157) do
ActiveRecord::Schema.define(version: 20200721135726) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"