Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promo migration fix 2 #1131

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions core/db/migrate/20120119024710_new_preferences.rb
Expand Up @@ -6,7 +6,6 @@ class OldPrefs < ActiveRecord::Base
attr_accessor :owner_klass
end


def up
add_column :spree_preferences, :key, :string
add_column :spree_preferences, :value_type, :string
Expand All @@ -21,18 +20,20 @@ def up
change_column :spree_preferences, :group_id, :integer, :null => true
change_column :spree_preferences, :group_type, :string, :null => true

cfgs = execute("select id, type from spree_configurations")

execute("select id, owner_id, name from spree_preferences where owner_type = 'Spree::Configuration'").each do |pref|
configuration = cfgs.detect { |c| c[0] == pref[1] }
execute "UPDATE spree_preferences set `key` = '#{configuration[1].underscore}/#{pref[2]}', `owner_type` = null, `owner_id` = null where id = #{pref[0]}"
spree_config = Spree::AppConfiguration.new
Spree::Preference.where(:owner_type => 'Spree::Configuration').each do |preference|
preference.key = spree_config.preference_cache_key(preference.name)
preference.value_type = spree_config.preference_type(preference.name)
say preference.inspect
say preference.save(:validate => false)
say preference.errors.inspect
end

OldPrefs.all.each do |old_pref|
next unless owner = (old_pref.owner rescue nil)

unless old_pref.owner_type == "Spree::Activator" || old_pref.owner_type == "Spree::PromotionRule"
unless old_pref.owner_type == "Spree::Activator" || old_pref.owner_type == "Spree::Configuration"
old_pref.key = [owner.class.name, old_pref.name, owner.id].join('::').underscore
old_pref.value_type = owner.preference_type(old_pref.name)
say "Migrating Preference: #{old_pref.key}"
old_pref.save
end
Expand Down
24 changes: 24 additions & 0 deletions promo/db/migrate/20120119024707_namespace_promo_tables.rb
Expand Up @@ -29,6 +29,12 @@ def replace_column_data(table_names, column_name)
end
end

class OldPrefs < ActiveRecord::Base
set_table_name "spree_preferences"
belongs_to :owner, :polymorphic => true
attr_accessor :owner_klass
end

def self.up
# namespace promo tables
rename_table :promotion_actions, :spree_promotion_actions
Expand All @@ -47,10 +53,28 @@ def self.up
add_column :spree_activators, :advertise, :boolean, :default => false

Spree::Preference.where(:owner_type => 'Spree::Activator').each do |preference|
preference.value_type = case preference.name
when 'usage_limit'
:integer
when 'advertise'
:boolean
else
:string
end
@activator = Spree::Activator.find(preference.owner_id)
@activator.update_attribute(preference.name, preference.value)
preference.destroy
end

OldPrefs.where(:owner_type => 'Spree::PromotionRule').each do |old_pref|
owner = old_pref.owner
old_pref.key = [owner.class.name, old_pref.name, owner.id].join('::').underscore
old_pref.value_type = owner.preference_type(old_pref.name)
say "Migrating Preference: #{old_pref.key}"
old_pref.save(:validate => false)
end


# This *should* be in the new_preferences migration inside of Core but...
# This is migration needs to have these keys around so that
# we can convert the promotions over correctly.
Expand Down