2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
2012-01-11 20:14:44 -05:00
|
|
|
|
|
|
|
module ActiveRecord
|
|
|
|
class Migration
|
|
|
|
class IndexTest < ActiveRecord::TestCase
|
|
|
|
attr_reader :connection, :table_name
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
@connection = ActiveRecord::Base.connection
|
|
|
|
@table_name = :testings
|
|
|
|
|
|
|
|
connection.create_table table_name do |t|
|
2016-08-06 13:37:57 -04:00
|
|
|
t.column :foo, :string, limit: 100
|
|
|
|
t.column :bar, :string, limit: 100
|
2012-01-11 20:16:16 -05:00
|
|
|
|
|
|
|
t.string :first_name
|
2016-08-06 13:37:57 -04:00
|
|
|
t.string :last_name, limit: 100
|
|
|
|
t.string :key, limit: 100
|
2012-01-11 20:16:16 -05:00
|
|
|
t.boolean :administrator
|
2012-01-11 20:14:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-14 00:35:58 -04:00
|
|
|
teardown do
|
2012-01-11 20:14:44 -05:00
|
|
|
connection.drop_table :testings rescue nil
|
|
|
|
ActiveRecord::Base.primary_key_prefix_type = nil
|
|
|
|
end
|
|
|
|
|
2014-05-16 14:05:45 -04:00
|
|
|
def test_rename_index
|
|
|
|
# keep the names short to make Oracle and similar behave
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.add_index(table_name, [:foo], name: "old_idx")
|
2016-08-06 12:26:20 -04:00
|
|
|
connection.rename_index(table_name, "old_idx", "new_idx")
|
2014-05-16 14:05:45 -04:00
|
|
|
|
2017-07-17 17:58:56 -04:00
|
|
|
assert_not connection.index_name_exists?(table_name, "old_idx")
|
|
|
|
assert connection.index_name_exists?(table_name, "new_idx")
|
2014-05-16 14:05:45 -04:00
|
|
|
end
|
2012-01-11 20:14:44 -05:00
|
|
|
|
2014-11-20 20:37:54 -05:00
|
|
|
def test_rename_index_too_long
|
2016-08-06 12:26:20 -04:00
|
|
|
too_long_index_name = good_index_name + "x"
|
2014-11-20 20:37:54 -05:00
|
|
|
# keep the names short to make Oracle and similar behave
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.add_index(table_name, [:foo], name: "old_idx")
|
2014-11-20 20:37:54 -05:00
|
|
|
e = assert_raises(ArgumentError) {
|
2016-08-06 12:26:20 -04:00
|
|
|
connection.rename_index(table_name, "old_idx", too_long_index_name)
|
2014-11-20 20:37:54 -05:00
|
|
|
}
|
|
|
|
assert_match(/too long; the limit is #{connection.allowed_index_name_length} characters/, e.message)
|
|
|
|
|
2016-10-02 18:03:11 -04:00
|
|
|
assert connection.index_name_exists?(table_name, "old_idx")
|
2014-11-20 20:37:54 -05:00
|
|
|
end
|
|
|
|
|
2014-05-16 14:05:45 -04:00
|
|
|
def test_double_add_index
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.add_index(table_name, [:foo], name: "some_idx")
|
2014-05-16 14:05:45 -04:00
|
|
|
assert_raises(ArgumentError) {
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.add_index(table_name, [:foo], name: "some_idx")
|
2014-05-16 14:05:45 -04:00
|
|
|
}
|
|
|
|
end
|
2012-01-11 20:14:44 -05:00
|
|
|
|
2014-05-16 14:05:45 -04:00
|
|
|
def test_remove_nonexistent_index
|
|
|
|
assert_raise(ArgumentError) { connection.remove_index(table_name, "no_such_index") }
|
2012-01-11 20:14:44 -05:00
|
|
|
end
|
|
|
|
|
2012-12-27 19:27:26 -05:00
|
|
|
def test_add_index_works_with_long_index_names
|
|
|
|
connection.add_index(table_name, "foo", name: good_index_name)
|
|
|
|
|
2016-10-02 18:03:11 -04:00
|
|
|
assert connection.index_name_exists?(table_name, good_index_name)
|
2012-12-27 19:27:26 -05:00
|
|
|
connection.remove_index(table_name, name: good_index_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_index_does_not_accept_too_long_index_names
|
2016-08-06 12:26:20 -04:00
|
|
|
too_long_index_name = good_index_name + "x"
|
2012-01-11 20:14:44 -05:00
|
|
|
|
2012-12-27 19:27:26 -05:00
|
|
|
e = assert_raises(ArgumentError) {
|
|
|
|
connection.add_index(table_name, "foo", name: too_long_index_name)
|
2012-01-11 20:14:44 -05:00
|
|
|
}
|
2013-02-20 22:51:39 -05:00
|
|
|
assert_match(/too long; the limit is #{connection.allowed_index_name_length} characters/, e.message)
|
2012-01-11 20:14:44 -05:00
|
|
|
|
2016-10-02 18:03:11 -04:00
|
|
|
assert_not connection.index_name_exists?(table_name, too_long_index_name)
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.add_index(table_name, "foo", name: good_index_name)
|
2012-12-27 19:27:26 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_internal_index_with_name_matching_database_limit
|
2016-08-06 12:26:20 -04:00
|
|
|
good_index_name = "x" * connection.index_name_length
|
2012-12-27 19:27:26 -05:00
|
|
|
connection.add_index(table_name, "foo", name: good_index_name, internal: true)
|
2012-01-11 20:14:44 -05:00
|
|
|
|
2016-10-02 18:03:11 -04:00
|
|
|
assert connection.index_name_exists?(table_name, good_index_name)
|
2012-12-27 19:27:26 -05:00
|
|
|
connection.remove_index(table_name, name: good_index_name)
|
2012-01-11 20:14:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_index_symbol_names
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.add_index table_name, :foo, name: :symbol_index_name
|
|
|
|
assert connection.index_exists?(table_name, :foo, name: :symbol_index_name)
|
2012-01-11 20:14:44 -05:00
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.remove_index table_name, name: :symbol_index_name
|
|
|
|
assert_not connection.index_exists?(table_name, :foo, name: :symbol_index_name)
|
2012-01-11 20:14:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_index_exists
|
|
|
|
connection.add_index :testings, :foo
|
|
|
|
|
|
|
|
assert connection.index_exists?(:testings, :foo)
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not connection.index_exists?(:testings, :bar)
|
2012-01-11 20:14:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_index_exists_on_multiple_columns
|
|
|
|
connection.add_index :testings, [:foo, :bar]
|
|
|
|
|
|
|
|
assert connection.index_exists?(:testings, [:foo, :bar])
|
|
|
|
end
|
|
|
|
|
2014-08-13 05:44:58 -04:00
|
|
|
def test_index_exists_with_custom_name_checks_columns
|
|
|
|
connection.add_index :testings, [:foo, :bar], name: "my_index"
|
|
|
|
assert connection.index_exists?(:testings, [:foo, :bar], name: "my_index")
|
|
|
|
assert_not connection.index_exists?(:testings, [:foo], name: "my_index")
|
|
|
|
end
|
|
|
|
|
2012-11-02 16:58:27 -04:00
|
|
|
def test_valid_index_options
|
|
|
|
assert_raise ArgumentError do
|
|
|
|
connection.add_index :testings, :foo, unqiue: true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-11 20:14:44 -05:00
|
|
|
def test_unique_index_exists
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.add_index :testings, :foo, unique: true
|
2012-01-11 20:14:44 -05:00
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
assert connection.index_exists?(:testings, :foo, unique: true)
|
2012-01-11 20:14:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_named_index_exists
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.add_index :testings, :foo, name: "custom_index_name"
|
2012-01-11 20:14:44 -05:00
|
|
|
|
2015-03-22 07:14:51 -04:00
|
|
|
assert connection.index_exists?(:testings, :foo)
|
2016-08-06 13:37:57 -04:00
|
|
|
assert connection.index_exists?(:testings, :foo, name: "custom_index_name")
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not connection.index_exists?(:testings, :foo, name: "other_index_name")
|
2012-01-11 20:14:44 -05:00
|
|
|
end
|
2012-01-11 20:16:16 -05:00
|
|
|
|
2015-04-03 04:55:53 -04:00
|
|
|
def test_remove_named_index
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.add_index :testings, :foo, name: "custom_index_name"
|
2015-04-03 04:55:53 -04:00
|
|
|
|
|
|
|
assert connection.index_exists?(:testings, :foo)
|
|
|
|
connection.remove_index :testings, :foo
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not connection.index_exists?(:testings, :foo)
|
2012-01-11 20:14:44 -05:00
|
|
|
end
|
2012-01-11 20:16:16 -05:00
|
|
|
|
2012-01-24 16:22:24 -05:00
|
|
|
def test_add_index_attribute_length_limit
|
2016-08-16 03:30:11 -04:00
|
|
|
connection.add_index :testings, [:foo, :bar], length: { foo: 10, bar: nil }
|
2012-01-24 16:22:24 -05:00
|
|
|
|
|
|
|
assert connection.index_exists?(:testings, [:foo, :bar])
|
|
|
|
end
|
|
|
|
|
2012-01-11 20:16:16 -05:00
|
|
|
def test_add_index
|
|
|
|
connection.add_index("testings", "last_name")
|
|
|
|
connection.remove_index("testings", "last_name")
|
|
|
|
|
2014-05-16 14:05:45 -04:00
|
|
|
connection.add_index("testings", ["last_name", "first_name"])
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.remove_index("testings", column: ["last_name", "first_name"])
|
2014-05-16 14:05:45 -04:00
|
|
|
|
|
|
|
# Oracle adapter cannot have specified index name larger than 30 characters
|
|
|
|
# Oracle adapter is shortening index name when just column list is given
|
|
|
|
unless current_adapter?(:OracleAdapter)
|
2012-01-11 20:16:16 -05:00
|
|
|
connection.add_index("testings", ["last_name", "first_name"])
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.remove_index("testings", name: :index_testings_on_last_name_and_first_name)
|
2012-01-11 20:16:16 -05:00
|
|
|
connection.add_index("testings", ["last_name", "first_name"])
|
2014-05-16 14:05:45 -04:00
|
|
|
connection.remove_index("testings", "last_name_and_first_name")
|
|
|
|
end
|
|
|
|
connection.add_index("testings", ["last_name", "first_name"])
|
|
|
|
connection.remove_index("testings", ["last_name", "first_name"])
|
2012-01-11 20:16:16 -05:00
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.add_index("testings", ["last_name"], length: 10)
|
2014-05-16 14:05:45 -04:00
|
|
|
connection.remove_index("testings", "last_name")
|
2012-01-11 20:16:16 -05:00
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
connection.add_index("testings", ["last_name"], length: { last_name: 10 })
|
2014-05-16 14:05:45 -04:00
|
|
|
connection.remove_index("testings", ["last_name"])
|
2012-01-11 20:16:16 -05:00
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.add_index("testings", ["last_name", "first_name"], length: 10)
|
2014-05-16 14:05:45 -04:00
|
|
|
connection.remove_index("testings", ["last_name", "first_name"])
|
2012-01-11 20:16:16 -05:00
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
connection.add_index("testings", ["last_name", "first_name"], length: { last_name: 10, first_name: 20 })
|
2014-05-16 14:05:45 -04:00
|
|
|
connection.remove_index("testings", ["last_name", "first_name"])
|
2012-01-11 20:16:16 -05:00
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.add_index("testings", ["key"], name: "key_idx", unique: true)
|
|
|
|
connection.remove_index("testings", name: "key_idx", unique: true)
|
2012-01-11 20:16:16 -05:00
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.add_index("testings", %w(last_name first_name administrator), name: "named_admin")
|
|
|
|
connection.remove_index("testings", name: "named_admin")
|
2012-01-11 20:16:16 -05:00
|
|
|
|
|
|
|
# Selected adapters support index sort order
|
2015-12-15 17:01:30 -05:00
|
|
|
if current_adapter?(:SQLite3Adapter, :Mysql2Adapter, :PostgreSQLAdapter)
|
2016-08-16 03:30:11 -04:00
|
|
|
connection.add_index("testings", ["last_name"], order: { last_name: :desc })
|
2012-01-11 20:16:16 -05:00
|
|
|
connection.remove_index("testings", ["last_name"])
|
2016-08-16 03:30:11 -04:00
|
|
|
connection.add_index("testings", ["last_name", "first_name"], order: { last_name: :desc })
|
2012-01-11 20:16:16 -05:00
|
|
|
connection.remove_index("testings", ["last_name", "first_name"])
|
2016-08-16 03:30:11 -04:00
|
|
|
connection.add_index("testings", ["last_name", "first_name"], order: { last_name: :desc, first_name: :asc })
|
2012-01-11 20:16:16 -05:00
|
|
|
connection.remove_index("testings", ["last_name", "first_name"])
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.add_index("testings", ["last_name", "first_name"], order: :desc)
|
2012-01-11 20:16:16 -05:00
|
|
|
connection.remove_index("testings", ["last_name", "first_name"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-08 11:18:54 -05:00
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
|
|
|
def test_add_partial_index
|
2016-08-06 13:37:57 -04:00
|
|
|
connection.add_index("testings", "last_name", where: "first_name = 'john doe'")
|
2013-11-08 11:18:54 -05:00
|
|
|
assert connection.index_exists?("testings", "last_name")
|
2012-02-09 16:04:07 -05:00
|
|
|
|
2013-11-08 11:18:54 -05:00
|
|
|
connection.remove_index("testings", "last_name")
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not connection.index_exists?("testings", "last_name")
|
2013-11-08 11:18:54 -05:00
|
|
|
end
|
2012-02-09 16:04:07 -05:00
|
|
|
end
|
2012-12-27 19:27:26 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
def good_index_name
|
2016-08-06 12:26:20 -04:00
|
|
|
"x" * connection.allowed_index_name_length
|
2012-12-27 19:27:26 -05:00
|
|
|
end
|
2012-01-11 20:14:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|