Skip to content

Commit 0adec1d

Browse files
authored
* improvement: in multitenant resources migration's generation, check if the relationship points at the primary key of the target then not adding the multitenancy attribute (#144 and #157)
1 parent 99e8029 commit 0adec1d

File tree

2 files changed

+109
-2
lines changed

2 files changed

+109
-2
lines changed

lib/migration_generator/operation.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
9393
} = attribute
9494
}) do
9595
with_match =
96-
if destination_attribute != reference_attribute do
96+
if !reference.primary_key? && destination_attribute != reference_attribute do
9797
"with: [#{as_atom(source_attribute)}: :#{as_atom(destination_attribute)}], match: :full"
9898
end
9999

@@ -485,7 +485,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
485485
end
486486

487487
with_match =
488-
if destination_attribute != reference_attribute do
488+
if !reference.primary_key? && destination_attribute != reference_attribute do
489489
"with: [#{as_atom(source_attribute)}: :#{as_atom(destination_attribute)}], match: :full"
490490
end
491491

test/migration_generator_test.exs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,29 @@ defmodule AshPostgres.MigrationGeneratorTest do
6060
end
6161
end
6262

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+
6386
describe "creating initial snapshots" do
6487
setup do
6588
on_exit(fn ->
@@ -877,6 +900,90 @@ defmodule AshPostgres.MigrationGeneratorTest do
877900

878901
assert after_drop =~ ~S[references(:posts]
879902
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
880987
end
881988

882989
describe "check constraints" do

0 commit comments

Comments
 (0)