aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/commands/dbconsole_test.rb
blob: 6ad96b28c7a5a81421067a032ba71a061ec23085 (plain) (blame)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# frozen_string_literal: true

require "abstract_unit"
require "minitest/mock"
require "rails/command"
require "rails/commands/dbconsole/dbconsole_command"

class Rails::DBConsoleTest < ActiveSupport::TestCase
  def setup
    Rails::DBConsole.const_set("APP_PATH", "rails/all")
  end

  def teardown
    Rails::DBConsole.send(:remove_const, "APP_PATH")
    %w[PGUSER PGHOST PGPORT PGPASSWORD DATABASE_URL].each { |key| ENV.delete(key) }
  end

  def test_config_with_db_config_only
    config_sample = {
      "test" => {
        "adapter" => "sqlite3",
        "host" => "localhost",
        "port" => "9000",
        "database" => "foo_test",
        "user" => "foo",
        "password" => "bar",
        "pool" => "5",
        "timeout" => "3000"
      }
    }
    app_db_config(config_sample) do
      assert_equal config_sample["test"], Rails::DBConsole.new.config
    end
  end

  def test_config_with_no_db_config
    app_db_config(nil) do
      assert_raise(ActiveRecord::AdapterNotSpecified) {
        Rails::DBConsole.new.config
      }
    end
  end

  def test_config_with_database_url_only
    ENV["DATABASE_URL"] = "postgresql://foo:bar@localhost:9000/foo_test?pool=5&timeout=3000"
    expected = {
      "adapter"  => "postgresql",
      "host"     => "localhost",
      "port"     => 9000,
      "database" => "foo_test",
      "username" => "foo",
      "password" => "bar",
      "pool"     => "5",
      "timeout"  => "3000"
    }.sort

    app_db_config(nil) do
      assert_equal expected, Rails::DBConsole.new.config.sort
    end
  end

  def test_config_choose_database_url_if_exists
    host = "database-url-host.com"
    ENV["DATABASE_URL"] = "postgresql://foo:bar@#{host}:9000/foo_test?pool=5&timeout=3000"
    sample_config = {
      "test" => {
        "adapter"  => "postgresql",
        "host"     => "not-the-#{host}",
        "port"     => 9000,
        "database" => "foo_test",
        "username" => "foo",
        "password" => "bar",
        "pool"     => "5",
        "timeout"  => "3000"
      }
    }
    app_db_config(sample_config) do
      assert_equal host, Rails::DBConsole.new.config["host"]
    end
  end

  def test_env
    assert_equal "test", Rails::DBConsole.new.environment

    ENV["RAILS_ENV"] = nil
    ENV["RACK_ENV"] = nil

    Rails.stub(:respond_to?, false) do
      assert_equal "development", Rails::DBConsole.new.environment

      ENV["RACK_ENV"] = "rack_env"
      assert_equal "rack_env", Rails::DBConsole.new.environment

      ENV["RAILS_ENV"] = "rails_env"
      assert_equal "rails_env", Rails::DBConsole.new.environment
    end
  ensure
    ENV["RAILS_ENV"] = "test"
    ENV["RACK_ENV"] = nil
  end

  def test_rails_env_is_development_when_argument_is_dev
    assert_deprecated do
      stub_available_environments([ "development", "test" ]) do
        assert_match("development", parse_arguments([ "dev" ])[:environment])
      end
    end
  end

  def test_rails_env_is_development_when_environment_option_is_dev
    stub_available_environments([ "development", "test" ]) do
      assert_match("development", parse_arguments([ "-e", "dev" ])[:environment])
    end
  end

  def test_rails_env_is_dev_when_argument_is_dev_and_dev_env_is_present
    assert_deprecated do
      stub_available_environments([ "dev" ]) do
        assert_match("dev", parse_arguments([ "dev" ])[:environment])
      end
    end
  end

  def test_mysql
    start(adapter: "mysql2", database: "db")
    assert !aborted
    assert_equal [%w[mysql mysql5], "db"], dbconsole.find_cmd_and_exec_args
  end

  def test_mysql_full
    start(adapter: "mysql2", database: "db", host: "locahost", port: 1234, socket: "socket", username: "user", password: "qwerty", encoding: "UTF-8")
    assert !aborted
    assert_equal [%w[mysql mysql5], "--host=locahost", "--port=1234", "--socket=socket", "--user=user", "--default-character-set=UTF-8", "-p", "db"], dbconsole.find_cmd_and_exec_args
  end

  def test_mysql_include_password
    start({ adapter: "mysql2", database: "db", username: "user", password: "qwerty" }, ["-p"])
    assert !aborted
    assert_equal [%w[mysql mysql5], "--user=user", "--password=qwerty", "db"], dbconsole.find_cmd_and_exec_args
  end

  def test_postgresql
    start(adapter: "postgresql", database: "db")
    assert !aborted
    assert_equal ["psql", "db"], dbconsole.find_cmd_and_exec_args
  end

  def test_postgresql_full
    start(adapter: "postgresql", database: "db", username: "user", password: "q1w2e3", host: "host", port: 5432)
    assert !aborted
    assert_equal ["psql", "db"], dbconsole.find_cmd_and_exec_args
    assert_equal "user", ENV["PGUSER"]
    assert_equal "host", ENV["PGHOST"]
    assert_equal "5432", ENV["PGPORT"]
    assert_not_equal "q1w2e3", ENV["PGPASSWORD"]
  end

  def test_postgresql_include_password
    start({ adapter: "postgresql", database: "db", username: "user", password: "q1w2e3" }, ["-p"])
    assert !aborted
    assert_equal ["psql", "db"], dbconsole.find_cmd_and_exec_args
    assert_equal "user", ENV["PGUSER"]
    assert_equal "q1w2e3", ENV["PGPASSWORD"]
  end

  def test_sqlite3
    start(adapter: "sqlite3", database: "db.sqlite3")
    assert !aborted
    assert_equal ["sqlite3", Rails.root.join("db.sqlite3").to_s], dbconsole.find_cmd_and_exec_args
  end

  def test_sqlite3_mode
    start({ adapter: "sqlite3", database: "db.sqlite3" }, ["--mode", "html"])
    assert !aborted
    assert_equal ["sqlite3", "-html", Rails.root.join("db.sqlite3").to_s], dbconsole.find_cmd_and_exec_args
  end

  def test_sqlite3_header
    start({ adapter: "sqlite3", database: "db.sqlite3" }, ["--header"])
    assert_equal ["sqlite3", "-header", Rails.root.join("db.sqlite3").to_s], dbconsole.find_cmd_and_exec_args
  end

  def test_sqlite3_db_absolute_path
    start(adapter: "sqlite3", database: "/tmp/db.sqlite3")
    assert !aborted
    assert_equal ["sqlite3", "/tmp/db.sqlite3"], dbconsole.find_cmd_and_exec_args
  end

  def test_sqlite3_db_without_defined_rails_root
    Rails.stub(:respond_to?, false) do
      start(adapter: "sqlite3", database: "config/db.sqlite3")
      assert !aborted
      assert_equal ["sqlite3", Rails.root.join("../config/db.sqlite3").to_s], dbconsole.find_cmd_and_exec_args
    end
  end

  def test_oracle
    start(adapter: "oracle", database: "db", username: "user", password: "secret")
    assert !aborted
    assert_equal ["sqlplus", "user@db"], dbconsole.find_cmd_and_exec_args
  end

  def test_oracle_include_password
    start({ adapter: "oracle", database: "db", username: "user", password: "secret" }, ["-p"])
    assert !aborted
    assert_equal ["sqlplus", "user/secret@db"], dbconsole.find_cmd_and_exec_args
  end

  def test_sqlserver
    start(adapter: "sqlserver", database: "db", username: "user", password: "secret", host: "localhost", port: 1433)
    assert_not aborted
    assert_equal ["sqsh", "-D", "db", "-U", "user", "-P", "secret", "-S", "localhost:1433"], dbconsole.find_cmd_and_exec_args
  end

  def test_unknown_command_line_client
    start(adapter: "unknown", database: "db")
    assert aborted
    assert_match(/Unknown command-line client for db/, output)
  end

  def test_primary_is_automatically_picked_with_3_level_configuration
    sample_config = {
      "test" => {
        "primary" => {
          "adapter" => "postgresql"
        }
      }
    }

    app_db_config(sample_config) do
      assert_equal "postgresql", Rails::DBConsole.new.config["adapter"]
    end
  end

  def test_specifying_a_custom_connection_and_environment
    stub_available_environments(["development"]) do
      dbconsole = parse_arguments(["-c", "custom", "-e", "development"])

      assert_equal "development", dbconsole[:environment]
      assert_equal "custom", dbconsole.connection
    end
  end

  def test_specifying_a_missing_connection
    app_db_config({}) do
      e = assert_raises(ActiveRecord::AdapterNotSpecified) do
        Rails::Command.invoke(:dbconsole, ["-c", "i_do_not_exist"])
      end

      assert_includes e.message, "'i_do_not_exist' connection is not configured."
    end
  end

  def test_specifying_a_missing_environment
    app_db_config({}) do
      e = assert_raises(ActiveRecord::AdapterNotSpecified) do
        Rails::Command.invoke(:dbconsole)
      end

      assert_includes e.message, "'test' database is not configured."
    end
  end

  def test_print_help_short
    stdout = capture(:stdout) do
      Rails::Command.invoke(:dbconsole, ["-h"])
    end
    assert_match(/bin\/rails dbconsole \[environment\]/, stdout)
  end

  def test_print_help_long
    stdout = capture(:stdout) do
      Rails::Command.invoke(:dbconsole, ["--help"])
    end
    assert_match(/bin\/rails dbconsole \[environment\]/, stdout)
  end

  attr_reader :aborted, :output
  private :aborted, :output

  private

    def app_db_config(results)
      Rails.application.config.stub(:database_configuration, results || {}) do
        yield
      end
    end

    def make_dbconsole
      Class.new(Rails::DBConsole) do
        attr_reader :find_cmd_and_exec_args

        def find_cmd_and_exec(*args)
          @find_cmd_and_exec_args = args
        end
      end
    end

    attr_reader :dbconsole

    def start(config = {}, argv = [])
      @dbconsole = make_dbconsole.new(parse_arguments(argv))
      @dbconsole.stub(:config, config.stringify_keys) do
        capture_abort { @dbconsole.start }
      end
    end

    def capture_abort
      @aborted = false
      @output = capture(:stderr) do
        begin
          yield
        rescue SystemExit
          @aborted = true
        end
      end
    end

    def stub_available_environments(environments)
      Rails::Command::DbconsoleCommand.class_eval do
        alias_method :old_environments, :available_environments

        define_method :available_environments do
          environments
        end
      end

      yield
    ensure
      Rails::Command::DbconsoleCommand.class_eval do
        undef_method :available_environments
        alias_method :available_environments, :old_environments
        undef_method :old_environments
      end
    end

    def parse_arguments(args)
      Rails::Command::DbconsoleCommand.class_eval do
        alias_method :old_perform, :perform
        define_method(:perform) do
          extract_environment_option_from_argument

          options
        end
      end

      Rails::Command.invoke(:dbconsole, args)
    ensure
      Rails::Command::DbconsoleCommand.class_eval do
        undef_method :perform
        alias_method :perform, :old_perform
        undef_method :old_perform
      end
    end
end