aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/tasks/postgresql_rake_test.rb
blob: a2e968aedfabad08399824d22fa5223816158430 (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
356
357
358
359
360
361
362
require "cases/helper"
require "active_record/tasks/database_tasks"

if current_adapter?(:PostgreSQLAdapter)
  module ActiveRecord
    class PostgreSQLDBCreateTest < ActiveRecord::TestCase
      def setup
        @connection    = stub(create_database: true)
        @configuration = {
          "adapter"  => "postgresql",
          "database" => "my-app-db"
        }

        ActiveRecord::Base.stubs(:connection).returns(@connection)
        ActiveRecord::Base.stubs(:establish_connection).returns(true)

        $stdout, @original_stdout = StringIO.new, $stdout
        $stderr, @original_stderr = StringIO.new, $stderr
      end

      def teardown
        $stdout, $stderr = @original_stdout, @original_stderr
      end

      def test_establishes_connection_to_postgresql_database
        ActiveRecord::Base.expects(:establish_connection).with(
          "adapter"            => "postgresql",
          "database"           => "postgres",
          "schema_search_path" => "public"
        )

        ActiveRecord::Tasks::DatabaseTasks.create @configuration
      end

      def test_creates_database_with_default_encoding
        @connection.expects(:create_database).
          with("my-app-db", @configuration.merge("encoding" => "utf8"))

        ActiveRecord::Tasks::DatabaseTasks.create @configuration
      end

      def test_creates_database_with_given_encoding
        @connection.expects(:create_database).
          with("my-app-db", @configuration.merge("encoding" => "latin"))

        ActiveRecord::Tasks::DatabaseTasks.create @configuration.
          merge("encoding" => "latin")
      end

      def test_creates_database_with_given_collation_and_ctype
        @connection.expects(:create_database).
          with("my-app-db", @configuration.merge("encoding" => "utf8", "collation" => "ja_JP.UTF8", "ctype" => "ja_JP.UTF8"))

        ActiveRecord::Tasks::DatabaseTasks.create @configuration.
          merge("collation" => "ja_JP.UTF8", "ctype" => "ja_JP.UTF8")
      end

      def test_establishes_connection_to_new_database
        ActiveRecord::Base.expects(:establish_connection).with(@configuration)

        ActiveRecord::Tasks::DatabaseTasks.create @configuration
      end

      def test_db_create_with_error_prints_message
        ActiveRecord::Base.stubs(:establish_connection).raises(Exception)

        $stderr.stubs(:puts).returns(true)
        $stderr.expects(:puts).
          with("Couldn't create database for #{@configuration.inspect}")

        assert_raises(Exception) { ActiveRecord::Tasks::DatabaseTasks.create @configuration }
      end

      def test_when_database_created_successfully_outputs_info_to_stdout
        ActiveRecord::Tasks::DatabaseTasks.create @configuration

        assert_equal "Created database 'my-app-db'\n", $stdout.string
      end

      def test_create_when_database_exists_outputs_info_to_stderr
        ActiveRecord::Base.connection.stubs(:create_database).raises(
          ActiveRecord::Tasks::DatabaseAlreadyExists
        )

        ActiveRecord::Tasks::DatabaseTasks.create @configuration

        assert_equal "Database 'my-app-db' already exists\n", $stderr.string
      end
    end

    class PostgreSQLDBDropTest < ActiveRecord::TestCase
      def setup
        @connection    = stub(drop_database: true)
        @configuration = {
          "adapter"  => "postgresql",
          "database" => "my-app-db"
        }

        ActiveRecord::Base.stubs(:connection).returns(@connection)
        ActiveRecord::Base.stubs(:establish_connection).returns(true)

        $stdout, @original_stdout = StringIO.new, $stdout
        $stderr, @original_stderr = StringIO.new, $stderr
      end

      def teardown
        $stdout, $stderr = @original_stdout, @original_stderr
      end

      def test_establishes_connection_to_postgresql_database
        ActiveRecord::Base.expects(:establish_connection).with(
          "adapter"            => "postgresql",
          "database"           => "postgres",
          "schema_search_path" => "public"
        )

        ActiveRecord::Tasks::DatabaseTasks.drop @configuration
      end

      def test_drops_database
        @connection.expects(:drop_database).with("my-app-db")

        ActiveRecord::Tasks::DatabaseTasks.drop @configuration
      end

      def test_when_database_dropped_successfully_outputs_info_to_stdout
        ActiveRecord::Tasks::DatabaseTasks.drop @configuration

        assert_equal "Dropped database 'my-app-db'\n", $stdout.string
      end
    end

    class PostgreSQLPurgeTest < ActiveRecord::TestCase
      def setup
        @connection    = stub(create_database: true, drop_database: true)
        @configuration = {
          "adapter"  => "postgresql",
          "database" => "my-app-db"
        }

        ActiveRecord::Base.stubs(:connection).returns(@connection)
        ActiveRecord::Base.stubs(:clear_active_connections!).returns(true)
        ActiveRecord::Base.stubs(:establish_connection).returns(true)
      end

      def test_clears_active_connections
        ActiveRecord::Base.expects(:clear_active_connections!)

        ActiveRecord::Tasks::DatabaseTasks.purge @configuration
      end

      def test_establishes_connection_to_postgresql_database
        ActiveRecord::Base.expects(:establish_connection).with(
          "adapter"            => "postgresql",
          "database"           => "postgres",
          "schema_search_path" => "public"
        )

        ActiveRecord::Tasks::DatabaseTasks.purge @configuration
      end

      def test_drops_database
        @connection.expects(:drop_database).with("my-app-db")

        ActiveRecord::Tasks::DatabaseTasks.purge @configuration
      end

      def test_creates_database
        @connection.expects(:create_database).
          with("my-app-db", @configuration.merge("encoding" => "utf8"))

        ActiveRecord::Tasks::DatabaseTasks.purge @configuration
      end

      def test_establishes_connection
        ActiveRecord::Base.expects(:establish_connection).with(@configuration)

        ActiveRecord::Tasks::DatabaseTasks.purge @configuration
      end
    end

    class PostgreSQLDBCharsetTest < ActiveRecord::TestCase
      def setup
        @connection    = stub(create_database: true)
        @configuration = {
          "adapter"  => "postgresql",
          "database" => "my-app-db"
        }

        ActiveRecord::Base.stubs(:connection).returns(@connection)
        ActiveRecord::Base.stubs(:establish_connection).returns(true)
      end

      def test_db_retrieves_charset
        @connection.expects(:encoding)
        ActiveRecord::Tasks::DatabaseTasks.charset @configuration
      end
    end

    class PostgreSQLDBCollationTest < ActiveRecord::TestCase
      def setup
        @connection    = stub(create_database: true)
        @configuration = {
          "adapter"  => "postgresql",
          "database" => "my-app-db"
        }

        ActiveRecord::Base.stubs(:connection).returns(@connection)
        ActiveRecord::Base.stubs(:establish_connection).returns(true)
      end

      def test_db_retrieves_collation
        @connection.expects(:collation)
        ActiveRecord::Tasks::DatabaseTasks.collation @configuration
      end
    end

    class PostgreSQLStructureDumpTest < ActiveRecord::TestCase
      def setup
        @connection    = stub(schema_search_path: nil, structure_dump: true)
        @configuration = {
          "adapter"  => "postgresql",
          "database" => "my-app-db"
        }
        @filename = "/tmp/awesome-file.sql"
        FileUtils.touch(@filename)

        ActiveRecord::Base.stubs(:connection).returns(@connection)
        ActiveRecord::Base.stubs(:establish_connection).returns(true)
        Kernel.stubs(:system)
      end

      def teardown
        FileUtils.rm_f(@filename)
      end

      def test_structure_dump
        Kernel.expects(:system).with("pg_dump", "-s", "-x", "-O", "-f", @filename, "my-app-db").returns(true)

        ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, @filename)
      end

      def test_structure_dump_header_comments_removed
        Kernel.stubs(:system).returns(true)
        File.write(@filename, "-- header comment\n\n-- more header comment\n statement \n-- lower comment\n")

        ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, @filename)

        assert_equal [" statement \n", "-- lower comment\n"], File.readlines(@filename).first(2)
      end

      def test_structure_dump_with_extra_flags
        expected_command = ["pg_dump", "-s", "-x", "-O", "-f", @filename, "--noop", "my-app-db"]

        assert_called_with(Kernel, :system, expected_command, returns: true) do
          with_structure_dump_flags(["--noop"]) do
            ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, @filename)
          end
        end
      end

      def test_structure_dump_with_ignore_tables
        ActiveRecord::SchemaDumper.expects(:ignore_tables).returns(["foo", "bar"])

        Kernel.expects(:system).with("pg_dump", "-s", "-x", "-O", "-f", @filename, "-T", "foo", "-T", "bar", "my-app-db").returns(true)

        ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, @filename)
      end

      def test_structure_dump_with_schema_search_path
        @configuration["schema_search_path"] = "foo,bar"

        Kernel.expects(:system).with("pg_dump", "-s", "-x", "-O", "-f", @filename, "--schema=foo", "--schema=bar", "my-app-db").returns(true)

        ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, @filename)
      end

      def test_structure_dump_with_schema_search_path_and_dump_schemas_all
        @configuration["schema_search_path"] = "foo,bar"

        Kernel.expects(:system).with("pg_dump", "-s", "-x", "-O", "-f", @filename,  "my-app-db").returns(true)

        with_dump_schemas(:all) do
          ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, @filename)
        end
      end

      def test_structure_dump_with_dump_schemas_string
        Kernel.expects(:system).with("pg_dump", "-s", "-x", "-O", "-f", @filename, "--schema=foo", "--schema=bar", "my-app-db").returns(true)

        with_dump_schemas("foo,bar") do
          ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, @filename)
        end
      end

      private
        def with_dump_schemas(value, &block)
          old_dump_schemas = ActiveRecord::Base.dump_schemas
          ActiveRecord::Base.dump_schemas = value
          yield
        ensure
          ActiveRecord::Base.dump_schemas = old_dump_schemas
        end

        def with_structure_dump_flags(flags)
          old = ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags
          ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = flags
          yield
        ensure
          ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = old
        end
    end

    class PostgreSQLStructureLoadTest < ActiveRecord::TestCase
      def setup
        @connection    = stub
        @configuration = {
          "adapter"  => "postgresql",
          "database" => "my-app-db"
        }

        ActiveRecord::Base.stubs(:connection).returns(@connection)
        ActiveRecord::Base.stubs(:establish_connection).returns(true)
        Kernel.stubs(:system)
      end

      def test_structure_load
        filename = "awesome-file.sql"
        Kernel.expects(:system).with("psql", "-v", "ON_ERROR_STOP=1", "-q", "-f", filename, @configuration["database"]).returns(true)

        ActiveRecord::Tasks::DatabaseTasks.structure_load(@configuration, filename)
      end

      def test_structure_load_with_extra_flags
        filename = "awesome-file.sql"
        expected_command = ["psql", "-v", "ON_ERROR_STOP=1", "-q", "-f", filename, "--noop", @configuration["database"]]

        assert_called_with(Kernel, :system, expected_command, returns: true) do
          with_structure_load_flags(["--noop"]) do
            ActiveRecord::Tasks::DatabaseTasks.structure_load(@configuration, filename)
          end
        end
      end

      def test_structure_load_accepts_path_with_spaces
        filename = "awesome file.sql"
        Kernel.expects(:system).with("psql", "-v", "ON_ERROR_STOP=1", "-q", "-f", filename, @configuration["database"]).returns(true)

        ActiveRecord::Tasks::DatabaseTasks.structure_load(@configuration, filename)
      end

      private
        def with_structure_load_flags(flags)
          old = ActiveRecord::Tasks::DatabaseTasks.structure_load_flags
          ActiveRecord::Tasks::DatabaseTasks.structure_load_flags = flags
          yield
        ensure
          ActiveRecord::Tasks::DatabaseTasks.structure_load_flags = old
        end
    end
  end
end