Skip to content

Commit d1ed46d

Browse files
committed
Added extra change_column with index test case
1 parent 708558f commit d1ed46d

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

test/cases/change_column_index_test_sqlserver.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,46 @@ def down
1616
end
1717
end
1818

19+
class CreateBlogPostsWithMultipleIndexesOnTheSameColumn < ActiveRecord::Migration[8.0]
20+
def up
21+
create_table :blog_posts do |t|
22+
t.string :title, limit: 15
23+
t.string :subtitle
24+
end
25+
add_index :blog_posts, :title, unique: true, where: "([blog_posts].[title] IS NOT NULL)", name: "custom_index_name"
26+
add_index :blog_posts, [:title, :subtitle], unique: true
27+
end
28+
29+
def down
30+
drop_table :blog_posts
31+
end
32+
end
33+
1934
class ChangeClientsNameLength < ActiveRecord::Migration[8.0]
2035
def up
2136
change_column :clients, :name, :string, limit: 30
2237
end
2338
end
2439

40+
class ChangeBlogPostsTitleLength < ActiveRecord::Migration[8.0]
41+
def up
42+
change_column :blog_posts, :title, :string, limit: 30
43+
end
44+
end
45+
2546
before do
47+
@old_verbose = ActiveRecord::Migration.verbose
48+
ActiveRecord::Migration.verbose = false
49+
2650
CreateClientsWithUniqueIndex.new.up
51+
CreateBlogPostsWithMultipleIndexesOnTheSameColumn.new.up
2752
end
2853

2954
after do
3055
CreateClientsWithUniqueIndex.new.down
56+
CreateBlogPostsWithMultipleIndexesOnTheSameColumn.new.down
57+
58+
ActiveRecord::Migration.verbose = @old_verbose
3159
end
3260

3361
def test_index_uniqueness_is_maintained_after_column_change
@@ -47,4 +75,36 @@ def test_index_uniqueness_is_maintained_after_column_change
4775
assert_equal indexes.first.name, "index_clients_on_name"
4876
assert indexes.first.unique
4977
end
78+
79+
def test_multiple_index_options_are_maintained_after_column_change
80+
indexes = ActiveRecord::Base.connection.indexes("blog_posts")
81+
columns = ActiveRecord::Base.connection.columns("blog_posts")
82+
assert_equal columns.find { |column| column.name == "title" }.limit, 15
83+
assert_equal indexes.size, 2
84+
85+
index_1 = indexes.find { |index| index.columns == ["title"] }
86+
assert_equal index_1.name, "custom_index_name"
87+
assert_equal index_1.where, "([blog_posts].[title] IS NOT NULL)"
88+
assert index_1.unique
89+
90+
index_2 = indexes.find { |index| index.columns == ["title", "subtitle"] }
91+
assert index_2.unique
92+
93+
94+
ChangeBlogPostsTitleLength.new.up
95+
96+
97+
indexes = ActiveRecord::Base.connection.indexes("blog_posts")
98+
columns = ActiveRecord::Base.connection.columns("blog_posts")
99+
assert_equal columns.find { |column| column.name == "title" }.limit, 30
100+
assert_equal indexes.size, 2
101+
102+
index_1 = indexes.find { |index| index.columns == ["title"] }
103+
assert_equal index_1.name, "custom_index_name"
104+
assert_equal index_1.where, "([blog_posts].[title] IS NOT NULL)"
105+
assert index_1.unique
106+
107+
index_2 = indexes.find { |index| index.columns == ["title", "subtitle"] }
108+
assert index_2.unique
109+
end
50110
end

0 commit comments

Comments
 (0)