|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "cases/helper" |
| 4 | + |
| 5 | +module ActiveRecord |
| 6 | + module CockroachDB |
| 7 | + class Migration |
| 8 | + class IndexTest < ActiveRecord::TestCase |
| 9 | + # These tests are identical to the ones found in Rails, save for the fact |
| 10 | + # that transactions are turned off for test runs. It is necessary to disable |
| 11 | + # transactional tests in order to assert on schema changes due to how |
| 12 | + # CockroachDB handles transactions. |
| 13 | + self.use_transactional_tests = false |
| 14 | + |
| 15 | + attr_reader :connection, :table_name |
| 16 | + |
| 17 | + def setup |
| 18 | + super |
| 19 | + @connection = ActiveRecord::Base.connection |
| 20 | + @table_name = :testings |
| 21 | + |
| 22 | + connection.create_table table_name do |t| |
| 23 | + t.column :foo, :string, limit: 100 |
| 24 | + t.column :bar, :string, limit: 100 |
| 25 | + |
| 26 | + t.string :first_name |
| 27 | + t.string :last_name, limit: 100 |
| 28 | + t.string :key, limit: 100 |
| 29 | + t.boolean :administrator |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + teardown do |
| 34 | + connection.drop_table :testings rescue nil |
| 35 | + ActiveRecord::Base.primary_key_prefix_type = nil |
| 36 | + end |
| 37 | + |
| 38 | + def test_rename_index |
| 39 | + # keep the names short to make Oracle and similar behave |
| 40 | + connection.add_index(table_name, [:foo], name: "old_idx") |
| 41 | + connection.rename_index(table_name, "old_idx", "new_idx") |
| 42 | + |
| 43 | + assert_not connection.index_name_exists?(table_name, "old_idx") |
| 44 | + assert connection.index_name_exists?(table_name, "new_idx") |
| 45 | + end |
| 46 | + |
| 47 | + def test_rename_index_too_long |
| 48 | + too_long_index_name = good_index_name + "x" |
| 49 | + # keep the names short to make Oracle and similar behave |
| 50 | + connection.add_index(table_name, [:foo], name: "old_idx") |
| 51 | + e = assert_raises(ArgumentError) { |
| 52 | + connection.rename_index(table_name, "old_idx", too_long_index_name) |
| 53 | + } |
| 54 | + assert_match(/too long; the limit is #{connection.allowed_index_name_length} characters/, e.message) |
| 55 | + |
| 56 | + assert connection.index_name_exists?(table_name, "old_idx") |
| 57 | + end |
| 58 | + |
| 59 | + def test_double_add_index |
| 60 | + connection.add_index(table_name, [:foo], name: "some_idx") |
| 61 | + assert_raises(ArgumentError) { |
| 62 | + connection.add_index(table_name, [:foo], name: "some_idx") |
| 63 | + } |
| 64 | + end |
| 65 | + |
| 66 | + def test_add_index_works_with_long_index_names |
| 67 | + connection.add_index(table_name, "foo", name: good_index_name) |
| 68 | + |
| 69 | + assert connection.index_name_exists?(table_name, good_index_name) |
| 70 | + connection.remove_index(table_name, name: good_index_name) |
| 71 | + end |
| 72 | + |
| 73 | + def test_internal_index_with_name_matching_database_limit |
| 74 | + good_index_name = "x" * connection.index_name_length |
| 75 | + connection.add_index(table_name, "foo", name: good_index_name, internal: true) |
| 76 | + |
| 77 | + assert connection.index_name_exists?(table_name, good_index_name) |
| 78 | + connection.remove_index(table_name, name: good_index_name) |
| 79 | + end |
| 80 | + |
| 81 | + def test_index_symbol_names |
| 82 | + connection.add_index table_name, :foo, name: :symbol_index_name |
| 83 | + assert connection.index_exists?(table_name, :foo, name: :symbol_index_name) |
| 84 | + |
| 85 | + connection.remove_index table_name, name: :symbol_index_name |
| 86 | + assert_not connection.index_exists?(table_name, :foo, name: :symbol_index_name) |
| 87 | + end |
| 88 | + |
| 89 | + def test_index_exists |
| 90 | + connection.add_index :testings, :foo |
| 91 | + |
| 92 | + assert connection.index_exists?(:testings, :foo) |
| 93 | + assert !connection.index_exists?(:testings, :bar) |
| 94 | + end |
| 95 | + |
| 96 | + def test_index_exists_on_multiple_columns |
| 97 | + connection.add_index :testings, [:foo, :bar] |
| 98 | + |
| 99 | + assert connection.index_exists?(:testings, [:foo, :bar]) |
| 100 | + end |
| 101 | + |
| 102 | + def test_index_exists_with_custom_name_checks_columns |
| 103 | + connection.add_index :testings, [:foo, :bar], name: "my_index" |
| 104 | + assert connection.index_exists?(:testings, [:foo, :bar], name: "my_index") |
| 105 | + assert_not connection.index_exists?(:testings, [:foo], name: "my_index") |
| 106 | + end |
| 107 | + |
| 108 | + def test_unique_index_exists |
| 109 | + connection.add_index :testings, :foo, unique: true |
| 110 | + |
| 111 | + assert connection.index_exists?(:testings, :foo, unique: true) |
| 112 | + end |
| 113 | + |
| 114 | + def test_named_index_exists |
| 115 | + connection.add_index :testings, :foo, name: "custom_index_name" |
| 116 | + |
| 117 | + assert connection.index_exists?(:testings, :foo) |
| 118 | + assert connection.index_exists?(:testings, :foo, name: "custom_index_name") |
| 119 | + assert !connection.index_exists?(:testings, :foo, name: "other_index_name") |
| 120 | + end |
| 121 | + |
| 122 | + def test_remove_named_index |
| 123 | + connection.add_index :testings, :foo, name: "custom_index_name" |
| 124 | + |
| 125 | + assert connection.index_exists?(:testings, :foo) |
| 126 | + connection.remove_index :testings, :foo |
| 127 | + assert !connection.index_exists?(:testings, :foo) |
| 128 | + end |
| 129 | + |
| 130 | + def test_add_index_attribute_length_limit |
| 131 | + connection.add_index :testings, [:foo, :bar], length: { foo: 10, bar: nil } |
| 132 | + |
| 133 | + assert connection.index_exists?(:testings, [:foo, :bar]) |
| 134 | + end |
| 135 | + |
| 136 | + def test_add_index |
| 137 | + connection.add_index("testings", "last_name") |
| 138 | + connection.remove_index("testings", "last_name") |
| 139 | + |
| 140 | + connection.add_index("testings", ["last_name", "first_name"]) |
| 141 | + connection.remove_index("testings", column: ["last_name", "first_name"]) |
| 142 | + |
| 143 | + # Oracle adapter cannot have specified index name larger than 30 characters |
| 144 | + # Oracle adapter is shortening index name when just column list is given |
| 145 | + unless current_adapter?(:OracleAdapter) |
| 146 | + connection.add_index("testings", ["last_name", "first_name"]) |
| 147 | + connection.remove_index("testings", name: :index_testings_on_last_name_and_first_name) |
| 148 | + connection.add_index("testings", ["last_name", "first_name"]) |
| 149 | + connection.remove_index("testings", "last_name_and_first_name") |
| 150 | + end |
| 151 | + connection.add_index("testings", ["last_name", "first_name"]) |
| 152 | + connection.remove_index("testings", ["last_name", "first_name"]) |
| 153 | + |
| 154 | + connection.add_index("testings", ["last_name"], length: 10) |
| 155 | + connection.remove_index("testings", "last_name") |
| 156 | + |
| 157 | + connection.add_index("testings", ["last_name"], length: { last_name: 10 }) |
| 158 | + connection.remove_index("testings", ["last_name"]) |
| 159 | + |
| 160 | + connection.add_index("testings", ["last_name", "first_name"], length: 10) |
| 161 | + connection.remove_index("testings", ["last_name", "first_name"]) |
| 162 | + |
| 163 | + connection.add_index("testings", ["last_name", "first_name"], length: { last_name: 10, first_name: 20 }) |
| 164 | + connection.remove_index("testings", ["last_name", "first_name"]) |
| 165 | + |
| 166 | + connection.add_index("testings", ["key"], name: "key_idx", unique: true) |
| 167 | + connection.remove_index("testings", name: "key_idx", unique: true) |
| 168 | + |
| 169 | + connection.add_index("testings", %w(last_name first_name administrator), name: "named_admin") |
| 170 | + connection.remove_index("testings", name: "named_admin") |
| 171 | + |
| 172 | + # Test support for index sort order |
| 173 | + connection.add_index("testings", ["last_name"], order: { last_name: :desc }) |
| 174 | + connection.remove_index("testings", ["last_name"]) |
| 175 | + connection.add_index("testings", ["last_name", "first_name"], order: { last_name: :desc }) |
| 176 | + connection.remove_index("testings", ["last_name", "first_name"]) |
| 177 | + connection.add_index("testings", ["last_name", "first_name"], order: { last_name: :desc, first_name: :asc }) |
| 178 | + connection.remove_index("testings", ["last_name", "first_name"]) |
| 179 | + connection.add_index("testings", ["last_name", "first_name"], order: :desc) |
| 180 | + connection.remove_index("testings", ["last_name", "first_name"]) |
| 181 | + end |
| 182 | + |
| 183 | + def test_add_partial_index |
| 184 | + connection.add_index("testings", "last_name", where: "first_name = 'john doe'") |
| 185 | + assert connection.index_exists?("testings", "last_name") |
| 186 | + |
| 187 | + connection.remove_index("testings", "last_name") |
| 188 | + assert_not connection.index_exists?("testings", "last_name") |
| 189 | + end |
| 190 | + |
| 191 | + private |
| 192 | + def good_index_name |
| 193 | + "x" * connection.index_name_length |
| 194 | + end |
| 195 | + |
| 196 | + end |
| 197 | + end |
| 198 | + end |
| 199 | +end |
0 commit comments