From 4b657407c98bbc8830ae66c2ac7ff3d737c55a83 Mon Sep 17 00:00:00 2001 From: Miguel Hargreaves Pimenta Date: Sat, 29 Aug 2026 01:43:45 +0100 Subject: [PATCH] feat(rails): support multi-database aliases (#13368) Rails applications can define more than one database (`primary`, `cache`, `cable`, `queue`, ...), and each one gets its own namespaced task. These commands accept an optional database name as the first argument: `rdc`, `rdd`, `rdm`, `rdmd`, `rdmr`, `rdms`, `rdmu`, `rdr`, `rdrs`, `rdsl` Co-authored-by: Robby Russell --- plugins/rails/README.md | 27 ++++++++++++++++++ plugins/rails/rails.plugin.zsh | 51 +++++++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/plugins/rails/README.md b/plugins/rails/README.md index f023031a6..5a3d0bf08 100644 --- a/plugins/rails/README.md +++ b/plugins/rails/README.md @@ -54,6 +54,33 @@ plugins=(... rails) | `rta` | `rails test:all` | Runs all Rails tests, including system tests | | `ru` | `rails runner` | Run Ruby code in the context of Rails | +### Multiple databases + +Rails applications can define more than one database (`primary`, `cache`, `cable`, +`queue`, ...), and each one gets its own namespaced task. These commands accept an +optional database name as the first argument: + +`rdc`, `rdd`, `rdm`, `rdmd`, `rdmr`, `rdms`, `rdmu`, `rdr`, `rdrs`, `rdsl` + +```zsh +rdm # rails db:migrate +rdm cache # rails db:migrate:cache +rdm VERSION=20231225 # rails db:migrate VERSION=20231225 +rdm cache STEP=2 # rails db:migrate:cache STEP=2 +rdm --trace # rails db:migrate --trace +``` + +The first argument is read as a database name only when it is a bare word. Flags +(`--trace`), variables (`STEP=2`) and other tasks (`db:seed`) are passed through to +`rails` unchanged, so previous usage keeps working. When a database name is given, +the resolved command is printed so it is clear which database is being used. + +`rdmrs` is not in the list above because Rails does not define a per-database +`db:migrate:reset` task. + +Note that these are implemented as shell functions rather than aliases, so they will +not appear in the output of `alias`. + ### Foreman | Alias | Command | Description | diff --git a/plugins/rails/rails.plugin.zsh b/plugins/rails/rails.plugin.zsh index 9d503768e..a01a2368b 100644 --- a/plugins/rails/rails.plugin.zsh +++ b/plugins/rails/rails.plugin.zsh @@ -47,19 +47,9 @@ alias rc='rails console' alias rcs='rails console --sandbox' alias rd='rails destroy' alias rdb='rails dbconsole' -alias rdc='rails db:create' -alias rdd='rails db:drop' -alias rdm='rails db:migrate' -alias rdmd='rails db:migrate:down' -alias rdmr='rails db:migrate:redo' alias rdmrs='rails db:migrate:reset' -alias rdms='rails db:migrate:status' alias rdmtc='rails db:migrate db:test:clone' -alias rdmu='rails db:migrate:up' -alias rdr='rails db:rollback' -alias rdrs='rails db:reset' alias rds='rails db:seed' -alias rdsl='rails db:schema:load' alias rdtc='rails db:test:clone' alias rdtp='rails db:test:prepare' alias rgen='rails generate' @@ -106,6 +96,47 @@ alias rkn='rake notes' alias rksts='rake stats' alias rkt='rake test' +# Database tasks +# +# Rails applications can define more than one database (primary, cache, cable, +# queue, ...). Each one gets its own namespaced task, e.g. `db:migrate:cache`. +# The functions below take an optional database name as the first argument: +# +# rdm -> rails db:migrate +# rdm cache -> rails db:migrate:cache +# rdm VERSION=20231225 -> rails db:migrate VERSION=20231225 +# rdm cache STEP=2 -> rails db:migrate:cache STEP=2 +# rdm --trace -> rails db:migrate --trace +# +# The first argument is read as a database name only when it is a bare word. +# Flags (--trace), variables (STEP=2) and other tasks (db:seed) are passed +# through to rails untouched, so the previous alias behavior still works. +function _rails_db_command() { + local task="$1" + shift + + if [[ $# -gt 0 && "$1" != -* && "$1" != *=* && "$1" != *:* ]]; then + task="$task:$1" + shift + # Echo the resolved task so it is clear which database is being used + local -a cmd=(rails "$task" "$@") + print -u2 -r -- "Running: ${cmd[*]}" + fi + + rails "$task" "$@" +} + +function rdc() { _rails_db_command db:create "$@" } +function rdd() { _rails_db_command db:drop "$@" } +function rdm() { _rails_db_command db:migrate "$@" } +function rdmd() { _rails_db_command db:migrate:down "$@" } +function rdmr() { _rails_db_command db:migrate:redo "$@" } +function rdms() { _rails_db_command db:migrate:status "$@" } +function rdmu() { _rails_db_command db:migrate:up "$@" } +function rdr() { _rails_db_command db:rollback "$@" } +function rdrs() { _rails_db_command db:reset "$@" } +function rdsl() { _rails_db_command db:schema:load "$@" } + # legacy stuff alias sc='ruby script/console' alias sd='ruby script/destroy'