Sha256: 15b5afd02dd7d1611107d45f3ef9d93b5537ad7d19093516ddf66d13b451369d

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

class AddNullConstraintToSources < SolidusSupport::Migration[4.2]
  def up
    payments = Spree::Payment.arel_table
    sources = SolidusPaypalBraintree::Source.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 = Spree::Payment.joins(join_sources).count
    Rails.logger.info("Updating #{count} problematic sources")

    Spree::Payment.joins(join_sources).find_each do |payment|
      Rails.logger.info("Updating source #{payment.source_id} with payment method id #{payment.payment_method_id}")
      SolidusPaypalBraintree::Source.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

5 entries across 5 versions & 1 rubygems

Version Path
solidus_paypal_braintree-1.2.0 db/migrate/20170505193712_add_null_constraint_to_sources.rb
solidus_paypal_braintree-1.1.2 db/migrate/20170505193712_add_null_constraint_to_sources.rb
solidus_paypal_braintree-1.1.1 db/migrate/20170505193712_add_null_constraint_to_sources.rb
solidus_paypal_braintree-1.1.0 db/migrate/20170505193712_add_null_constraint_to_sources.rb
solidus_paypal_braintree-1.0.0 db/migrate/20170505193712_add_null_constraint_to_sources.rb