Search code examples
sqlpostgresqlrustrust-diesel

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

  • 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.