From 4e655d786691da490acfe2c0c660aa79f1f90b5b Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 12 Nov 2014 14:39:59 +1100 Subject: [PATCH] Uncouple migrations from models --- ...30919010513_ensure_shipping_methods_have_distributors.rb | 4 ++++ db/migrate/20140402033428_add_foreign_keys.rb | 6 +++++- db/migrate/20140828023619_add_owner_to_enterprise.rb | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/db/migrate/20130919010513_ensure_shipping_methods_have_distributors.rb b/db/migrate/20130919010513_ensure_shipping_methods_have_distributors.rb index 271fd5f9df..e233227242 100644 --- a/db/migrate/20130919010513_ensure_shipping_methods_have_distributors.rb +++ b/db/migrate/20130919010513_ensure_shipping_methods_have_distributors.rb @@ -1,4 +1,8 @@ class EnsureShippingMethodsHaveDistributors < ActiveRecord::Migration + class Enterprise < ActiveRecord::Base + scope :is_distributor, where(is_distributor: true) + end + def up d = Enterprise.is_distributor.first sms = Spree::ShippingMethod.where('distributor_id IS NULL') diff --git a/db/migrate/20140402033428_add_foreign_keys.rb b/db/migrate/20140402033428_add_foreign_keys.rb index fce1ffd009..1535f4f3f9 100644 --- a/db/migrate/20140402033428_add_foreign_keys.rb +++ b/db/migrate/20140402033428_add_foreign_keys.rb @@ -1,12 +1,15 @@ class AddForeignKeys < ActiveRecord::Migration class AdjustmentMetadata < ActiveRecord::Base; end + class CoordinatorFee < ActiveRecord::Base; end + class Enterprise < ActiveRecord::Base + belongs_to :address, :class_name => 'Spree::Address' + end class ExchangeVariant < ActiveRecord::Base; end class Spree::InventoryUnit < ActiveRecord::Base; end class Spree::LineItem < ActiveRecord::Base; end class Spree::Address < ActiveRecord::Base; end class Spree::Order < ActiveRecord::Base; end class Spree::Taxon < ActiveRecord::Base; end - class CoordinatorFee < ActiveRecord::Base; end def change setup_foreign_keys @@ -60,6 +63,7 @@ class AddForeignKeys < ActiveRecord::Migration address = Spree::Address.create!(firstname: 'Dummy distributor', lastname: 'Dummy distributor', phone: '12345678', state: state, address1: 'Dummy distributor', city: 'Dummy distributor', zipcode: '1234', country: country) + Enterprise.reset_column_information deleted_distributor = Enterprise.create!(name: "Deleted distributor", address: address) orphaned_orders = Spree::Order.joins('LEFT OUTER JOIN enterprises ON enterprises.id=spree_orders.distributor_id').where('enterprises.id IS NULL') diff --git a/db/migrate/20140828023619_add_owner_to_enterprise.rb b/db/migrate/20140828023619_add_owner_to_enterprise.rb index 8491e9c599..d59cd2250f 100644 --- a/db/migrate/20140828023619_add_owner_to_enterprise.rb +++ b/db/migrate/20140828023619_add_owner_to_enterprise.rb @@ -7,7 +7,7 @@ class AddOwnerToEnterprise < ActiveRecord::Migration owner = e.users.find{ |u| !u.admin? } admin_owner = e.users.find &:admin? any_admin = Spree::User.admin.first - any_user = Spree::User.first + any_user = Spree::User.first || Spree::User.new(email: 'owner@example.com', password: 'owner123').tap { |u| u.save(validate: false) } e.update_column :owner_id, (owner || admin_owner || any_admin || any_user ) end