Skip to Content
Zenlev
Guides

Staged deployments

Coordinate schema expansion, backfills, and cleanup across release checkpoints.

A release can span several database changes. Some must precede application deployment. Others need the new application to run first. Cleanup can wait until old application versions and rollback windows have passed.

Give these changes separate migration files. Assign namespaces to the groups your pipeline invokes, then declare database prerequisites by migration ID.

  1. Before deployment

    Add compatible schema. Inspect and apply the predeploy namespace.

  2. During deployment

    Deploy compatible application code. Run backfills after its readiness checkpoint.

  3. After deployment

    Verify traffic and data. Retire old schema when the previous application has stopped.

Your release pipeline owns application deployment and readiness checks. Invoke Zenlev at each chosen checkpoint. Namespace names label migration groups, and dependsOn declares their database prerequisites.

Plan an email-column replacement

Suppose the application currently reads email and will move to contact_email. A safe release can use these changes.

MigrationNamespacePrerequisitesReadiness condition
Add nullable contact_emailpredeployExisting customer tableOld application remains compatible
Create its supporting indexpredeployNew column migrationReview index cost and dialect restrictions
Backfill contact_emaildeployNew column migrationDeployed application writes both columns
Remove emailpostdeployIndex and backfill migrationsTraffic uses the new column and old writers have stopped
Add replacement columnpredeploy
Backfill existing rowsdeploy
Create supporting indexpredeploy
Retire old columnpostdeploy
The new column precedes both backfill and index creation. Cleanup depends on both. Backfill and indexing have no dependency between them. Zenlev runs the selected migrations sequentially, using lexical ID order for independent migrations.

The graph captures database prerequisites. Pipeline checks enforce the application readiness conditions in the last column. Namespace spelling has no built-in release ordering.

Create the migration files

Generate the expansion first.

bun run zenlev migrate create --name add_contact_email --namespace predeploy --empty

Read its generated meta.id. Use that ID when creating its direct dependents.

bun run zenlev migrate create --name index_contact_email --namespace predeploy --depends-on <expand-id> --empty
bun run zenlev migrate create --name backfill_contact_email --namespace deploy --depends-on <expand-id> --empty
bun run zenlev migrate create --name retire_email --namespace postdeploy --depends-on <index-id>,<backfill-id> --empty

Replace angle-bracket placeholders with the generated migration IDs before running these commands. Edit each file's up handler. Give each operation a down handler only when its reversal has understood semantics. Keep application compatibility and backfill batching requirements in the migration review.

For long backfills, consider smaller dependency-linked migrations with restartable data operations. A migration handler can perform arbitrary TypeScript work. Its author controls data checks and batching.

Apply expansion before deployment

Choose the destination through your project factory and inspect the graph and plan.

bun run zenlev --env staging migrate tree
bun run zenlev --env staging migrate apply --namespace predeploy --plan-only
bun run zenlev --env staging migrate apply --namespace predeploy

Review which IDs are included. The namespace selects all pending migrations carrying that label. Add graph bounds, explicit IDs, or tags when the release requires a narrower selection.

A pending prerequisite outside the selected scope blocks application. Apply the prerequisite at its own checkpoint or revise the selected scope after review.

Deploy compatible application code

Deploy code that understands both columns and writes both values. Wait for the readiness check appropriate to your rollout.

Run the data migration when the application can maintain consistency during the backfill.

bun run zenlev --env staging migrate apply --namespace deploy --plan-only
bun run zenlev --env staging migrate apply --namespace deploy

Verify row counts and the application's read behavior. Switch reads to contact_email through your application release process.

Retire old storage after verification

Confirm that old application instances and background workers have stopped using email. Decide whether the rollback window has closed.

bun run zenlev --env staging migrate apply --namespace postdeploy --plan-only
bun run zenlev --env staging migrate apply --namespace postdeploy

The cleanup migration's dependencies require the recorded backfill and index. Your release process must establish the remaining application conditions before invoking cleanup.

Repeat the reviewed sequence against production with --env production. Your factory must select its production connection. Each destination keeps its own applied history.

Adjust the schedule while preserving dependencies

Independent work can move between release checkpoints when application compatibility allows it.

ScheduleFirstSecondThirdLast
Build the index before deployingAdd columnBuild indexDeploy compatible code and backfillRetire old column after checks
Delay the index to a maintenance windowAdd columnDeploy compatible code and backfillBuild indexRetire old column after checks

The second schedule requires the application to work acceptably while the index is pending. Use explicit IDs to preview the expansion on its own and to preview indexing in the later window. Set EXPAND_ID and INDEX_ID to the generated IDs.

bun run zenlev --env staging migrate apply --only "$EXPAND_ID" --plan-only
bun run zenlev --env staging migrate apply --only "$INDEX_ID" --plan-only

Preview the index after the expansion is applied. Until then, the pending prerequisite blocks that selection. Cleanup still requires the index and backfill in either schedule.

Use one comma-separated namespace selection to inspect several groups together.

bun run zenlev --env staging migrate apply --namespace predeploy,deploy --plan-only

This combines the selected groups in dependency order. It also includes every pending migration matching those groups and any other selection bounds. Inspect the result before using a combined apply.

Read Dependency graph for bounds and tie-breaking, and Rollback and recovery for failure handling.

On this page