Skip to content

fix(drizzle): preserve input during write transformation - #19483

Merged
DanRibbens merged 4 commits into
mainfrom
fix/drizzle-preserve-write-input
Nov 10, 2026
Merged

DanRibbens merged 4 commits into
mainfrom
fix/drizzle-preserve-write-input

Conversation

@DanRibbens

@DanRibbens DanRibbens commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Written with AI

Description

Drizzle write transformation mutates caller-owned input while moving array/block IDs into _uuid storage fields and annotating nested groups. Reusing that input after transformation therefore observes missing IDs and internal storage metadata.

Deep-copy the input once with deepCopyObjectSimple before traversal so relational storage transformations preserve the caller's document data.

Extracted from #19375 as an independent prerequisite targeting main. This fixes shared adapter behavior and requires no content branching configuration or schema changes. The branch-specific logical-row-ID schema changes remain in #19375.

Changes

  • Copy input at the transformForWrite entry point before recursive traversal.
  • Add the regression to test/database/int.spec.ts using test.options with db: 'all', so the existing database matrix exercises every adapter. Register its collection in the shared database config and clean up its records after the test.
  • Save a version containing blocks, nested arrays, ordinary groups, and localized groups through the adapter API. Assert that the caller's input remains unchanged, then read the version back and verify its logical row IDs and content.

Test Plan

  • The regression passed inside the shared database suite on MongoDB, PostgreSQL, and SQLite, using real databases and no adapter mocks (filtered runs).
  • Verified the same integration regression fails on SQLite without the fix: input IDs are removed and _uuid properties are added to caller-owned objects.
  • Formatting and git diff --check passed. The existing database spec has 53 lint diagnostics; verified the updated file has the same diagnostics and introduces none.
  • Checks ran under Node 22.23.1; the repository declares Node >=24.22.1. The remaining adapter variants are covered by CI discovery and have not been run locally.

@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Drizzle write transform now traverses a deep copy of input data. The database test configuration adds a nested, versioned collection and tests that transformation and version creation preserve input data and saved version data.

Changes: Write input preservation

Layer / File(s) Summary
Nested write fixture and types
test/database/collections/WriteInput/index.ts, test/database/payload-types.ts, test/database/shared.ts, test/database/getConfig.ts
The database test configuration adds the versioned WriteInput collection, its generated types, and its collection slug.
Copy input before traversal
packages/drizzle/src/transform/write/index.ts, test/auth/int.spec.ts, test/database/int.spec.ts
transformForWrite passes a deep copy of data to traverseFields. Tests check that the transform and createVersion leave input unchanged, and that findVersions returns the nested version data.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related PRs

  • payloadcms/payload#19101: Adds branch-aware nested writes and changes the Drizzle write-transformation path, which this PR also updates.

Suggested reviewers: r1tsuu

Merge Risk: Minimal · up to 367f7

Write transforms now copy caller input before traversal. No merge-blocking issue was established; remaining adapter checks can run in CI.

Pre-merge checks: Passed 5

Check name Status Explanation
Title check Passed The title clearly summarizes the main change: preserving caller-owned input during Drizzle write transformation.
Description check Passed The description explains the input-mutation issue, the deep-copy fix, and the regression tests. It is relevant to the changeset.
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 7 files.

Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between c025beb and 367f789.

Files selected for processing: 1

  • test/auth/int.spec.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@github-actions

Copy link
Copy Markdown
Contributor

📦 esbuild Bundle Analysis for payload

This analysis was generated by esbuild-bundle-analyzer. 🤖
This PR introduced no changes to the esbuild bundle! 🙌

@DanRibbens
DanRibbens marked this pull request as ready for review November 10, 2026 17:32
@DanRibbens
DanRibbens merged commit ea00c5f into main Nov 10, 2026
290 checks passed
@DanRibbens
DanRibbens deleted the fix/drizzle-preserve-write-input branch November 10, 2026 19:13
@github-actions

Copy link
Copy Markdown
Contributor

🚀 This is included in version v4.0.0-beta.8

DanRibbens added a commit that referenced this pull request Nov 10, 2026
)

Written with AI

## Description

Publishing an inherited page on a content branch can fail with `UNIQUE
constraint failed: pages_blocks_content.id` on SQLite. The editor
submits main’s existing block and array IDs after the document has been
forked, but those IDs were also used as database primary keys.

Store logical array/block IDs separately from database primary keys for
branchable SQL collections and globals, allowing main and branch copies
to retain the same logical IDs.

This PR targets `fix/branch-name-only-19112` and contains only the
branch-specific schema changes and publishing regression coverage.

## Prerequisite

Depends on #19483, which targets `main` and preserves caller-owned input
during database writes. Its deep-copy implementation and
input-preservation regression have been removed from this PR’s
incremental diff.

Merge #19483 and propagate it through the feature stack before merging
this PR. On the current base without that prerequisite, the
nested-publish regressions fail because logical row IDs are lost during
writes. Validation with the prerequisite is recorded below.

## Changes

- Store logical array/block IDs separately from generated database
primary keys for branchable SQL collections and globals, using the
existing version-table mechanism.
- Add an adapter-independent integration suite covering direct
publication and autosave followed by publication of inherited localized
blocks with nested arrays. Verify persisted IDs, isolation of main,
merge preflight/execution, and preservation of the other locale.
- Add schema coverage confirming branchable entities use separate
logical IDs while excluded collections retain their existing schema.

Existing SQL databases require a schema/data migration that preserves
existing logical row IDs and nested relationships. The regression tests
use fresh schemas; migration of existing databases is not covered here.

## Test Plan

- After extraction: schema unit suite passed (9 tests).
- With only #19483’s production deep-copy fix temporarily applied for
validation: both nested-publish integration cases passed on SQLite and
PostgreSQL (2 cases per adapter). The prerequisite overlay was removed
afterward and is not part of this PR’s diff.
- Confirmed both SQLite cases fail on the current base without #19483,
losing logical block/array IDs.
- Targeted production/schema unit-file lint and formatting passed; `git
diff --check` passed.
- Diff audit: 7 files remain, with no changes under
`packages/drizzle/src/transform/write`.
- The integration suite uses `db: 'all'` for the existing database
matrix. Other adapter variants were not rerun after this extraction.
- Local checks used Node 22.23.1; the repository declares Node
>=24.22.1.
r1tsuu pushed a commit that referenced this pull request Nov 11, 2026
Written with AI

## Description

Backports #19483 to `3.x`.

Drizzle write transformation mutates caller-owned input while moving
array/block IDs into `_uuid` storage fields and annotating nested
groups. Deep-copy the input once with `deepCopyObjectSimple` before
traversal so relational storage transformations preserve the caller's
document data.

## Changes

- Preserve the production fix from #19483 without modification.
- Backport the database version regression and its collection, covering
blocks, nested arrays, ordinary groups, localized groups, and logical
row IDs.
- Adapt regression tests to the existing `3.x` test helpers and preserve
its auth security assertions. The fixture-based `test.suite` API used on
`main` does not exist on `3.x`.
- Generate the new collection types using `3.x`, excluding unrelated
existing type drift.

## Test Plan

- Passed the targeted database version regression on SQLite.
- Passed the targeted auth constrained-session regression on SQLite.
- Confirmed the database regression fails without the fix because
caller-owned IDs are replaced by `_uuid` metadata.
- Prettier checks and `git diff --check` passed.
- Local tests used available dependencies under Node 22.23.1 and a
temporary Vitest OXC JSX configuration adjustment; the runner adjustment
is excluded from this PR. Other database adapters were not rerun for
this backport.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants