# frozen_string_literal: truerequire"active_support/core_ext/string/filters"require"active_support/deprecation"require"rails/command/environment_argument"moduleRailsclassDBConsoledefself.start(*args)new(*args).startenddefinitialize(options={})@options=optionsenddefstartENV["RAILS_ENV"]||=@options[:environment]||environmentconfig=db_config.configuration_hashcasedb_config.adapterwhen/^(jdbc)?mysql/args={host: "--host",port: "--port",socket: "--socket",username: "--user",encoding: "--default-character-set",sslca: "--ssl-ca",sslcert: "--ssl-cert",sslcapath: "--ssl-capath",sslcipher: "--ssl-cipher",sslkey: "--ssl-key"}.filter_map{|opt,arg|"#{arg}=#{config[opt]}"ifconfig[opt]}ifconfig[:password]&&@options[:include_password]args<<"--password=#{config[:password]}"elsifconfig[:password]&&!config[:password].to_s.empty?args<<"-p"endargs<<db_config.databasefind_cmd_and_exec(["mysql","mysql5"],*args)when/^postgres|^postgis/ENV["PGUSER"]=config[:username]ifconfig[:username]ENV["PGHOST"]=config[:host]ifconfig[:host]ENV["PGPORT"]=config[:port].to_sifconfig[:port]ENV["PGPASSWORD"]=config[:password].to_sifconfig[:password]&&@options[:include_password]ENV["PGSSLMODE"]=config[:sslmode].to_sifconfig[:sslmode]ENV["PGSSLCERT"]=config[:sslcert].to_sifconfig[:sslcert]ENV["PGSSLKEY"]=config[:sslkey].to_sifconfig[:sslkey]ENV["PGSSLROOTCERT"]=config[:sslrootcert].to_sifconfig[:sslrootcert]find_cmd_and_exec("psql",db_config.database)when"sqlite3"args=[]args<<"-#{@options[:mode]}"if@options[:mode]args<<"-header"if@options[:header]args<<File.expand_path(db_config.database,Rails.respond_to?(:root)?Rails.root:nil)find_cmd_and_exec("sqlite3",*args)when"oracle","oracle_enhanced"logon=""ifconfig[:username]logon=config[:username].duplogon<<"/#{config[:password]}"ifconfig[:password]&&@options[:include_password]logon<<"@#{db_config.database}"ifdb_config.databaseendfind_cmd_and_exec("sqlplus",logon)when"sqlserver"args=[]args+=["-d","#{db_config.database}"]ifdb_config.databaseargs+=["-U","#{config[:username]}"]ifconfig[:username]args+=["-P","#{config[:password]}"]ifconfig[:password]ifconfig[:host]host_arg=+"tcp:#{config[:host]}"host_arg<<",#{config[:port]}"ifconfig[:port]args+=["-S",host_arg]endfind_cmd_and_exec("sqlcmd",*args)elseabort"Unknown command-line client for #{db_config.database}."endenddefdb_configreturn@db_configifdefined?(@db_config)# If the user provided a database, use that. Otherwise find# the first config in the database.ymlifdatabase@db_config=configurations.configs_for(env_name: environment,name: database,include_hidden: true)else@db_config=configurations.find_db_config(environment)endunless@db_configmissing_db=database?"'#{database}' database is not":"No databases are"raiseActiveRecord::AdapterNotSpecified,"#{missing_db} configured for '#{environment}'. Available configuration: #{configurations.inspect}"end@db_configenddefenvironmentRails.respond_to?(:env)?Rails.env:Rails::Command.environmentenddefdatabase@options[:database]endprivatedefconfigurations# :doc:requireAPP_PATHActiveRecord::Base.configurations=Rails.application.config.database_configurationActiveRecord::Base.configurationsenddeffind_cmd_and_exec(commands,*args)# :doc:commands=Array(commands)dirs_on_path=ENV["PATH"].to_s.split(File::PATH_SEPARATOR)unless(ext=RbConfig::CONFIG["EXEEXT"]).empty?commands=commands.map{|cmd|"#{cmd}#{ext}"}endfull_path_command=nilfound=commands.detectdo|cmd|dirs_on_path.detectdo|path|full_path_command=File.join(path,cmd)beginstat=File.stat(full_path_command)rescueSystemCallErrorelsestat.file?&&stat.executable?endendendiffoundexecfull_path_command,*argselseabort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.")endendendmoduleCommandclassDbconsoleCommand<Base# :nodoc:includeEnvironmentArgumentclass_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 (html, list, line, column)."class_option:header,type: :booleanclass_option:database,aliases: "--db",type: :string,desc: "Specifies the database to use."defperformextract_environment_option_from_argument# RAILS_ENV needs to be set before config/application is required.ENV["RAILS_ENV"]=options[:environment]require_application_and_environment!Rails::DBConsole.start(options)endendendend