Skip to content

Instantly share code, notes, and snippets.

@pdamer
Created October 12, 2012 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdamer/3880089 to your computer and use it in GitHub Desktop.
Save pdamer/3880089 to your computer and use it in GitHub Desktop.
Migrate Promotions in spree upgrade to .7
class CreateActivators < ActiveRecord::Migration
def self.up
rename_table 'promotions', 'activators'
rename_column 'activators', 'code', 'to_migrate_code'
rename_column 'activators', 'usage_limit', 'to_migrate_usage_limit'
rename_column 'activators', 'combine', 'to_migrate_combine'
rename_column 'activators', 'match_policy', 'to_migrate_match_policy'
add_column 'activators', 'type', :string
add_column 'activators', 'event_name', :string
execute "UPDATE activators set type = 'Promotion'"
execute "UPDATE activators set event_name = 'spree.checkout.coupon_code_added'"
execute "UPDATE calculators set calculable_type = 'Activator' where calculable_type = 'Promotion'"
end
def self.down
drop_table 'activators';
create_table "promotions", :force => true do |t|
t.string "name"
t.string "code"
t.string "description"
t.integer "usage_limit"
t.boolean "combine"
t.datetime "expires_at"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "starts_at"
t.string "match_policy", :default => "all"
end
end
end
class PromotionChangesToSubclassOfActivator < ActiveRecord::Migration
def self.up
rename_column :promotion_rules, :promotion_id, :activator_id
end
def self.down
rename_column :promotion_rules, :activator_id, :promotion_id
end
end
class CreatePromotionActions < ActiveRecord::Migration
def self.up
create_table :promotion_actions do |t|
t.integer :activator_id
t.integer :position
t.string :type
end
Promotion.class_eval do
has_one :calculator, :as => :calculable, :dependent => :destroy
end
Promotion.all.each do |promo|
promo.actions << Promotion::Actions::CreateAdjustment.create!
c = promo.calculator
c.calculable = promo.actions.first
c.save!
promo.code = promo.to_migrate_code
promo.match_policy = promo.to_migrate_match_policy
promo.usage_limit = promo.to_migrate_usage_limit
promo.name = promo.description.first(25) if promo.name.blank?
promo.name = 'promotion name' if promo.name.blank?
promo.save!
end
remove_column 'activators', 'to_migrate_code'
remove_column 'activators', 'to_migrate_usage_limit'
remove_column 'activators', 'to_migrate_combine'
remove_column 'activators', 'to_migrate_match_policy'
end
def self.down
drop_table :promotion_actions
add_column 'activators', 'to_migrate_code'
add_column 'activators', 'to_migrate_usage_limit'
add_column 'activators', 'to_migrate_combine'
add_column 'activators', 'to_migrate_match_policy'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment