Files
ipal-kit/docs/agent/skills/ipal-migrate-fields/SKILL.md
T
Radosław SmolińskiandClaude Opus 5.5 2d9f3e15d4 Docs in English (part 1), seeding guide, Coolify deployment
- Translate to English every doc the code branches did not touch: AGENT.md,
  developer guide, vibe, CHANGELOG, docs index, module pages, all skills
  except ipal-seo-check (14 pages follow after the security/SEO merge).
  Placeholder marker is now [TO FILL: …]; skill resources renamed to
  section-code.md / nav-code.md. AGENT.md and the ipal-skills pointer
  block say the docs are in English.
- docs/seeding.md: create-only, natural keys, never delete, no users or
  secrets, empty-field globals, fallbackLocale: false, dry run, backup and
  restore, production update guard, seeds vs migrations, Coolify Terminal.
  Seed templates gain --dry-run, the SEED_UPDATE production guard
  (IPAL_SEED_ALLOW_PROD_UPDATE), skip of edited records and a summary.
  "Seeding on production" rule block in AGENT.md.
- Coolify is the single deployment path (deployment, env, developer guide,
  ipal-setup): build vs runtime env, migrations on container start
  (prodMigrations), health check and rolling updates, rollback, backups and
  restore, DNS/Let's Encrypt/CAA. R2: custom domain needs a Cloudflare zone;
  default is media served by the app (R2_PUBLIC_URL empty).
- docsConsistency: no Polish prose outside code/quoted data (pending pages
  listed); IPAL_* names may also be read by shipped skill templates.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-22 17:39:03 +02:00

107 lines
7.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
name: ipal-migrate-fields
description: Safely changes the shape of fields that already hold content in an ipal-kit / Payload 3 project — renaming a field, replacing old CTA fields with buildLinkField/buildLinksField, moving fields into a group, splitting or merging blocks, changing a field's type, making a field localized. Three phases (add new → copy data with a dry-run script → remove old), all locales, no content loss. Use whenever a schema change touches fields that may already have data in the database, including refactors suggested by ipal-audit.
---
# ipal-migrate-fields
Payload **does not migrate data for you**. Renaming a field or changing its type in code
= the old data stays in the database under the old key, the admin panel shows an empty field, the front end
renders nothing. On Postgres there is also a schema migration that can **drop the column
with the data**. So always three phases, never "change it and see".
## When you do NOT need this skill
- Project without content (fresh, seeded from scratch) → change the schema, re-seed.
- You are adding a **new** field (not replacing an old one) → a normal change.
- You only change `label`, `admin.*`, `validate`, `defaultValue` → no effect on data.
Everything else (rename, type, moving into a group/array, `localized: true/false`,
replacing CTA fields with `buildLinkField`, splitting a block) → this skill.
## Before you start (on your own, without asking)
1. **Database backup** — Mongo: `mongodump --uri "$DATABASE_URI" --out backup/$(date +%F)`;
Postgres: `pg_dump "$DATABASE_URI" > backup/$(date +%F).sql`. Do not start without a backup.
2. Identify the database adapter (`payload.config.ts` → `mongooseAdapter` / `postgresAdapter`).
3. Write down: collection/global, block type, OLD fields → NEW fields, which ones are `localized`.
4. Find every place that reads the old fields: `grep -rn "<oldName>" src/`.
## Phase 1 — add the NEW fields next to the OLD ones
- Add the new fields to the schema; the old ones **stay**, mark them in the panel:
`admin: { description: 'DEPRECATED — migrated to <new>', readOnly: true }`.
- Front end: read the NEW fields with a fallback to the OLD ones (during the transition):
`const links = block.links?.length ? block.links : legacyToLinks(block)`.
- `pnpm payload generate:types` · Postgres: `pnpm payload migrate:create add-<name>` +
`pnpm payload migrate` (the migration only ADDS columns — review the file before you run it).
- `tsc --noEmit` clean.
## Phase 2 — move the data with a script
1. Copy the template: `resources/migrate-block-fields.template.ts` →
`scripts/migrate-<name>.ts`. Change only the `CONFIG` section and `migrateBlock()`.
The template: all documents × all locales, idempotent,
`context: { ipalMigration: true, ipalSkipIndexNow: true, disableRevalidate: true }`.
2. Rules in `migrateBlock()`:
- **The first pass (defaultLocale) creates the structure** (array rows, blocks).
- **Other locales only fill `localized` fields** in existing rows —
use the row `id`s from the read (new `id` = Payload creates new rows, and content
disappears in other languages).
- Relations at `depth: 0` are IDs — copy IDs, not objects.
- Write non-localized fields only in the defaultLocale.
- Always write another language with `fallbackLocale: false` (it is in the template) — otherwise
Payload saves the defaultLocale text as the translation.
3. Dry run: `pnpm payload run scripts/migrate-<name>.ts` → read the list of changes,
check 2–3 documents by hand (especially the second language). When the migration creates new
rows, the dry run shows only the defaultLocale pass (the rows do not exist yet) —
you will see the other languages in the APPLY output.
4. Write: `APPLY=1 pnpm payload run scripts/migrate-<name>.ts`.
5. Verify: another dry run → `0 block change(s)`; in the panel open the document in
every language; on the front end `pnpm build && pnpm start` + `ipal-seo-check`.
Globals (Header, Footer): instead of `find` → `findGlobal`/`updateGlobal({ slug, data, locale })`
— the same loop over locales. Collection with `versions.drafts`: the script updates
the main version; if a document has a newer draft, add `draft: true` to the read and
the write, or publish the drafts before the migration (ask only if they are the client's drafts).
Project hooks that send emails/revalidate/call webhooks: with `context.ipalMigration`
they must do nothing (`if (context?.ipalMigration) return`). Check before APPLY. Plugin hooks
have their own flags (they are in the template): `ipalSkipIndexNow` (IndexNow with `indexNow: true` in
production would ping Bing for every page), `disableRevalidate` (`buildRevalidateHook`).
## Phase 3 — remove the OLD fields
- **Production with client content → ask the user before phase 3** (the only question
in this skill). Locally / on staging → do it yourself.
- Remove the old fields from the schema, the fallback from the front end, `grep` → zero hits.
- `generate:types`; Postgres: `migrate:create remove-<name>` — this migration
**DROPS columns**; run it only after phase 2 on the same database.
- Mongo: the old keys stay in the documents (harmless). Cleanup is optional:
`$unset` in a separate script.
- `ipal-audit --quick` → 0 errors.
## Common cases → what goes in migrateBlock
| Change | First pass | Other locales |
|---|---|---|
| rename `title` → `heading` | `{ heading: block.title }` | the same, if localized |
| CTA (`ctaLabel`, `ctaUrl`) → `buildLinkField` | `{ link: { type: 'external', url, label, appearance } }` (page from the panel: `type: 'internal', page: id`) | only `link.label` |
| CTA with a relation → `buildLinksField` | row `{ link: { type: 'internal', page: id, … } }` (template) | `links[i].link.label`, keeping the `id` |
| fields → `group` | `{ group: { a: block.a, b: block.b } }` | localized fields in the group |
| `text` → `richText` | `{ body: textToLexical(block.text) }` (paragraphs → `paragraph` nodes) | the same |
| non-localized → `localized` | copy the value to the defaultLocale | (optional) the same value as a starting translation — ask |
| split block A → A + B | add a new block after A (`blockType: 'b'`, no `id`) | fill the localized fields by the new block's `id` |
Lexical from text:
```ts
const textToLexical = (t: string) => ({ root: { type: 'root', format: '', indent: 0, version: 1, direction: 'ltr',
children: t.split(/\n{2,}/).map((p) => ({ type: 'paragraph', format: '', indent: 0, version: 1, direction: 'ltr', textFormat: 0,
children: [{ type: 'text', text: p, format: 0, style: '', mode: 'normal', detail: 0, version: 1 }] })) } })
```
## When something goes wrong
- Duplicated rows in the second language → row `id`s were not used; restore the backup, fix, repeat.
- `ValidationError` on APPLY → a new `required` field has no value in the old data;
add a default value in `migrateBlock` or temporarily drop `required`.
- The script does not start (`Cannot find module '@payload-config'`) → alias in `tsconfig.json`
(`"@payload-config": ["./src/payload.config.ts"]`) — standard in the Payload template.
Final report: how many documents / blocks / locales were changed, where the database backup is,
whether phase 3 was done (or is waiting for approval).