Skip to the page

Guides · Shipping & operations

Live schema changes

Add a required property, rename a property, give a changed query a new name and index a big type while open tabs keep the old app working.

API
—
In the schema
—

What you’ll build#

You’ll change the first app’s schema while people use the app. The schema and the app deploy seconds apart, and a tab opened before the deploy keeps running the old app. Each change on this page ships in steps, and the old app keeps working at every step.

This guide walks through four rollouts: adding a required property, reshaping a deployed query, renaming a property and indexing a large type, along with handling interactive migration decisions. Each step ships in its own pull request so the review comment previews the migration plan before anything reaches main.

Before you start#

You need the first app’s project, deployed as Environments & going live describes. A merge or CI step pushes dbschema/default.gel and every query in dbschema/queries to main in one migration.

Napalm refuses any push that breaks a deployed query, so no single push can drop a property that live clients still read. The steps below put that safety check to work.

napalm schema push sends your schema file verbatim without injecting hidden lines, so every schema must declare using extension napalm; explicitly or the push is refused.

Deployed-query compatibility gate

napalm schema push compiles every .edgeql file in dbschema/queries/ against the target SDL before touching PostgreSQL, refusing any push that breaks live callers.

Set up a key for backfills#

A backfill is an update statement over rows missing a value. napalm query runs the statement with a service key under your schema’s access policies. The CLI reads the key from --key, then NAPALM_KEY, then the key saved by napalm keys create --save.

A service key has no global caller. Because the first app’s only rule admits each note’s owner alone, a service key cannot read or update any note, since an update touches only rows permitted by the caller’s policies (see Update and delete). Give the backfill a dedicated rule and a short-lived key.

Rollback backfill previews with --dry-run

napalm query --dry-run executes the real update under your backfill key's access policy, prints before-and-after row diffs and rolls the transaction back.

dbschema/default.gel
module default {  # Set for one key with --global. No request can set a global.  global role: str;   type Note {    # … as before, and:    access policy backfill_can_update      allow select, update      using (global role ?= 'backfill');  }}
Terminal
# A key with the role the rule names, for one day. napalm query reads the key from NAPALM_KEY.export NAPALM_KEY=$(napalm keys create notes --label backfill --global default::role=backfill --days 1)
The key’s role
--global sets role for the backfill key’s calls alone. Service keys covers per-key globals.
Dry run first
--dry-run runs the statement in a transaction that rolls back and prints each changed note before and after.
Big types
A request gets 60 seconds before the project answers 503 project_unavailable. Update a big type in batches with limit, as the next section shows.
Writes allowed
A write answers query_failed with a --read-only key or a read-only API. Passthrough none holds every caller to deployed queries, so the backfill needs keys or all.
Cleanup
The key expires after one day, and napalm keys revoke ends it sooner. Remove the rule in the last push of the change.

Add a required property#

Each note gets a summary with the first 120 characters of the body. The property goes in as required with an empty default, a backfill writes the real summaries and a last push drops the default.

  1. Add the property with an empty default

    The default fills the notes that exist at the push. In the same pull request, have addNote write the summary.

    dbschema/default.gel
    type Note {  # … as before, and:  required summary: str {    # Fills the notes that exist now. The backfill replaces the empty values.    default := '';  };}
    dbschema/queries/addNote.edgeql
    # The same variables as before, so a tab with the old app still calls addNote.insert Note {  owner := global caller,  title := <str>$title,  body := <str>$body,  summary := (<str>$body)[:120],}

    addNote takes the same variables as before. A tab with the old app still calls addNote, and the tab’s new notes get a summary too. Taking the summary from the person in a $summary variable would change the insert’s shape, and a new shape needs a new name.

  2. Backfill the old notes

    Terminal
    # Each note the statement would change, before and after. The transaction rolls back.napalm query notes "update Note filter .summary = '' and .body != '' set { summary := .body[:120] }" --dry-run # The backfillnapalm query notes "update Note filter .summary = '' and .body != '' set { summary := .body[:120] }" # A big type in batches of 1,000 notes. Run the statement until the answer is [].napalm query notes "update (select Note filter .summary = '' and .body != '' limit 1000) set { summary := .body[:120] }"

    The statements run with the backfill key in NAPALM_KEY. Each run updates only notes with an empty summary, so a second run is safe. A note with an empty body keeps an empty summary.

  3. Drop the default

    dbschema/default.gel
    type Note {  # … as before, and the default gone:  required summary: str;}

    Without the default, every insert has to set summary. Napalm refuses the push while a deployed query inserts notes with no summary.

Give a changed query a new name#

A deployed query with new variables or a new result needs a new name, because the open app keeps calling the old name with the old shape. In this example the notes list shows each note’s summary in place of the body.

dbschema/queries/myNoteSummaries.edgeql
# The notes list, with each note's summary in place of the bodyselect Note { id, title, summary }order by .title
  1. Deploy the new query beside the old query

    Add the file and merge. myNotes keeps serving the open app, and napalm generate notes --deployed --lang ts types the new call for the app.

  2. Ship the app with the new call

    New tabs call myNoteSummaries. Tabs opened before the release still call myNotes.

  3. Wait for the old calls to stop

    napalm insights notes --days 7 counts each caller’s calls to each deployed query over the last 7 days. Go on once myNotes has no calls.

  4. Remove the old query

    Delete dbschema/queries/myNotes.edgeql and merge. A deploy replaces the deployed set, so myNotes stops serving. napalm queries push notes deploys the folder without a schema push and asks before the push stops serving a query.

Rename a property#

These steps rename summary to preview, and the same steps rename any property. Both properties stay in the schema until no query reads summary.

  1. Add the new property

    dbschema/default.gel
    type Note {  # … as before, and:  required summary: str;  required preview: str {    default := '';  };}

    The empty default fills the notes that exist at the push. Every query that writes summary writes preview too. In addNote, add preview := (<str>$body)[:120] beside the summary, so each new note gets both values.

  2. Backfill the new property

    Terminal
    napalm query notes "update Note filter .preview != .summary set { preview := .summary }"

    The statement copies each summary that differs from the note’s preview, so a second run changes nothing.

  3. Move the queries to the new property

    Point every query at preview. Each deployed query keeps the variables and result keys of the open app so myNoteSummaries continues returning summary.

    dbschema/queries/myNoteSummaries.edgeql
    # Read from preview. The open app still gets summary.select Note { id, title, summary := .preview }order by .title

    Writes keep setting both properties until the last step. A new result key is a new shape and needs a new name.

  4. Remove the old property

    dbschema/default.gel
    type Note {  # … as before, without summary and the backfill rule, and the default gone:  required preview: str;}

    The schema drops summary, the backfill rule and the default on preview. In the same pull request, addNote stops writing summary. Napalm refuses the push while a deployed query still reads the summary property. Without --yes, the CLI warns that dropped data survives only in backups.

Answer what a migration asks#

Some changes need an answer that a schema can’t hold. The engine plans a migration one step at a time and stops at a step that needs an answer. Three kinds of step need an EdgeQL expression for the rows that exist. A rename the engine guesses at needs a choice between a rename and a replacement.

Target schema & decisions

dbschema/decisions.yaml pairs dbschema/default.gel with explicit fill, convert, single and rename answers so migrations stay deterministic.

Row migration expressions

Each fill, convert or single decision runs an EdgeQL expression over existing rows inside the transactional migration.

Per-shard local execution

On multi-shard projects, the cluster routes each migration expression cleanly to every shard to transform local rows in parallel.

KindThe engine asks whenAn answer
fillA property or link becomes required, or a new required field has no default. The answer gives each existing object a value.An answer.body[:120] ?? ''
convertA property changes to a type with no implicit cast, such as str to int64. The answer turns each existing value into the new type.An answer<int64>.score
singleA multi property or link becomes single. The answer gives each existing object one value.An answerarray_join(array_agg(.tags), ', ')
renameThe engine guesses that a field or a type has a new name. The answer is rename or replace.An answerchoice: rename

The answers live in dbschema/decisions.yaml beside the schema files. napalm schema push sends every entry with the schema. --decisions names another file. An entry names the field as Type.field, as module::Type.field for a type in another module, or as Type.link@property for a property of a link. A guessed rename of a type names the type alone, with the new type in to.

dbschema/decisions.yaml
# Answers to the questions a migration asks. Delete an entry once main has the change.decisions:  # Notes from before the summary start with the body's first 120 characters.  - field: Note.summary    kind: fill    expression: ".body[:120] ?? ''"  # The score was text. A fill in the same step as a conversion gives the old type.  - field: Metric.score    kind: fill    expression: "'0'"  - field: Metric.score    kind: convert    expression: "<int64>.score"  # A post keeps every tag in one string.  - field: Post.tags    kind: single    expression: "array_join(array_agg(.tags), ', ')"  # The title is the headline now. replace would drop every note's title.  - field: Note.title    to: headline    kind: rename    choice: rename

Answers at a terminal#

In an interactive terminal, napalm schema push prompts for each required decision: an EdgeQL expression for fill, convert or single, and rename or replace for a detected rename. Because later migration steps can depend on earlier choices, the CLI replans after each round and stops prompting if you submit an empty answer. Before applying the migration, the CLI offers to save your answers to dbschema/decisions.yaml while preserving existing comments and entries, though the push still applies if you skip saving.

Your terminal
napalm schema push notes
! The migration needs an answer to each question below. An empty answer stops.
Note.summary (fill, std::str, 1,204 existing rows)
Please specify an expression to populate existing objects in order to make property 'summary' of object type 'default::Note' required
Expression: .body[:120] ?? ''
Note.title → headline (rename, 67% confidence)
Did you rename property 'title' of object type 'default::Note' to 'headline'?
Rename keeps the data. Replace drops Note.title and the data in Note.title.
Rename or replace? [rename/replace] rename
Write the 2 answers to dbschema/decisions.yaml? [y/N] y
Wrote the 2 answers to dbschema/decisions.yaml.
Commit dbschema/decisions.yaml with the schema. A pull request and main then take the same answers.
ALTER TYPE default::Note {
ALTER PROPERTY summary {
SET REQUIRED USING (((.body)[:120] ?? ''));
};
};
ALTER TYPE default::Note {
ALTER PROPERTY title {
RENAME TO headline;
};
};
The migration takes 2 decisions:
Note.summary (fill, 1,204 existing rows): .body[:120] ?? ''
Please specify an expression to populate existing objects in order to make property 'summary' of object type 'default::Note' required
Note.title → headline (rename, 67% confidence): rename
Did you rename property 'title' of object type 'default::Note' to 'headline'?
Writes to Note wait while the migration rewrites the existing rows.
Apply the migration to notes? [y/N] y
Migrated notes: 2 changes

Answers in CI#

Without a terminal (or when --yes is passed), the push never prompts interactively. Instead, it prints the YAML snippet needed in dbschema/decisions.yaml and exits with status 1 without applying changes.

A pull request’s job
napalm schema push notes --yes
The migration needs a decision first:
Note.summary (fill, std::str, 1,204 existing rows)
Please specify an expression to populate existing objects in order to make property 'summary' of object type 'default::Note' required
Add the answers to dbschema/decisions.yaml, then push again:
decisions:
- field: Note.summary
kind: fill
expression: "..." # std::str
Guessed renames
A rename the engine guesses at stops the push until the file answers the guess. rename keeps the data. replace drops the old field and its data, then creates the new field. The push counts that drop like any other drop. A rename the engine is sure of needs no answer.
Rows rewritten
The plan shows the existing rows each fill, convert or single answer rewrites. Writes to the type wait while the migration rewrites the rows. On a big type, add the property with a default and backfill in batches, as the first steps on this page do.
Expressions
An answer is one EdgeQL expression of up to 8 KiB. The expression runs on each existing object and reads its own fields. A , or ; goes only inside brackets. A string interpolation left open and a substitution token such as \(fill_expr) are refused. The engine refuses an expression of the wrong type and an expression that leaves an object empty, stopping the push with the engine’s error.
A fill with a convert
When a single step both makes a field required and converts its type, the engine runs fill before convert, so the fill expression must return the old type (such as '0' for a str field converting to int64).
Shards
Each shard of a branch on more than one shard runs the expression on the shard’s own objects. An expression never reads objects on another shard. The row count is the base shard’s.
Unused entries
After a push to main applies, the push names each entry the migration didn’t use. Delete those entries. A push to another branch names none. main needs each entry until main has the change.
Other questions
A question no decision answers stops the push with the engine’s own words. Push the change in smaller steps.

Add an index to a big type#

PostgreSQL builds an index during the migration that adds it, and writes to that type wait until the build finishes. Because the build takes longer on a large type, schedule it during a quiet window.

napalm insights recommends indexes for your slowest statements, and napalm schema index applies an index directly to main.

Terminal
# The slowest statements, then indexes tested on a replica, each with the command that adds the indexnapalm insights notes # The migration for one index. Nothing is applied.napalm schema index notes Note title --plan # Asks first, then adds the index to mainnapalm schema index notes Note title

The command asks for confirmation and warns that writes to Note wait while PostgreSQL builds the index. Add index on (.title); to Note in dbschema/default.gel as well, or the next schema push will drop the index. Indexes added via a push build the same way. Scripts can add an index through POST /projects/{id}/schema/indexes. See Recommended indexes.

Review each step on a pull request#

napalm schema push notes --review prints the migration plan as Markdown for a pull request comment without applying any changes. The comment lists every DDL statement, the count of destructive drops, each type’s inferred root, affected notify webhooks and their hosts, engine warnings, the decisions taken with the rows each answer rewrites, the decisions still needed with YAML to add to dbschema/decisions.yaml and each deployed query’s compile result.

Pull-request branches and --review plans

Rehearse each expand-and-contract step on an isolated branch and emit DDL, drop counts, root paths and query compile verdicts with napalm schema push --review.

Terminal
# The plan as Markdown for the pull request's comment. Nothing is applied.napalm schema push notes --review > review.md # The same plan in the terminal, with every type's root. Nothing is applied.napalm schema push notes --plan
A failing check
The command exits with an error when the plan needs a decision or breaks a query, so the pull request’s check fails.
Drops
A drop passes --review, and the comment counts the statements that drop something. With the GitHub App, a step that drops something waits for approval from someone with write access to the repository.
In the terminal
--plan prints the same plan in the terminal and stops before applying. The terminal plan lists every type’s root.
From a script
POST /projects/{id}/schema without isApplied answers the same plan and review. Schema migrations documents the route.

Branches has the workflow that posts the review on each pull request.

Check the schema changes#

Run four quick checks to confirm that open tabs, backfilled rows and deployed queries survived every rollout step cleanly.

  1. Add a note from a tab opened earlier

    Open the app in a tab before the step that adds summary merges. After the merge, add a note in that tab without reloading. The note saves.

  2. Count the notes the backfill missed

    With the backfill key in NAPALM_KEY, run napalm query notes "select count(Note filter .summary = '' and .body != '')" before the push that drops the default. The count is 0.

  3. Confirm the old query is gone

    napalm insights notes --days 7 shows no calls to myNotes before the file goes. After the merge, napalm queries ls notes lists myNoteSummaries and no myNotes.

  4. Review main after the last step

    napalm schema push notes --review reports that the schema is main’s already and that every query compiles. The command exits with status 0.

Type a word or two to search every docs page.