feat: add rollback/down migrations support#5476
Open
roor0 wants to merge 4 commits intodrizzle-team:betafrom
Open
feat: add rollback/down migrations support#5476roor0 wants to merge 4 commits intodrizzle-team:betafrom
roor0 wants to merge 4 commits intodrizzle-team:betafrom
Conversation
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.
5755197 to
c3f9e83
Compare
- 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds automatic down migration generation and runtime rollback support to Drizzle.
Down SQL Generation (drizzle-kit)
drizzle-kit generatenow produces adown.sqlfile alongside eachmigration.sqlRENAMEstatements instead of destructiveDROP/CREATEdownMigrationsrecord inmigrations.jsRuntime Rollback (drizzle-orm)
rollback(db, config, steps?)export on every driver migratorFiles Changed
generate-common.ts,generate-down-helpers.ts(new), allgenerate-*.tsfilesmigrator.ts, all dialectdialect.tsfiles, all drivermigrator.tsfilesdown-sql.test.ts,down-helpers.test.tsTest plan
writeResultdown SQL file generationembeddedMigrationsdown SQL bundlingwithCapturerename capturemakeInverseResolverrename inversiontsc --noEmit)