Skip to content

feat: add rollback/down migrations support#5476

Open
roor0 wants to merge 4 commits intodrizzle-team:betafrom
roor0:feat/down-migrations
Open

feat: add rollback/down migrations support#5476
roor0 wants to merge 4 commits intodrizzle-team:betafrom
roor0:feat/down-migrations

Conversation

@roor0
Copy link
Copy Markdown

@roor0 roor0 commented Mar 12, 2026

Summary

Adds automatic down migration generation and runtime rollback support to Drizzle.

Down SQL Generation (drizzle-kit)

  • drizzle-kit generate now produces a down.sql file alongside each migration.sql
  • Down SQL is generated by running the schema diff in reverse with rename-aware resolvers
  • Renames produce proper RENAME statements instead of destructive DROP/CREATE
  • Supported for all dialects: PostgreSQL, MySQL, SQLite, LibSQL, SingleStore, CockroachDB
  • Bundled migrations (Expo, Durable SQLite, OP-SQLite) gain a downMigrations record in migrations.js

Runtime Rollback (drizzle-orm)

  • New rollback(db, config, steps?) export on every driver migrator
  • Reads the N most-recently-applied migrations from the tracking table
  • Executes their down SQL in reverse order within a transaction
  • Removes the corresponding tracking rows
  • Supports all 30+ driver adapters

Files Changed

  • drizzle-kit: generate-common.ts, generate-down-helpers.ts (new), all generate-*.ts files
  • drizzle-orm: migrator.ts, all dialect dialect.ts files, all driver migrator.ts files
  • Tests: down-sql.test.ts, down-helpers.test.ts

Test plan

  • Unit tests for writeResult down SQL file generation
  • Unit tests for embeddedMigrations down SQL bundling
  • Unit tests for withCapture rename capture
  • Unit tests for makeInverseResolver rename inversion
  • TypeScript compilation passes (tsc --noEmit)
  • Integration tests with actual database rollback (future)

@roor0 roor0 changed the title feat: add rollback/down migrations support [All]: Add rollback/down migrations support Mar 12, 2026
roor0 added 2 commits March 24, 2026 20:02
Generate down.sql files alongside migration.sql during `drizzle-kit generate`.
Down SQL is produced by running the schema diff in reverse with rename-aware
resolvers so that renames produce RENAME statements instead of DROP/CREATE.

Adds `rollback()` export to every driver migrator and dialect class.
Rollback reads the N most-recently-applied migrations from the tracking table,
executes their down SQL in reverse order, and removes the tracking rows.

Bundled migration support (Expo, Durable SQLite, OP-SQLite) gains a
`downMigrations` record in the generated migrations.js file.
@roor0 roor0 force-pushed the feat/down-migrations branch from 5755197 to c3f9e83 Compare March 25, 2026 02:03
@roor0 roor0 changed the base branch from main to beta March 25, 2026 02:03
@roor0 roor0 changed the title [All]: Add rollback/down migrations support feat: add rollback/down migrations support Mar 25, 2026
roor0 added 2 commits March 24, 2026 23:48
- Use sql.identifier() in proxy rollback DELETE statements instead of
  raw string interpolation (mysql-proxy, pg-proxy, sqlite-proxy,
  singlestore-proxy)
- Use path.dirname() instead of string replace for Windows-safe
  down.sql detection in embeddedMigrations
- Standardize SELECT id (not rowid) in SQLite/durable-sqlite rollback
- Tighten invertRenames matching to include schema/table properties,
  not just name, to correctly handle column renames across tables
- Fix embeddedMigrations output indentation
- Remove redundant downSqlDelimiter variable
- Add TODO comments for missing node-mssql and effect-postgres rollback
- Expand test coverage: breakpoints:false, multiple captures,
  schema/table disambiguation, partial match, breakpoint count/order
…e-hash collision

When two migrations have identical SQL content they share the same hash.
The previous `migrations.find(m => m.hash === dbMigration.hash)` could
return the wrong migration's downSql. Now all rollback lookups fetch
`name` from the tracking table and match on both hash and name, with a
guard for old rows that lack a name value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: Reverse/Down Migrations [FEATURE]: Migration Rollback feature

1 participant