5
5
class PostgresqlRenameTableTest < ActiveRecord ::PostgreSQLTestCase
6
6
def setup
7
7
@connection = ActiveRecord ::Base . connection
8
- @connection . create_table :before_rename , force : true
9
8
end
10
9
11
10
def teardown
@@ -14,14 +13,36 @@ def teardown
14
13
end
15
14
16
15
test "renaming a table also renames the primary key index" do
17
- assert_changes ( -> { num_indices_named ( "before_rename_pkey" ) } , from : 1 , to : 0 ) do
18
- assert_changes ( -> { num_indices_named ( "after_rename_pkey" ) } , from : 0 , to : 1 ) do
19
- @connection . rename_table :before_rename , :after_rename
20
- end
16
+ @connection . create_table :before_rename , force : true
17
+
18
+ assert_renames_index ( "before_rename_pkey" , "after_rename_pkey" ) do
19
+ @connection . rename_table :before_rename , :after_rename
20
+ end
21
+ end
22
+
23
+ test "renaming a table with uuid primary key and uuid_generate_v4() default also renames the primary key index" do
24
+ @connection . create_table :before_rename , force : true , id : :uuid , default : -> { "uuid_generate_v4()" }
25
+
26
+ assert_renames_index ( "before_rename_pkey" , "after_rename_pkey" ) do
27
+ @connection . rename_table :before_rename , :after_rename
28
+ end
29
+ end
30
+
31
+ test "renaming a table with uuid primary key and gen_random_uuid() default also renames the primary key index" do
32
+ @connection . create_table :before_rename , force : true , id : :uuid , default : -> { "gen_random_uuid()" }
33
+
34
+ assert_renames_index ( "before_rename_pkey" , "after_rename_pkey" ) do
35
+ @connection . rename_table :before_rename , :after_rename
21
36
end
22
37
end
23
38
24
39
private
40
+ def assert_renames_index ( from , to , &block )
41
+ assert_changes ( -> { num_indices_named ( from ) } , from : 1 , to : 0 ) do
42
+ assert_changes ( -> { num_indices_named ( to ) } , from : 0 , to : 1 , &block )
43
+ end
44
+ end
45
+
25
46
def num_indices_named ( name )
26
47
@connection . execute ( <<~SQL ) . values . length
27
48
SELECT 1 FROM "pg_index"
0 commit comments