Last active
June 12, 2018 08:33
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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