Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inability to Revert Migrations in Drizzle #183

Open
wojtekKrol opened this issue Sep 3, 2023 · 7 comments
Open

Inability to Revert Migrations in Drizzle #183

wojtekKrol opened this issue Sep 3, 2023 · 7 comments

Comments

@wojtekKrol
Copy link

Problem Description

In TypeORM, reverting a database migration is straightforward due to the up() and down() methods generated with each migration file. Drizzle, however, lacks this essential feature, making it difficult to rollback database schema changes.

Steps to Reproduce

  1. Initial Schema: Assume we have a table TODO with fields {id, title}.
  2. Add Field: Add a new field description to the TODO table so that the schema becomes {id, title, description}.
    • Generate a migration file and migrate changes to the database.
  3. Attempt to Revert: Now, let's say you want to revert these changes to {id, title}.
    • Run drizzle-kit drop to choose which migration file to delete. Assume you remove the last generated file that adds the 'description' field.
    • Run the application to try and revert the schema change. This fails to revert the database to {id, title}.

Issues Encountered

  1. Drizzle doesn't offer an automatic way to revert migrations.
  2. After dropping the last migration file, generating a new migration file via drizzle-kit generate:{dialect} results in an empty file. This is because Drizzle checks the last migration file and the current schema, both of which are {id, title}, thus seeing no changes to migrate.

Suggested Improvement

Implement functionality similar to TypeORM’s up() and down() methods to facilitate easy migration rollbacks.

Note

I would have contributed this feature myself, but unfortunately, Drizzle-kit doesn't seem to be open source.

@fireayehu
Copy link

@AndriiSherman URGENT This is big deal, running npx drizzle-kit drop will remove the migration but does not revert it. There needs to be a way to revert the migrations we run.

@AndriiSherman
Copy link
Member

@fireayehu what is the case for you to revert the migration? Would it be production deploy or something else?

@wojtekKrol
Copy link
Author

@fireayehu I created post on reddit how to work with migrations and what to not do because of potential mess up.

@fireayehu
Copy link

@fireayehu I created post on reddit how to work with migrations and what to not do because of potential mess up.

Got your concerns, but at least there needs to be a way to revert last run migrations. Awesome post btw

@fireayehu
Copy link

@fireayehu what is the case for you to revert the migration? Would it be production deploy or something else?

@AndriiSherman In both cases, but for now on development. Let's say I created a couple of table schemas, then generated and run the migration. After that if I changed my mind and wanted to remove on of those tables from my last migration, in drizzle way I can drop my last migration, then remove one of the schema and generate a new migration and run it. But in the database both of the tables with their constraints will be available. So I manually have to go to the db and make the changes

The way am seeing it is npx drizzle-kit drop should only drop the last migration and also do rollback on the database for the dropped migration.

NB: One more thing I noticed is dropping a migration does not remove the migration row from the migration table in the database.

@JeromeBu
Copy link

linked to #36

@StationSoen
Copy link

In the drizzle-orm repository, the issue was closed by the drizzle team with a wont fix. (Note: link)
I still think a revert migration feature is needed, but I also agree with the Drizzle team on some points.

Anyway, Drizzle has a DX that I'm happy with, and I'm using it well. Thanks to the Drizzle team. 👍🏻

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

No branches or pull requests

5 participants