@nvok/sandpiper 0.0.0-stage → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +601 -2
  2. package/bin/sandpiper.mjs +27865 -0
  3. package/package.json +66 -4
package/README.md CHANGED
@@ -1,3 +1,602 @@
1
- # Temporary Holding Version
1
+ <p align="center">
2
+ <img src="assets/sandpiper-icon.png" alt="Sandpiper icon" width="160">
3
+ </p>
2
4
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
5
+ # sandpiper
6
+
7
+ An interactive, human-only migration flow for Node.js projects. Named after the Arctic sandpipers,
8
+ the small long-winged shorebirds that breed on the tundra. A sandpiper's bill is short or slightly
9
+ drooped, and its legs are dark. A godwit's bill is very long and slightly upturned.
10
+
11
+ A person runs it from the project directory. It can check migration status, apply committed
12
+ migrations to staging or production, and, in a Payload project, generate a migration, review the
13
+ SQL, and commit that migration on its own. It shows which database it is about to touch, refuses the
14
+ applies that would be unsafe, and leaves a record. The chosen URL is injected into the child
15
+ process. The URL and the command line are never printed.
16
+
17
+ It refuses to run without a TTY, so CI, bots, and coding agents cannot drive it. `--help` and
18
+ `--version` still print, and they do not need a `package.json` or a terminal.
19
+
20
+ ## Install in a project
21
+
22
+ The package is `@nvok/sandpiper`. The command is `sandpiper`. Unscoped `sandpiper` on npm is a
23
+ different package.
24
+
25
+ ```sh
26
+ pnpm add -D @nvok/sandpiper
27
+ ```
28
+
29
+ Add a script:
30
+
31
+ ```json
32
+ {
33
+ "scripts": {
34
+ "sandpiper": "sandpiper"
35
+ }
36
+ }
37
+ ```
38
+
39
+ Then tell coding agents not to touch it. A suggested `AGENTS.md` snippet:
40
+
41
+ ```markdown
42
+ `pnpm sandpiper` is the manual migration flow: check status or apply committed migrations to staging
43
+ and production, and in a Payload project generate and commit a migration on the feature branch. It
44
+ refuses to run without a TTY. Agents must not run it, or a tool's migrate or migrate-create command;
45
+ point the user to it instead.
46
+ ```
47
+
48
+ ### Requirements
49
+
50
+ - Node.js `>=22.13.0`. Development of this repo uses the version in `.nvmrc`.
51
+ - A terminal. Without a TTY on stdin and stdout, Sandpiper exits 1 with
52
+ `Run sandpiper in a terminal.`
53
+ - The project's own migration tool, installed in that project. Sandpiper does not download one.
54
+ - git, for the branch checks, the dirty-folder refusal, and the migration commit.
55
+ - The [1Password CLI](https://developer.1password.com/docs/cli/) (`op`), signed in, when a target's
56
+ value is an `op://` reference. Sandpiper reads that one reference with `op read`. It does not run
57
+ varlock to resolve targets.
58
+ - The [GitHub CLI](https://cli.github.com/) (`gh`), signed in, for the [merge gate](#the-merge-gate)
59
+ and the [migrate log](#migrate-log). Without it, the gate says why and asks for the override
60
+ phrase, and the log line is printed instead of posted.
61
+
62
+ ## Usage
63
+
64
+ ```bash
65
+ pnpm sandpiper
66
+ sandpiper --help
67
+ sandpiper --version
68
+ ```
69
+
70
+ Run it from the project root. In a monorepo, that is the package that owns the migrations. There are
71
+ no subcommands and no `--env`, `--action`, or `--yes` flags. The menu is the interface. Ctrl+C at a
72
+ prompt exits 130.
73
+
74
+ ## The flow
75
+
76
+ 1. **Generate**, in a Payload project, on the feature branch.
77
+ 2. **Push** and wait for CI.
78
+ 3. **Migrate to staging.**
79
+ 4. **Merge** the feature branch once CI is green.
80
+ 5. **Migrate to production** from the same feature branch. The merge gate checks that the branch's
81
+ pull request is merged and green first.
82
+
83
+ Status is read-only. Use it at any point to see which migrations a database has run, and to confirm
84
+ the host Sandpiper will talk to.
85
+
86
+ ### Menu
87
+
88
+ **What do you want to do?** `Status` is the default. `Migrate` applies. `Generate` is offered when
89
+ the `payload` package is installed. Generate does not ask for an environment, and it does not use a
90
+ database.
91
+
92
+ When the project has no `db:migrate` or `migrate` script and more than one tool is detected,
93
+ Sandpiper first asks **More than one tool was found. Which should Sandpiper run?** and lists only
94
+ the detected tools. A script is the command for Status and Migrate, so several tools do not prompt
95
+ in that case.
96
+
97
+ **Which environment?** `Staging` and `Production` are listed only when that environment has a URL.
98
+ One available environment is still shown, so the target is confirmed with Enter. Sandpiper does not
99
+ auto-run because only one URL was set. A configured project can offer other names. See
100
+ [Configuration](#configuration).
101
+
102
+ Before that list, and again at the start of Generate, Sandpiper warns if `DATABASE_URL` (or
103
+ [`dbEnvVar`](#configuration)) is already exported in the shell. It prints that value's masked host
104
+ and says to unset it unless you mean to develop against that database. It does not unset the
105
+ variable, and it does not stop. Under varlock the process environment wins over the schema, so a dev
106
+ server would use whatever that variable points at.
107
+
108
+ ### status
109
+
110
+ A read-only smoke test.
111
+
112
+ 1. Pick a target. The menu shows an `op://` reference as that reference, and a literal URI already
113
+ masked.
114
+ 2. Resolve only that target. See [Resolving a target](#resolving-a-target).
115
+ 3. Print the variable Sandpiper will set, and the masked host. See [Masked host](#masked-host).
116
+ 4. Run the tool's status command. The child's own output is the record. Sandpiper does not print the
117
+ command.
118
+ 5. For Payload, also read `payload_migrations`. Warn when the database is push-managed, or has run a
119
+ migration this checkout does not have. Status lists those names and does not stop.
120
+ 6. Repeat which target and host it talked to.
121
+
122
+ Run it once per project against staging after adopting the tool. If the host it prints is the
123
+ staging host, the override reaches the database Migrate will use.
124
+
125
+ Drizzle has no database status command. Status runs `drizzle-kit check` and prints
126
+ `Drizzle has no database status command. This checks migration files for conflicts and does not query the database.`
127
+ That check does not connect. There is no pending list to read, so the pending, drift, and branch
128
+ checks do not apply to Drizzle.
129
+
130
+ ### migrate
131
+
132
+ Migrate applies the tool's pending migrations. It does not roll a migration back.
133
+
134
+ 1. **Preflight.** Refuses when the migrations folder has uncommitted changes, so a database cannot
135
+ record a migration the repo does not have. Committed migrations that are not pushed, or a branch
136
+ with no upstream, warn, and the run continues. The folder is `src/migrations` for Payload,
137
+ `prisma/migrations` for Prisma, and `supabase/migrations` for the Supabase CLI, unless
138
+ [`migrationsDir`](#configuration) is set. Drizzle's folder lives in `drizzle.config`, which
139
+ Sandpiper does not load.
140
+ 2. **Pick a target.** Only the variable you pick is resolved, and only after you pick it.
141
+ 3. **Production confirmation.** Migrate against Production asks `Apply migrations to production?`
142
+ and defaults to No. No prints `Cancelled.` and exits 0. Status does not ask, including against
143
+ production. Other targets do not ask this question.
144
+ 4. **Merge gate**, for Production and any target flagged `requiresMerge`. This runs before that
145
+ target's secret is resolved. See [The merge gate](#the-merge-gate).
146
+ 5. **Resolve** the chosen target and print the masked host. A host that looks like a pooler warns.
147
+ See [Pooler hosts](#pooler-hosts).
148
+ 6. **Nothing pending.** For Payload, Prisma, and the Supabase CLI, Sandpiper reads that tool's
149
+ status first. No pending rows means the target is up to date, and the migrate command does not
150
+ run. If that status cannot be read, Migrate refuses. It does not apply blind. Drizzle has no
151
+ pending list, so this step does not apply to it.
152
+ 7. **Drift.** If the database has a migration the checkout does not have, Migrate stops. Status only
153
+ lists the names. See
154
+ [Migrations the checkout does not have](#migrations-the-checkout-does-not-have).
155
+ 8. **Push-managed guard**, Payload only. See [Push-managed databases](#push-managed-databases).
156
+ 9. **Other branches.** A pending migration that is not from this branch requires typing `apply all`.
157
+ See [This branch](#this-branch).
158
+ 10. **Confirm** by typing the target's name (`Staging`, `Production`, or the configured label). The
159
+ prompt says to take a backup or snapshot first if the database matters. Any other answer
160
+ cancels. Cancelling before the migrate command applies nothing and records nothing.
161
+ 11. **Run** the tool. The command line is not printed. The URL is not printed. The child's stdout
162
+ and stderr are inherited.
163
+ 12. **Record** one line on the branch's pull request, whether migrate succeeded or failed, then show
164
+ status again after a success. See [Migrate log](#migrate-log).
165
+
166
+ On success the line is `Migrations completed.` For status it is `Status completed.` A failed child
167
+ exits with that child's status code. Sandpiper's own failures exit 1. `varlock run` exits with its
168
+ own status when it fails before the command. A child killed by signal `k` exits `128 + k`.
169
+
170
+ #### How the URL is passed
171
+
172
+ Sandpiper sets `DATABASE_URL` on the child to the chosen URL. It does that on the outermost spawn,
173
+ including a `varlock run` wrapper, so the selected database wins. It does not edit
174
+ `prisma.config.ts`, `drizzle.config.ts`, or `payload.config.ts`, and it does not append flags onto a
175
+ user script.
176
+
177
+ A user script runs through the package manager with `DATABASE_URL` set. The script is responsible
178
+ for honoring it. When the script contains `supabase`, or Supabase was detected, Sandpiper warns
179
+ first:
180
+ `This script is not passed the selected URL. The Supabase CLI reads --remote, not DATABASE_URL, so the script uses whatever target it already has.`
181
+ Then it runs the script unchanged.
182
+
183
+ Default commands are used only when there is no `db:migrate` or `migrate` script. They also pass the
184
+ flag that tool reads. `db:migrate` wins when both scripts exist. Sandpiper runs one of them, not
185
+ both.
186
+
187
+ | Tool | Status | Migrate |
188
+ | ------------------ | ---------------------------------------- | ----------------------------------------------- |
189
+ | Prisma 8+ | `prisma migration status --db <url>` | `prisma db migrate --db <url> --no-advance-ref` |
190
+ | Prisma 7 and older | `prisma migrate status` | `prisma migrate deploy` |
191
+ | Drizzle | `drizzle-kit check` | `drizzle-kit migrate --url <url>` |
192
+ | Payload | `payload migrate:status` | `payload migrate` |
193
+ | Supabase CLI | `supabase migration list --remote=<url>` | `supabase db push --remote=<url> --yes` |
194
+
195
+ The URL in a default command's arguments is never printed. Payload's default command also sets
196
+ `DATABASE_URI` to the same value. If `@payloadcms/db-vercel-postgres` is declared, it also sets
197
+ `POSTGRES_URL`. Otherwise `POSTGRES_URL` is left alone.
198
+
199
+ When the project depends on `varlock` (`dependencies`, `devDependencies`, or
200
+ `optionalDependencies`), a script that does not mention `varlock` is wrapped in `varlock run`. A
201
+ script that already mentions `varlock` is left alone. Payload's default command is the exception: it
202
+ is not wrapped, whether it is the project's `payload` script or the `payload` binary. Payload loads
203
+ varlock itself, and an outer `varlock run` makes that integration throw. A user `db:migrate` or
204
+ `migrate` script in a Payload project is still wrapped when that script does not mention `varlock`.
205
+
206
+ The package manager is the target project's: the `packageManager` field, otherwise the lockfile in
207
+ the working directory (`pnpm-lock.yaml`, then `bun.lock` or `bun.lockb`, then `yarn.lock`, then
208
+ `package-lock.json`), otherwise npm, with `No packageManager field or lockfile found. Using npm.`
209
+ Sandpiper runs the installed binary. It does not `dlx` a different copy. If that binary, or a
210
+ declared `varlock` binary, is missing, it exits 1 and names what to install.
211
+
212
+ ### generate
213
+
214
+ Payload only. Prisma `migrate dev`, Drizzle's generate, and Supabase `migration new` are different
215
+ commands and are not this action. No database is used. Payload compares the current config with the
216
+ latest JSON snapshot in the migrations folder.
217
+
218
+ 1. **Preflight.** The migrations folder must be clean. The branch must not be behind the base
219
+ branch, or the new migration can repeat changes someone else already made. Being on the base
220
+ branch, `main`, or `master` warns and asks to continue. A failed fetch asks to continue without
221
+ that check. The shell-export warning runs here too.
222
+ 2. **Regenerate types and the import map**, using the project's `generate:types` and
223
+ `generate:importmap` scripts, or `payload generate:types` and `payload generate:importmap`. Files
224
+ that change are listed so you commit them with the feature, not with the migration.
225
+ 3. **Create** the migration with `payload migrate:create [name] --skip-empty`. No schema changes
226
+ means it stops. The first migration is announced, confirmed, and named `initial` by default. The
227
+ folder is created first and removed again if Payload writes nothing. Later migrations default to
228
+ a timestamp-only name when the prompt is left blank. A project whose only adapter is
229
+ `@payloadcms/db-mongodb` is refused, because those migrations are written by hand.
230
+ `migrationsDir` is not read from the Payload config. Sandpiper warns when the folder does not
231
+ exist yet.
232
+ 4. **Review** the new file. See [Reviewing SQL](#reviewing-sql).
233
+ 5. **Commit** the new migration files and `index.ts` by themselves, then offer to push. See
234
+ [The migration commit](#the-migration-commit).
235
+
236
+ ## Masked host
237
+
238
+ Before status, and again before migrate runs, Sandpiper prints the variable it will set and a masked
239
+ URI: scheme, host, port, and database name. The user and the password are removed. A `file:` URI is
240
+ shown as-is, because a SQLite path has no credentials. An unparseable value is shown as
241
+ `(unparseable URI)`.
242
+
243
+ In the target menu, a secret still stored as `op://` is shown as that reference. A literal URI is
244
+ shown already masked.
245
+
246
+ ## Resolving a target
247
+
248
+ Targets come from `.env.schema`, or from [`schemaFile`](#configuration). The convention is
249
+ `DATABASE_URL_<TARGET>_UNPOOLED`, or `dbEnvVar` plus that shape. An explicit
250
+ [`sandpiper.targets`](#configuration) map replaces the convention. The part between the prefix and
251
+ `_UNPOOLED` is the target's name (`STAGING` is shown as `Staging`).
252
+
253
+ Sandpiper does not run `varlock load`, and it does not parse `.env`, `.env.local`, or `.env.schema`
254
+ as dotenv files. A non-empty value already in the process environment is used for that name, and it
255
+ wins over the schema. If the names are only in the process environment, that is enough. A schema
256
+ file is not required.
257
+
258
+ Otherwise, after you pick a target, and only then, Sandpiper resolves that value:
259
+
260
+ - An `op://` reference anywhere in the expression is read with `op read`.
261
+ - A plain or quoted URI is used as text.
262
+ - Anything else is skipped and explained. A target written as another varlock function, with no
263
+ `op://` and no plain URI, is skipped.
264
+
265
+ Production's secret is not fetched while staging is on screen, and nothing is written to a varlock
266
+ cache. A target that is not marked `@sensitive` (honoring `@defaultSensitive`) gets a warning,
267
+ because varlock would otherwise print the value in its own summaries. Extra variables for that
268
+ target, from `extraEnvVars`, are resolved the same way, and only for the target that was picked.
269
+ They are looked up as `<NAME>_<TARGET>`.
270
+
271
+ Within a staging or production pair, the `_UNPOOLED` name wins over the bare name. If that pair uses
272
+ the bare name because the unpooled name is unset, Sandpiper warns before the prompt, even if you
273
+ might pick the other environment:
274
+ `Using DATABASE_URL_STAGING without an unpooled URL. Migrations should use an unpooled connection.`
275
+ The production sentence is the same shape. A pair that has the unpooled name does not warn for the
276
+ name. Neither pair set is an error:
277
+
278
+ `No staging or production database URL is set. Set DATABASE_URL_STAGING_UNPOOLED or DATABASE_URL_STAGING, and/or DATABASE_URL_PRODUCTION_UNPOOLED or DATABASE_URL_PRODUCTION.`
279
+
280
+ The four names are `DATABASE_URL_STAGING_UNPOOLED`, `DATABASE_URL_STAGING`,
281
+ `DATABASE_URL_PRODUCTION_UNPOOLED`, and `DATABASE_URL_PRODUCTION`. Empty string is unset. Sandpiper
282
+ does not rewrite the URL.
283
+
284
+ ## Pooler hosts
285
+
286
+ After the URL is resolved, if the host looks like a pooler (`-pooler` or `pooler.`), Sandpiper warns
287
+ that migrations need the unpooled URL. The check is on the host, not on the variable name. The run
288
+ continues. A pooled host stored under the unpooled name still warns. Which URL was selected does not
289
+ change.
290
+
291
+ ## This branch
292
+
293
+ Sandpiper fetches the base branch: `staging` when the remote has it, otherwise the remote's default
294
+ branch, or `sandpiper.baseBranch` when set. Migrations this branch adds are the files added between
295
+ the merge-base with that branch and `HEAD`.
296
+
297
+ Migrate prints two lists. Pending on the target, each marked "this branch" or "not from this
298
+ branch". Added by this branch, each marked pending or already applied. A pending migration that is
299
+ not from this branch requires typing `apply all`, because applying will run the other work too.
300
+
301
+ When that set cannot be computed, every pending migration counts as not from this branch and
302
+ `apply all` is required. That is the case on the base branch, `main`, or `master`; when the fetch
303
+ failed; when there is no common history; and when a merge commit adds nothing relative to the base.
304
+ A squash merge still works after the merge. A merge commit does not.
305
+
306
+ The check is the same for Payload, Prisma, and the Supabase CLI once the pending names are known.
307
+ Drizzle has no pending list.
308
+
309
+ ## Migrations the checkout does not have
310
+
311
+ `payload migrate:status` hides rows that have no file, and Payload hides the dev-mode row from its
312
+ own migrate commands. Status and Migrate write a short read-only script, run it with `payload run`
313
+ (`PAYLOAD_MIGRATING=true`, no `onInit`), and delete it. If the database has a row other than the dev
314
+ row whose name is not a file in the migrations folder, Migrate stops. The usual cause is another
315
+ branch that was applied before it merged. Status lists those names and does not stop. A missing
316
+ `payload_migrations` table means nothing has been applied.
317
+
318
+ Prisma's status output and Supabase's `migration list` already describe that drift. Sandpiper uses
319
+ that text, without a side script. Drizzle has no database status, so this check does not apply to
320
+ it.
321
+
322
+ If the read fails, Migrate says why and continues only if you type `unchecked`. That also skips the
323
+ push-managed check.
324
+
325
+ ## The merge gate
326
+
327
+ Production, in any capitalization, and any target with `requiresMerge: true` are gated. Before that
328
+ target's secret is resolved, Sandpiper runs `gh pr view` for the current branch. The pull request
329
+ must be `MERGED`, and every entry in `statusCheckRollup` must have finished as success, neutral, or
330
+ skipped. A pull request with no checks does not pass.
331
+
332
+ You can type `skip merge check` to continue. The production yes/no, when that question applies, and
333
+ the typed target name still follow. After the pending list is known, migrations that are in neither
334
+ the merged pull request's head commit nor the base branch (committed after the merge) need
335
+ `skip merge check` again.
336
+
337
+ `requiresMerge: false` turns the gate off for a target named Production. Staging is not gated unless
338
+ that target sets `requiresMerge`. The gate does not check out or pull. Applying from `main` cannot
339
+ find the feature pull request, so it needs the override. If `gh` is missing, signed out, or there is
340
+ no pull request, Sandpiper says why and offers the same phrase.
341
+
342
+ ## Push-managed databases
343
+
344
+ When Payload runs in dev mode with `push` enabled, it writes a `payload_migrations` row named `dev`
345
+ with batch `-1`. That database was built by push, not by migrations. The first migration collides
346
+ with tables that already exist, and `payload migrate` would ask about data loss.
347
+
348
+ Migrate stops when it finds that row, before `payload migrate`. Status warns. The read is the same
349
+ one as the drift check. A failed read is the `unchecked` phrase.
350
+
351
+ The same pass warns when the only adapter is `@payloadcms/db-d1-sqlite`. D1 is reached through a
352
+ Worker binding, not a URI. The prompt asks whether to continue.
353
+
354
+ ## Reviewing SQL
355
+
356
+ After create, Sandpiper prints the new file's `up` function, or the whole file if `up` cannot be
357
+ found. It highlights lines that can lose data or break the running app:
358
+
359
+ - `DROP` of a table, column, type, index, constraint, or schema
360
+ - `ALTER COLUMN ... TYPE`
361
+ - `SET NOT NULL`
362
+ - `RENAME`
363
+ - `TRUNCATE`
364
+ - `DELETE FROM`
365
+
366
+ Any hit requires typing `reviewed`. A SQLite table rebuild (`__new_`) adds a note to check that the
367
+ `INSERT` copies every column. No hits asks "Does this SQL look right?". Cancelling leaves the files
368
+ uncommitted.
369
+
370
+ The check is a line pattern. It does not parse SQL, and it does not read `down`. It runs on the
371
+ Payload file just created. It does not review Prisma or Supabase SQL.
372
+
373
+ ## The migration commit
374
+
375
+ After review, Sandpiper refuses to commit if anything else is already staged. It lists the new
376
+ migration files and `index.ts`, asks to commit them (default yes), and prompts for a message
377
+ (default `chore: migrations`, editable, or `commitMessage` from config). The commit contains only
378
+ those paths. Generated type and import-map files stay out of it.
379
+
380
+ It then offers to push (default yes), using the existing upstream or `push -u` to the configured
381
+ remote. Skipping the commit leaves the files. The closing note is to wait for CI, then migrate to
382
+ staging before merging. An initial migration adds that it only applies cleanly to an empty database.
383
+
384
+ ## Migrate log
385
+
386
+ Every migrate that reaches the tool's migrate command posts one comment with `gh pr comment`,
387
+ whether migrate succeeded or failed:
388
+
389
+ ```text
390
+ sandpiper migrate | 2026-10-27T21:45:09Z | Staging | postgresql://ep-calm-sea.neon.tech/app | applied | 20261020_101010_posts | Jamie Summers
391
+ ```
392
+
393
+ The fields are fixed: UTC time, target, masked host, `applied` or `failed`, the migrations that were
394
+ pending, and `git user.name` or else `user.email`. The line is wrapped in backticks so
395
+ `grep 'sandpiper migrate |'` over the pull request's comments shows each database's history for that
396
+ change.
397
+
398
+ Recording never fails the run, because the database has already changed. With no pull request, or
399
+ when `gh` cannot comment, the line is printed to paste. Migrates that stop earlier record nothing.
400
+ After a successful migrate, Sandpiper prints status again.
401
+
402
+ ## Typed phrases
403
+
404
+ | Phrase | Where | Means |
405
+ | ------------------ | ----------------------- | ---------------------------------------------------------------- |
406
+ | `reviewed` | generate, review | The migration can lose data, and you read those statements. |
407
+ | `skip merge check` | migrate, merge gate | Migrate to a gated target without a merged, green pull request. |
408
+ | `apply all` | migrate, other branches | Also run pending migrations that are not from this branch. |
409
+ | `unchecked` | migrate, drift | Go on although the migrations table or status could not be read. |
410
+ | the target's name | migrate, last step | Run the migrate command against that target. |
411
+
412
+ ## Configuration
413
+
414
+ Everything under `sandpiper` in the target project's `package.json` is optional. Without it,
415
+ Sandpiper offers Staging and Production, uses `src/migrations` for Payload, and uses the built-in
416
+ base-branch rule.
417
+
418
+ ```json
419
+ {
420
+ "sandpiper": {
421
+ "migrationsDir": "src/migrations",
422
+ "schemaFile": ".env.schema",
423
+ "dbEnvVar": "DATABASE_URL",
424
+ "targets": {
425
+ "Staging": "DATABASE_URL_STAGING_UNPOOLED",
426
+ "Production": "DATABASE_URL_PRODUCTION_UNPOOLED",
427
+ "Live EU": { "key": "DATABASE_URL_LIVE_EU_UNPOOLED", "requiresMerge": true }
428
+ },
429
+ "extraEnvVars": [],
430
+ "baseBranch": "staging",
431
+ "remote": "origin",
432
+ "commitMessage": "chore: migrations"
433
+ }
434
+ }
435
+ ```
436
+
437
+ | Key | Default | Role |
438
+ | --------------- | ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
439
+ | `migrationsDir` | `src/migrations` | Payload folder. Must match `db.migrationDir` when that is set. Prisma and Supabase keep their own folders unless this is set. |
440
+ | `schemaFile` | `.env.schema` | Where targets are declared. |
441
+ | `dbEnvVar` | `DATABASE_URL` | Variable the app reads. Set `DATABASE_URI` for an older project. Auto-discovery then looks for `DATABASE_URI_<TARGET>_UNPOOLED`. |
442
+ | `targets` | unset | Name to variable, or `{ "key", "requiresMerge" }`. Replaces the naming convention. The name is what you type. `requiresMerge` defaults to true only for Production. |
443
+ | `extraEnvVars` | `[]` | Other per-target variables, looked up as `<NAME>_<TARGET>`. |
444
+ | `baseBranch` | `staging`, else the remote default | Branch features merge into. |
445
+ | `remote` | `origin` | Remote to fetch and push. |
446
+ | `commitMessage` | `chore: migrations` | Default message for the migration commit. You can edit it at the prompt. |
447
+
448
+ ## Tools
449
+
450
+ Detection looks at the current directory only. It does not walk parents. A dependency counts in
451
+ `dependencies`, `devDependencies`, or `optionalDependencies`. `peerDependencies` do not count.
452
+
453
+ | Tool | Detected from |
454
+ | ------------ | -------------------------------------------- |
455
+ | Prisma | `prisma` or `@prisma/client` |
456
+ | Drizzle | `drizzle-kit` or `drizzle-orm` |
457
+ | Payload | `payload` |
458
+ | Supabase CLI | `supabase`, or a `supabase/config.toml` file |
459
+
460
+ `@payloadcms/db-postgres` does not count. The `payload` package is the CLI.
461
+
462
+ - A migrate script is enough on its own. If none of the four is detected, Sandpiper runs the script
463
+ and does not invent a tool.
464
+ - No script and no tool exits 1:
465
+ `No db:migrate or migrate script found, and no supported tool (Prisma, Drizzle, Payload, or Supabase) was detected.`
466
+ - No script and exactly one tool warns
467
+ `No db:migrate or migrate script found. Using the default <tool> command.` and uses the table
468
+ under [How the URL is passed](#how-the-url-is-passed).
469
+ - No script and more than one tool asks which tool to run. Payload on Supabase, and Prisma on
470
+ Supabase, are the common cases. Sandpiper does not pick silently.
471
+
472
+ Prisma and Drizzle need their CLI packages, not just the client:
473
+
474
+ - Prisma detected only from `@prisma/client`, with no `prisma` package, exits 1:
475
+ `Prisma was detected from @prisma/client, but the prisma CLI is not installed.`
476
+ - Drizzle detected only from `drizzle-orm`, with no `drizzle-kit`, exits 1:
477
+ `Drizzle was detected from drizzle-orm, but drizzle-kit is not installed.`
478
+
479
+ The installed Prisma major is read from `node_modules/prisma/package.json`. Major 8 and newer,
480
+ including an `8.x.x-rc`, uses `migration status` and `db migrate`. Older releases use
481
+ `migrate status` and `migrate deploy`. If `prisma` is declared and that file is missing, Sandpiper
482
+ exits 1 and tells you to install with the detected package manager. It does not guess, and it does
483
+ not `npx` a fresh Prisma.
484
+
485
+ Supabase detected only from `supabase/config.toml`, with no `supabase` package, runs `supabase` from
486
+ `PATH`. If it is missing, the error is `Install the Supabase CLI, or add the supabase package.` A
487
+ declared package that is not installed uses the same install error as the other tools. Sandpiper
488
+ does not `npx supabase`.
489
+
490
+ The working directory must contain `package.json`. Missing file:
491
+ `Current directory is not a Node.js project (no package.json).` Invalid JSON exits 1 with the parse
492
+ error. Sandpiper does not dump the file. A missing `scripts` object means there are no scripts. A
493
+ script counts when its value is a non-empty string.
494
+
495
+ ## Known gaps
496
+
497
+ - The command Sandpiper is about to run is never printed. The child's output is the record. Package
498
+ managers that would echo the script are started with their silent flag. The outer `pnpm sandpiper`
499
+ line is the parent process, and Sandpiper does not hide it.
500
+ - Sandpiper does not roll back a migration.
501
+ - `migrationsDir` is not read from the Payload config, and a Drizzle folder is not read from
502
+ `drizzle.config`. Loading either would mean loading TypeScript. If Payload sets `db.migrationDir`,
503
+ mirror it in `sandpiper.migrationsDir`. Generate warns when the folder does not exist yet.
504
+ - Target values are resolved by Sandpiper, not by varlock. Only an `op://` reference, a plain URI,
505
+ or a value already in the process environment is used. Another varlock function is listed as
506
+ skipped. The schema is read line by line: a `KEY=` line and the comment block directly above it.
507
+ Multi-line values are not understood.
508
+ - The risky-SQL check is a line pattern on the new Payload `up` function. It catches the statements
509
+ listed above, not every possible one, and it does not read `down`. Prisma and Supabase SQL files
510
+ are not reviewed.
511
+ - Generate checks that the branch is not behind the base branch. It cannot tell whether another open
512
+ pull request is about to add a migration. Two feature branches that both generate still need the
513
+ usual rebase. Migrate catches the other half: a target that already ran the other branch's
514
+ migration is refused until that work is merged in.
515
+ - "This branch" is computed from the merge base. Squash merges keep working after the merge. A
516
+ branch merged with a merge commit adds nothing relative to the base afterwards, so its migrations
517
+ show as not from this branch and `apply all` is required.
518
+ - The merge gate looks up the pull request for the current branch name only. Migrating to production
519
+ from `main` needs `skip merge check`.
520
+ - If status output cannot be parsed, and Payload's table cannot be read either, Migrate refuses.
521
+ Drizzle never has that table. Its status command checks files for conflicts and does not query a
522
+ database.
523
+ - A project that runs Payload with `push: true` against a remote database is outside what this can
524
+ protect before the `dev` row exists. Migrate refuses that database once the row is there. Nothing
525
+ here stops a dev server from pushing to it. The shell-export warning is the notice for that risk.
526
+
527
+ ## Development
528
+
529
+ ```sh
530
+ pnpm install
531
+ pnpm dev # run the CLI from source
532
+ pnpm build # bundle to bin/sandpiper.mjs
533
+ pnpm start # run the built CLI
534
+ ```
535
+
536
+ Pass CLI arguments after the script name, for example `pnpm dev --help`. pnpm is pinned in
537
+ `package.json`.
538
+
539
+ ### Scripts
540
+
541
+ | Script | What it does |
542
+ | ---------------- | ------------------------------------------------------------ |
543
+ | `pnpm dev` | Runs `src/index.ts` directly with Node.js type stripping |
544
+ | `pnpm build` | Bundles the CLI into `bin/sandpiper.mjs` with tsdown |
545
+ | `pnpm start` | Runs the built CLI |
546
+ | `pnpm lint` | Lints with Oxlint (`pnpm lint:fix` applies fixes) |
547
+ | `pnpm format` | Formats with Oxfmt (`pnpm format:check` only reports) |
548
+ | `pnpm typecheck` | Type-checks with `tsc --noEmit` |
549
+ | `pnpm check` | Verification gate: lint, format check, typecheck, then build |
550
+ | `pnpm release` | Bumps the version, updates `CHANGELOG.md`, commits, and tags |
551
+
552
+ `pnpm check` is the verification gate. Run it before pushing.
553
+
554
+ ### Project layout
555
+
556
+ ```
557
+ src/index.ts Executable entry point: parses argv and maps failures to exit codes
558
+ src/cli.ts Commander program. The default action runs the migration flow
559
+ src/run.ts Prompt order, fail-closed checks, and the child environment
560
+ src/project.ts package.json, scripts, package manager, and tool detection
561
+ src/env.ts Staging and production URL resolution
562
+ src/command.ts Script and default-command argv, including the varlock wrapper
563
+ src/execute.ts Spawns the command and maps its status
564
+ bin/ Build output
565
+ tsdown.config.ts Bundler configuration
566
+ .oxlintrc.json Linter configuration
567
+ .oxfmtrc.json Formatter configuration
568
+ assets/ README logo
569
+ ```
570
+
571
+ ### Build output
572
+
573
+ `pnpm build` produces one self-contained, executable ES module, `bin/sandpiper.mjs`. Every
574
+ dependency is inlined into it, which is why `chalk`, `commander`, `inquirer`, and `ora` are
575
+ devDependencies. The published package needs nothing installed besides Node.js.
576
+
577
+ ## Releasing
578
+
579
+ Release from a clean checkout of `main`. The published package is `@nvok/sandpiper`. The command is
580
+ still `sandpiper`. CI publishes the tag and does not bump the version. Do not push a baseline tag
581
+ just to mark `1.0.0`.
582
+
583
+ ```sh
584
+ pnpm release --dry-run # preview the bump, changelog, and tag
585
+ pnpm release # bump package.json, update CHANGELOG.md, commit, and tag
586
+ git push --follow-tags
587
+ ```
588
+
589
+ Pass `--dry-run` directly. `pnpm release -- --dry-run` does not turn dry-run on.
590
+ `commit-and-tag-version` is the `release` script. It bumps `package.json`, updates `CHANGELOG.md`,
591
+ commits, and tags `v*`. Oxfmt ignores `CHANGELOG.md`, because the generated compare links are wider
592
+ than the format check and would fail `pnpm check` on the tag.
593
+
594
+ `.github/workflows/publish.yml` runs only on `v*` tags. It installs, runs `pnpm check` (which builds
595
+ `bin/sandpiper.mjs`), packs with `npm pack`, installs that tarball in a temporary directory, and
596
+ runs the installed `sandpiper --help` and `sandpiper --version`. The reported version must match the
597
+ tag with the leading `v` removed. If that version is already on npm, the job fails and does not
598
+ publish. Otherwise it publishes that same tarball.
599
+
600
+ `publishConfig.access` is `public`. Unscoped `sandpiper` is a different package already on npm.
601
+ There is no `NPM_TOKEN`. Publish uses npm trusted publishing: `permissions.id-token: write`,
602
+ workflow filename `publish.yml`, GitHub repository `nvok-io/sandpiper`, environment `Release`.