feat: add rollback/down migrations support#5476
Conversation
5755197 to
c3f9e83
Compare
|
I'm curious if this is still in the works? My team just started using drizzle and this rollback piece would def come in handy. |
9c397cd to
e8e001c
Compare
|
please let's push this asap its very needed |
|
do we really need to put this all over reddit? Please prioritize |
|
Thanks for you PR! The question is, how would custom migrations be handled? How would it handle the cases, where someone changed generated sql file with some custom statements? |
Addresses the custom-migration question on drizzle-team#5476: - `drizzle-kit generate --custom` now writes a `down.sql` scaffold (`-- Custom SQL rollback file, put your reverse statements below! --`) alongside the empty `migration.sql`, giving users a clear home for hand-written rollback SQL. - Auto-generated `down.sql` files are now prefixed with a header noting they are editable and should be kept in sync with `migration.sql` when the developer hand-edits it. - New `generateDownMigrations` config option (defaults to `true`) for teams that prefer to author rollback SQL entirely by hand — skips both the reverse diff and the down.sql file emission.
e8e001c to
66d84f9
Compare
|
Good question — a couple of things worth calling out explicitly, and I've just pushed a follow-up commit addressing them: 1. 2. Hand-edited generated migrations — the auto-generated 3. Opt-out — a new Happy to iterate further if you'd like a different contract here. |
66d84f9 to
d919cbc
Compare
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.
- 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.
Addresses the custom-migration question on drizzle-team#5476: - `drizzle-kit generate --custom` now writes a `down.sql` scaffold (`-- Custom SQL rollback file, put your reverse statements below! --`) alongside the empty `migration.sql`, giving users a clear home for hand-written rollback SQL. - Auto-generated `down.sql` files are now prefixed with a header noting they are editable and should be kept in sync with `migration.sql` when the developer hand-edits it. - New `generateDownMigrations` config option (defaults to `true`) for teams that prefer to author rollback SQL entirely by hand — skips both the reverse diff and the down.sql file emission.
d919cbc to
609a762
Compare
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)