Ever find yourself re-running Rails migrations? Up, down, redo-ing etc. Have you forgotten that long VERSION
number again? Or what the migration actually migrates? These handy aliases just might be for you.
I’ve been running individual migrations a lot recently, so I took some time to set up these aliases with fzf (a command-line fuzzy finder).
rdbm # bundle exec rake db:migrate (no auto-completion) rdbmu # bundle exec rake db:migrate:up rdbmd # bundle exec rake db:migrate:down rdbmr # bundle exec rake db:migrate:redo
With this script:
#!/bin/bash # helper to echo and execute a command function _echo_and_execute() { echo $1 eval $1 } # fzf (with preview) a rails migration and pass it as the command VERSION function _fzf_rails_migration() { echo $(ls ./db/migrate/ | fzf --preview 'head -20 ./db/migrate/{}' | xargs | cut -d '_' -f 1 | xargs | tr -d 'n') } function rdbm() { if [ ! -d "./db/migrate" ]; then echo "Opps, you're not in a Rails directory" else if [ $# -eq 0 ]; then _echo_and_execute "bundle exec rake db:migrate" else migrate_version=$(_fzf_rails_migration) if [ ! -z $migrate_version ]; then _echo_and_execute "$*$migrate_version" fi fi fi } # db migrate up/down/redo with a VERSION alias rdbmu="rdbm bundle exec rake db:migrate:up VERSION=" alias rdbmd="rdbm bundle exec rake db:migrate:down VERSION=" alias rdbmr="rdbm bundle exec rake db:migrate:redo VERSION="
For example running rdbmr
to redo a migration:
Auto-completion for your migrations with a useful preview! To install this for yourself:
brew install fzf curl https://gist.githubusercontent.com/matthutchinson/6c1bc7681b323d4d2ef5d5a55626a5cf/raw > ~/.fzf_migrations source ~/.fzf_migrations
If this has piqued your interest, I have other fzf related aliases to help with common git and shell commands.