@@ -9,45 +9,87 @@ class Rails::Command::DbconsoleCommand < Rails::Command::Base
9
9
def perform
10
10
require "rake"
11
11
Rake . with_application ( &:load_rakefile ) # Needed to initialize Rails.application
12
- Rails :: DBConsole . start ( options )
12
+ start!
13
13
end
14
- end
15
-
16
- class Rails ::DBConsole
17
- DBConfig = Struct . new ( :configuration_hash , :adapter , :database )
18
14
19
15
private
20
16
21
- def db_config
22
- @db_config ||= DBConfig . new ( configuration_hash , adapter , database )
17
+ # See ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.dbconsole
18
+ def start!
19
+ ENV [ "PGUSER" ] = pg_config [ :username ] if pg_config [ :username ]
20
+ ENV [ "PGHOST" ] = pg_config [ :host ] if pg_config [ :host ]
21
+ ENV [ "PGPORT" ] = pg_config [ :port ] . to_s if pg_config [ :port ]
22
+
23
+ if pg_config [ :password ] && options [ :include_password ]
24
+ ENV [ "PGPASSWORD" ] =
25
+ pg_config [ :password ] . to_s
26
+ end
27
+
28
+ ENV [ "PGSSLMODE" ] = pg_config [ :sslmode ] . to_s if pg_config [ :sslmode ]
29
+ ENV [ "PGSSLCERT" ] = pg_config [ :sslcert ] . to_s if pg_config [ :sslcert ]
30
+ ENV [ "PGSSLKEY" ] = pg_config [ :sslkey ] . to_s if pg_config [ :sslkey ]
31
+ ENV [ "PGSSLROOTCERT" ] = pg_config [ :sslrootcert ] . to_s if pg_config [ :sslrootcert ]
32
+
33
+ if pg_config [ :variables ]
34
+ ENV [ "PGOPTIONS" ] = pg_config [ :variables ] . filter_map do |name , value |
35
+ "-c #{ name } =#{ value . to_s . gsub ( /[ \\ ]/ , '\\\\\0' ) } " unless value . in? ( [ ":default" , :default ] )
36
+ end . join ( " " )
37
+ end
38
+
39
+ find_cmd_and_exec ( "psql" , database )
23
40
end
24
41
25
- def configuration_hash
26
- return @configuration_hash if defined? ( @configuration_hash )
42
+ def pg_config
43
+ @pg_config ||= begin
44
+ rails_db_config = Rails . application . config . database_configuration
27
45
28
- rails_db_config = Rails . application . config . database_configuration
46
+ sequel_configuration = SequelRails ::Configuration . new
47
+ SequelRails . configuration = sequel_configuration . merge! ( raw : rails_db_config )
29
48
30
- sequel_configuration = SequelRails ::Configuration . new
31
- SequelRails . configuration = sequel_configuration . merge! ( raw : rails_db_config )
49
+ storage = SequelRails ::Storage . adapter_for ( Rails . env )
50
+ config = storage . config . with_indifferent_access
32
51
33
- storage = SequelRails ::Storage . adapter_for ( Rails . env )
34
- config = storage . config . with_indifferent_access
52
+ if @options [ :server ]
53
+ server_config = config . fetch ( :servers ) . fetch ( @options [ :server ] )
54
+ config . merge! ( server_config )
55
+ end
35
56
36
- if @options [ :server ]
37
- server_config = config . fetch ( :servers ) . fetch ( @options [ :server ] )
38
- config . merge! ( server_config )
57
+ config
39
58
end
40
-
41
- @configuration_hash = config
42
59
end
43
60
44
- def adapter
45
- mapping = SequelRails ::DbConfig ::ADAPTER_MAPPING . invert
46
- value = configuration_hash . fetch ( :adapter )
47
- mapping [ value ] || value
61
+ # See ActiveRecord::ConnectionAdapters::AbstractAdapter.find_cmd_and_exec
62
+ def find_cmd_and_exec ( commands , *args ) # rubocop:disable Metrics/MethodLength
63
+ commands = Array ( commands )
64
+
65
+ dirs_on_path = ENV [ "PATH" ] . to_s . split ( File ::PATH_SEPARATOR )
66
+ unless ( ext = RbConfig ::CONFIG [ "EXEEXT" ] ) . empty?
67
+ commands = commands . map { |cmd | "#{ cmd } #{ ext } " }
68
+ end
69
+
70
+ full_path_command = nil
71
+ found = commands . detect do |cmd |
72
+ dirs_on_path . detect do |path |
73
+ full_path_command = File . join ( path , cmd )
74
+ begin
75
+ stat = File . stat ( full_path_command )
76
+ rescue SystemCallError
77
+ else
78
+ stat . file? && stat . executable?
79
+ end
80
+ end
81
+ end
82
+
83
+ if found
84
+ exec ( *[ full_path_command , *args ] . compact )
85
+ else
86
+ abort (
87
+ "Couldn't find database client: #{ commands . join ( ', ' ) } . Check your $PATH and try again." ,
88
+ )
89
+ end
48
90
end
49
91
50
92
def database
51
- @ options[ :database ] || configuration_hash . fetch ( :database )
93
+ options [ :database ] || pg_config . fetch ( :database )
52
94
end
53
95
end
0 commit comments