DEV Community

Dave Gerton
Dave Gerton

Posted on

4

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.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay