aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/adapters/postgresql/create_unlogged_tables_test.rb
blob: a02bae145396aebed242ed7ef057279cd7caaad6 (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
# frozen_string_literal: true

require "cases/helper"
require "support/schema_dumping_helper"

class UnloggedTablesTest < ActiveRecord::PostgreSQLTestCase
  include SchemaDumpingHelper

  TABLE_NAME = "things"
  LOGGED_FIELD = "relpersistence"
  LOGGED_QUERY = "SELECT #{LOGGED_FIELD} FROM pg_class WHERE relname = '#{TABLE_NAME}'"
  LOGGED = "p"
  UNLOGGED = "u"
  TEMPORARY = "t"

  class Thing < ActiveRecord::Base
    self.table_name = TABLE_NAME
  end

  def setup
    @connection = ActiveRecord::Base.connection
    ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables = false
  end

  teardown do
    @connection.drop_table TABLE_NAME, if_exists: true
    ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables = false
  end

  def test_logged_by_default
    @connection.create_table(TABLE_NAME) do |t|
    end
    assert_equal @connection.execute(LOGGED_QUERY).first[LOGGED_FIELD], LOGGED
  end

  def test_unlogged_in_test_environment_when_unlogged_setting_enabled
    ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables = true

    @connection.create_table(TABLE_NAME) do |t|
    end
    assert_equal @connection.execute(LOGGED_QUERY).first[LOGGED_FIELD], UNLOGGED
  end

  def test_not_included_in_schema_dump
    ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables = true

    @connection.create_table(TABLE_NAME) do |t|
    end
    assert_no_match(/unlogged/i, dump_table_schema(TABLE_NAME))
  end

  def test_not_changed_in_change_table
    @connection.create_table(TABLE_NAME) do |t|
    end

    ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables = true

    @connection.change_table(TABLE_NAME) do |t|
      t.column :name, :string
    end
    assert_equal @connection.execute(LOGGED_QUERY).first[LOGGED_FIELD], LOGGED
  end

  def test_gracefully_handles_temporary_tables
    @connection.create_table(TABLE_NAME, temporary: true) do |t|
    end

    # Temporary tables are already unlogged, though this query results in a
    # different result ("t" vs. "u"). This test is really just checking that we
    # didn't try to run `CREATE TEMPORARY UNLOGGED TABLE`, which would result in
    # a PostgreSQL error.
    assert_equal @connection.execute(LOGGED_QUERY).first[LOGGED_FIELD], TEMPORARY
  end
end