I have been switching between branches in a project and each of them have different migrations... This is the scenario:
$ rake db:migrate:status
Status Migration ID Migration Name -------------------------------------------------- ... up 20130307154128 Change columns in traffic capture up 20130311155109 Remove log settings up 20130311160901 Remove log alarm table up 20130320144219 ********** NO FILE ********** up 20130320161939 ********** NO FILE ********** up 20130320184628 ********** NO FILE ********** up 20130322004817 Add replicate to root settings up 20130403190042 ********** NO FILE ********** up 20130403195300 ********** NO FILE ********** up 20130403214000 ********** NO FILE ********** up 20130405164752 Fix ap hostnames up 20130410194222 ********** NO FILE **********
The problem is rake db:rollback
don't work at all because of the missing files...
What should I do to be able to rollback again and get rid of the NO FILE messages?
Btw, rake db:reset
or rake db:drop
are not an option, I cannot lose data from other tables...
Every time a migration is generated using the rails g migration command, Rails generates the migration file with a unique timestamp. The timestamp is in the format YYYYMMDDHHMMSS . Whenever a migration is run, Rails inserts the migration timestamp into an internal table schema_migrations .
Go to /db/migrate folder and edit the migration file you made.
I ended up solving the problem like this:
(1) Go to the branches that has the migration files and roll them back. This is not trivial when you have many branches which are will result in many conflicts if you try to merge them. So I use this commands to find out the branches of each orphan migration belongs to.
So, I need to find commit of the last time the migration was modified.
git log --all --reverse --stat | grep <LASTEST_ORPHAN_MIGRATION_ID> -C 10
I take the commit hash and determine which branch it belongs like this:
git branch --contains <COMMIT_HASH>
Then I can go back to that branch, do a rollback and repeat this process for all the missing files.
(2) Run migrations: checkout the branch you finally want to work on and run the migrations and you should be good to go.
Troubleshooting
I also ran in some cases where orphaned migrations where on deleted branches.
To solve this I created dummy migration files with the same migration_id of the missing files and roll them back. After that, I was able to delete they dummy migrations and have a clean migration status :)
Another alternative is deleting the missing files from the database directly:
delete from schema_migrations where version='<MIGRATION_ID>';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With