@@ -16,18 +16,46 @@ def down
16
16
end
17
17
end
18
18
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
+
19
34
class ChangeClientsNameLength < ActiveRecord ::Migration [ 8.0 ]
20
35
def up
21
36
change_column :clients , :name , :string , limit : 30
22
37
end
23
38
end
24
39
40
+ class ChangeBlogPostsTitleLength < ActiveRecord ::Migration [ 8.0 ]
41
+ def up
42
+ change_column :blog_posts , :title , :string , limit : 30
43
+ end
44
+ end
45
+
25
46
before do
47
+ @old_verbose = ActiveRecord ::Migration . verbose
48
+ ActiveRecord ::Migration . verbose = false
49
+
26
50
CreateClientsWithUniqueIndex . new . up
51
+ CreateBlogPostsWithMultipleIndexesOnTheSameColumn . new . up
27
52
end
28
53
29
54
after do
30
55
CreateClientsWithUniqueIndex . new . down
56
+ CreateBlogPostsWithMultipleIndexesOnTheSameColumn . new . down
57
+
58
+ ActiveRecord ::Migration . verbose = @old_verbose
31
59
end
32
60
33
61
def test_index_uniqueness_is_maintained_after_column_change
@@ -47,4 +75,36 @@ def test_index_uniqueness_is_maintained_after_column_change
47
75
assert_equal indexes . first . name , "index_clients_on_name"
48
76
assert indexes . first . unique
49
77
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
50
110
end
0 commit comments