Skip to content

Commit

Permalink
Print bin/rails help on unrecognized bare options
Browse files Browse the repository at this point in the history
Prior to this commit, `bin/rails` would pass unrecognized bare options
on to Rake:

  ```console
  $ bin/rails -v
  Rails 7.2.0.alpha

  $ bin/rails -V
  rake, version 13.0.6

  $ bin/rails -s
  Running 0 tests in a single process (parallelization threshold is 50)
  ...

  $ bin/rails -S
  invalid option: -S

  $ bin/rails -T
  # Prints list of Rake tasks...
  ```

This commit changes `bin/rails` to print its help message when given an
unrecognized bare option:

  ```console
  $ bin/rails -v
  Rails 7.2.0.alpha

  $ bin/rails -V
  Usage:
    bin/rails COMMAND [options]

  You must specify a command. The most common commands are:
  ...

  $ bin/rails -s
  Usage:
    bin/rails COMMAND [options]

  You must specify a command. The most common commands are:
  ...

  $ bin/rails -S
  Usage:
    bin/rails COMMAND [options]

  You must specify a command. The most common commands are:
  ...

  $ bin/rails -T
  Usage:
    bin/rails COMMAND [options]

  You must specify a command. The most common commands are:
  ...
  ```

Addresses rails#50712.
Closes rails#50700.
  • Loading branch information
jonathanhefner committed Jan 18, 2024
1 parent 4816684 commit faaadb1
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 34 deletions.
62 changes: 32 additions & 30 deletions guides/source/active_record_multiple_databases.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,39 +160,41 @@ multiply the number of connections you have since Rails uses the model class nam
connection specification name.

Now that we have the `database.yml` and the new model set up, it's time to create the databases.
Rails 6.0 ships with all the rails tasks you need to use multiple databases in Rails.
Rails ships with all the commands you need to use multiple databases.

You can run `bin/rails -T` to see all the commands you're able to run. You should see the following:
You can run `bin/rails --help` to see all the commands you're able to run. You should see the following:

```bash
$ bin/rails -T
bin/rails db:create # Create the database from DATABASE_URL or config/database.yml for the ...
bin/rails db:create:animals # Create animals database for current environment
bin/rails db:create:primary # Create primary database for current environment
bin/rails db:drop # Drop the database from DATABASE_URL or config/database.yml for the cu...
bin/rails db:drop:animals # Drop animals database for current environment
bin/rails db:drop:primary # Drop primary database for current environment
bin/rails db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)
bin/rails db:migrate:animals # Migrate animals database for current environment
bin/rails db:migrate:primary # Migrate primary database for current environment
bin/rails db:migrate:status # Display status of migrations
bin/rails db:migrate:status:animals # Display status of migrations for animals database
bin/rails db:migrate:status:primary # Display status of migrations for primary database
bin/rails db:reset # Drop and recreates all databases from their schema for the current environment and loads the seeds
bin/rails db:reset:animals # Drop and recreates the animals database from its schema for the current environment and loads the seeds
bin/rails db:reset:primary # Drop and recreates the primary database from its schema for the current environment and loads the seeds
bin/rails db:rollback # Roll the schema back to the previous version (specify steps w/ STEP=n)
bin/rails db:rollback:animals # Rollback animals database for current environment (specify steps w/ STEP=n)
bin/rails db:rollback:primary # Rollback primary database for current environment (specify steps w/ STEP=n)
bin/rails db:schema:dump # Create a database schema file (either db/schema.rb or db/structure.sql ...
bin/rails db:schema:dump:animals # Create a database schema file (either db/schema.rb or db/structure.sql ...
bin/rails db:schema:dump:primary # Create a db/schema.rb file that is portable against any DB supported ...
bin/rails db:schema:load # Load a database schema file (either db/schema.rb or db/structure.sql ...
bin/rails db:schema:load:animals # Load a database schema file (either db/schema.rb or db/structure.sql ...
bin/rails db:schema:load:primary # Load a database schema file (either db/schema.rb or db/structure.sql ...
bin/rails db:setup # Create all databases, loads all schemas, and initializes with the seed data (use db:reset to also drop all databases first)
bin/rails db:setup:animals # Create the animals database, loads the schema, and initializes with the seed data (use db:reset:animals to also drop the database first)
bin/rails db:setup:primary # Create the primary database, loads the schema, and initializes with the seed data (use db:reset:primary to also drop the database first)
$ bin/rails --help
...
db:create # Create the database from DATABASE_URL or config/database.yml for the ...
db:create:animals # Create animals database for current environment
db:create:primary # Create primary database for current environment
db:drop # Drop the database from DATABASE_URL or config/database.yml for the cu...
db:drop:animals # Drop animals database for current environment
db:drop:primary # Drop primary database for current environment
db:migrate # Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)
db:migrate:animals # Migrate animals database for current environment
db:migrate:primary # Migrate primary database for current environment
db:migrate:status # Display status of migrations
db:migrate:status:animals # Display status of migrations for animals database
db:migrate:status:primary # Display status of migrations for primary database
db:reset # Drop and recreates all databases from their schema for the current environment and loads the seeds
db:reset:animals # Drop and recreates the animals database from its schema for the current environment and loads the seeds
db:reset:primary # Drop and recreates the primary database from its schema for the current environment and loads the seeds
db:rollback # Roll the schema back to the previous version (specify steps w/ STEP=n)
db:rollback:animals # Rollback animals database for current environment (specify steps w/ STEP=n)
db:rollback:primary # Rollback primary database for current environment (specify steps w/ STEP=n)
db:schema:dump # Create a database schema file (either db/schema.rb or db/structure.sql ...
db:schema:dump:animals # Create a database schema file (either db/schema.rb or db/structure.sql ...
db:schema:dump:primary # Create a db/schema.rb file that is portable against any DB supported ...
db:schema:load # Load a database schema file (either db/schema.rb or db/structure.sql ...
db:schema:load:animals # Load a database schema file (either db/schema.rb or db/structure.sql ...
db:schema:load:primary # Load a database schema file (either db/schema.rb or db/structure.sql ...
db:setup # Create all databases, loads all schemas, and initializes with the seed data (use db:reset to also drop all databases first)
db:setup:animals # Create the animals database, loads the schema, and initializes with the seed data (use db:reset:animals to also drop the database first)
db:setup:primary # Create the primary database, loads the schema, and initializes with the seed data (use db:reset:primary to also drop the database first)
...
```

Running a command like `bin/rails db:create` will create both the primary and animals databases.
Expand Down
62 changes: 62 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,65 @@
* `bin/rails` now prints its help message when given an unrecognized bare
option.

__Before__

```console
$ bin/rails -v
Rails 7.2.0.alpha

$ bin/rails -V
rake, version 13.0.6

$ bin/rails -s
Running 0 tests in a single process (parallelization threshold is 50)
...

$ bin/rails -S
invalid option: -S

$ bin/rails -T
# Prints list of Rake tasks...
```

__After__

```console
$ bin/rails -v
Rails 7.2.0.alpha

$ bin/rails -V
Usage:
bin/rails COMMAND [options]

You must specify a command. The most common commands are:
...

$ bin/rails -s
Usage:
bin/rails COMMAND [options]

You must specify a command. The most common commands are:
...

$ bin/rails -S
Usage:
bin/rails COMMAND [options]

You must specify a command. The most common commands are:
...

$ bin/rails -T
Usage:
bin/rails COMMAND [options]

You must specify a command. The most common commands are:
...
```

To print the list of Rake tasks, use `bin/rake -T` / `bin/rake --tasks`.

*Jonathan Hefner*

* Ensure `autoload_paths`, `autoload_once_paths`, `eager_load_paths`, and
`load_paths` only have directories when initialized from engine defaults.
Previously, files under the `app` directory could end up there too.
Expand Down
8 changes: 4 additions & 4 deletions railties/lib/rails/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ def rails_new_with_no_path?(args)

def split_namespace(namespace)
case namespace
when /^(.+):(\w+)$/
[$1, $2]
when ""
["help", "help"]
when HELP_MAPPINGS, "help"
["help", "help_extended"]
when VERSION_MAPPINGS
["version", "version"]
when /^-/, ""
["help", "help"]
when /^(.+):(\w+)$/
[$1, $2]
else
[namespace, namespace]
end
Expand Down
6 changes: 6 additions & 0 deletions railties/test/command/help_integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ class Rails::Command::HelpIntegrationTest < ActiveSupport::TestCase
setup :build_app
teardown :teardown_app

test "prints help on unrecognized bare option" do
%w[-z --zzz -T --tasks].each do |option|
assert_match "You must specify a command.", rails(option)
end
end

test "prints helpful error on unrecognized command" do
output = rails "vershen", allow_failure: true

Expand Down

0 comments on commit faaadb1

Please sign in to comment.