diff --git a/db/migrate/20200721135726_move_calculators_preferences_outside_spree_namespace.rb b/db/migrate/20200721135726_move_calculators_preferences_outside_spree_namespace.rb new file mode 100644 index 0000000000..82f5094273 --- /dev/null +++ b/db/migrate/20200721135726_move_calculators_preferences_outside_spree_namespace.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 8df4304ca2..f56c95f405 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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"