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

Avoid running validations in current_order #2068

Merged
merged 1 commit into from Jul 10, 2017

Conversation

jhawthorn
Copy link
Contributor

@jhawthorn jhawthorn commented Jul 7, 2017

Previously we updated the IP address on each request by calling

update(last_ip_address: ip_address)

Though this doesn't run a SQL UPDATE if not necessary, it will still run validations (resulting in two SELECTs).

This commit adds a new record_ip_address(ip_address) method to Spree::Order, which will only update the ip_address if it has changed.

This also switches update to update_attributes!, so that any errors will be raised.

Before

  Spree::Order Load (0.2ms)  SELECT  "spree_orders".* FROM "spree_orders" WHERE "spree_orders"."completed_at" IS NULL AND "spree_orders"."currency" = ? AND "spree_orders"."guest_token" = ? AND "spree_orders"."store_id" = ? AND "spree_orders"."user_id" = ? LIMIT ?  [["currency", "USD"], ["guest_token", "aCYxT9EsjJSLEoxCaFLCmA"], ["store_id", 1], ["user_id", 1], ["LIMIT", 1]]
  Spree::Adjustment Load (0.1ms)  SELECT "spree_adjustments".* FROM "spree_adjustments" WHERE "spree_adjustments"."adjustable_type" = ? AND "spree_adjustments"."adjustable_id" = 5 ORDER BY "spree_adjustments"."created_at" ASC  [["adjustable_type", "Spree::Order"]]
   (0.0ms)  begin transaction
  Spree::Store Load (0.0ms)  SELECT  "spree_stores".* FROM "spree_stores" WHERE "spree_stores"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Spree::Order Exists (0.1ms)  SELECT  1 AS one FROM "spree_orders" WHERE "spree_orders"."number" = ? AND ("spree_orders"."id" != ?) LIMIT ?  [["number", "R097935381"], ["id", 5], ["LIMIT", 1]]
   (0.0ms)  commit transaction

After

  Spree::Order Load (0.2ms)  SELECT  "spree_orders".* FROM "spree_orders" WHERE "spree_orders"."completed_at" IS NULL AND "spree_orders"."currency" = ? AND "spree_orders"."guest_token" = ? AND "spree_orders"."store_id" = ? AND "spree_orders"."user_id" = ? LIMIT ?  [["currency", "USD"], ["guest_token", "aCYxT9EsjJSLEoxCaFLCmA"], ["store_id", 1], ["user_id", 1], ["LIMIT", 1]]
  Spree::Adjustment Load (0.2ms)  SELECT "spree_adjustments".* FROM "spree_adjustments" WHERE "spree_adjustments"."adjustable_type" = ? AND "spree_adjustments"."adjustable_id" = 5 ORDER BY "spree_adjustments"."created_at" ASC  [["adjustable_type", "Spree::Order"]]

Previously we updated the IP address on each request by calling

    update(last_ip_address: ip_address)

Though this doesn't run a SQL UPDATE if not necessary, it will still run
validations (resulting in two SELECTs).

This commit adds a new record_ip_address(ip_address) method to
Spree::Order, which will only update the ip_address if it has changed.

This also switches `update` to `update_attributes!`, so that any errors
will be raised.
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.

👍

@@ -742,6 +742,12 @@ def add_default_payment_from_wallet
alias_method :assign_default_credit_card, :add_default_payment_from_wallet
deprecate assign_default_credit_card: :add_default_payment_from_wallet, deprecator: Spree::Deprecation

def record_ip_address(ip_address)
if last_ip_address != ip_address
update_attributes!(last_ip_address: ip_address)
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

We've overriden update!. :)

Copy link
Member

Choose a reason for hiding this comment

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

We are strange :)

@gmacdougall gmacdougall merged commit 297a873 into solidusio:master Jul 10, 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.

None yet

5 participants