From 37131e9930503d837eade230be6d6e84f7961051 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Smoli=C5=84ski?= Date: Thu, 29 Oct 2026 16:39:55 +0100 Subject: [PATCH] fillGlobal: a field already holding the seeded value is skipped, not rewritten Seeding exactly the default (allowThemeToggle: true) used to count as unset, rewrite it and report create on every run. Now: skip (exists), no write. A choice at its default is still replaced by a different seeded value; editor changes stay. Co-Authored-By: Claude Opus 5.5 --- docs/CHANGELOG.md | 3 ++- docs/agent/skills/ipal-reference/resources/api.md | 2 +- docs/seeding.md | 2 +- reference/.agents/skills/ipal-reference/resources/api.md | 2 +- src/modules/seed/seeder.ts | 4 +++- test/seed.test.ts | 9 +++++++++ 6 files changed, 17 insertions(+), 5 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index de19da8..10c5b18 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -93,7 +93,8 @@ fifteen ready page sections and ten page templates. A new site in one command: ` 400. With R2 nothing changes; a production build on localhost still never opens private IPs. - **`fillGlobal`:** a choice field (select, radio, checkbox, number, date) still at its `defaultValue` counts as empty, so e.g. Theme → `defaultTheme: 'system'` can be seeded over the stored default `light`. Text is never - overwritten, even when it equals a default. + overwritten, even when it equals a default. A field already holding + exactly the seeded value is not rewritten (`skip (exists)`), so a seed of defaults stays idempotent. - **Docs:** `LanguageSwitcher` with `renderLabel` needs a `'use client'` wrapper (i18n.md, ipal-site). ### Changed diff --git a/docs/agent/skills/ipal-reference/resources/api.md b/docs/agent/skills/ipal-reference/resources/api.md index fc0838c..c789cd2 100644 --- a/docs/agent/skills/ipal-reference/resources/api.md +++ b/docs/agent/skills/ipal-reference/resources/api.md @@ -560,7 +560,7 @@ Server only (`pnpm payload run`); never import it from client code, the Payload | `createSeeder(payload, { dryRun?, update?, force?, only?, guard?, editCheck?, context?, pagesCollection?, formsCollection?, label?, log? })` | `@intecion/ipal-kit/seed` | The seed rules in one place → `{ upsertBySlug, upsertBy, fillGlobal, summary, dryRun, update, force, local, only }`. `only` (or `SEED_ONLY`) = the records the run may touch (`pages:kontakt`, `forms:`, `global:<slug>`, `pages:*`), the rest → `skip (not listed)`; a run without it against production / a non-local database throws unless `IPAL_SEED_ALLOW_FULL=1` / `guard.allowFull: true` (after launch: named records only; dry runs are never refused; `guard: false` turns every guard off). Create-only by default, natural keys, never deletes, globals: empty fields only, every locale with `fallbackLocale: false` onto the same row ids, skips records edited since seeding, writes with `context` `{ ipalSeed, disableRevalidate, ipalSkipIndexNow }`. Throws when `update` runs on production / a non-local database without `IPAL_SEED_ALLOW_PROD_UPDATE=1`. Both seed templates use it. | | `upsertBySlug(collection, data \| data[], { slugField? })` | (method of `createSeeder`) | Matches by the default-locale slug; an array creates missing records first, so `{ $page: 'slug' }` links work in any order; `{ $form: 'title' }` = the form's id (`formsCollection`, default `forms`). | | `upsertBy(collection, key, data)` | (method) | Matches by other keys, e.g. a form by `{ title }`. | -| `fillGlobal(slug, data)` | (method) | Fills only empty fields of a global — a choice (select, radio, checkbox, number, date) still at its `defaultValue` counts as empty, text never does (all seeded fields with `update` when untouched). | +| `fillGlobal(slug, data)` | (method) | Fills only empty fields of a global — a choice (select, radio, checkbox, number, date) still at its `defaultValue` counts as empty, text never does; a field already holding the seeded value is skipped (all seeded fields with `update` when untouched). | | `summary()` | (method) | Prints and returns `{ created, skipped, updated, skippedEdited, errors }`; exit 1 when `errors`. | | `isLocalDatabase(url, { localHosts? })` | `@intecion/ipal-kit/seed` | `true` for SQLite files, Unix sockets, localhost / 127.0.0.1 / ::1 (+ `SEED_LOCAL_HOSTS`). | | `writeLocales(payload, target, data, { context?, draft? })` | `@intecion/ipal-kit/seed` | Writes seed values (`{ pl, en }` per field) to `{ collection, id }` or `{ global }`: default locale first, then every other locale with `fallbackLocale: false` onto the same row ids. | diff --git a/docs/seeding.md b/docs/seeding.md index 64684dd..bc4965a 100644 --- a/docs/seeding.md +++ b/docs/seeding.md @@ -203,7 +203,7 @@ is an error for that record, never an empty relation. Ready pages made of librar |---|---| | `upsertBySlug(collection, data \| data[], { slugField })` | matches by the default-locale slug (`slugField`, default `slug`). With an array, missing records are created first (without fields that hold `$page` / `$form` links), then every record gets its full content, so pages can link to each other in any order | | `upsertBy(collection, key, data)` | matches by other natural keys, e.g. `{ title: 'Contact form' }` (all keys must match, default locale) | -| `fillGlobal(slug, data)` | fills only the global's empty fields (the default locale decides). A choice — select, radio, checkbox, number, date — still at the field's `defaultValue` counts as empty (Payload stores defaults on the first save, so `defaultTheme: 'system'` can be seeded over `light`); text is never treated so, even when it equals a default. With `update` and an untouched global: every seeded field | +| `fillGlobal(slug, data)` | fills only the global's empty fields (the default locale decides). A choice — select, radio, checkbox, number, date — still at the field's `defaultValue` counts as empty (Payload stores defaults on the first save, so `defaultTheme: 'system'` can be seeded over `light`); text is never treated so, even when it equals a default. A field that already holds exactly the seeded value is not written (`skip (exists)`), so re-running a seed of defaults changes nothing. With `update` and an untouched global: every seeded field | | `summary()` | prints the summary and returns `{ created, skipped, updated, skippedEdited, errors }` | Each method prints one line per record (see [Running a seed](#running-a-seed)) and returns diff --git a/reference/.agents/skills/ipal-reference/resources/api.md b/reference/.agents/skills/ipal-reference/resources/api.md index fc0838c..c789cd2 100644 --- a/reference/.agents/skills/ipal-reference/resources/api.md +++ b/reference/.agents/skills/ipal-reference/resources/api.md @@ -560,7 +560,7 @@ Server only (`pnpm payload run`); never import it from client code, the Payload | `createSeeder(payload, { dryRun?, update?, force?, only?, guard?, editCheck?, context?, pagesCollection?, formsCollection?, label?, log? })` | `@intecion/ipal-kit/seed` | The seed rules in one place → `{ upsertBySlug, upsertBy, fillGlobal, summary, dryRun, update, force, local, only }`. `only` (or `SEED_ONLY`) = the records the run may touch (`pages:kontakt`, `forms:<title>`, `global:<slug>`, `pages:*`), the rest → `skip (not listed)`; a run without it against production / a non-local database throws unless `IPAL_SEED_ALLOW_FULL=1` / `guard.allowFull: true` (after launch: named records only; dry runs are never refused; `guard: false` turns every guard off). Create-only by default, natural keys, never deletes, globals: empty fields only, every locale with `fallbackLocale: false` onto the same row ids, skips records edited since seeding, writes with `context` `{ ipalSeed, disableRevalidate, ipalSkipIndexNow }`. Throws when `update` runs on production / a non-local database without `IPAL_SEED_ALLOW_PROD_UPDATE=1`. Both seed templates use it. | | `upsertBySlug(collection, data \| data[], { slugField? })` | (method of `createSeeder`) | Matches by the default-locale slug; an array creates missing records first, so `{ $page: 'slug' }` links work in any order; `{ $form: 'title' }` = the form's id (`formsCollection`, default `forms`). | | `upsertBy(collection, key, data)` | (method) | Matches by other keys, e.g. a form by `{ title }`. | -| `fillGlobal(slug, data)` | (method) | Fills only empty fields of a global — a choice (select, radio, checkbox, number, date) still at its `defaultValue` counts as empty, text never does (all seeded fields with `update` when untouched). | +| `fillGlobal(slug, data)` | (method) | Fills only empty fields of a global — a choice (select, radio, checkbox, number, date) still at its `defaultValue` counts as empty, text never does; a field already holding the seeded value is skipped (all seeded fields with `update` when untouched). | | `summary()` | (method) | Prints and returns `{ created, skipped, updated, skippedEdited, errors }`; exit 1 when `errors`. | | `isLocalDatabase(url, { localHosts? })` | `@intecion/ipal-kit/seed` | `true` for SQLite files, Unix sockets, localhost / 127.0.0.1 / ::1 (+ `SEED_LOCAL_HOSTS`). | | `writeLocales(payload, target, data, { context?, draft? })` | `@intecion/ipal-kit/seed` | Writes seed values (`{ pl, en }` per field) to `{ collection, id }` or `{ global }`: default locale first, then every other locale with `fallbackLocale: false` onto the same row ids. | diff --git a/src/modules/seed/seeder.ts b/src/modules/seed/seeder.ts index 405c245..4c2ed25 100644 --- a/src/modules/seed/seeder.ts +++ b/src/modules/seed/seeder.ts @@ -431,7 +431,9 @@ export function createSeeder(payload: BasePayload, options: SeederOptions = {}): const fieldOf = (name: string) => (global?.flattenedFields as { defaultValue?: unknown; name?: string; type?: string }[] | undefined)?.find((f) => f.name === name) const cur = await readSeedTarget(payload, target, defaultLocale) // Empty, or a choice still at its default value (e.g. Theme → light): never text an editor wrote. - const empty = Object.keys(data).filter((k) => isUnsetValue(cur[k], fieldOf(k))) + // A field already holding exactly the seeded value is left alone (no write, reported as skip). + const same = (k: string) => JSON.stringify(cur[k]) === JSON.stringify(localize(data[k], defaultLocale, cur[k], codes)) + const empty = Object.keys(data).filter((k) => isUnsetValue(cur[k], fieldOf(k)) && !same(k)) let keys = empty let edited: null | string = null if (update) { diff --git a/test/seed.test.ts b/test/seed.test.ts index e345261..b1ac821 100644 --- a/test/seed.test.ts +++ b/test/seed.test.ts @@ -256,6 +256,15 @@ describe('createSeeder', () => { expect(first.note).toBe('empty fields filled: mode, toggle') const look = (await payload.findGlobal({ slug: 'look' as never })) as { greeting: string; mode: string; toggle: boolean } expect(look).toMatchObject({ greeting: 'Hello', mode: 'system', toggle: false }) + // Seeding exactly what is stored (here the default) writes nothing and reports a skip, every run. + await payload.updateGlobal({ slug: 'look' as never, data: { mode: 'light', toggle: true } as never }) + const before = ((await payload.findGlobal({ slug: 'look' as never })) as { updatedAt: string }).updatedAt + for (let run = 0; run < 2; run++) { + expect(await seeder().fillGlobal('look', { mode: 'light', toggle: true })).toMatchObject({ status: 'skip (exists)' }) + } + expect(((await payload.findGlobal({ slug: 'look' as never })) as { updatedAt: string }).updatedAt).toBe(before) + // A different seeded value still replaces a choice at its default. + expect(await seeder().fillGlobal('look', { mode: 'system', toggle: true })).toMatchObject({ note: 'empty fields filled: mode', status: 'create' }) // An editor's choice that differs from the default stays. await payload.updateGlobal({ slug: 'look' as never, data: { mode: 'dark' } as never }) await seeder().fillGlobal('look', { mode: 'system' })