Skip to content

Instantly share code, notes, and snippets.

@matthutchinson
Last active June 12, 2018 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthutchinson/6c1bc7681b323d4d2ef5d5a55626a5cf to your computer and use it in GitHub Desktop.
Save matthutchinson/6c1bc7681b323d4d2ef5d5a55626a5cf to your computer and use it in GitHub Desktop.
Run rails migration commands via aliases with auto-completion for version numbers (via fzf)
#!/bin/bash
# TDLR; run rails migration commands via aliases with auto-completion for version numbers (via fzf)
#
# brew install fzf
# curl https://gist.githubusercontent.com/matthutchinson/6c1bc7681b323d4d2ef5d5a55626a5cf/raw/fzf_migrations > ~/.fzf_migrations
# source ~/.fzf_migrations
#
# Then run these commands to migrate:
#
# 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
#
# [Read more](https://github.com/junegunn/fzf) about fzf
# I have other useful fuzzy find helpers [here](https://github.com/matthutchinson/workbench/blob/master/fuzzy_finder) (for git, bash etc.)
# 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 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="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment