Last Updated: February 25, 2016
· bsimpson

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}'`

4 Responses
Add your response


bro, rake db:version will give you the current version much easier

rake db:version | awk '{print $3}'

over 1 year ago ·

@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

over 1 year ago ·

@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

over 1 year ago ·

@cpjolicoeur I noticed that actually. My use case is almost always just rolling back the last created migration.

over 1 year ago ·