As a busy Rails developer, you are probably inconvenienced at least twice a year when you have to run a migration that's not the last one in the db/migrate
directory. You have to ls -lrt
the contents of that directory, copy the version number, and then paste it into your terminal so you can do that down migration:
I mean! Those are precious seconds wasted. Well, no more. Just put this snippet in your .bash_profile
:
Now when you type rake db:migrate:down VERSION=2018
and hit the tab key Bash will list out all the migration version files from 2018. And when you select one, even though we're placing the entire filename to the right of the VERSION=
it still works. That's because the db:migrate:down
Rake task uses String#to_i
to process the VERSION
environment variable, and that handles it. Per the documentation, String#to_i
"Returns the result of interpreting leading characters in str as an integer base base (between 2 and 36). Extraneous characters past the end of a valid number are ignored."
Hats off to Mike English for his very helpful Bash completion article!