schema_test.rb 15.4 KB
Newer Older
1
require "cases/helper"
2

3
class SchemaTest < ActiveRecord::TestCase
4 5
  self.use_transactional_fixtures = false

6
  SCHEMA_NAME = 'test_schema'
7
  SCHEMA2_NAME = 'test_schema2'
8
  TABLE_NAME = 'things'
9
  CAPITALIZED_TABLE_NAME = 'Things'
10 11
  INDEX_A_NAME = 'a_index_things_on_name'
  INDEX_B_NAME = 'b_index_things_on_different_columns_in_each_schema'
12
  INDEX_C_NAME = 'c_index_full_text_search'
13
  INDEX_D_NAME = 'd_index_things_on_description_desc'
D
doabit 已提交
14
  INDEX_E_NAME = 'e_index_things_on_name_vector'
15 16 17
  INDEX_A_COLUMN = 'name'
  INDEX_B_COLUMN_S1 = 'email'
  INDEX_B_COLUMN_S2 = 'moment'
18
  INDEX_C_COLUMN = %q{(to_tsvector('english', coalesce(things.name, '')))}
19
  INDEX_D_COLUMN = 'description'
D
doabit 已提交
20
  INDEX_E_COLUMN = 'name_vector'
21 22 23
  COLUMNS = [
    'id integer',
    'name character varying(50)',
24
    'email character varying(50)',
25
    'description character varying(100)',
D
doabit 已提交
26
    'name_vector tsvector',
27 28
    'moment timestamp without time zone default now()'
  ]
29
  PK_TABLE_NAME = 'table_with_pk'
30 31
  UNMATCHED_SEQUENCE_NAME = 'unmatched_primary_key_default_value_seq'
  UNMATCHED_PK_TABLE_NAME = 'table_with_unmatched_sequence_for_pk'
32

33
  class Thing1 < ActiveRecord::Base
34
    self.table_name = "test_schema.things"
35 36 37
  end

  class Thing2 < ActiveRecord::Base
38
    self.table_name = "test_schema2.things"
39 40 41
  end

  class Thing3 < ActiveRecord::Base
42
    self.table_name = 'test_schema."things.table"'
43 44
  end

45
  class Thing4 < ActiveRecord::Base
46
    self.table_name = 'test_schema."Things"'
47 48
  end

49
  class Thing5 < ActiveRecord::Base
50
    self.table_name = 'things'
51 52
  end

53 54 55
  def setup
    @connection = ActiveRecord::Base.connection
    @connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
56
    @connection.execute "CREATE TABLE #{SCHEMA_NAME}.\"#{TABLE_NAME}.table\" (#{COLUMNS.join(',')})"
57
    @connection.execute "CREATE TABLE #{SCHEMA_NAME}.\"#{CAPITALIZED_TABLE_NAME}\" (#{COLUMNS.join(',')})"
58 59 60 61 62
    @connection.execute "CREATE SCHEMA #{SCHEMA2_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
    @connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_A_COLUMN});"
    @connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_A_COLUMN});"
    @connection.execute "CREATE INDEX #{INDEX_B_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_B_COLUMN_S1});"
    @connection.execute "CREATE INDEX #{INDEX_B_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_B_COLUMN_S2});"
63 64
    @connection.execute "CREATE INDEX #{INDEX_C_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME}  USING gin (#{INDEX_C_COLUMN});"
    @connection.execute "CREATE INDEX #{INDEX_C_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME}  USING gin (#{INDEX_C_COLUMN});"
65 66
    @connection.execute "CREATE INDEX #{INDEX_D_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_D_COLUMN} DESC);"
    @connection.execute "CREATE INDEX #{INDEX_D_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_D_COLUMN} DESC);"
D
doabit 已提交
67 68
    @connection.execute "CREATE INDEX #{INDEX_E_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME}  USING gin (#{INDEX_E_COLUMN});"
    @connection.execute "CREATE INDEX #{INDEX_E_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME}  USING gin (#{INDEX_E_COLUMN});"
69
    @connection.execute "CREATE TABLE #{SCHEMA_NAME}.#{PK_TABLE_NAME} (id serial primary key)"
70 71
    @connection.execute "CREATE SEQUENCE #{SCHEMA_NAME}.#{UNMATCHED_SEQUENCE_NAME}"
    @connection.execute "CREATE TABLE #{SCHEMA_NAME}.#{UNMATCHED_PK_TABLE_NAME} (id integer NOT NULL DEFAULT nextval('#{SCHEMA_NAME}.#{UNMATCHED_SEQUENCE_NAME}'::regclass), CONSTRAINT unmatched_pkey PRIMARY KEY (id))"
72 73
  end

G
Guo Xiang Tan 已提交
74
  teardown do
75
    @connection.execute "DROP SCHEMA #{SCHEMA2_NAME} CASCADE"
76
    @connection.execute "DROP SCHEMA #{SCHEMA_NAME} CASCADE"
77 78
  end

79
  def test_schema_names
80
    assert_equal ["public", "schema_1", "test_schema", "test_schema2"], @connection.schema_names
81 82
  end

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  def test_create_schema
    begin
      @connection.create_schema "test_schema3"
      assert @connection.schema_names.include? "test_schema3"
    ensure
      @connection.drop_schema "test_schema3"
    end
  end

  def test_raise_create_schema_with_existing_schema
    begin
      @connection.create_schema "test_schema3"
      assert_raises(ActiveRecord::StatementInvalid) do
        @connection.create_schema "test_schema3"
      end
    ensure
      @connection.drop_schema "test_schema3"
    end
  end

  def test_drop_schema
    begin
D
dfens 已提交
105
      @connection.create_schema "test_schema3"
106 107 108 109 110 111 112 113 114 115 116 117
    ensure
      @connection.drop_schema "test_schema3"
    end
    assert !@connection.schema_names.include?("test_schema3")
  end

  def test_raise_drop_schema_with_nonexisting_schema
    assert_raises(ActiveRecord::StatementInvalid) do
      @connection.drop_schema "test_schema3"
    end
  end

118 119 120 121 122 123
  def test_raise_wraped_exception_on_bad_prepare
    assert_raises(ActiveRecord::StatementInvalid) do
      @connection.exec_query "select * from developers where id = ?", 'sql', [[nil, 1]]
    end
  end

124
  def test_schema_change_with_prepared_stmt
J
Jon Leighton 已提交
125
    altered = false
126 127
    @connection.exec_query "select * from developers where id = $1", 'sql', [[nil, 1]]
    @connection.exec_query "alter table developers add column zomg int", 'sql', []
J
Jon Leighton 已提交
128
    altered = true
129 130
    @connection.exec_query "select * from developers where id = $1", 'sql', [[nil, 1]]
  ensure
J
Jon Leighton 已提交
131 132 133
    # We are not using DROP COLUMN IF EXISTS because that syntax is only
    # supported by pg 9.X
    @connection.exec_query("alter table developers drop column zomg", 'sql', []) if altered
134 135
  end

136 137 138 139 140 141 142
  def test_table_exists?
    [Thing1, Thing2, Thing3, Thing4].each do |klass|
      name = klass.table_name
      assert @connection.table_exists?(name), "'#{name}' table should exist"
    end
  end

143 144 145 146 147 148 149 150 151 152 153 154
  def test_table_exists_when_on_schema_search_path
    with_schema_search_path(SCHEMA_NAME) do
      assert(@connection.table_exists?(TABLE_NAME), "table should exist and be found")
    end
  end

  def test_table_exists_when_not_on_schema_search_path
    with_schema_search_path('PUBLIC') do
      assert(!@connection.table_exists?(TABLE_NAME), "table exists but should not be found")
    end
  end

155 156 157 158
  def test_table_exists_wrong_schema
    assert(!@connection.table_exists?("foo.things"), "table should not exist")
  end

159 160 161 162 163 164 165 166
  def test_table_exists_quoted_names
    [ %("#{SCHEMA_NAME}"."#{TABLE_NAME}"), %(#{SCHEMA_NAME}."#{TABLE_NAME}"), %(#{SCHEMA_NAME}."#{TABLE_NAME}")].each do |given|
      assert(@connection.table_exists?(given), "table should exist when specified as #{given}")
    end
    with_schema_search_path(SCHEMA_NAME) do
      given = %("#{TABLE_NAME}")
      assert(@connection.table_exists?(given), "table should exist when specified as #{given}")
    end
167 168
  end

169 170 171 172 173 174
  def test_table_exists_quoted_table
    with_schema_search_path(SCHEMA_NAME) do
        assert(@connection.table_exists?('"things.table"'), "table should exist")
    end
  end

175 176 177 178 179 180
  def test_with_schema_prefixed_table_name
    assert_nothing_raised do
      assert_equal COLUMNS, columns("#{SCHEMA_NAME}.#{TABLE_NAME}")
    end
  end

181 182 183 184 185 186
  def test_with_schema_prefixed_capitalized_table_name
    assert_nothing_raised do
      assert_equal COLUMNS, columns("#{SCHEMA_NAME}.#{CAPITALIZED_TABLE_NAME}")
    end
  end

187 188 189 190 191 192 193 194
  def test_with_schema_search_path
    assert_nothing_raised do
      with_schema_search_path(SCHEMA_NAME) do
        assert_equal COLUMNS, columns(TABLE_NAME)
      end
    end
  end

195 196 197 198 199 200 201 202 203 204 205 206 207
  def test_proper_encoding_of_table_name
    assert_equal '"table_name"', @connection.quote_table_name('table_name')
    assert_equal '"table.name"', @connection.quote_table_name('"table.name"')
    assert_equal '"schema_name"."table_name"', @connection.quote_table_name('schema_name.table_name')
    assert_equal '"schema_name"."table.name"', @connection.quote_table_name('schema_name."table.name"')
    assert_equal '"schema.name"."table_name"', @connection.quote_table_name('"schema.name".table_name')
    assert_equal '"schema.name"."table.name"', @connection.quote_table_name('"schema.name"."table.name"')
  end

  def test_classes_with_qualified_schema_name
    assert_equal 0, Thing1.count
    assert_equal 0, Thing2.count
    assert_equal 0, Thing3.count
208
    assert_equal 0, Thing4.count
209 210 211 212 213

    Thing1.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
    assert_equal 1, Thing1.count
    assert_equal 0, Thing2.count
    assert_equal 0, Thing3.count
214
    assert_equal 0, Thing4.count
215 216 217 218 219

    Thing2.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
    assert_equal 1, Thing1.count
    assert_equal 1, Thing2.count
    assert_equal 0, Thing3.count
220
    assert_equal 0, Thing4.count
221 222 223 224 225

    Thing3.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
    assert_equal 1, Thing1.count
    assert_equal 1, Thing2.count
    assert_equal 1, Thing3.count
226 227 228 229 230 231 232
    assert_equal 0, Thing4.count

    Thing4.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
    assert_equal 1, Thing1.count
    assert_equal 1, Thing2.count
    assert_equal 1, Thing3.count
    assert_equal 1, Thing4.count
233 234
  end

235
  def test_raise_on_unquoted_schema_name
236
    assert_raises(ActiveRecord::StatementInvalid) do
237 238 239 240 241
      with_schema_search_path '$user,public'
    end
  end

  def test_without_schema_search_path
242
    assert_raises(ActiveRecord::StatementInvalid) { columns(TABLE_NAME) }
243 244 245 246 247 248
  end

  def test_ignore_nil_schema_search_path
    assert_nothing_raised { with_schema_search_path nil }
  end

249 250 251 252 253 254 255 256 257 258 259 260
  def test_index_name_exists
    with_schema_search_path(SCHEMA_NAME) do
      assert @connection.index_name_exists?(TABLE_NAME, INDEX_A_NAME, true)
      assert @connection.index_name_exists?(TABLE_NAME, INDEX_B_NAME, true)
      assert @connection.index_name_exists?(TABLE_NAME, INDEX_C_NAME, true)
      assert @connection.index_name_exists?(TABLE_NAME, INDEX_D_NAME, true)
      assert @connection.index_name_exists?(TABLE_NAME, INDEX_E_NAME, true)
      assert @connection.index_name_exists?(TABLE_NAME, INDEX_E_NAME, true)
      assert_not @connection.index_name_exists?(TABLE_NAME, 'missing_index', true)
    end
  end

261
  def test_dump_indexes_for_schema_one
D
doabit 已提交
262
    do_dump_index_tests_for_schema(SCHEMA_NAME, INDEX_A_COLUMN, INDEX_B_COLUMN_S1, INDEX_D_COLUMN, INDEX_E_COLUMN)
263 264 265
  end

  def test_dump_indexes_for_schema_two
D
doabit 已提交
266
    do_dump_index_tests_for_schema(SCHEMA2_NAME, INDEX_A_COLUMN, INDEX_B_COLUMN_S2, INDEX_D_COLUMN, INDEX_E_COLUMN)
267 268
  end

269
  def test_dump_indexes_for_schema_multiple_schemas_in_search_path
D
doabit 已提交
270
    do_dump_index_tests_for_schema("public, #{SCHEMA_NAME}", INDEX_A_COLUMN, INDEX_B_COLUMN_S1, INDEX_D_COLUMN, INDEX_E_COLUMN)
271 272
  end

273 274
  def test_with_uppercase_index_name
    ActiveRecord::Base.connection.execute "CREATE INDEX \"things_Index\" ON #{SCHEMA_NAME}.things (name)"
275
    assert_nothing_raised { ActiveRecord::Base.connection.remove_index! "things", "#{SCHEMA_NAME}.things_Index"}
276 277 278

    ActiveRecord::Base.connection.execute "CREATE INDEX \"things_Index\" ON #{SCHEMA_NAME}.things (name)"
    ActiveRecord::Base.connection.schema_search_path = SCHEMA_NAME
279
    assert_nothing_raised { ActiveRecord::Base.connection.remove_index! "things", "things_Index"}
280 281 282
    ActiveRecord::Base.connection.schema_search_path = "public"
  end

283
  def test_primary_key_with_schema_specified
284 285 286 287 288
    [
      %("#{SCHEMA_NAME}"."#{PK_TABLE_NAME}"),
      %(#{SCHEMA_NAME}."#{PK_TABLE_NAME}"),
      %(#{SCHEMA_NAME}.#{PK_TABLE_NAME})
    ].each do |given|
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
      assert_equal 'id', @connection.primary_key(given), "primary key should be found when table referenced as #{given}"
    end
  end

  def test_primary_key_assuming_schema_search_path
    with_schema_search_path(SCHEMA_NAME) do
      assert_equal 'id', @connection.primary_key(PK_TABLE_NAME), "primary key should be found"
    end
  end

  def test_primary_key_raises_error_if_table_not_found_on_schema_search_path
    with_schema_search_path(SCHEMA2_NAME) do
      assert_raises(ActiveRecord::StatementInvalid) do
        @connection.primary_key(PK_TABLE_NAME)
      end
    end
  end

307 308 309
  def test_pk_and_sequence_for_with_schema_specified
    [
      %("#{SCHEMA_NAME}"."#{PK_TABLE_NAME}"),
310
      %("#{SCHEMA_NAME}"."#{UNMATCHED_PK_TABLE_NAME}")
311 312 313
    ].each do |given|
      pk, seq = @connection.pk_and_sequence_for(given)
      assert_equal 'id', pk, "primary key should be found when table referenced as #{given}"
314 315
      assert_equal "#{PK_TABLE_NAME}_id_seq", seq, "sequence name should be found when table referenced as #{given}" if given == %("#{SCHEMA_NAME}"."#{PK_TABLE_NAME}")
      assert_equal "#{UNMATCHED_SEQUENCE_NAME}", seq, "sequence name should be found when table referenced as #{given}" if given ==  %("#{SCHEMA_NAME}"."#{UNMATCHED_PK_TABLE_NAME}")
316 317 318
    end
  end

319 320
  def test_current_schema
    {
321 322
      %('$user',public)                        => 'public',
      SCHEMA_NAME                              => SCHEMA_NAME,
323 324 325 326 327 328 329
      %(#{SCHEMA2_NAME},#{SCHEMA_NAME},public) => SCHEMA2_NAME,
      %(public,#{SCHEMA2_NAME},#{SCHEMA_NAME}) => 'public'
    }.each do |given,expect|
      with_schema_search_path(given) { assert_equal expect, @connection.current_schema }
    end
  end

330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
  def test_prepared_statements_with_multiple_schemas

    @connection.schema_search_path = SCHEMA_NAME
    Thing5.create(:id => 1, :name => "thing inside #{SCHEMA_NAME}", :email => "thing1@localhost", :moment => Time.now)

    @connection.schema_search_path = SCHEMA2_NAME
    Thing5.create(:id => 1, :name => "thing inside #{SCHEMA2_NAME}", :email => "thing1@localhost", :moment => Time.now)

    @connection.schema_search_path = SCHEMA_NAME
    assert_equal 1, Thing5.count

    @connection.schema_search_path = SCHEMA2_NAME
    assert_equal 1, Thing5.count
  end

345 346 347 348 349 350 351 352 353 354 355
  def test_schema_exists?
    {
      'public'     => true,
      SCHEMA_NAME  => true,
      SCHEMA2_NAME => true,
      'darkside'   => false
    }.each do |given,expect|
      assert_equal expect, @connection.schema_exists?(given)
    end
  end

356 357 358 359 360 361 362 363 364 365 366 367 368
  private
    def columns(table_name)
      @connection.send(:column_definitions, table_name).map do |name, type, default|
        "#{name} #{type}" + (default ? " default #{default}" : '')
      end
    end

    def with_schema_search_path(schema_search_path)
      @connection.schema_search_path = schema_search_path
      yield if block_given?
    ensure
      @connection.schema_search_path = "'$user', public"
    end
369

D
doabit 已提交
370
    def do_dump_index_tests_for_schema(this_schema_name, first_index_column_name, second_index_column_name, third_index_column_name, fourth_index_column_name)
371 372
      with_schema_search_path(this_schema_name) do
        indexes = @connection.indexes(TABLE_NAME).sort_by {|i| i.name}
D
doabit 已提交
373
        assert_equal 4,indexes.size
374 375 376

        do_dump_index_assertions_for_one_index(indexes[0], INDEX_A_NAME, first_index_column_name)
        do_dump_index_assertions_for_one_index(indexes[1], INDEX_B_NAME, second_index_column_name)
377
        do_dump_index_assertions_for_one_index(indexes[2], INDEX_D_NAME, third_index_column_name)
D
doabit 已提交
378
        do_dump_index_assertions_for_one_index(indexes[3], INDEX_E_NAME, fourth_index_column_name)
379

D
doabit 已提交
380 381 382 383
        indexes.select{|i| i.name != INDEX_E_NAME}.each do |index|
           assert_equal :btree, index.using
        end
        assert_equal :gin, indexes.select{|i| i.name == INDEX_E_NAME}[0].using
384
        assert_equal :desc, indexes.select{|i| i.name == INDEX_D_NAME}[0].orders[INDEX_D_COLUMN]
385 386 387 388 389 390 391 392 393
      end
    end

    def do_dump_index_assertions_for_one_index(this_index, this_index_name, this_index_column)
      assert_equal TABLE_NAME, this_index.table
      assert_equal 1, this_index.columns.size
      assert_equal this_index_column, this_index.columns[0]
      assert_equal this_index_name, this_index.name
    end
394
end