@@ -60,6 +60,29 @@ defmodule AshPostgres.MigrationGeneratorTest do
60
60
end
61
61
end
62
62
63
+ defmacrop defresource ( mod , table , do: body ) do
64
+ quote do
65
+ Code . compiler_options ( ignore_module_conflict: true )
66
+
67
+ defmodule unquote ( mod ) do
68
+ use Ash.Resource , data_layer: AshPostgres.DataLayer
69
+
70
+ postgres do
71
+ table unquote ( table )
72
+ repo ( AshPostgres.TestRepo )
73
+ end
74
+
75
+ actions do
76
+ defaults ( [ :create , :read , :update , :destroy ] )
77
+ end
78
+
79
+ unquote ( body )
80
+ end
81
+
82
+ Code . compiler_options ( ignore_module_conflict: false )
83
+ end
84
+ end
85
+
63
86
describe "creating initial snapshots" do
64
87
setup do
65
88
on_exit ( fn ->
@@ -877,6 +900,90 @@ defmodule AshPostgres.MigrationGeneratorTest do
877
900
878
901
assert after_drop =~ ~S[ references(:posts]
879
902
end
903
+
904
+ test "references with added only when needed on multitenant resources" do
905
+ defresource Org , "orgs" do
906
+ attributes do
907
+ uuid_primary_key ( :id , writable?: true )
908
+ attribute ( :name , :string )
909
+ end
910
+
911
+ multitenancy do
912
+ strategy ( :attribute )
913
+ attribute ( :id )
914
+ end
915
+ end
916
+
917
+ defresource User , "users" do
918
+ attributes do
919
+ uuid_primary_key ( :id , writable?: true )
920
+ attribute ( :secondary_id , :uuid )
921
+ attribute ( :name , :string )
922
+ attribute ( :org_id , :uuid )
923
+ end
924
+
925
+ multitenancy do
926
+ strategy ( :attribute )
927
+ attribute ( :org_id )
928
+ end
929
+
930
+ relationships do
931
+ belongs_to ( :org , Org )
932
+ end
933
+ end
934
+
935
+ defresource UserThing1 , "user_things1" do
936
+ attributes do
937
+ attribute ( :id , :string , primary_key?: true , allow_nil?: false )
938
+ attribute ( :name , :string )
939
+ attribute ( :org_id , :uuid )
940
+ end
941
+
942
+ multitenancy do
943
+ strategy ( :attribute )
944
+ attribute ( :org_id )
945
+ end
946
+
947
+ relationships do
948
+ belongs_to ( :org , Org )
949
+ belongs_to ( :user , User , destination_attribute: :secondary_id )
950
+ end
951
+ end
952
+
953
+ defresource UserThing2 , "user_things2" do
954
+ attributes do
955
+ uuid_primary_key ( :id , writable?: true )
956
+ attribute ( :name , :string )
957
+ end
958
+
959
+ multitenancy do
960
+ strategy ( :attribute )
961
+ attribute ( :org_id )
962
+ end
963
+
964
+ relationships do
965
+ belongs_to ( :org , Org )
966
+ belongs_to ( :user , User )
967
+ end
968
+ end
969
+
970
+ defapi ( [ Org , User , UserThing1 , UserThing2 ] )
971
+
972
+ AshPostgres.MigrationGenerator . generate ( Api ,
973
+ snapshot_path: "test_snapshots_path" ,
974
+ migration_path: "test_migration_path" ,
975
+ quiet: true ,
976
+ format: false
977
+ )
978
+
979
+ assert [ file ] = Path . wildcard ( "test_migration_path/**/*_migrate_resources*.exs" )
980
+
981
+ assert File . read! ( file ) =~
982
+ ~S[ references(:users, column: :secondary_id, with: [org_id: :org_id\] , match: :full, name: "user_things1_user_id_fkey", type: :uuid, prefix: "public")]
983
+
984
+ assert File . read! ( file ) =~
985
+ ~S[ references(:users, column: :id, name: "user_things2_user_id_fkey", type: :uuid, prefix: "public")]
986
+ end
880
987
end
881
988
882
989
describe "check constraints" do
0 commit comments