Skip to content

Avoid unnecessary adjustment persistence in tax calculations#6315

Merged
tvdeyen merged 2 commits intosolidusio:mainfrom
SuperGoodSoft:avoid-unnecessary-adjustment-persistence-in-tax-calculations
Sep 9, 2025
Merged

Avoid unnecessary adjustment persistence in tax calculations#6315
tvdeyen merged 2 commits intosolidusio:mainfrom
SuperGoodSoft:avoid-unnecessary-adjustment-persistence-in-tax-calculations

Conversation

@benjaminwil
Copy link
Contributor

Summary

These commits were originally part of the work on the in-memory order updater (#5872).

Rather than updating or deleting adjustment records on recalculate, we can assign new attributes or mark the records for destruction and reduce the total distinct writes necessary to recalculate an order during updates.

Checklist

@benjaminwil benjaminwil requested a review from a team as a code owner August 8, 2025 17:52
@github-actions github-actions bot added the changelog:solidus_core Changes to the solidus_core gem label Aug 8, 2025
@codecov
Copy link

codecov bot commented Aug 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.35%. Comparing base (1f0a28f) to head (c2cd061).
⚠️ Report is 11 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6315   +/-   ##
=======================================
  Coverage   89.35%   89.35%           
=======================================
  Files         961      961           
  Lines       20180    20181    +1     
=======================================
+ Hits        18031    18032    +1     
  Misses       2149     2149           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@benjaminwil benjaminwil force-pushed the avoid-unnecessary-adjustment-persistence-in-tax-calculations branch from a389114 to 805c295 Compare August 13, 2025 17:16
@jarednorman jarednorman requested a review from a team August 26, 2025 16:48
Harmony Bouvier and others added 2 commits September 5, 2025 17:59
To support the in-memory order updater, we want adjustments to be
modified without writing to the database immediately.

We added `autosave: true` in the association on line_items ensures that
the adjustment amount will be saved with the line_item, or by extension
the order when it is eventually persisted by the order updater.

The tests for the OrderTaxation have been updated to use `size` instead
of `count` to ensure an in-memory reported count/size.

Co-authored-by: Adam Mueller <adam@super.gd>
Co-authored-by: Andrew Stewart <andrew@super.gd>
Co-authored-by: Jared Norman <jared@super.gd>
Co-authored-by: Noah Silvera <noah@super.gd>
Co-authored-by: benjamin wil <benjamin@super.gd>
Co-authored-by: Alistair Norman <alistair@super.gd>
Instead of actually destroying adjustments in the OrderTaxation class,
we can just mark them for removal. This will allow them to be removed
when the order is persisted, instead of being removed during the
recalculate process. This is an improvement in service of recalculating
order changes without writing to the database.

We also need to ensure `Order#adjustments` association has autosave
enabled so that the marked for destruction adjustments are removed when
we persist the order.

We're also improving our OrderUpdater tests to better verify our changes
maintain the existing behaviour.

Co-authored-by: Adam Mueller <adam@super.gd>
Co-authored-by: Alistair Norman <alistair@super.gd>
Co-authored-by: Chris Todorov <chris@super.gd>
Co-authored-by: Harmony Bouvier <harmony@super.gd>
Co-authored-by: Kendra Riga <kendra@super.gd>
Co-authored-by: Senem Soy <senem@super.gd>
Co-authored-by: Sofia Besenski <sofia@super.gd>
Co-authored-by: benjamin wil <benjamin@super.gd>
@tvdeyen tvdeyen force-pushed the avoid-unnecessary-adjustment-persistence-in-tax-calculations branch from 805c295 to c2cd061 Compare September 5, 2025 15:59
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.

Amazing! TIL mark_for_destruction

@tvdeyen tvdeyen added this to the 4.6 milestone Sep 5, 2025
@tvdeyen tvdeyen merged commit 17d1ee5 into solidusio:main Sep 9, 2025
38 of 39 checks passed
has_one :product, through: :variant

has_many :adjustments, as: :adjustable, inverse_of: :adjustable, dependent: :destroy
has_many :adjustments, as: :adjustable, inverse_of: :adjustable, dependent: :destroy, autosave: true
Copy link
Contributor

@Noah-Silvera Noah-Silvera Oct 17, 2025

Choose a reason for hiding this comment

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

@benjaminwil @jarednorman we missed autosaving the shipment adjustments in this PR, and it's resulting in shipment tax adjustments not getting persisted correctly in Solidus 4.6.0 .... draft PR incoming to address it!

Noah-Silvera added a commit to SuperGoodSoft/solidus that referenced this pull request Oct 17, 2025
A commit was made in solidusio#6315 (5979f25) in service of the in memory order updater solidusio#5872 that changed the behavior in the OrderTaxation class to set the adjustment value but not persist it. Instead, the persistence happens when the order is saved. If a shipment is changed, and an associated adjustment is changed, the shipment adjustment will be autosaved when the order is.

However, if a shipment is *not* changed, but the adjustment is, the adjustment will *not* be autosaved when the order is.

A scenario where this can happen is a recalculation that just adjusts shipment taxes on an order that would otherwise not change. Let's walk through what happens in the OrderUpdater

1. `update_totals` is called which in turn...
2. calls `update_adjustment_total`, which in turn...
3. calls `recalculate_adjustments` which in turn...
4. Calls `update_tax_adjustments`
- `update_tax_adjustments` creates and sets the values of the tax adjustments for a shipment. At this point, the shipment adjustment has been changed, but not persisted, and the shipment has *not* been changed.
5. Calls `update_item_totals` which calls...
6. `Spree::Config.item_total_class.new(item).recalculate!`
- `Spree::Config.item_total_class` sets the `included_tax_total`, `additional_tax_total` and `adjustment_total` on the shipment to their new values, calculated from the non-persisted adjustment. At this point, both the shipment *and* adjustment have been changed but not persisted.
- If you were to call `order.save!` at this point, the shipment adjustment would autosave.
7. Next, `item.update_columns` is called with the columns that the `item_total_class` updated.
- At this point, the shipment, it's changes having been persisted to the database, will no longer be marked as `changed`.
- This means that the default behavior of autosave in rails will *not* save the shipment when the order is saved, which means the associated (and changed) adjustment will not get saved either

In solidusio#6315, we ensured that these changed associations cannot get left behind by setting `autosave: true`, which forces the model to check if it has any associations that need saving instead of telling it's relation "nope I haven't changed"

We can do the same thing here for the shipment to ensure changing  adjustments will make the shipment tell it's order "I have changed", even if none of it's direct properties have changed.

You can test this change by removing the `autosave: true` option the adjustments and running 'when the address has changed to a different state' specs. They fail with the error "expected `order.shipments.first.adjustments.first.amount` to have changed from 1 to 2, but did not change", demonstrating a scenario where the shipment adjustment changes do not get persisted.
Noah-Silvera added a commit to SuperGoodSoft/solidus that referenced this pull request Oct 17, 2025
A commit was made in solidusio#6315 (5979f25) in service of the in memory order updater solidusio#5872 that changed the behavior in the OrderTaxation class to set the adjustment value but not persist it. Instead, the persistence happens when the order is saved. If a shipment is changed, and an associated adjustment is changed, the shipment adjustment will be autosaved when the order is.

However, if a shipment is *not* changed, but the adjustment is, the adjustment will *not* be autosaved when the order is.

In solidusio#6315, we ensured that these changed associations cannot get left behind by setting `autosave: true`, which forces the model to check if it has any associations that need saving instead of telling it's relation "nope I haven't changed"

We can do the same thing here for the shipment to ensure changing  adjustments will make the shipment tell it's order "I have changed", even if none of it's direct properties have changed.

You can test this change by removing the `autosave: true` option the adjustments and running 'when the address has changed to a different state' specs. They fail with the error "expected `order.shipments.first.adjustments.first.amount` to have changed from 1 to 2, but did not change", demonstrating a scenario where the shipment adjustment changes do not get persisted.
Noah-Silvera added a commit to SuperGoodSoft/solidus that referenced this pull request Oct 17, 2025
A commit was made in solidusio#6315 (5979f25) in service of the in memory order updater solidusio#5872 that changed the behavior in the OrderTaxation class to set the adjustment value but not persist it. Instead, the persistence happens when the order is saved. If a shipment is changed, and an associated adjustment is changed, the shipment adjustment will be autosaved when the order is.

However, if a shipment is *not* changed, but the adjustment is, the adjustment will *not* be autosaved when the order is.

In solidusio#6315, we ensured that these changed associations cannot get left behind by setting `autosave: true`, which forces the model to check if it has any associations that need saving instead of telling it's relation "nope I haven't changed"

We can do the same thing here for the shipment to ensure changing  adjustments will make the shipment tell it's order "I have changed", even if none of it's direct properties have changed.

You can test this change by removing the `autosave: true` option the adjustments and running 'when the address has changed to a different state' specs. They fail with the error "expected `order.shipments.first.adjustments.first.amount` to have changed from 1 to 2, but did not change", demonstrating a scenario where the shipment adjustment changes do not get persisted.
Noah-Silvera added a commit to SuperGoodSoft/solidus that referenced this pull request Oct 17, 2025
A commit was made in solidusio#6315 (5979f25) in service of the in memory order updater solidusio#5872 that changed the behavior in the OrderTaxation class to set the adjustment value but not persist it. Instead, the persistence happens when the order is saved. If a shipment is changed, and an associated adjustment is changed, the shipment adjustment will be autosaved when the order is.

However, if a shipment is *not* changed, but the adjustment is, the adjustment will *not* be autosaved when the order is.

In solidusio#6315, we ensured that these changed associations cannot get left behind by setting `autosave: true`, which forces the model to check if it has any associations that need saving instead of telling it's relation "nope I haven't changed"

We can do the same thing here for the shipment to ensure changing  adjustments will make the shipment tell it's order "I have changed", even if none of it's direct properties have changed.

You can test this change by removing the `autosave: true` option the adjustments and running 'when the address has changed to a different state' specs. They fail with the error "expected `order.shipments.first.adjustments.first.amount` to have changed from 1 to 2, but did not change", demonstrating a scenario where the shipment adjustment changes do not get persisted.
github-actions bot pushed a commit that referenced this pull request Oct 21, 2025
A commit was made in #6315 (5979f25) in service of the in memory order updater #5872 that changed the behavior in the OrderTaxation class to set the adjustment value but not persist it. Instead, the persistence happens when the order is saved. If a shipment is changed, and an associated adjustment is changed, the shipment adjustment will be autosaved when the order is.

However, if a shipment is *not* changed, but the adjustment is, the adjustment will *not* be autosaved when the order is.

In #6315, we ensured that these changed associations cannot get left behind by setting `autosave: true`, which forces the model to check if it has any associations that need saving instead of telling it's relation "nope I haven't changed"

We can do the same thing here for the shipment to ensure changing  adjustments will make the shipment tell it's order "I have changed", even if none of it's direct properties have changed.

You can test this change by removing the `autosave: true` option the adjustments and running 'when the address has changed to a different state' specs. They fail with the error "expected `order.shipments.first.adjustments.first.amount` to have changed from 1 to 2, but did not change", demonstrating a scenario where the shipment adjustment changes do not get persisted.

(cherry picked from commit b47e0f0)
tvdeyen pushed a commit that referenced this pull request Oct 21, 2025
A commit was made in #6315 (5979f25) in service of the in memory order updater #5872 that changed the behavior in the OrderTaxation class to set the adjustment value but not persist it. Instead, the persistence happens when the order is saved. If a shipment is changed, and an associated adjustment is changed, the shipment adjustment will be autosaved when the order is.

However, if a shipment is *not* changed, but the adjustment is, the adjustment will *not* be autosaved when the order is.

In #6315, we ensured that these changed associations cannot get left behind by setting `autosave: true`, which forces the model to check if it has any associations that need saving instead of telling it's relation "nope I haven't changed"

We can do the same thing here for the shipment to ensure changing  adjustments will make the shipment tell it's order "I have changed", even if none of it's direct properties have changed.

You can test this change by removing the `autosave: true` option the adjustments and running 'when the address has changed to a different state' specs. They fail with the error "expected `order.shipments.first.adjustments.first.amount` to have changed from 1 to 2, but did not change", demonstrating a scenario where the shipment adjustment changes do not get persisted.

(cherry picked from commit b47e0f0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog:solidus_core Changes to the solidus_core gem

Projects

Development

Successfully merging this pull request may close these issues.

4 participants