Sha256: 0ad923ee34bf839bdf97262d00003ac29bb6c2f3fd850a0e2dc291803a2d4a06

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

class AddNullConstraintToSources < SolidusSupport::Migration[4.2]
  class SpreePayment < ActiveRecord::Base
  end

  class SolidusPaypalBraintreeSource < ActiveRecord::Base
  end

  def up
    payments = SpreePayment.arel_table
    sources = SolidusPaypalBraintreeSource.arel_table
    join_sources = payments.join(sources).on(
      payments[:source_id].eq(sources[:id]).and(
        payments[:source_type].eq("SolidusPaypalBraintree::Source")
      ).and(
        sources[:payment_method_id].eq(nil)
      )
    ).join_sources

    count = SpreePayment.joins(join_sources).count
    Rails.logger.info("Updating #{count} problematic sources")

    SpreePayment.joins(join_sources).find_each do |payment|
      Rails.logger.info("Updating source #{payment.source_id} with payment method id #{payment.payment_method_id}")
      SolidusPaypalBraintreeSource.where(id: payment.source_id).update_all(
        payment_method_id: payment.payment_method_id
      )
    end

    # We use a foreign key constraint on the model,
    # but it doesnt make sense to have this model exist without a payment method
    # as two of its methods delegate to the payment method.
    change_column_null(:solidus_paypal_braintree_sources, :payment_method_id, false)
  end

  def down
    change_column_null(:solidus_paypal_braintree_sources, :payment_method_id, true)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
solidus_braintree-3.3.0 db/migrate/20170505193712_add_null_constraint_to_sources.rb
solidus_braintree-3.2.0 db/migrate/20170505193712_add_null_constraint_to_sources.rb
solidus_braintree-3.1.0 db/migrate/20170505193712_add_null_constraint_to_sources.rb
solidus_braintree-3.0.1 db/migrate/20170505193712_add_null_constraint_to_sources.rb
solidus_braintree-3.0.0 db/migrate/20170505193712_add_null_constraint_to_sources.rb
solidus_braintree-2.0.0 db/migrate/20170505193712_add_null_constraint_to_sources.rb