Repository navigation
refactor(storage): take the plugin's own alwaysInsertFields instead of the shadow column - #1173
Conversation
`withObjectKeyColumn` hand-declared `_objectKey` so the column existed in the schema where the Cloudflare credentials are absent — local dev, the test suite, `migrate:create`. That mirrored a literal upstream owns, kept in sync by nothing but a unit spec, and an upstream bump moved it. `@payloadcms/plugin-cloud-storage` ships `alwaysInsertFields` for exactly this, and the disabled branch honouring it runs `getFields` while touching no hooks, no `upload.adapter`, no `disableLocalStorage` and no static handler. Setting it on all three call sites, not only the disabled two: `getFields` inserts `prefix` on `alwaysInsertFields` as well, so the disabled branches alone would give dev and `migrate:create` a column production lacks — #865 re-armed on `prefix` instead of `_objectKey`. Both disabled branches now name the real slug set with `adapter: null` rather than `collections: {}`, which is what makes `getFields` run for them at all. The spec now pins the invariant rather than the literal: the stored column names agree across the credentialed and credential-less paths, and the four computed fields keep the `custom.virtualFieldInputs` declarations #1084 depends on — upstream splices `url` out and re-pushes it, so that spread is load-bearing. Each case was watched failing against the defect it guards. Refs #1164 Co-authored-by: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjYwK4f4NCbYaATHDiN
`getFields` inserts upstream's `prefix` field under `alwaysInsertFields`, so the eight upload collections gain the column it has always declared where a prefix was configured. Paid once; Payload v4 bills it anyway. `_meditations_v.version_prefix` rides along because `meditations` is the only upload collection with versions enabled. `_objectKey` emits no DDL — it has shipped since #865. Generated with the storage credentials stubbed, as the ticket asks. Then regenerated twice as a probe, once with the stubs and once without: both runs wrote no files, which is the evidence that the three branches now agree on the schema rather than only the argument that they should. Refs #1164 Co-authored-by: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjYwK4f4NCbYaATHDiN
The fixture gave all eight collections `virtualUrlField`, but only five mount it. `frames`, `files` and `videos` mount `mixedMediaUrlField` plus `hlsUrlField`, `mp4UrlField` and `previewUrlField` — four computed fields each, with a different `virtualFieldInputs` declaration. So the case that exists to prove upstream's `url` splice preserves those declarations was proving it for a factory those three collections never use. Running over the real collection array, the way `upload-notice-coverage` already does, makes the slug set and the per-collection field set derived rather than restated. `STORAGE_BACKENDS` is exported for that, so the spec reads the registry all three branches derive from instead of keeping a fourth copy of the eight slugs. Watched failing: dropping the flag from the enabled branch, from the two disabled branches, and upstream re-pushing `url` without merging ours. Refs #1164 Co-authored-by: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjYwK4f4NCbYaATHDiN
The ⚠ explains that the key comes from the factory's own `prefix`, which `storagePlugin` never passes. With a per-document `prefix` column now on all eight collections, that sentence no longer rules out the reading it was written to rule out. Refs #1164 Co-authored-by: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjYwK4f4NCbYaATHDiN
…storage-always-insert-fields
|
🚅 Deployed to the SahajCloud-pr-1173 environment in sahajcloud
1 service not affected by this PR
|
sydevs-bot
left a comment
There was a problem hiding this comment.
1 question for the reviewer, one per thread.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
|
🤖 no session running recent dispatchesWritten by the sydevs dispatcher. Sessions never edit this comment. |
…storage-always-insert-fields main brought the Payload 3.92.1 bump (#1165). Three files conflicted. `storagePlugin.ts`: main reworded the comment above `withObjectKeyColumn`, the transform this branch deletes. Took this branch's comment, which documents `SCHEMA_ONLY_COLLECTIONS` instead. Nothing of main's intent is lost — its own wording already named #1164 as what replaces the transform. `storage-object-key.spec.ts`: main added `schemaShape`, which strips `admin` before comparing, because 3.92.1 added four admin flags to upstream's `_objectKey` and main compared that against our hand-written literal. This branch has one producer on both sides, so the difference it worked around cannot arise. Took this branch's spec whole, and confirmed the whole-field comparison stays green on 3.92.1. `src/migrations/index.ts`: both migrations, in timestamp order — `20261115_184420_payload_3_92` before `20261116_171937_storage_prefix`. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjZA13k1h23uDZrPtgW
`20261116_171937_storage_prefix.json` was generated before main's `20261115_184420_payload_3_92` landed, and carries the higher timestamp. So the next `migrate:create` diffed against it and re-emitted all 32 `CREATE INDEX` statements that migration already ships — the out-of-order snapshot trap (#566, `src/migrations/AGENTS.md`). Replaying one aborts the boot migration on a Railway deploy. The generated file keeps only its `.json`, which was built on the merged tree. A probe run after emptying the DDL wrote no files. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjZA13k1h23uDZrPtgW
|
Merged
A second commit heals the migration chain. Lean gate green. 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 Adversarial review
The diff is the ticket's size: one switch at three call sites, one derived slug map, the shadow transform gone, and the column it costs. Nothing is unasked-for, and the _objectKey deep equality the ticket wanted is restored. The contract holds — prefix is optional in payload-types.ts, and neither consumer parses SahajCloud with a strict schema, so the re-sync is additive.
Two of three findings are about what the PR says, not what it does. The one simplification worth having — collapsing the branches — is already an open question thread, so I am not re-raising it.
Findings
- should-fix
tests/unit/storage-object-key.spec.ts:99— the body's first reviewer note is wrong on both halves. - should-fix
src/plugins/storage/r2NativeAdapter.ts:127— upstream'safterReaddoes read the document'sprefix. - consider
docs/rules/storage.md:206— the replacement exemplar is additive, not skip-if-declared.
What was checked
- Upstream 3.92.1 read from the tarball:
getFields(both branches,urlsplice,prefixand_objectKeypushes),plugin.js(adapter: null→undefined, no hooks, no handlers),afterRead,beforeChange,normalizeUploadPrefix,buildStoragePathData. - Ladder:
SCHEMA_ONLY_COLLECTIONSand theSTORAGE_BACKENDSexport remove a copy rather than add a layer; the four spec helpers each have a caller; the emptymerge_main_snapshot_10is documented (src/migrations/AGENTS.md:293). - Consumers: no
.strict()in SahajAtlasWebsrc/types/or WeMeditateWebserver/, so an extra key cannot red a parse. urlFields.tsandstoragePlugin.ts:210read whole: no adapter declaresgenerateURLorgenerateFileURL.- Preview: 302s to the Cloudflare Access host. This environment holds no
PREVIEW_ACCESS_*credentials, so the two upload checks are unverified here.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
…storage-always-insert-fields
Upstream's `url` afterRead reads the document's `prefix` and appends `?prefix=` to a stored `url`. The comment said nothing read it, which hides what a stored `url` on one of these eight would revive. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjVBuo2xSihkpxwEnKa
`withUploadNotice` appends to an array slot, so it never answers the override question `adminThumbnail` asks. With `withObjectKeyColumn` gone no transform in-tree shows that shape, and the paragraph now says so. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjVBuo2xSihkpxwEnKa
|
Adopted all three critic findings.
Both commits are comment-only — The question thread on collapsing the three call sites stays open. It waits on you. 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
The credentials resolve to adapters or `null`, so `enabled` and the `collections` map are both derived rather than branched on. Every branch naming the same eight slugs stops being a rule anyone can forget: there is one map, off `STORAGE_BACKENDS`. `SCHEMA_ONLY_COLLECTIONS` and the ⚠ guarding that agreement go with it. Reviewer's answer on the open question thread (#1173). Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjW3aWeJrLw5qmtAeF1
Four comments counted `storagePlugin`'s returns or its branches. The notice is still mounted above the plugin call, and the spec still runs the three paths — only the shape they describe changed. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjW3aWeJrLw5qmtAeF1
|
Adopted C — one
The schema is unchanged — both #1164's first two criteria specified three call sites. They are replaced by one criterion marked 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
Summary
storagePluginasks upstream for its own schema fields instead of hand-mirroring its_objectKeyliteral.prefixcolumn that switch inserts. Nothing here composes it into a key or a URL.Change outline
The schema fields now come from one producer — upstream's
getFields— instead of two that hadto agree. The credentials resolve to adapters or
null, so onecloudStoragePlugincall servesevery environment.
One map, derived from
STORAGE_BACKENDS. "Every branch names the same eight slugs" was a rulethe ⚠ above
SCHEMA_ONLY_COLLECTIONSasked a reader to keep; it is now structural, and both theconst and the ⚠ are gone.
alwaysInsertFieldsis likewise stated once, so it cannot be set on onepath and missed on another — which is what would give dev and
migrate:createa column productionlacks.
Upstream reads
disablePayloadAccessControlonly insideif (adapter), and writes noupload.handlers, noupload.disableLocalStorageand no hooks on itsenabled: falsebranch, sothe null-adapter entry stays bare and the three paths each get exactly what they got before.
Contract — the eight upload collections
images, frames, videos, user-choices, song-tags, meditations, songs, files url, previewUrl, hlsUrl, mp4Url virtual unchanged; `custom.virtualFieldInputs` intact (#1084) _objectKey text same shape, now upstream's literal rather than a copy + prefix text default '', admin-hidden, inert while every `url` is virtualPreview
Railway builds the preview from the PR number, so these are live once the deploy finishes. Sign-in is GitHub through Cloudflare Access.
Data: not seeded. A PR preview forks configuration, never volume data, so every content table starts empty (
docs/rules/storage.md), and this repo sets nopreviewUrl.seedfor a run to call. These link the create pages rather than the empty lists, because the thing to check is the upload.Contract impact
src/payload-types.tsgains an optionalprefixon the eight upload document and select types — 16 added lines, nothing removed or renamed. It is acontractSyncsource, so WeMeditateWeb (server/payload-types.ts) and SahajAtlasWeb (src/types/payload/payload-types.ts) re-sync in a bot PR after this merges. The field is optional, so neither consumer needs work and no issue is filed.Migration
src/migrations/20261116_171937_storage_prefix.tsADD COLUMN "prefix" varchar DEFAULT ''onimages,frames,videos,user_choices,song_tags,meditations,songs,files, plusversion_prefixon_meditations_v. Additive only — no data read, written or transformed. The default is non-volatile, so Postgres records it in the catalog rather than rewriting the heap.downdrops the same nine columns, which only ever hold''.Test results
videos.int,meditations.int,image-orientation.intpnpm buildsrc/plugins/storage/trigger path.Manual verification
urlresolves toimagedelivery.net.urlresolves to the R2 delivery domain.Both are the ticket's last acceptance criterion. Nothing local can cover them: the adapters only run where every Cloudflare credential is set.
Notes for reviewer
package.jsonpins@payloadcms/plugin-cloud-storage3.92.1 onmainand here, and this branch carries main'sschemaShapehelper — theadminexclusion the bump added. This PR deletes it, so that acceptance criterion is met here rather than pre-satisfied. After this, an upstream bump moving the literal cannot redden the spec, because both sides of every comparison are upstream's._meditations_v.version_prefix.meditationsis the only one of the eight with versions enabled, so that is the versions companion, not a surprise.prefixcolumn is API-writable and inert — but not unread. Checked rather than assumed: no adapter here declaresgenerateURLand nothing passesgenerateFileURL, andr2NativeAdapter's three handlers compose their key from the factory's ownprefix— whichstoragePluginnever passes — not from the document's. Upstream'surlafterReaddoes read the column: with neither of those declared it falls through to appending?prefix=<value>to a storedurl. Everyurlon these eight is virtual, so the value it guards on isundefinedand that branch never fires. The⚠onr2NativeAdapter.tsnow says exactly that, since that comment is what a future reader will check.alwaysInsertFieldsinstead of the shadow column #1164).tests/utils/(shared by three specs today) — pre-existing, and it would edit two specs this change does not otherwise touch. Same for derivingr2FilenameHookModesfromSTORAGE_BACKENDS, which the file already flags with its own⚠; both belong to their own ticket.Detail
Why the spec runs over the real collection array. The first version used a fixture that gave all eight collections
virtualUrlField. Only five mount it —frames,filesandvideosmountmixedMediaUrlFieldplushlsUrlField,mp4UrlFieldandpreviewUrlField, four computed fields each, carrying a differentvirtualFieldInputsdeclaration. So the case that exists to prove upstream'surlsplice preserves those declarations was proving it for a factory three of the collections never use. Reading@/collectionsthe wayupload-notice-coverage.spec.tsalready does makes both the slug set and the field set derived;STORAGE_BACKENDSis exported so the slug list is the registry rather than a fourth copy of it.Each case was watched failing, against the three-call-site shape these commits replace: dropping the flag from the enabled branch reddened the stored-column comparison, dropping it from the two disabled branches reddened three cases, and patching upstream to re-push
urlwithout merging ours reddened the computed-field case with exactly the #1084 symptom. With one call site the flag can no longer be set on one path and missed on another, so that first probe now reds every case at once — the shape stopped the defect the probe was aimed at, rather than the spec stopping it.Alternatives rejected are in the ticket: deep-importing upstream's
baseObjectKeyFieldis blocked by the package'sexportsmap, and vendoring it is the same duplication with more lines.Closes #1164