Rails migration VERSION fetcher
This can be used to get the last migration version number in Rails:
ls db/migrate/ | tail -1 | awk -F _ '{print $1}'
Combine this with rake commands by setting the environmental variable VERSION. This will migrate down the latest migration number:
rake db:migrate:down VERSION=`ls db/migrate/ | tail -1 | awk -F _ '{print $1}'`
Written by Ben Simpson
Related protips
4 Responses
bro, rake db:version
will give you the current version much easier
rake db:version | awk '{print $3}'
@cpjolicoeur I guess if you don't mind waiting 10 seconds. I'm not a patient man
Plus, you would still have to awk that since it pretty prints
@bsimpson doing an ls
on the directory doesn't actually tell you if you have run the most recent migration, it just tells you which one it is, which means db:rollback
might not rollback the version you think or in your case, might fail completely
@cpjolicoeur I noticed that actually. My use case is almost always just rolling back the last created migration.