fix(orm): restore lazy $onUpdate evaluation in buildUpdateSet#5871
Open
tsushanth wants to merge 1 commit into
Open
fix(orm): restore lazy $onUpdate evaluation in buildUpdateSet#5871tsushanth wants to merge 1 commit into
tsushanth wants to merge 1 commit into
Conversation
Closes drizzle-team#5780. The 0.45.x refactor of `buildUpdateSet` introduced eager evaluation of the `$onUpdate` callback for every column with one — including columns whose value was explicitly supplied in the caller's `set` object, where the result is then discarded via `??`. Side-effecting callbacks (throwing, logging, reading external state) now run on every UPDATE, which is a breaking regression from the 0.44.6 contract where the callback only fired when the column was absent. Restore lazy evaluation while keeping the 0.45.x `SQL`-type check that motivated the refactor: const onUpdateFnResult = set[colName] !== undefined ? undefined : col.onUpdateFn?.(); Applied identically across pg-core, mysql-core, sqlite-core, singlestore-core, and gel-core — the bug had the same shape in each dialect's `buildUpdateSet`. A regression test (`drizzle-orm/tests/on-update.test.ts`) exercises the pg-core path with a throwing `$onUpdate` callback to assert the callback is not invoked when the column is explicitly set, plus a second test that confirms the callback IS invoked exactly once when the column is absent. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #5780.
Summary
The 0.45.x refactor of
buildUpdateSetintroduced eager evaluation of the$onUpdatecallback for every column that defines one — including columns whose value is explicitly supplied in the caller'ssetobject, where the callback's return value is then discarded via??. Side-effecting callbacks (throwing, logging, reading external state) now run on every UPDATE, which is a breaking regression from the 0.44.6 contract where the callback only fired when the column was absent.Fix
Restore lazy evaluation while keeping the 0.45.x
SQL-type check that motivated the refactor:Applied identically across all five dialects that carry the same
buildUpdateSetshape:drizzle-orm/src/pg-core/dialect.tsdrizzle-orm/src/mysql-core/dialect.tsdrizzle-orm/src/sqlite-core/dialect.tsdrizzle-orm/src/singlestore-core/dialect.tsdrizzle-orm/src/gel-core/dialect.tsThe
buildInsertQuerypaths (around line ~548 in each file) are already lazy by construction — they're gated oncolValue === undefined, so the INSERT default fallback path is untouched.Tests
drizzle-orm/tests/on-update.test.ts(new):$onUpdatecallback that throwsError('should not be called when column is explicitly set'). The first test exercises an UPDATE with the column explicitly provided insetand asserts no throw + the expected SQL.$onUpdatecallback to confirm the absent-column path still invokes it exactly once and the value flows through to params.Verified that the first test fails on the buggy code (the callback throws) and passes after the fix. All 568 existing unit tests pass.
I scoped the regression test to pg-core to keep the diff focused; the identical fix in the other four dialects is auditable by inspection and the integration test suite under
integration-tests/tests/exercises them against live DBs.