'How to re-run Diesel migrations?

I'm using Diesel with PostgreSQL. I added my migrations, and they worked fine, outputting everything in the schema.rs file. Until I noticed that I was missing the created_at fields on some of my migrations. I edited the SQL and ran diesel migration run. Nothing happened, no errors, no success. Is there a way to fix this and re-run my migrations?



Solution 1:[1]

The command

diesel migration run

Only applies migrations. If you would like to revert a migration you have to run:

diesel migration revert

Using these commands together you can "redo" an applied migration like this:

diesel migration revert
diesel migration run

This pattern is common enough that diesel provides this shortcut command that does the same thing as the above 2 commands:

diesel migration redo

Note: all of these commands only run, revert, or redo a single migration at a time. If you want to run, revert, or redo multiple migrations or all migrations you're going to have to manually run the commands multiple times, that is until a new version of diesel is released and this feature becomes available, when you'll be able to redo all migrations by simply running:

diesel migration redo --all

Note: all of the commands will only work correctly if you've written a down.sql for every migration you intend to revert or redo.

Solution 2:[2]

The correct procedure is to create a new migration (diesel migration generate), then add ALTER statement to up.sql to alter your existing schema. Then you can simply diesel migration run. Once that is done, you can use diesel print-schema to get the updated code to write into schema.rs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Krishna
Solution 2 crusty-dave