DEV Community 👩‍💻👨‍💻

Dave Gerton
Dave Gerton

Posted on

Fix "NO FILE" Migrations in Rails

You may find yourself in the situation where running...

rails db:migrate:status
Enter fullscreen mode Exit fullscreen mode

...and notice some entries that look like:

up     20230902123456  ********** NO FILE **********
Enter fullscreen mode Exit fullscreen mode

It means that you switched to a branch with a migration in it, ran rails db:migrate, and then switched to another branch. The migration changed your database, but because you switched branches, the migration file is no longer in your code.

To correct this problem, you may try the following:

1. Find the branch that where the missing migration lives. Use this, replacing the timestamp in this example with the one from rails db:migrate:status.

git log --all -- '**/20230902123456*'
Enter fullscreen mode Exit fullscreen mode

2. Switch to the branch that is returned with git checkout <branch>. (You may need to either commit or stash any unsaved local changes.)

3. Reverse the migration (if reversible) with the following (again, using the timestamp from status):

rails db:migrate:down VERSION=20230902123456
Enter fullscreen mode Exit fullscreen mode

4. Abandon the change to the schema it just made with git checkout db\schema.rb.

5. Switch back to your original branch. If you used stash before, restore your stash with git stash pop.

Top comments (0)

Welcome! 👋 New to DEV?

Head over to our Welcome Thread and tell us a bit about yourself!