/
dbconsole_command.rb
96 lines (78 loc) · 2.83 KB
/
dbconsole_command.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true
require "active_support/core_ext/string/filters"
require "rails/command/environment_argument"
module Rails
class DBConsole
def self.start(*args)
new(*args).start
end
def initialize(options = {})
@options = options
@options[:environment] ||= Rails::Command.environment
end
def start
adapter_class.dbconsole(db_config, @options)
rescue NotImplementedError, ActiveRecord::AdapterNotFound, LoadError => error
abort error.message
end
def db_config
@db_config ||= begin
# If the user provided a database, use that. Otherwise find
# the first config in the database.yml
config = if database
@db_config = configurations.configs_for(env_name: environment, name: database, include_hidden: true)
else
@db_config = configurations.find_db_config(environment)
end
unless config
missing_db = database ? "'#{database}' database is not" : "No databases are"
raise ActiveRecord::AdapterNotSpecified,
"#{missing_db} configured for '#{environment}'. Available configuration: #{configurations.inspect}"
end
config.validate!
config
end
end
def database
@options[:database]
end
def environment
@options[:environment]
end
private
def adapter_class
ActiveRecord::ConnectionAdapters.resolve(db_config.adapter)
rescue LoadError
ActiveRecord::ConnectionAdapters::AbstractAdapter
end
def configurations # :doc:
require APP_PATH
ActiveRecord::Base.configurations = Rails.application.config.database_configuration
ActiveRecord::Base.configurations
end
def find_cmd_and_exec(commands, *args) # :doc:
Rails.deprecator.warn(<<~MSG.squish)
Rails::DBConsole#find_cmd_and_exec is deprecated and will be removed in Rails 7.2.
Please use find_cmd_and_exec on the connection adapter class instead.
MSG
ActiveRecord::Base.lease_connection.find_cmd_and_exec(commands, *args)
end
end
module Command
class DbconsoleCommand < Base # :nodoc:
include EnvironmentArgument
class_option :include_password, aliases: "-p", type: :boolean,
desc: "Automatically provide the password from database.yml"
class_option :mode, enum: %w( html list line column ), type: :string,
desc: "Automatically put the sqlite3 database in the specified mode"
class_option :header, type: :boolean
class_option :database, aliases: "--db", type: :string,
desc: "Specify the database to use."
desc "dbconsole", "Start a console for the database specified in config/database.yml"
def perform
boot_application!
Rails::DBConsole.start(options)
end
end
end
end