Skip to content

feat: [#1000] add Increment and Decrement to orm.Query - #1578

Open
JGamboa wants to merge 3 commits into
goravel:masterfrom
JGamboa:feat/orm-query-increment
Open

JGamboa wants to merge 3 commits into
goravel:masterfrom
JGamboa:feat/orm-query-increment

Conversation

@JGamboa

@JGamboa JGamboa commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

📑 Description

Part of goravel/goravel#1000 (section D). Follows #1575. Roadmap: goravel/goravel#1015

Adds Increment(column string, value ...uint64) error and Decrement(...) to orm.Query, with the same signatures as db.Query. The amount defaults to 1, and the column is quoted by the grammar (UPDATE ... SET `votes` = `votes` + ?).

facades.Orm().Query().Model(&Post{}).Where("id", 1).Increment("views")
facades.Orm().Query().Model(&user).Decrement("credits", 5) // also updates user.Credits in memory

Semantics. Both are built on Update, so they behave like any other ORM update rather than the raw db.Query version:

  • Conditions, scopes and soft deletes: conditions, global scopes and soft deletes are applied. WithoutGlobalScopes() and WithTrashed() lift them as usual.
  • Timestamps: updated_at is touched.
  • Events: like Update, Saving/Updating/Updated/Saved fire only when the query has a model with a primary key (Model(&user).Increment(...)).
    • Query-level increments fire no events, matching Laravel's query builder.
    • WithoutEvents() is the equivalent of incrementQuietly.
  • In-memory struct: Model(&user).Increment(...) also syncs the field of the loaded struct and its UpdatedAt, as Laravel does. This only happens when a row was actually updated and no event replaced the expression.

Happy to change any of these choices if you prefer them closer to the DB module.

✅ Checks

  • Added test cases for my code
    • Unit: TestIncrementAndDecrement and TestIncrementAndDecrementTouchUpdatedAt.
    • Integration: TestIncrementAndDecrement, covering conditions, Table, a loaded model with updated_at, and soft deletes. It passes on Postgres, MySQL, SQLite and SQL Server.

Align orm.Query with db.Query by adding Increment(column, value...)
and Decrement(column, value...) with the same signatures. The amount
defaults to 1 and the column is quoted by the grammar
(UPDATE ... SET `votes` = `votes` + ?).

Semantics: both are built on top of Update, so they behave like any
other ORM update instead of the raw db.Query version:

- Conditions, global scopes and soft deletes are applied, so
  Where(...).Increment(...) only touches the rows Get() would return.
  WithoutGlobalScopes() and WithTrashed() lift them as usual.
- updated_at is touched (GORM auto-update time), like any ORM update.
- Events: like Update, Saving/Updating/Updated/Saved are fired only
  when the query has a model with a primary key, e.g.
  Model(&user).Increment("votes"). Query-level increments such as
  Model(&User{}).Where(...).Increment(...) fire no events, matching
  Laravel's query builder. WithoutEvents() is the equivalent of
  Laravel's incrementQuietly.
- Model(&user).Increment(...) also syncs the field of the loaded struct
  in memory (and its UpdatedAt), as Laravel does, but only when a row
  was actually updated and no event replaced the expression.
@codecov

codecov Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.50000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.49%. Comparing base (84af91f) to head (5232491).

Files with missing lines Patch % Lines
database/gorm/query.go 92.50% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1578      +/-   ##
==========================================
+ Coverage   72.85%   73.49%   +0.63%     
==========================================
  Files         412      412              
  Lines       26820    26860      +40     
==========================================
+ Hits        19541    19742     +201     
+ Misses       7278     7117     -161     
  Partials        1        1              

☔ View full report in Codecov by Harness.
📢 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.

@JGamboa
JGamboa marked this pull request as ready for review October 18, 2026 23:47
@JGamboa

JGamboa commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

Review Ready

@JGamboa
JGamboa requested a review from a team as a code owner October 18, 2026 23:47
…at/orm-query-increment

# Conflicts:
#	database/gorm/query_test.go
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.

1 participant