@@ -1052,4 +1052,74 @@ defmodule AshSqlite.MigrationGeneratorTest do
10521052 ~S[ modify :post_id, references(:posts, column: :id, name: "comments_post_id_fkey", type: :uuid)]
10531053 end
10541054 end
1055+
1056+ describe "renaming multiple relationships" do
1057+ setup % { snapshot_path: snapshot_path , migration_path: migration_path } do
1058+ defposts do
1059+ attributes do
1060+ uuid_primary_key ( :id )
1061+ end
1062+
1063+ relationships do
1064+ belongs_to ( :creator , AshSqlite.Test.User )
1065+ belongs_to ( :contributer , AshSqlite.Test.User )
1066+ end
1067+ end
1068+
1069+ defdomain ( [ Post , AshSqlite.Test.User ] )
1070+
1071+ AshSqlite.MigrationGenerator . generate ( Domain ,
1072+ snapshot_path: snapshot_path ,
1073+ migration_path: migration_path ,
1074+ quiet: true ,
1075+ format: false ,
1076+ auto_name: true
1077+ )
1078+
1079+ :ok
1080+ end
1081+
1082+ test "renames columns without adding duplicate columns" , % {
1083+ snapshot_path: snapshot_path ,
1084+ migration_path: migration_path
1085+ } do
1086+ defposts do
1087+ attributes do
1088+ uuid_primary_key ( :id )
1089+ end
1090+
1091+ relationships do
1092+ belongs_to ( :creator2 , AshSqlite.Test.User )
1093+ belongs_to ( :contributer2 , AshSqlite.Test.User )
1094+ end
1095+ end
1096+
1097+ defdomain ( [ Post , AshSqlite.Test.User ] )
1098+
1099+ send ( self ( ) , { :mix_shell_input , :yes? , true } )
1100+ send ( self ( ) , { :mix_shell_input , :prompt , "creator2_id" } )
1101+ send ( self ( ) , { :mix_shell_input , :yes? , true } )
1102+
1103+ AshSqlite.MigrationGenerator . generate ( Domain ,
1104+ snapshot_path: snapshot_path ,
1105+ migration_path: migration_path ,
1106+ quiet: true ,
1107+ format: false ,
1108+ auto_name: true
1109+ )
1110+
1111+ assert [ _file1 , file2 ] =
1112+ Enum . sort ( Path . wildcard ( "#{ migration_path } /**/*_migrate_resources*.exs" ) )
1113+
1114+ # Up migration
1115+ assert File . read! ( file2 ) =~ ~S[ rename table(:posts), :creator_id, to: :creator2_id]
1116+ assert File . read! ( file2 ) =~ ~S[ rename table(:posts), :contributer_id, to: :contributer2_id]
1117+
1118+ refute File . read! ( file2 ) =~ ~S[ alter table(:posts)]
1119+
1120+ # Down migration
1121+ assert File . read! ( file2 ) =~ ~S[ rename table(:posts), :creator2_id, to: :creator_id]
1122+ assert File . read! ( file2 ) =~ ~S[ rename table(:posts), :contributer2_id, to: :contributer_id]
1123+ end
1124+ end
10551125end
0 commit comments