Skip to content

refactor(seeds): provision the preview admin at deploy, not from tests - #666

Merged
sydevs-bot merged 2 commits into
mainfrom
claude/refactor-preview-admin-seed
Sep 1, 2026
Merged

sydevs-bot merged 2 commits into
mainfrom
claude/refactor-preview-admin-seed

Conversation

@sydevs-bot

Copy link
Copy Markdown
Contributor

Closes #662.

A preview now reconciles its admin on every deploy, so rotating PREVIEW_ADMIN_PASSWORD takes effect on the next deploy instead of orphaning the environment. ensureAdmin in the smoke helper collapses to a login, and the 403 special-case that existed to explain the orphaned state goes with it.

The gate is three conditions, and each one excludes a place this must never run:

Condition What it excludes
A Railway environment name is present local dev, CI, and both test lanes
Not production (by that name, never NODE_ENV) production
A password was supplied environments forked before 2026-08-27, explicitly out of scope on the ticket

The middle one is the trap worth naming: Railway previews run NODE_ENV=production, which is exactly what once sent preview mail through Resend to real addresses. This reads isProductionDeployment(), the same Railway environment-name check the email adapter and the storage guard already make.

The first one is the one that is easy to get wrong here. CI genuinely holds PREVIEW_ADMIN_PASSWORD as a secret, so a gate reading only the password would provision an admin into the integration lane's database on every pnpm test:int. tests/unit/preview-admin-gate.spec.ts covers that case by name.

⚠ What I could not verify, and you should know before merging

I cannot run the integration lane — this environment has no PostgreSQL (127.0.0.1:5432 refuses, DATABASE_URL unset). That is the gap #663 was filed for, and it matters more than usual here because this PR touches src/payload.config.ts, which every integration suite boots.

What that leaves unproven locally: that adding onInit does not disturb the ~66 integration suites. The gate is proven inert off-Railway by unit test and onInit returns on its first line there, so the expected blast radius is zero — but CI is the first thing that actually runs it.

The smoke lane is also unverifiable from here, and it is the half that proves the feature works at all. Its first run against this PR's Railway preview is the real test: it must log in with no first-register fallback left to save it.

Design decisions, and the three library behaviours I read out of `node_modules` rather than assumed

Why onInit rather than a preDeployCommand. The ticket said "predeploy step". onInit is the same in-process pattern this repo already uses for migrations (prodMigrations), it runs after them, and it needs no assumptions about what the Railpack deploy image contains — pnpm start runs .next/standalone/server.js, which carries only traced production deps, so a tsx script in a predeploy hook is a bet on tooling that may not be there.

Why src/plugins/previewAdmin/. src/plugins/ is defined as "modules registered in payload.config.ts", which onInit is. src/lib/ was wrong on its own rule — one consumer means it is not shared.

Three things read from the installed Payload source, because getting any of them wrong produces a row that cannot authenticate:

  • payload.update with data.password does hash it — collections/operations/utilities/update.js:239 runs generatePasswordSaltHash and deletes the plaintext. This is why the ticket's "local API, never raw SQL" instruction is load-bearing.
  • _verified: true must be passed explicitly. The Managers collection configures auth.verify, and auth/operations/login.js:184 refuses a login while _verified === false. create.js:183 preserves a supplied true (Boolean(result._verified) || false), so passing it works; first-register used to set it implicitly and a plain create does not.
  • loginAttempts / lockUntil are real base fields (auth/baseFields/accountLock.js), so the reconcile clears them. A rotated secret means the smoke lane has been failing to log in, and maxLoginAttempts: 5 locks the account after five — fixing the password without clearing the lock would leave it shut for another ten minutes.

It never throws. A preview that cannot seed still boots and serves; the smoke lane's login is what reports the problem. Failing the deploy would take a whole preview down over a test credential.

One consequence I did not design for but should flag: Railway sets RAILWAY_ENVIRONMENT at build time as well as runtime, so if anything initialises Payload during next build on a preview, the reconcile runs there too. It is an idempotent upsert, so this is harmless — but it means the [previewAdmin] log line may appear twice in a deploy.

Every assertion in the new spec was made to fail before being kept, per .claude/rules/tests.md:

defect reintroduced red
gate reads only the password 3
gate drops the production term 1

Gates run locally: pnpm lint, pnpm typecheck, pnpm typecheck:tests, pnpm test:unit (1502 passed, was 1502 + 5 new = 114 files). The integration and smoke lanes are CI's, for the reason above.

Verification checklist for you

The ticket's checklist is mostly things only a real deploy can answer:

  • Railway predeploy/boot log on this PR's preview names a [previewAdmin] created … or reconciled … line
  • This PR's first smoke run passes the auth-dependent specs
  • Change PREVIEW_ADMIN_PASSWORD, redeploy this preview, confirm smoke still passes — the failure mode this removes
  • A production deploy log shows no [previewAdmin] line at all
  • grep -rn "first-register" tests/ returns nothing

🤖 Generated with Claude Code

https://claude.ai/code/session_01HgTsT7Qp7vZsZDfvAenWLd


Generated by Claude Code

Closes #662.

A preview's Postgres volume outlives its deploys, so an admin created once by
`first-register` in the smoke run kept the password THAT run used — the
credential became a property of the database rather than of the current secret,
and rotating `PREVIEW_ADMIN_PASSWORD` orphaned every already-seeded preview.

`onInit` now reconciles the admin on every boot of a preview environment, after
the migrations the Postgres adapter applies. `ensureAdmin` collapses to a login,
and the 403 special-case that reported the orphaned state goes with it.

The gate is three conditions and each excludes a place this must never run: on
Railway at all (so local dev, CI and both test lanes are inert — CI genuinely
holds the password as a secret), not production (by Railway's environment name,
never NODE_ENV, since previews run NODE_ENV=production), and a password present
(pre-2026-08-27 environments never receive one and stay out of scope).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@railway-app

railway-app Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

🚅 Deployed to the SahajCloud-pr-666 environment in sahajcloud

Service Status Web Updated (UTC)
SahajCloud ✅ Success (View Logs) Sep 1, 2026 at 6:34 pm
1 service not affected by this PR
  • mailpit

@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Correction to the description: I could run the integration lane after all, and it passes. 67 files, 1135 tests, green.

The claim above — "this environment has no PostgreSQL" — was wrong, and I found out while revising #663 an hour later. PostgreSQL 16 is already installed in the routine image (/usr/lib/postgresql/16/bin/); it is simply not on $PATH and no cluster is running. initdb + pg_ctl start + createdb payload_test yields exactly DEFAULT_TEST_DATABASE_URL, and the lane runs unmodified.

So the one thing this PR could not verify locally — that adding onInit does not disturb the suites that boot payload.config.ts — is now verified:

Test Files  67 passed (67)
     Tests  1135 passed (1135)

The reasoning in the description still holds (the gate returns on its first line off Railway), but it is now measured rather than argued.

What that leaves for review is narrower than the description implies. Only the smoke half is still unverifiable from here, and it remains the half that proves the feature works at all — this PR's Railway preview must log in with no first-register fallback left to save it. The four deploy-dependent boxes in the checklist are unchanged.

I have rewritten #663 around the corrected finding; the ticket is now about starting the Postgres that is already there, not installing one.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot

Copy link
Copy Markdown
Contributor Author

CI is green, and you should not read that as this PR being verified. The smoke lane skipped.

Discover Railway preview URL polled for 12 minutes (18:12:15 → 18:24:32) and found nothing, so Run smoke specs against the Railway preview was skipped and the job still concluded success. That is the behaviour open issue #661 exists to fix, and this PR is the worst possible case to hit it on: the smoke lane is the only thing that can prove this change works, because the whole feature is "a preview deploy provisions an admin the smoke lane can log in as".

step result
pnpm lint · typecheck · typecheck:tests ✅
pnpm test (unit + integration) ✅ 6m40s
Discover Railway preview URL ✅ — but found no preview after 12m
Run smoke specs ⏭️ skipped

So what is actually established: the change is sound in isolation and disturbs nothing — 1502 unit and 1135 integration tests pass, on CI and locally. What is not established: that a Railway preview boots, runs onInit, and yields an admin that logs in.

Whether that needs action from you depends on something I cannot see: if this branch has no Railway preview environment at all, there is nothing to fix and the verification simply has to happen after merge (or on a preview created by hand). If a preview did deploy and discovery missed it, that is #661 biting. The discovery step's own log says which.

Handing this back to you either way — the four deploy-dependent boxes in the checklist are unchanged, and the first one ([previewAdmin] created … in a preview's boot log) is now the thing standing between this and confidence.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@sydevs-bot

Copy link
Copy Markdown
Contributor Author

Correcting my last comment: a preview DID deploy, successfully. The smoke lane skipped because the preview has no public URL — which means this needs a Railway configuration check, not a wait-and-see.

I said discovery "found no preview". Railway's own events arrived afterwards and say otherwise: SahajCloud-pr-666 built at 18:04 and reported ✅ Success at 18:06:46, six minutes before discovery even started polling at 18:12:15.

Three independent signals agree on why it was still not usable:

signal what it shows
Commit status on 6e7cacd context sahajcloud - SahajCloud, state success, description: "Success" — no domain
Railway's PR comment the Web column is empty
sahajcloud-sahajcloud-pr-666.up.railway.app/api/health 404 from Railway's edge (and the shorter variant too)

scripts/get-railway-preview-url.ts parses the domain out of that status description — its docblock cites "Success - sahajcloud-sahajcloud-pr-470.up.railway.app" as the expected shape, and matches /([a-z0-9-]+\.(?:up\.)?railway\.app)/i against it. A bare "Success" yields nothing, so it polled the full 12 minutes and emitted an empty preview_url.

So discovery behaved correctly given its input. The gap is upstream: this preview environment appears to have no public domain assigned. That is distinct from #661 — which is about the lane skipping silently once there is no URL — though the two compound, and #661 is exactly why the job still went green.

What I'd check first: whether PR preview environments in the sahajcloud Railway project are configured to generate a public domain at all. If they stopped, every preview since is in this state and the smoke lane has been green-but-inert on all of them, which would also explain why #661 was filed. I cannot see the Railway project from here — no API token — so this is where it has to hand off.

⚠ One honest limit on the evidence: the 404 proves those two guessed hostnames do not exist, not that no domain exists — Railway could be using a different scheme or a custom domain. The empty Web column and the domain-less status description are what make me confident rather than the probe alone.

Nothing about the code in this PR changed; it remains green on lint, typecheck, typecheck:tests and the full unit + integration suite, locally and on CI. What is still unproven is the same one thing: that a preview boots, runs onInit, and yields an admin that logs in. With a reachable preview that becomes a one-command check.

🤖 Written by the sydevs autonomous loop — see what this is


Generated by Claude Code

@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-666 September 1, 2026 18:31 Destroyed
@sydevs-bot
sydevs-bot merged commit fea548a into main Sep 1, 2026
2 checks passed

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-666 — 85152f4e Deployed Sep 1, 2026 by railway-app[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(seeds): provision the preview admin at deploy, not from tests

3 participants