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.
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'); }}# 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
--globalsetsrolefor the backfill key’s calls alone. Service keys covers per-key globals.- Dry run first
--dry-runruns 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 withlimit, as the next section shows. - Writes allowed
- A write answers
query_failedwith a--read-onlykey or a read-only API. Passthroughnoneholds every caller to deployed queries, so the backfill needskeysorall. - Cleanup
- The key expires after one day, and
napalm keys revokeends 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.
Add the property with an empty default
The default fills the notes that exist at the push. In the same pull request, have
addNotewrite 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],}addNotetakes the same variables as before. A tab with the old app still callsaddNote, and the tab’s new notes get a summary too. Taking the summary from the person in a$summaryvariable would change the insert’s shape, and a new shape needs a new name.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.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.
# The notes list, with each note's summary in place of the bodyselect Note { id, title, summary }order by .titleDeploy the new query beside the old query
Add the file and merge.
myNoteskeeps serving the open app, andnapalm generate notes --deployed --lang tstypes the new call for the app.Ship the app with the new call
New tabs call
myNoteSummaries. Tabs opened before the release still callmyNotes.Wait for the old calls to stop
napalm insights notes --days 7counts each caller’s calls to each deployed query over the last 7 days. Go on oncemyNoteshas no calls.Remove the old query
Delete
dbschema/and merge. A deploy replaces the deployed set, soqueries/ myNotes.edgeql myNotesstops serving.napalm queries push notesdeploys 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.
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
summarywritespreviewtoo. InaddNote, addpreview := (<str>$body)[:120]beside the summary, so each new note gets both values.Backfill the new property
Terminalnapalm 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.
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 somyNoteSummariescontinues returningsummary.dbschema/queries/myNoteSummaries.edgeql # Read from preview. The open app still gets summary.select Note { id, title, summary := .preview }order by .titleWrites keep setting both properties until the last step. A new result key is a new shape and needs a new name.
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 onpreview. In the same pull request,addNotestops writingsummary. Napalm refuses the push while a deployed query still reads thesummaryproperty. 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.
| Kind | The engine asks when | An answer |
|---|---|---|
fill | A 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] ?? '' |
convert | A 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 |
single | A multi property or link becomes single. The answer gives each existing object one value. | An answerarray_join(array_agg(.tags), ', ') |
rename | The 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.
# 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: renameAnswers 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.
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.
- Guessed renames
- A rename the engine guesses at stops the push until the file answers the guess.
renamekeeps the data.replacedrops 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,convertorsingleanswer 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
fillbeforeconvert, so thefillexpression must return the old type (such as'0'for astrfield converting toint64). - 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
mainapplies, the push names each entry the migration didn’t use. Delete those entries. A push to another branch names none.mainneeds each entry untilmainhas 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.
# 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 titleThe 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 /. 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.
# 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
--planprints the same plan in the terminal and stops before applying. The terminal plan lists every type’s root.- From a script
POST /withoutprojects/ {id}/ schema isAppliedanswers 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.
Add a note from a tab opened earlier
Open the app in a tab before the step that adds
summarymerges. After the merge, add a note in that tab without reloading. The note saves.Count the notes the backfill missed
With the backfill key in
NAPALM_KEY, runnapalm query notes "select count(Note filter .summary = '' and .body != '')"before the push that drops the default. The count is 0.Confirm the old query is gone
napalm insights notes --days 7shows no calls tomyNotesbefore the file goes. After the merge,napalm queries ls noteslistsmyNoteSummariesand nomyNotes.Review main after the last step
napalm schema push notes --reviewreports that the schema ismain’s already and that every query compiles. The command exits with status 0.