From 91e4f99c19787ecd48328554acf09be55a7b1e96 Mon Sep 17 00:00:00 2001 From: Pedro Costa Date: Sun, 28 Oct 2018 20:20:15 +0000 Subject: [PATCH] Fix new preferences migration Why: * In a clean environment, running the new preferences migration fails, due to a missing file from Spree core being manually required. This file has now been missing since ab707cf. This change addresses the issue by: * Copying the missing file from Spree Core 1.3.6.beta into the migration. This fixes the issue for now, but also means that a migration merge and/or rewrite might be in order for the future. --- db/migrate/20120327000645_new_preferences.rb | 37 ++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/db/migrate/20120327000645_new_preferences.rb b/db/migrate/20120327000645_new_preferences.rb index 60fd79519e..86e5dbbf57 100644 --- a/db/migrate/20120327000645_new_preferences.rb +++ b/db/migrate/20120327000645_new_preferences.rb @@ -1,4 +1,37 @@ -require 'spree/core/preference_rescue' +# Spree 1.3.6.beta preference rescue implementation, required for the new +# preferences migration, which is broken since commit ab707cf due to the +# absence of this file. +# +# Migration: db/migrate/20120327000645_new_preferences.rb +# Source: https://raw.githubusercontent.com/spree/spree/1-3-stable/core/lib/spree/core/preference_rescue.rb +# +# rubocop:disable all +module Spree + class OldPrefs < ActiveRecord::Base + self.table_name = "spree_preferences" + belongs_to :owner, :polymorphic => true + attr_accessor :owner_klass + end + + class PreferenceRescue + def self.try + OldPrefs.where(:key => nil).each do |old_pref| + next unless owner = (old_pref.owner rescue nil) + unless old_pref.owner_type == "Spree::Activator" || old_pref.owner_type == "Spree::Configuration" + begin + old_pref.key = [owner.class.name, old_pref.name, owner.id].join('::').underscore + old_pref.value_type = owner.preference_type(old_pref.name) + puts "Migrating Preference: #{old_pref.key}" + old_pref.save + rescue NoMethodError => ex + puts ex.message + end + end + end + end + end +end +# rubocop:enable all class NewPreferences < ActiveRecord::Migration @@ -45,4 +78,4 @@ class NewPreferences < ActiveRecord::Migration add_column :spree_preferences, :group_id, :integer add_column :spree_preferences, :group_type, :string end -end \ No newline at end of file +end