Fix orphaned migrations

Sometimes when switching between branches, you can end up with migrations that are "orphaned". Meaning, the migration files have been removed, but those migrations are still in the database. This prevents you from being able to rails db:rollback .

See the current list of migrations:

rails db:migrate:status

If any in the list read "NO FILE" then those are orphans, which need to be removed.

Take note of the migration IDs for each of the orphaned (NO FILE) migrations in the list.

Go into the Rails database console

rails dbconsole

Delete each orphaned migration

Run the following for each of the orphaned migrations (replace the migration ID):

delete from schema_migrations where version='20190926172526';

When finished, if you run rails db:migrate:status again, you should no longer see any migrations with NO FILE.

Run rails db:migrate

You'll probably want to run rails db:migrate to ensure that the schema file is up to date and accurate with your current set of migrations.

Last updated