# Payload CMS Migration Regression Test: Fail Fast, Not Green

- Author: Abdullah Chaudary
- Category: Next.js
- Published: 2026-11-30T19:00:01.754Z
- Updated: 2026-11-30T19:00:01.807Z
- Reading time: 7 min
- Tags: Payload CMS, Postgres, Docker, CI/CD, GitHub Actions

> A Payload CMS migration regression test for CI seeds the dev marker row and a pending migration, runs the step with no TTY under a deadline, and checks exit code and time.

On 2 November 2026 I ran `payload migrate` against a throwaway Postgres with stdin set to `/dev/null`, and it was still waiting at the 600-second mark. I killed it with `timeout`, which exits 124. Closing stdin, setting `CI=true` and attaching an open pipe all did the same. The one variation that came back quickly was answering "n": exit 0 in 9.4 seconds, with the migration not applied.

A Payload CMS migration regression test for CI has to catch both outcomes, the wait and the green step that did nothing. The cause is a marker row in `payload_migrations`, which [my earlier post diagnoses](/blog/payload-migrate-hang-docker-postgres) and which I do not retell here. Two readers asked for a regression fixture and a timeout on the step. This post is the build and what it measured.

**TL;DR:** To test a Payload CMS migration step in CI, seed a throwaway Postgres with one pending migration and the `dev` row, run the step with stdin from `/dev/null` under a hard deadline, and check the exit code, the elapsed time and the `payload_migrations` table. In my sandbox the plain step failed the fixture twice, and a pre-check plus a timeout passed it three times.

## Why Doesn't CI=true or a Closed stdin Stop payload migrate From Waiting?

Because nothing in the prompt path looks at either. In Payload 3.88.0, `@payloadcms/drizzle` asks a yes/no question whenever `payload_migrations` holds a row with batch -1, using the `prompts` library, and `prompts` reads `process.stdin` with no check for a missing terminal.

I ran four variations with the marker row present and one migration pending. Each printed the first line of the prompt after about 10 to 15 seconds of CLI start-up and then waited until `timeout` killed it:

- stdin from `/dev/null`: still waiting at 600 seconds, exit 124.
- stdin closed with `<&-`: exit 124 at 60 seconds.
- `CI=true` with stdin from `/dev/null`: exit 124 at 60 seconds.
- stdin an open pipe that never sends data: exit 124 at 60 seconds.

In `node_modules/prompts/lib`, `grep -rn isTTY` finds only `prompt.js` lines 25 and 41. The only terminal branch switches raw mode on. The library reads no `CI` variable, and nothing resolves the prompt when the stream ends. About 8 minutes into the first run, `ps` showed 12 seconds of CPU time, `/proc/<pid>/fd/0` pointing at `/dev/null`, and one idle Postgres connection.

## What Should a Migration Test Assert Besides Exit Code 0?

That every migration file has a row in `payload_migrations` with a positive batch. An exit code of 0 proves only that the process ended, and answering the prompt with "n" ends it normally. In my run, `echo n | npx payload migrate` exited 0 and applied nothing.

```text
$ echo n | timeout 60 npx payload migrate
If you'd like to run migrations, data loss will occur. Would you like to proceed? … no
exit=0 elapsed=9.44s
          name           | batch
-------------------------+-------
 20261102_134021_initial |     1
 dev                     |    -1
(2 rows)

assert-no-pending: FAIL, 1 migration(s) not applied: 20261102_134042_add_note_body
```

The post-check is a short Node script. It lists the migration files, selects the names with `batch > 0`, and exits 4 for any file with no row:

```js
const { rows } = await client.query('SELECT name FROM payload_migrations WHERE batch > 0')
const applied = new Set(rows.map((r) => r.name))
const pending = files.filter((f) => !applied.has(f))
if (pending.length) process.exit(4)
```

Answering "y" is not a fix either. `echo y |` applied the migration as batch 2 in 10.9 seconds, and the `dev` row stayed. Payload filters the row out of an in-memory list at `migrate.js` lines 44 and 45 and never deletes it. The next run, with nothing pending, prompted again and waited until the 40-second limit killed it.

## How Do You Write a Payload CMS Migration Regression Test for CI?

Seed a known state, run the exact step CI runs, and compare exit code and elapsed time with what you expect. My fixture is one Bash file and a seed script. The seed script drops the public schema, so it refuses any database that is not on loopback. It runs three cases under an outer 60-second limit.

```bash
run_case "pending migration applies"                pending     0   45 ""
run_case "dev marker is caught by pre-check"        pending+dev 3   15 ""
run_case "dev marker without pre-check hits timeout" pending+dev 124 45 "SKIP_PRECHECK=1 MIGRATE_TIMEOUT=30"
```

The columns are the case name, the seeded state, the wanted exit code and the most seconds the case may take. The first case migrates a clean database and then un-applies the second migration. The other two add the `dev | -1` row.

The step under test is a wrapper with three parts. A pre-check queries for `batch = -1` and exits 3, naming the row, before Payload starts. The migrate call runs with stdin from `/dev/null` under `timeout --kill-after=10`, and a 124 or 137 fails the step. The post-check from the last section runs last.

```bash
timeout --kill-after=10 "$limit" npx payload migrate </dev/null
code=$?
if [ "$code" -eq 124 ] || [ "$code" -eq 137 ]; then
  echo "::error::payload migrate did not finish within ${limit}s (exit $code)."
  exit 124
fi
```

The pre-check failed in 0.2 to 0.36 seconds with the row's id, name and batch in the log. This is the pipeline [this site's deploy runs inside](/blog/one-vps-nextjs-payload-cloudflare-nginx), and the proposed change is a diff to its migrate step. I have not applied that diff, and as of 30 November the workflow in my working tree still runs plain `npx payload migrate`.

## How Do You Know the Test Fails When the Guard Is Removed?

Run the same fixture against the unguarded step and watch it go red. A test that has never failed has not shown it can. I ran it twice against each step, and the verdicts matched.

Against the plain step, `npx payload migrate </dev/null`, the happy path passed in 13.3 seconds and the two marker cases hit the 60-second outer limit:

```text
    PASS exit=0 in 13.3s
    (outer fixture limit of 60s hit: the step hung)
    FAIL exit=124 in 60.1s (wanted exit 3 within 15s)
    FAIL exit=124 in 60.1s (wanted exit 124 within 45s)
--- 2 FAILED
fixture exit=1 total=182s
```

Against the wrapper, all three passed in 87 seconds, 30 of them spent on the deliberate timeout case:

```text
    PASS exit=0 in 13.7s
    PASS exit=3 in 0.3s
    PASS exit=124 in 30.2s
--- ALL PASS
fixture exit=0 total=87s
```

The red run is also the reason to keep the fixture. Both of B07's earlier fixes chased the wrong cause, and a seeded marker row makes the original failure reproducible on demand. It is the same lesson as [a grep step that exits 0 without looking](/blog/enforce-agents-md-rules-in-ci): a check that cannot run has to fail the job.

## Is a Step-Level timeout-minutes Enough for the Migrate Step?

No. GitHub's `timeout-minutes` on a step is the maximum number of minutes before the process is killed, and it must be a positive integer, so the shortest deadline is one minute and the failure says nothing about the cause. A job's default is 360 minutes. This site's job sets 30, and the migrate step sets nothing.

In the sandbox, the pre-check named the row in 0.3 seconds. A shell `timeout` of 30 seconds failed with a message pointing at the probable cause. A 30-minute job limit would have waited 30 minutes for the same exit. I would still set the step-level limit as a backstop for a stall the checks do not know about. The proposed diff uses 2 minutes for the pre-check, 5 for the migrate step and 2 for the post-check.

I did not run any of this on GitHub Actions. I do not know whether a step that hits `timeout-minutes` is reported as failed or as cancelled, and I did not record which stdin a self-hosted runner gives a step.

## How Long Should the Migrate Deadline Be?

Long enough for a clean run, short enough to hurt. I chose 120 seconds, about eight times the slowest clean local run, which took 14.1 seconds including 9 to 15 seconds of Payload start-up. B07's CI logs showed about 6 seconds for a whole clean step. The 120 is my judgement, not a figure derived from CI timings.

A longer migration changes the answer. A real data migration can legitimately run past two minutes, and a fixed `MIGRATE_TIMEOUT` would kill it. Make the limit a variable, and let the migration's author raise it in the same commit.

## Limitations

- Every run used Payload 3.88.0, `@payloadcms/db-postgres` 3.88.0, `prompts` 2.4.2, `pg` 8.20.0, Postgres 17.11 and Node 24.14.1. CI pins Node 22, which I did not test.
- On 30 November 2026, Payload's `main` branch had a `shouldPrompt` option on the Drizzle adapter's `migrate`, which returns `cancelled: true` instead of prompting. Version 3.88.0 has no such option, the latest release on npm was 3.94.2 (published 24 November), and I ran neither it nor the `payload migrate` command against `main`. I do not know whether the command sets that option.
- I inferred, and did not trace, why the process stays alive after the stream ends. An idle connection fits a pool holding the event loop open, but I took no handle dump.
- The `--kill-after` branch for exit 137 never ran, because SIGTERM ended every timed-out process.
- Only the dev-mode prompt was tested. I did not read the rest of Payload's CLI for other prompts.
- The pre-check and the fixture ran against throwaway databases only. I do not know whether this site's build or production database has a `batch = -1` row today.
- The GitHub Actions job that runs the fixture against a `postgres:17-alpine` service container parses as YAML and has not run on GitHub.

If you run `payload migrate` in a pipeline, what else in that pipeline can wait for a keypress, and how would you find out before the job timeout does? The [rest of the Next.js and Payload field notes](/blog/category/next-js) from this rebuild are in the category, and [the project page](/projects/abchaudary-me) covers the site this pipeline deploys.

## References

- [Payload CMS: Migrations](https://payloadcms.com/docs/database/migrations): the migrate commands, and the advice to run `payload migrate` in CI before the build.
- \[GitHub Docs: Workflow syntax, \`jobs.<job\_id>.steps[\*\].timeout-minutes\`](https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax#jobsjob_idstepstimeout-minutes) and [`jobs.<job_id>.timeout-minutes`](https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax#jobsjob_idtimeout-minutes): the step limit and the 360-minute job default, read on 30 November 2026.
- [Payload source: `packages/drizzle/src/migrate.ts` on `main`](https://github.com/payloadcms/payload/blob/main/packages/drizzle/src/migrate.ts): the `shouldPrompt` option and the `cancelled` return, read on 30 November 2026.
- Payload 3.88.0 and `prompts` 2.4.2 source in `node_modules`: `@payloadcms/drizzle/dist/migrate.js`, `payload/dist/bin/migrate.js`, `prompts/lib/elements/prompt.js` and `confirm.js`.
- My own sandbox: a minimal Payload project and Postgres 17 container, captured on 2 November 2026. The primary source for every timing above.
