fzfをつかってRailsのmigrationを実行しやすくする
イントロ
Railsのmigationを実行する機会はそこまで多くはないと思うのですが、
generateしてつくったあとにVERSIONの選択が意外と面倒だったりします。
以前の記事
でgit statusをfzfのpreviewやbindを使って操作しやすくしましたが、
railsのmigration管理もpreviewとbindと親和性が高いなと思い作ってみました。
fish shell の function
以下のようなfish の function を作成しています。
function migrate if test ! -d 'db/migrate' echo 'Go to Rails Root' return end set -l out (ls -1 db/migrate | grep -v -e '^\.' | \ fzf --exit-0 \ --preview="bat --color=always db/migrate/{}" \ --expect=ctrl-u,ctrl-d,ctrl-r,ctrl-m \ --header='C-u: up, C-d: down, C-r: redo, C-m(Enter): edit' \ ) [ $status != 0 ]; and commandline -f repaint; and return if string length -q -- $out set -l key $out[1] set -l time (echo $out[2] | awk -F '_' '{ print $1 }') echo $key if test $key = 'ctrl-u' commandline "./bin/rails db:migrate:up VERSION=$time" else if test $key = 'ctrl-d' commandline "./bin/rails db:migrate:down VERSION=$time" else if test $key = 'ctrl-r' commandline "./bin/rails db:migrate:redo VERSION=$time" else if test $key = 'ctrl-m' commandline "$EDITOR db/migrate/$out[2]" end commandline -f execute end end
操作方法
functionを登録してコマンドを実行するとfzf が起動します。
$ migrate
migrate:up migrate:down migrate:redo を key bind しているので
バージョンを指定してアクションを決めて実行することができます。
これで少し便利になりました。
previewにはbatコマンドを使っていますが、headなどでもいいかと思います。
よかったら使ってみてください。