Skip to main content
Rollback migrations to restore the database to a previous state.

Rollback Methods

Sonamu UI

Rollback button on the Migrations tab Rolls back the most recent batch

runAction()

Call the Migrator API directly For scripts and automation

migrate status

Check status before rollback Verify batches

down function

Reverse operation in the migration file Determines the rollback result
There is no rollback CLI command. sonamu migrate provides exactly four subcommands: run, apply, generate, and status. Rollback is performed only from Sonamu UI or via Migrator.runAction("rollback", ...).

Basic Rollback

Rollback from Sonamu UI

  1. Start the API dev server (pnpm dev).
  2. Open the Migrations tab at http://localhost:34900/sonamu-ui.
  3. Select the database connections to roll back.
  4. Click Rollback and confirm in the modal.
A rollback reverts every migration in the most recent batch of the selected database.

Rollback from a Script

For CI or operational scripts, call Migrator directly.
src/scripts/rollback-migration.ts

Check Current Status

Output:

Rollback Granularity

Batch-based Rollback

The unit of a rollback is a batch. A single rollback reverts every migration in the most recent batch.

Reverting Multiple Batches

There is no way to roll back a single selected migration or several batches at once. To go further back, run the rollback once per batch, checking pnpm sonamu migrate status between runs.
Repeating rollbacks eventually returns the database to its initial schema (all tables dropped). Verify the impact of each run in production.

The down Function

Basic Structure

CREATE TABLE Rollback

ALTER TABLE Rollback

FOREIGN KEY Rollback

Rollback Scenarios

Scenario 1: Fix Incorrect Migration

Scenario 2: Production Emergency Rollback

The production database is often unreachable from wherever Sonamu UI runs, so run the rollback script with production environment variables.
When NODE_ENV is not local, only the connection whose name matches the environment is allowed as a target. Any other target is rejected with an error.

Scenario 3: Partial Rollback

Data Preservation

Data Loss on Rollback

Data loss warning!Data cannot be recovered when columns/tables are dropped. Backup is required in production!

Safe Rollback Pattern

Non-reversible Cases

Data Transformation

Data Deletion

Rollback Strategies

1. Backup First

2. Staging Test

3. Incremental Rollback

Error Handling

Rollback Failure

When a down function fails during rollback, an error like the following is raised. Error:
Causes:
  • Migration was only partially applied
  • down function doesn’t match up
  • DB was modified manually
Solutions:
  1. Manually verify DB state
  2. Modify knex_migrations table
  3. Fix down function and retry

Forced Rollback

Rollback Logs

Checking the Result

runAction("rollback", ...) returns the batch number and the list of rolled back files per target connection.
Sonamu UI shows the same information in the result modal.

Querying Rollback History

Practical Tips

1. Verify down Function

2. Document Rollback Plan

3. Auto-rollback Script

Next Steps

Strategies

Safe migration strategies

Running Migrations

Apply with migrate run

Creating Migrations

Generate in Sonamu UI

How It Works

Understanding auto-generation