class ActiveRecord::PostgreSQLStructureDumpTest

Public Instance Methods

setup() click to toggle source
# File activerecord/test/cases/tasks/postgresql_rake_test.rb, line 221
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
teardown() click to toggle source
# File activerecord/test/cases/tasks/postgresql_rake_test.rb, line 235
def teardown
  FileUtils.rm_f(@filename)
end
test_structure_dump() click to toggle source
# File activerecord/test/cases/tasks/postgresql_rake_test.rb, line 239
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
test_structure_dump_execution_fails() click to toggle source
# File activerecord/test/cases/tasks/postgresql_rake_test.rb, line 298
def test_structure_dump_execution_fails
  filename = "awesome-file.sql"
  Kernel.expects(:system).with("pg_dump", "-s", "-x", "-O", "-f", filename, "my-app-db").returns(nil)

  e = assert_raise(RuntimeError) do
    ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename)
  end
  assert_match("failed to execute:", e.message)
end
test_structure_dump_header_comments_removed() click to toggle source
# File activerecord/test/cases/tasks/postgresql_rake_test.rb, line 245
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
test_structure_dump_with_dump_schemas_string() click to toggle source
# File activerecord/test/cases/tasks/postgresql_rake_test.rb, line 290
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
test_structure_dump_with_extra_flags() click to toggle source
# File activerecord/test/cases/tasks/postgresql_rake_test.rb, line 254
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
test_structure_dump_with_ignore_tables() click to toggle source
# File activerecord/test/cases/tasks/postgresql_rake_test.rb, line 264
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
test_structure_dump_with_schema_search_path() click to toggle source
# File activerecord/test/cases/tasks/postgresql_rake_test.rb, line 272
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
test_structure_dump_with_schema_search_path_and_dump_schemas_all() click to toggle source
# File activerecord/test/cases/tasks/postgresql_rake_test.rb, line 280
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

Private Instance Methods

with_dump_schemas(value) { || ... } click to toggle source
# File activerecord/test/cases/tasks/postgresql_rake_test.rb, line 309
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
with_structure_dump_flags(flags) { || ... } click to toggle source
# File activerecord/test/cases/tasks/postgresql_rake_test.rb, line 317
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