Adding customer reference to orders

This commit is contained in:
Rob Harrington
2015-05-13 11:39:17 +10:00
parent ed941e211d
commit 1559b4e30a
2 changed files with 20 additions and 1 deletions

View File

@@ -0,0 +1,16 @@
class AddCustomerToOrders < ActiveRecord::Migration
def change
add_column :spree_orders, :customer_id, :integer
add_index :spree_orders, :customer_id
add_foreign_key :spree_orders, :customers, column: :customer_id
Spree::Order.where("spree_orders.email IS NOT NULL AND distributor_id IS NOT NULL AND customer_id IS NULL").each do |order|
customer = Customer.find_by_email_and_enterprise_id(order.email, order.distributor_id)
unless customer.present?
user = Spree::User.find_by_email(order.email)
customer = Customer.create!(email: order.email, enterprise_id: order.distributor_id, user_id: user.andand.id )
end
order.update_attribute(:customer, customer)
end
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20150508072454) do
ActiveRecord::Schema.define(:version => 20150508072938) do
create_table "adjustment_metadata", :force => true do |t|
t.integer "adjustment_id"
@@ -623,8 +623,10 @@ ActiveRecord::Schema.define(:version => 20150508072454) do
t.string "currency"
t.string "last_ip_address"
t.integer "cart_id"
t.integer "customer_id"
end
add_index "spree_orders", ["customer_id"], :name => "index_spree_orders_on_customer_id"
add_index "spree_orders", ["number"], :name => "index_orders_on_number"
create_table "spree_payment_methods", :force => true do |t|
@@ -1206,6 +1208,7 @@ ActiveRecord::Schema.define(:version => 20150508072454) do
add_foreign_key "spree_option_values_variants", "spree_variants", name: "spree_option_values_variants_variant_id_fk", column: "variant_id"
add_foreign_key "spree_orders", "carts", name: "spree_orders_cart_id_fk"
add_foreign_key "spree_orders", "customers", name: "spree_orders_customer_id_fk"
add_foreign_key "spree_orders", "enterprises", name: "spree_orders_distributor_id_fk", column: "distributor_id"
add_foreign_key "spree_orders", "order_cycles", name: "spree_orders_order_cycle_id_fk"
add_foreign_key "spree_orders", "spree_addresses", name: "spree_orders_bill_address_id_fk", column: "bill_address_id"