@@ -1062,6 +1062,83 @@ defmodule AshPostgres.MigrationGeneratorTest do
1062
1062
assert File . read! ( file2 ) =~ ~S[ rename table(:posts), :subject, to: :title]
1063
1063
end
1064
1064
1065
+ test "when renaming a field with an identity, it asks which field you are renaming it to, and updates indexes in the correct order" do
1066
+ defposts do
1067
+ identities do
1068
+ identity ( :subject , [ :subject ] )
1069
+ end
1070
+
1071
+ attributes do
1072
+ uuid_primary_key ( :id )
1073
+ attribute ( :subject , :string , public?: true )
1074
+ end
1075
+ end
1076
+
1077
+ defdomain ( [ Post ] )
1078
+
1079
+ send ( self ( ) , { :mix_shell_input , :yes? , true } )
1080
+ send ( self ( ) , { :mix_shell_input , :prompt , "subject" } )
1081
+
1082
+ AshPostgres.MigrationGenerator . generate ( Domain ,
1083
+ snapshot_path: "test_snapshots_path" ,
1084
+ migration_path: "test_migration_path" ,
1085
+ quiet: true ,
1086
+ format: false
1087
+ )
1088
+
1089
+ assert [ _file1 , file2 ] =
1090
+ Enum . sort ( Path . wildcard ( "test_migration_path/**/*_migrate_resources*.exs" ) )
1091
+ |> Enum . reject ( & String . contains? ( & 1 , "extensions" ) )
1092
+
1093
+ contents = File . read! ( file2 )
1094
+ [ up_side , down_side ] = String . split ( contents , "def down" , parts: 2 )
1095
+
1096
+ up_side_parts =
1097
+ String . split ( up_side , "\n " , trim: true )
1098
+ |> Enum . map ( & String . trim / 1 )
1099
+
1100
+ drop_index =
1101
+ Enum . find_index ( up_side_parts , fn x ->
1102
+ x == "drop_if_exists unique_index(:posts, [:title], name: \" posts_title_index\" )"
1103
+ end )
1104
+
1105
+ rename_table =
1106
+ Enum . find_index ( up_side_parts , fn x ->
1107
+ x == "rename table(:posts), :title, to: :subject"
1108
+ end )
1109
+
1110
+ create_index =
1111
+ Enum . find_index ( up_side_parts , fn x ->
1112
+ x == "create unique_index(:posts, [:subject], name: \" posts_subject_index\" )"
1113
+ end )
1114
+
1115
+ assert drop_index < rename_table
1116
+ assert rename_table < create_index
1117
+
1118
+ down_side_parts =
1119
+ String . split ( down_side , "\n " , trim: true )
1120
+ |> Enum . map ( & String . trim / 1 )
1121
+
1122
+ drop_index =
1123
+ Enum . find_index ( down_side_parts , fn x ->
1124
+ x ==
1125
+ "drop_if_exists unique_index(:posts, [:subject], name: \" posts_subject_index\" )"
1126
+ end )
1127
+
1128
+ rename_table =
1129
+ Enum . find_index ( down_side_parts , fn x ->
1130
+ x == "rename table(:posts), :subject, to: :title"
1131
+ end )
1132
+
1133
+ create_index =
1134
+ Enum . find_index ( down_side_parts , fn x ->
1135
+ x == "create unique_index(:posts, [:title], name: \" posts_title_index\" )"
1136
+ end )
1137
+
1138
+ assert drop_index < rename_table
1139
+ assert rename_table < create_index
1140
+ end
1141
+
1065
1142
test "when renaming a field, it asks which field you are renaming it to, and adds it if you arent" do
1066
1143
defposts do
1067
1144
attributes do
0 commit comments