* 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)

This commit is contained in:
Alessio Montagnani 2023-09-25 21:33:28 +02:00 committed by GitHub
parent 99e8029499
commit 0adec1d163
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 109 additions and 2 deletions

View file

@ -93,7 +93,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
} = attribute
}) do
with_match =
if destination_attribute != reference_attribute do
if !reference.primary_key? && destination_attribute != reference_attribute do
"with: [#{as_atom(source_attribute)}: :#{as_atom(destination_attribute)}], match: :full"
end
@ -485,7 +485,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
end
with_match =
if destination_attribute != reference_attribute do
if !reference.primary_key? && destination_attribute != reference_attribute do
"with: [#{as_atom(source_attribute)}: :#{as_atom(destination_attribute)}], match: :full"
end

View file

@ -60,6 +60,29 @@ defmodule AshPostgres.MigrationGeneratorTest do
end
end
defmacrop defresource(mod, table, do: body) do
quote do
Code.compiler_options(ignore_module_conflict: true)
defmodule unquote(mod) do
use Ash.Resource, data_layer: AshPostgres.DataLayer
postgres do
table unquote(table)
repo(AshPostgres.TestRepo)
end
actions do
defaults([:create, :read, :update, :destroy])
end
unquote(body)
end
Code.compiler_options(ignore_module_conflict: false)
end
end
describe "creating initial snapshots" do
setup do
on_exit(fn ->
@ -877,6 +900,90 @@ defmodule AshPostgres.MigrationGeneratorTest do
assert after_drop =~ ~S[references(:posts]
end
test "references with added only when needed on multitenant resources" do
defresource Org, "orgs" do
attributes do
uuid_primary_key(:id, writable?: true)
attribute(:name, :string)
end
multitenancy do
strategy(:attribute)
attribute(:id)
end
end
defresource User, "users" do
attributes do
uuid_primary_key(:id, writable?: true)
attribute(:secondary_id, :uuid)
attribute(:name, :string)
attribute(:org_id, :uuid)
end
multitenancy do
strategy(:attribute)
attribute(:org_id)
end
relationships do
belongs_to(:org, Org)
end
end
defresource UserThing1, "user_things1" do
attributes do
attribute(:id, :string, primary_key?: true, allow_nil?: false)
attribute(:name, :string)
attribute(:org_id, :uuid)
end
multitenancy do
strategy(:attribute)
attribute(:org_id)
end
relationships do
belongs_to(:org, Org)
belongs_to(:user, User, destination_attribute: :secondary_id)
end
end
defresource UserThing2, "user_things2" do
attributes do
uuid_primary_key(:id, writable?: true)
attribute(:name, :string)
end
multitenancy do
strategy(:attribute)
attribute(:org_id)
end
relationships do
belongs_to(:org, Org)
belongs_to(:user, User)
end
end
defapi([Org, User, UserThing1, UserThing2])
AshPostgres.MigrationGenerator.generate(Api,
snapshot_path: "test_snapshots_path",
migration_path: "test_migration_path",
quiet: true,
format: false
)
assert [file] = Path.wildcard("test_migration_path/**/*_migrate_resources*.exs")
assert File.read!(file) =~
~S[references(:users, column: :secondary_id, with: [org_id: :org_id\], match: :full, name: "user_things1_user_id_fkey", type: :uuid, prefix: "public")]
assert File.read!(file) =~
~S[references(:users, column: :id, name: "user_things2_user_id_fkey", type: :uuid, prefix: "public")]
end
end
describe "check constraints" do