Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate all reusable credit card to wallet sources #1765

Merged
merged 1 commit into from
Mar 16, 2017

Conversation

jhawthorn
Copy link
Contributor

Follow up to #1707

The behaviour prior to adding the Wallet was to allow reuse of any CreditCard which had a payment profile. However the migration was only migrating CreditCards marked default.

This now migrates all credit cards with an associated gateway_customer_profile_id.

@jhawthorn jhawthorn requested a review from jordan-brough March 13, 2017 21:42
Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

default: credit_card.read_attribute(:default),
)
payment_source: credit_card
) do |wallet_source|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL 👍

def up
credit_cards = Spree::CreditCard.
where(default: true).
where("gateway_customer_profile_id IS NOT NULL").
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where.not(gateway_customer_profile_id: nil)

Copy link
Contributor

@jordan-brough jordan-brough left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fixes! I left a few questions/comments.

class WalletPaymentSource < ActiveRecord::Base
self.table_name = 'spree_wallet_payment_sources'
end

def up
credit_cards = Spree::CreditCard.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you intend to use CreditCard here instead of Spree::CreditCard?

where.not(user_id: nil)

credit_cards.find_each do |credit_card|
Spree::WalletPaymentSource.create!(
Spree::WalletPaymentSource.find_or_create_by!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WalletPaymentSource instead of Spree::WalletPaymentSource?

)
payment_source: credit_card
) do |wallet_source|
wallet_source.default = credit_card[:default]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this in a block instead of alongside the other attributes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you use your in-migration classes you can just do credit_card.default right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing a block to find_or_create_by behaves the same as passing a block to create if the record doesn't already exist.

We want to do this here, because we absolutely only want to have one WalletPaymentSource per credit card, the block specifies the values if one doesn't already exist. This makes the up migration idempotent, which we want since the down migration doesn't remove WalletPaymentSources.

Copy link
Contributor

@jordan-brough jordan-brough Mar 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I stared at this at least 10 times and still managed to miss the fact that you'd changed it from create to find_or_create_by :)

@@ -19,7 +27,7 @@ def down
add_column :spree_credit_cards, :default, :boolean, default: false, null: false

wallet_payment_sources = Spree::WalletPaymentSource.
where(default: true, source_type: 'Spree::CreditCard').
where(default: true, payment_source_type: 'Spree::CreditCard').
includes(:source)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'll need some tweaking for the includes to work with the in-migration classes right?

payment_source: credit_card
) do |wallet_source|
wallet_source.default = credit_card[:default]
end
end

remove_column :spree_credit_cards, :default, :boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should hold off on actually dropping this column for another release.
Looking at it again right now I feel nervous about dropping a payment-related column like that. Holding off would make the "down" migration simpler also.
Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 As long as we provide deprecations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Spree::CreditCard#default method does have deprecations, but if someone was doing a Spree::CreditCard.where(default: true) I'm not sure how we could add a deprecation for that. Perhaps we could rename the column to deprecated_default instead of dropping it? Or just go ahead and drop it?

user_id: credit_card.user_id,
payment_source: credit_card
) do |wallet_source|
wallet_source.default = credit_card.default]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unexpected token tRBRACK

@jhawthorn
Copy link
Contributor Author

@jordan-brough I think I have addressed your feedback. It no longer removes the spree_credit_cards.default column, which eliminates the need for the down migrations's behaviour. It now only uses the CreditCard and WalletPaymentSource defined in the migration.

The behaviour prior to adding the Wallet was to allow reuse of any
CreditCard which had a payment profile. However the migration was only
migrating CreditCards marked "default".

This now migrates all credit cards with an associated
gateway_customer_profile_id.

This also no longer removes the "default" column from the credit card,
as we decided this was safer to leave in for at least one release.
Copy link
Contributor

@jordan-brough jordan-brough left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Thanks!

Copy link
Contributor

@cbrunsdon cbrunsdon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable to me, thanks.

@jhawthorn jhawthorn merged commit 7bade4c into solidusio:master Mar 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants