Skip to content

Rollback

Before each migration run, schema-flow captures a MigrationSnapshot in _smplcty_schema_flow.snapshots. This records the operations that were applied.

schema-flow down computes reverse operations from the latest snapshot and executes them.

Terminal window
# Rollback the last migration
npx @smplcty/schema-flow down --db postgresql://user:pass@localhost:5432/mydb
Forward operationReverse
create_tableDROP TABLE
add_columnDROP COLUMN
add_indexDROP INDEX
add_foreign_keyDROP CONSTRAINT
add_foreign_key_not_validDROP CONSTRAINT
add_checkDROP CONSTRAINT
add_check_not_validDROP CONSTRAINT
add_unique_constraintDROP CONSTRAINT
create_enumDROP TYPE
create_functionDROP FUNCTION
create_triggerDROP TRIGGER
drop_triggerCREATE TRIGGER
create_policyDROP POLICY
drop_policyCREATE POLICY
create_viewDROP VIEW
create_materialized_viewDROP MATERIALIZED VIEW
enable_rlsDISABLE RLS
create_extensionDROP EXTENSION
create_roleDROP ROLE
grant_*REVOKE

These are skipped during rollback (no data loss from rollback itself):

  • alter_column — column type/default changes cannot be safely reversed
  • add_enum_value — PostgreSQL cannot remove enum values in a transaction
import {
ensureSnapshotsTable,
saveSnapshot,
getLatestSnapshot,
listSnapshots,
deleteSnapshot,
computeRollback,
runDown,
} from '@smplcty/schema-flow';