Sha256: 1e1e488213a08414b35b8ecd40569529ee83cf27654a09e5fb06fa97943423a3

Contents?: true

Size: 1.6 KB

Versions: 25

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true
# This migration comes from spree (originally 20170412103617)

class TransformTaxRateCategoryRelation < ActiveRecord::Migration[5.0]
  class TaxRate < ActiveRecord::Base
    self.table_name = "spree_tax_rates"
  end

  class TaxRateTaxCategory < ActiveRecord::Base
    self.table_name = "spree_tax_rate_tax_categories"
  end

  def up
    create_table :spree_tax_rate_tax_categories do |t|
      t.integer :tax_category_id, index: true, null: false
      t.integer :tax_rate_id, index: true, null: false
    end

    add_foreign_key :spree_tax_rate_tax_categories, :spree_tax_categories, column: :tax_category_id
    add_foreign_key :spree_tax_rate_tax_categories, :spree_tax_rates, column: :tax_rate_id

    TaxRate.where.not(tax_category_id: nil).find_each do |tax_rate|
      TaxRateTaxCategory.create!(
        tax_rate_id: tax_rate.id,
        tax_category_id: tax_rate.tax_category_id
      )
    end

    remove_column :spree_tax_rates, :tax_category_id
  end

  def down
    add_column :spree_tax_rates, :tax_category_id, :integer, index: true
    add_foreign_key :spree_tax_rates, :spree_tax_categories, column: :tax_category_id

    TaxRate.find_each do |tax_rate|
      tax_category_ids = TaxRateTaxCategory.where(tax_rate_id: tax_rate.id).pluck(:tax_category_id)

      tax_category_ids.each_with_index do |category_id, i|
        if i.zero?
          tax_rate.update!(tax_category_id: category_id)
        else
          new_tax_rate = tax_rate.dup
          new_tax_rate.update!(tax_category_id: category_id)
        end
      end
    end

    drop_table :spree_tax_rate_tax_categories
  end
end

Version data entries

25 entries across 25 versions & 9 rubygems

Version Path
solidus_i18n-2.2.0 spec/dummy/db/migrate/20210218213832_transform_tax_rate_category_relation.spree.rb
solidus_i18n-2.1.1 spec/dummy/db/migrate/20210218213832_transform_tax_rate_category_relation.spree.rb
solidus_auth_devise-2.5.4 spec/dummy/db/migrate/20211112052452_transform_tax_rate_category_relation.spree.rb
solidus_reports-1.0.0 spec/dummy/db/migrate/20211108200421_transform_tax_rate_category_relation.spree.rb
solidus_i18n-2.1.0 spec/dummy/db/migrate/20210218213832_transform_tax_rate_category_relation.spree.rb
solidus_auth_devise-2.5.3 spec/dummy/db/migrate/20210129115364_transform_tax_rate_category_relation.spree.rb
solidus_auth_devise-2.5.2 spec/dummy/db/migrate/20201006131555_transform_tax_rate_category_relation.spree.rb
solidus_auth_devise-2.5.1 spec/dummy/db/migrate/20201006131555_transform_tax_rate_category_relation.spree.rb
solidus_auth_devise-2.5.0 spec/dummy/db/migrate/20201006131555_transform_tax_rate_category_relation.spree.rb
solidus_reviews-1.4.1 spec/dummy/db/migrate/20200810122121_transform_tax_rate_category_relation.spree.rb
solidus_billing_address-0.0.7 spec/dummy/db/migrate/20191128105354_transform_tax_rate_category_relation.spree.rb
solidus_billing_address-0.0.6 spec/dummy/db/migrate/20191122155253_transform_tax_rate_category_relation.spree.rb
solidus_drip-0.1.0 spec/dummy/db/migrate/20191101230320_transform_tax_rate_category_relation.spree.rb
solidus_drip-0.0.1 spec/dummy/db/migrate/20191101230320_transform_tax_rate_category_relation.spree.rb
solidus_billing_address-0.0.5 spec/dummy/db/migrate/20191028084766_transform_tax_rate_category_relation.spree.rb
solidus_billing_address-0.0.4 spec/dummy/db/migrate/20190926134957_transform_tax_rate_category_relation.spree.rb
solidus_billing_address-0.0.3 spec/dummy/db/migrate/20190924075651_transform_tax_rate_category_relation.spree.rb
solidus_billing_address-0.0.2 spec/dummy/db/migrate/20190920154641_transform_tax_rate_category_relation.spree.rb
solidus_billing_address-0.0.1 spec/dummy/db/migrate/20190823131017_transform_tax_rate_category_relation.spree.rb
solidus_bank_transfer-0.1.0 spec/dummy/db/migrate/20190822093451_transform_tax_rate_category_relation.spree.rb