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
Basic Rollback
Rollback from Sonamu UI
- Start the API dev server (
pnpm dev). - Open the Migrations tab at
http://localhost:34900/sonamu-ui. - Select the database connections to roll back.
- 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, callMigrator directly.
src/scripts/rollback-migration.ts
Check Current Status
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, checkingpnpm sonamu migrate status between runs.
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
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:- Migration was only partially applied
- down function doesnβt match up
- DB was modified manually
- Manually verify DB state
- Modify knex_migrations table
- 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.
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