10

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?

Henry
  • 1,311
  • 1
  • 9
  • 26

2 Answers2

10

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.

Krishna
  • 6,107
  • 2
  • 40
  • 43
pretzelhammer
  • 13,874
  • 15
  • 47
  • 98
  • I run `diesel migration redo` and it working but it still isn't updating `schema.rs` – Henry Jan 23 '21 at 14:41
  • GitHub repo here: https://github.com/henryboisdequin/vsreview – Henry Jan 23 '21 at 14:42
  • I've updated my answer. Run `diesel migration redo all`. – pretzelhammer Jan 23 '21 at 14:50
  • I get an error when I run `diesel migration redo all`. `error: Found argument 'all' which wasn't expected, or isn't valid in this context` – Henry Jan 23 '21 at 15:04
  • 2
    Oh, [this PR was merged 3 months ago](https://github.com/diesel-rs/diesel/pull/2401) but unfortunately diesel hasn't been released since then so the feature isn't available yet. You're going to have to manually run `diesel migration revert` for however many migrations you need to revert and then run `diesel migration run` to redo them all. – pretzelhammer Jan 23 '21 at 15:07
  • 1
    Thanks, it works, but Diesel needs to release that feature though... – Henry Jan 23 '21 at 15:09
1

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.