From 832fbb7d5d72cafa94d321fc6c1d5396d2e61cfb Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 5 May 2021 11:47:47 +0100 Subject: [PATCH] Update data migration model definitions The good_migrations gem was not working with polymorphic associations. --- ...6161242_migrate_enterprise_fee_tax_amounts.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/db/migrate/20210406161242_migrate_enterprise_fee_tax_amounts.rb b/db/migrate/20210406161242_migrate_enterprise_fee_tax_amounts.rb index 3e7f8fa4c3..b97283d85e 100644 --- a/db/migrate/20210406161242_migrate_enterprise_fee_tax_amounts.rb +++ b/db/migrate/20210406161242_migrate_enterprise_fee_tax_amounts.rb @@ -1,3 +1,8 @@ +# It turns out the good_migrations gem doesn't play nicely with loading classes on polymorphic +# associations. The only workaround seems to be to load the class explicitly, which essentially +# skips the whole point of good_migrations... :/ +require 'enterprise_fee' + class MigrateEnterpriseFeeTaxAmounts < ActiveRecord::Migration[5.0] class Spree::Adjustment < ApplicationRecord belongs_to :originator, -> { with_deleted }, polymorphic: true @@ -8,9 +13,6 @@ class MigrateEnterpriseFeeTaxAmounts < ActiveRecord::Migration[5.0] scope :enterprise_fee, -> { where(originator_type: 'EnterpriseFee') } end - class EnterpriseFee < ApplicationRecord - belongs_to :tax_category, class_name: 'Spree::TaxCategory', foreign_key: 'tax_category_id' - end class Spree::LineItem < ApplicationRecord belongs_to :variant, class_name: "Spree::Variant" has_one :product, through: :variant @@ -23,6 +25,14 @@ class MigrateEnterpriseFeeTaxAmounts < ActiveRecord::Migration[5.0] belongs_to :tax_category, class_name: 'Spree::TaxCategory' has_many :variants, class_name: 'Spree::Variant' end + class Spree::TaxCategory < ApplicationRecord + has_many :tax_rates, dependent: :destroy, inverse_of: :tax_category + end + class Spree::TaxRate < ApplicationRecord + belongs_to :zone, class_name: "Spree::Zone", inverse_of: :tax_rates + belongs_to :tax_category, class_name: "Spree::TaxCategory", inverse_of: :tax_rates + has_many :adjustments, as: :originator + end def up migrate_enterprise_fee_taxes!