agentleFS
nrjdalalnrjdalal/zerostarter/.agents/skills/db-migration/SKILL.md

db-migration

Skill65 starsChanged 15 days agoMITView on GitHub

Create and apply a Drizzle schema change. Use when adding or altering tables, columns, or indexes in @packages/db, or when asked for a migration.

  • Reads credentials

24 other projects use a version of this, 1 of them edited.

Adopted by

Most recently taken up by

Used with

Projects with it also use write-tests, git-workflow and 3 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • SentrySkill

    write-tests

    Write tests following project conventions. Use when adding new tests or modifying existing ones.

    Also in 3 projects that use this

  • Microsoft AzureSkill

    git-workflow

    Squad branching model: dev-first workflow with insiders preview channel

    Also in 3 projects that use this

  • Microsoft AzureSkill

    verify

    End-to-end app testing pipeline for browser-based web apps on cloud browsers via Azure Playwright Workspaces: verify the live app, author tests for working flows, heal failing tests, then re-run with video and produce one consolidated report. Three phases (app verification, test authoring, test healing) plus a scoped final sanity run; each phase emits a sub-report. Use when asked to "verify my app", "make sure my app works and has good test coverage", "do a full QA pass", "add tests where I'm missing coverage", "fix failing tests and add new ones", "smoke test before deploy", "QA my deployment", or any combined verify-and-test ask. Requires Azure CLI (`az login`) and a Playwright Workspace resource ID — bootstraps configs on first invocation. Browser-based web apps with Playwright only — not for unit/API tests, native mobile/desktop, load/perf, or full accessibility audits.

    Also in 4 projects that use this

  • AnthropicSkill

    debug

    Structured debugging session — reproduce, isolate, diagnose, and fix. Trigger with an error message or stack trace, "this works in staging but not prod", "something broke after the deploy", or when behavior diverges from expected and the cause isn't obvious.

    Also in 3 projects that use this

  • GoogleSkill

    grill-me

    A relentless interview to sharpen a plan or design.

    Also in 3 projects that use this

The file

We split this skill into the rules it gives an agent

2 rules. 0 are common, 1 is distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 21 edited versions we read keep these.

  • 1

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Read the generated packages/db/drizzle/NNNN_*.sql.

    line 30
  2. Distinctive:

    Never edit an applied migration.

    line 70

“Common” is measured: kept by at least 6 in 10 of the 21 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Database Migration

PostgreSQL with Drizzle ORM. Schema lives in packages/db/src/schema/, migrations in packages/db/drizzle/. Schema, SQL, and snapshot travel together in one PR.

0. Show the schema and wait

Never generate or apply a migration before the user has seen the schema and said go. Post the table or the altered columns, name the choices that are hard to reverse (nullability, what a null means, foreign keys and their delete behaviour, indexes, whether anything is derived or generated, retention), and stop there.

Two reasons this is a hard stop rather than a courtesy. A shape is cheap to argue about and expensive to change once rows exist, so the review has to happen before the write. And canary and production share one database, so applying reaches production the moment it runs, not when the release ships.

1. Edit the schema

  • auth.ts is generated, never edited: declare a plugin or an additionalFields column in packages/auth/src/schema.ts, run bun run auth:schema, then continue below with the regenerated file. A hand edit fails tests/packages/db/src/schema/auth.test.ts.
  • Schema files group by concern, not one per table: auth.ts holds the Better Auth tables, console.ts the ones the console owns. A new table joins the file for its concern, or starts a new one when it is a new concern, and that file needs an export from index.ts: export * from "@/schema/<name>". Miss that export and the table never reaches a migration.
  • For examples, read auth.ts (tables, relations, indexes) and waitlist.ts (a minimal non-auth table).
  • Conventions: text primary keys (.$defaultFn(() => crypto.randomUUID()) on non-auth tables), timestamp("created_at").defaultNow().notNull(), snake_case columns, onDelete: "cascade" on FKs, and an index() on every FK column. Both bend for the same kind of column: use onDelete: "set null" when the row outlives the reference and the column is only provenance, and skip the index when nothing queries by it and the table is small enough that the delete-time scan is free. The console.ts tables are both: their actor_id must not take a rule or a history entry with it when an account is deleted, and although the lists join, search and sort through it, every one of those drives off user's primary key rather than this column, so an index here would serve only the delete-time sweep. Index a FK when a query narrows on the column itself, or when the table is large enough for the sweep to matter. When a set null column is the only record of who acted, store the readable text beside it (activity.actor, allowlist.actor) so a row does not become anonymous, and can name an actor that was never an account at all.

2. Generate and review

bun run db:generate

Read the generated packages/db/drizzle/NNNN_*.sql. Done when that SQL, its meta/NNNN_snapshot.json, and a new meta/_journal.json entry all appear and the SQL matches the schema edit.

3. Point at a throwaway database first

Local work runs against a disposable container, never the POSTGRES_URL in the root .env. That URL is the shared Neon database, and canary and production both read it, so a migration applied there is applied to production, and a column renamed under running code takes that code down. This is not hypothetical: it has happened, and step 0 and this step both exist because of it.

bunx pglaunch -k                                    # disposable Postgres, -k keeps it across restarts

Put the URL it prints in the worktree's own .env as POSTGRES_URL, then migrate and seed into it. The output is colorized, so a script that captures it must strip the escapes first (| sed $'s/\033\\[[0-9;]*m//g'): an escape left on the end of the URL makes drizzle-kit migrate exit 1 with no message at all. One container per worktree, so two branches with different migrations cannot corrupt each other. Two things follow:

  • Seed your own fake rows rather than copying real ones. The shared database holds real people, and a screenshot or a response body from it must never reach a PR.
  • Applying to the shared database is a deliberate, separately-authorized act. It happens when the change merges, not while it is being built.

4. Apply

bun run db:migrate

This is local or ad-hoc only. On production and canary deploys the API build auto-applies pending migrations (packages/scripts/src/migrate-on-deploy.ts, gated on VERCEL_ENV/VERCEL_GIT_COMMIT_REF, PR previews skipped), so a migration merged to canary applies itself on the next deploy.

5. Make the running stack see it

bunx turbo run build --filter=@packages/db

The API consumes @packages/db's built dist. If dev is running and the API imports the new table, restart dev entirely: bun --hot does not pick up new files or exports reliably (see the dev skill).

6. Inspect data

bun run db:studio

Notes

  • POSTGRES_URL comes from the root .env, which is why step 3 overrides it per worktree: the value that ships in .env is the shared one.
  • Never edit an applied migration; generate a new one instead.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.