Migrating · Migration steps
Moving with a coding agent
Give a coding agent your existing schema and migration brief, and verify every step with the CLI.
- API
- —
- In the schema
- —
What you’ll build#
This guide sets up a coding agent to draft and verify your migration on an isolated branch. Your agent reads exported schema and rule files from the legacy system, writes dbschema/default.gel and access policies, converts data calls into .edgeql query files and builds the batch export script while you review the diffs and run final cutover.
Compiler-checked agent loop
Your coding agent translates schemas, policies and queries against live compiler feedback.
Branch-scoped key isolation
A branch-bound service key confines every test import to rehearsal while protecting main.
Migrations suit coding agents well because deterministic CLI checks gate every step: schemas either compile and migrate cleanly or fail with a line error, queries type-check against the branch schema and imported row counts match your source totals.
Before you start#
Coding agents covers project MCP setup and agent tooling. For a migration, create a dedicated rehearsal branch and a branch-bound service key so agent experiments can never touch main.
# Configure agent instructions and connect your project's MCP servernapalm init notesnapalm endpoint notes --share-schema # Create an isolated rehearsal branch and a branch-scoped service keynapalm branches create notes rehearsalnapalm endpoint notes --branch rehearsal --enable --share-schemanapalm keys create notes --label move-agent --branch rehearsal export NAPALM_KEY=ndb_…export NAPALM_PROJECT_URL=https://<project-address>/branches/rehearsalOn rehearsal, the agent executes EdgeQL statements as text over HTTPS. Named deployed queries live on main, which you push yourself after reviewing the branch.
Export files from your old stack#
Place exported schema, policy and function files into an old/ directory containing zero customer rows or credentials.
| Source stack | Files to export into old/ | What the agent learns |
|---|---|---|
| Postgres | pg_dump --schema-only --no-owner, your ORM schema and data-access modules | What the agent learnsTable definitions, foreign keys and the application-layer authorization checks to turn into access rules. |
| Supabase | supabase db dump --schema public and supabase/functions | What the agent learnsTables, constraints and the SQL RLS policies that translate into schema rules. |
| Firebase | firestore.rules, storage.rules, firestore.indexes.json, functions/ and redacted JSON document samples | What the agent learnsSecurity rules alongside real document shapes, since Firestore has no static schema file. |
| Convex | The convex/ directory | What the agent learnsTable validators in schema.ts and the per-function authorization checks to lift into rules. |
| Gel | dbschema/ and application files containing EdgeQL | What the agent learnsExisting object types, inline queries to extract into .edgeql files and open types needing access rules. |
For Postgres, Supabase, Prisma or Convex, generate an initial draft with napalm schema draft into old/draft.gel so the agent starts from compiled EdgeQL types and a checklist of # draft: comments. How a schema is translated explains every generated construct.
# Postgres or Supabase: generate an initial schema draft to refinenapalm schema draft --from old/old-schema.sql -o old/draft.gel # Prisma: generate a draft from your Prisma schemanapalm schema draft --from prisma/schema.prisma -o old/draft.gel # Convex: generate a draft from convex/schema.tsnapalm schema draft --from convex/schema.ts -o old/draft.gelGive the agent the migration brief#
Paste the prompt below into your coding agent, substituting your project slug and branch name. Every Napalm documentation page also serves a plain Markdown copy at its URL plus .md, which the brief links directly.
Move this app's database to Napalm. The old system is described by the files in old/.Work on the branch "rehearsal" of the project "notes". Never push to main. 1. Write dbschema/default.gel from the old tables or collections. - For Postgres, Supabase, Prisma or Convex, start from old/draft.gel, generated by napalm schema draft, and resolve every comment that starts with draft:. - Convert foreign keys and stored IDs into links, and join tables or ID arrays into multi links. - Every type keeps the old system's id in old_id (str, exclusive). - Model users with a Person type holding an optional identity link, an optional email and old_sign_ins, following https://napalmdata.com/developers/migrating/moving-users.md.2. Add access rules to every type. Above each policy, cite the source file in old/ that the rule replaces (an RLS policy, security rule or handler check). A type with no rule is closed. Add a comment when leaving a type closed intentionally.3. Put each application read and write in dbschema/queries, one statement per file, named for the operation. Queries never accept a caller ID parameter and read global caller instead.4. Put batch import statements in dbschema/move: one file per type accepting <json>$rows and upserting on old_id with "unless conflict on .old_id else (update …)". Import parents first.5. Write the export script for this source database in scripts/, following https://napalmdata.com/developers/migrating/switching-over.md.6. Compile every EdgeQL statement with the check_query tool before saving it. Verify each step with the commands in "Verify each step with a command" and show the output.When the old system relies on behavior a single statement cannot express (triggers, storedprocedures, offline write queues or password hashes), stop and list each item instead ofapproximating. Never write a service key, connection string or password into any file.- Traceable rule comments
- Requiring a comment above each access policy naming its legacy source file lets you audit every rule side by side and spot invented permissions immediately.
- Explicit gap reporting
- Procedural triggers, custom extensions and password columns have no silent 1-to-1 translation. Instructing the agent to stop and list unsupported constructs prevents silent behavioral regressions.
- Generated AGENTS.md rules
napalm initwrites Napalm’s repository conventions intoAGENTS.md: EdgeQL only, declarative schema access rules, parameterized inputs andcheck_queryverification before saving any.edgeqlfile.
Verify each step with a command#
Every step of the brief pairs with a CLI or MCP check that returns a deterministic pass or fail result.
| Step | Verification command | Passing result |
|---|---|---|
| Schema | napalm schema push notes --branch rehearsal --plan | Passing resultPrints a clean migration plan covering every expected object type. |
| Individual statement | MCP check_query tool | Passing resultConfirms parameter types, return shape and read or write mode with zero compiler errors. |
| Application queries | napalm queries push notes --branch rehearsal | Passing resultCompiles every file in dbschema/queries against the rehearsal schema. |
| Import statements | napalm queries push notes --queries dbschema/ | Passing resultCompiles every batch upsert file in dbschema/move without deploying. |
| Service key reach | napalm keys reach notes --new --branch rehearsal | Passing resultConfirms the migration key reaches expected types while restricted types report zero rows. |
| Idempotent row import | Run the export script twice | Passing resultBoth passes complete cleanly and print counts matching the source database. |
| Per-user reach | napalm people reach notes <person> --branch rehearsal | Passing resultConfirms an imported user reads only their own and shared rows. |
Decisions to keep in human hands#
Keep credentials, access policy sign-off and production cutover under your direct control.
- Credentials
- Export
OLD_DATABASE_URLandNAPALM_KEYin your own shell environment rather than placing secrets in files the agent can read. - Production pushes to main
- Although the
ndb_…service key is restricted torehearsal, your localnapalmCLI is authenticated with your org token. Keepmainpushes human-approved and run cutover commands yourself. - Access rule review
- Read every
access policyindbschema/default.gelalongside the legacy policy or handler check it replaces before pushing tomain, even when automated checks pass. - Production cutover
- Deciding when to pause legacy writes and switch client traffic stays with you. Switching over walks through the cutover checklist.
Check the agent’s work#
Ask the agent to summarize the legacy schema
Confirm the agent reads from
old/and accounts for every table, collection and join table.Send a request to main using the branch service key
mainrejects the credential because the service key is scoped exclusively torehearsal.Review the agent’s list of untranslated items
Almost any production app produces at least one item, such as password hashes, procedural triggers or offline write queues.
Run the verification commands yourself
Re-run each command in the verification table from your own terminal and confirm the output matches.