-
Notifications
You must be signed in to change notification settings - Fork 22k
Allow to pass a connection to the dbconsole
command
#29358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -99,6 +99,10 @@ def environment | |||
Rails.respond_to?(:env) ? Rails.env : Rails::Command.environment | |||
end | |||
|
|||
def connection | |||
@options[:connection] || "primary" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use thor's default value?
class_option :connection, aliases: "-c", type: :string,
desc: "Specifies the connection to use.", default: "primary"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, totally fair point and thus, this will be printed running --help
, thanks !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I added it back because Rails::DBConsole
is public API so people may be using it directly, without Thor. 😊
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes please!
@@ -87,10 +87,10 @@ def start | |||
|
|||
def config | |||
@config ||= begin | |||
if configurations[environment].blank? | |||
if configurations[environment].blank? && configurations[connection].blank? | |||
raise ActiveRecord::AdapterNotSpecified, "'#{environment}' database is not configured. Available configuration: #{configurations.inspect}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error will be misleading if the connection was provided.
raise ActiveRecord::AdapterNotSpecified, "'#{environment}' database is not configured. Available configuration: #{configurations.inspect}" | ||
else | ||
configurations[environment] | ||
configurations[environment] || configurations[connection] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If configurations[environment]
is {}
and configurations[connection]
is present, we end up returning the blank env config here instead of the conn config.
def connection | ||
@options["connection"] || "primary" | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice case for Hash#fetch
@@ -145,6 +149,9 @@ class DbconsoleCommand < Base # :nodoc: | |||
class_option :environment, aliases: "-e", type: :string, | |||
desc: "Specifies the environment to run this console under (test/development/production)." | |||
|
|||
class_option :connection, aliases: "-c", type: :string, | |||
desc: "Specifies the connection to use.", default: "primary" | |||
|
|||
def perform | |||
extract_environment_option_from_argument |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd expect to be able to provide the conn as a plain arg. Example scenarios/possibilities:
rails dbconsole production replica
rails dbconsole production:replica
RAILS_ENV=production rails dbconsole replica
rails dbconsole reporting-replica
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think we should deprecate the positional environment arg then and have:
rails dbconsole replica -e production
railties/CHANGELOG.md
Outdated
command when using a 3-level database configuration. | ||
|
||
*Robin Dupret* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's show an example of this invocation style, too 👍
Jeremy, there is now a Kasper, passing the environment as a regular argument is deprecated as you suggested ; this is certainly going to be easier for us to handle only the connection as a regular argument. Such flavor is not deprecated for the Finally, there's a tiny fix attached with this pull request because if we are going to deprecate passing the environment as a regular argument, people may encounter issues as the Thanks for the feedback guys ! :-) |
Can you rebase? The changelog conflicts. I think we should deprecate the environment positional command argument for |
Since 0a4f600, it's possible to specify a 3-level database configuration to gather connections by environment. The `dbconsole` command will try to look for a database configuration which points to the current environment but with such flavour, the environment key is flushed out so let's add the ability to specify the connection and pick `primary` by default to be consistent with Active Record.
People should rather rely on the `-e` or `--environment` options to specify in which environment they want to work. This will allow us to specify the connection to pick as a regular argument in the future.
Running the `console` and `dbconsole` commands with a regular argument as the environment's name automatically expand it to match an existing environment (e.g. dev for development). This feature wasn't available using the `--environment` (a.k.a `-e`) option.
@kaspth : Rebased and deprecated the environment argument for the |
end | ||
|
||
private | ||
def extract_environment_option_from_argument | ||
if environment | ||
self.options = options.merge(environment: acceptable_environment(environment)) | ||
elsif !options[:environment] | ||
|
||
ActiveSupport::Deprecation.warn "Passing the environment's name as a " \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Much cleaner to have this straight in here 👏
I mistype `rails server production` instead of `rails server -e production` expecting to lunch a server in the production environment all the time. However, the signature of `rails server --help` is: ``` Usage: rails server [puma, thin etc] [options] ``` This means that the `production` argument is being interpreted as a Rack server handler like Puma, Thin or Unicorn. Should we argue for the `rails server production`? I'm not sure of the reasons, but the `rails console production` behavior was deprecated in: rails#29358, so parity with the existing `rails console production` usage may not hold anymore. In any case, this PR introduces an explicit option for the Rack servers configuration. The option is called `--using` (or `-u` for short) to avoid the `rails server --server` tantrum. The new interface of `rails server` is: ``` Usage: rails server [using] [options] Options: -p, [--port=port] # Runs Rails on the specified port - defaults to 3000. -b, [--binding=IP] # Binds Rails to the specified IP - defaults to 'localhost' in development and '0.0.0.0' in other environments'. -c, [--config=file] # Uses a custom rackup configuration. # Default: config.ru -d, [--daemon], [--no-daemon] # Runs server as a Daemon. -e, [--environment=name] # Specifies the environment to run this server under (development/test/production). -u, [--using=name] # Specifies the Rack server used to run the application (thin/puma/webrick). -P, [--pid=PID] # Specifies the PID file. # Default: tmp/pids/server.pid -C, [--dev-caching], [--no-dev-caching] # Specifies whether to perform caching in development. [--early-hints], [--no-early-hints] # Enables HTTP/2 early hints. ``` As a bonus, if you mistype the server to use, you'll get an auto-correction message: ``` $ rails s tin Could not find handler "tin". Maybe you meant "thin" or "cgi"? Run `rails server --help` for more options. ```
This commit resolves deprecation warnings like below: >DEPRECATION WARNING: Passing the environment's name as a regular argument is deprecated and will be removed in the next Rails version. Please, use the -e option instead. cf. rails/rails#29358
Hello,
Since 0a4f600, it's possible to specify a 3-level database configuration to gather connections by environment.
The
dbconsole
command will try to look for a database configuration which points to the current environment but with such flavour, the environment key is flushed out so let's add the ability to specify the connection and pickprimary
by default to be consistent with Active Record.Have a nice day !