[v4] `localize-status` migration unpublishes every document that has a draft newer than its published version

Open
#19,405 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
mongodb, postgresql, sqlite, typescript
Domain
backend, databases

Research direction

Start with the linked MongoDB and Postgres migration files and the named SQLite migration, then compare how each adapter derives and writes locale status; check the corresponding global migrations too. Reproduce the published document with a newer draft and the document with content only in en using the linked script. Done means the migration preserves the intended publication state consistently across adapters and the migration guide documents the behavior for locales without content.

Written by the indexing model from the issue text.

Description

Bug created-by: Contributor db: mongodb db: postgres db: sqlite status: needs-triage v3
Describe the Bug

The predefined localize-status migration sets the main document's per-locale _status from the document's latest version. On Payload 3, that isn't the document's publication state. Saving a draft on a published document creates a newer version with _status: 'draft', while the main document stays published and keeps serving the published content.

After the migration, such a document is draft in every locale, including locales nobody edited:

  • it disappears from where: { _status: { equals: 'published' } } queries and from draft: false reads, though nobody unpublished it;
  • the main document still holds the published content. Only its status changed.

Payload 4 itself doesn't behave this way. With the same history on a v4 database (publish en and de, then save a draft in en), the main document stays published in both locales.

Repro output, 4.0.0-beta.7. The document was published in en and de on Payload 3, and its versions are, oldest first: published, published, draft (latest: true):

main doc _status after migration find _status: published, en / de
MongoDB { en: 'draft', de: 'draft' } missing / missing
Postgres draft in both docs_locales rows missing / missing
SQLite draft in both docs_locales rows missing / missing
Same history created on v4 (all three) published in both locales found / found

The same logic is in all three adapters (main at 3eb46cf):

  • MongoDB, migrateLocalizeStatus.ts#L249-L267 (collections) and #L296-L310 (globals):
    // Get the latest version for this document to determine status per locale
    const latestVersions = await connection
      .collection(versionsCollection)
      .find({ parent: doc._id }, { session })
      .sort({ createdAt: -1 })
      .limit(1)
    
  • Postgres, localize-status/migrateMainCollection.ts#L37-L53: ORDER BY v.created_at DESC LIMIT 1 per locale. migrateMainGlobal.ts does the same.
  • SQLite: packages/drizzle/src/sqlite/predefinedMigrations/localize-status/migrateMainCollection.ts, same query.

Expected: the main document keeps the publication state it had. On Payload 3 data, a main document with scalar _status: 'published' is published in its locales, whatever its latest version says. The versions' own statuses can stay as computed.

Related: the adapters disagree on locales without content

Take a published document whose content exists only in en (title: { en }, no de row). On Payload 3, _status isn't localized, so find({ locale: 'de', where: { _status: { equals: 'published' } } }) returns it, and de reads en through fallback. After the migration:

main doc _status after migration find _status: published, de
MongoDB { en: 'published', de: 'published' } found
Postgres only an en row (published); no de row missing
SQLite only an en row (published); no de row missing
Same history created on v4 (all three) en: 'published'; de not published (Mongo stores de: 'draft') missing

Why they differ:

  • MongoDB writes a status for every configured locale.
  • Postgres and SQLite only UPDATE ... WHERE _parent_id = ? AND _locale = ? existing _locales rows, so a locale without a row gets no status.

The same v3 database ends up with different publication states depending on the adapter. MongoDB's result also differs from what v4 itself writes for the same history. Either behaviour could be the intended one, but they should agree, and the migration guide should say which one applies.

Link to the code that reproduces this issue

https://github.com/VeiaG/payload-4-reproductions/tree/repro-localize-status-unpublishes-newer-draft

Reproduction Steps

From apps/next after pnpm install:

  1. MongoDB (in-memory): pnpm exec payload run ./scripts/localize-status.ts
  2. SQLite: DEV_DB=sqlite DATABASE_URI_SQLITE=file:./.sqlite/localize-status.db pnpm exec payload run ./scripts/localize-status.ts. Use a fresh file each run, because the script changes the schema.
  3. Postgres: docker compose up -d in the repo root, then DEV_DB=postgres pnpm exec payload run ./scripts/localize-status.ts. Run it on an empty database.
  4. Postgres through payload migrate:
    1. DEV_DB=postgres LS_STEP=setup pnpm exec payload run ./scripts/localize-status.ts
    2. DEV_DB=postgres pnpm exec payload migrate --force-accept-warning
    3. DEV_DB=postgres LS_STEP=report pnpm exec payload run ./scripts/localize-status.ts

On every adapter, "draft on top" goes from published to draft in every locale, while the v4 control stays published. For "en only", MongoDB publishes de, and Postgres and SQLite don't.

In a real upgrade, the steps are:

  1. On Payload 3, publish a document in en and de.
  2. Save a draft on top of it in one locale.
  3. Upgrade to v4.
  4. Run payload migrate:create --file @payloadcms/db-mongodb/localize-status (or the db-postgres / db-sqlite variant), then payload migrate.
Which area(s) are affected?

db: mongodb, db: postgres, db: sqlite

Environment Info
payload: 4.0.0-beta.7
@payloadcms/db-mongodb: 4.0.0-beta.7
@payloadcms/db-postgres: 4.0.0-beta.7 (Postgres 17)
@payloadcms/db-sqlite: 4.0.0-beta.7
Node: 24
OS: Windows 11
Dominant language
TypeScript
Stars
45.8k
Forks
4.4k
Avg merge
2d 16h
Merged PRs (30d)
251

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from payloadcms/payload

All issues in payloadcms/payload

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.