Skip to content

fix(orm): restore lazy $onUpdate evaluation in buildUpdateSet#5871

Open
tsushanth wants to merge 1 commit into
drizzle-team:mainfrom
tsushanth:fix/lazy-on-update-fn
Open

fix(orm): restore lazy $onUpdate evaluation in buildUpdateSet#5871
tsushanth wants to merge 1 commit into
drizzle-team:mainfrom
tsushanth:fix/lazy-on-update-fn

Conversation

@tsushanth

Copy link
Copy Markdown

Closes #5780.

Summary

The 0.45.x refactor of buildUpdateSet introduced eager evaluation of the $onUpdate callback for every column that defines one — including columns whose value is explicitly supplied in the caller's set object, 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:

const onUpdateFnResult =
  set[colName] !== undefined ? undefined : col.onUpdateFn?.();
const value = set[colName]
  ?? (is(onUpdateFnResult, SQL) ? onUpdateFnResult : sql.param(onUpdateFnResult, col));

Applied identically across all five dialects that carry the same buildUpdateSet shape:

  • drizzle-orm/src/pg-core/dialect.ts
  • drizzle-orm/src/mysql-core/dialect.ts
  • drizzle-orm/src/sqlite-core/dialect.ts
  • drizzle-orm/src/singlestore-core/dialect.ts
  • drizzle-orm/src/gel-core/dialect.ts

The buildInsertQuery paths (around line ~548 in each file) are already lazy by construction — they're gated on colValue === undefined, so the INSERT default fallback path is untouched.

Tests

drizzle-orm/tests/on-update.test.ts (new):

  • pg-core: $onUpdate callback that throws Error('should not be called when column is explicitly set'). The first test exercises an UPDATE with the column explicitly provided in set and asserts no throw + the expected SQL.
  • pg-core: A counting $onUpdate callback 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.

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>
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.

[BUG]: $onUpdate callback invoked eagerly in buildUpdateSet even when column is explicitly provided in set (regression in 0.45.x)

1 participant