Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/data_layer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ defmodule AshPostgres.DataLayer do
through_query.limit != nil || through_query.order_bys != [] ||
(through_query.joins && through_query.joins != []) ||
has_subqueries_in_wheres? ||
(Ash.Resource.Info.multitenancy_strategy(through_relationship.source) ==
(Ash.Resource.Info.multitenancy_strategy(through_resource.resource) ==
:context &&
source_query.tenant)

Expand All @@ -1340,11 +1340,11 @@ defmodule AshPostgres.DataLayer do
set_subquery_prefix(
through_query,
source_query,
through_relationship.source
through_resource.resource
)
)
else
set_subquery_prefix(through_query, source_query, through_relationship.source)
set_subquery_prefix(through_query, source_query, through_resource.resource)
end

if query.__ash_bindings__[:__order__?] do
Expand Down Expand Up @@ -2536,7 +2536,7 @@ defmodule AshPostgres.DataLayer do

fields_to_upsert =
upsert_fields --
Keyword.keys(Enum.at(changesets, 0).atomics) -- keys
(Keyword.keys(Enum.at(changesets, 0).atomics) -- keys)

fields_to_upsert =
case fields_to_upsert do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
defmodule AshPostgres.TestRepo.Migrations.AddCrossSchemaM2MTest do
@moduledoc """
Migration for cross-schema many_to_many regression test.

Creates:
- "interest" schema
- interests table in "interest" schema
- profile_interests table in "profiles" schema (join table)

This tests true cross-schema many_to_many between two custom schemas:
- Interest in "interest" schema
- Profile in "profiles" schema
- ProfileInterest join table in "profiles" schema
"""
use Ecto.Migration

def up do
# Create the "interest" schema
execute "CREATE SCHEMA IF NOT EXISTS interest"

# Interest table in "interest" schema
create table(:interests, primary_key: false, prefix: "interest") do
add :id, :uuid, null: false, primary_key: true
add :name, :text
end

# ProfileInterest join table in "profiles" schema
create table(:profile_interests, primary_key: false, prefix: "profiles") do
add :id, :uuid, null: false, primary_key: true

add :profile_id,
references(:profile,
column: :id,
name: "profile_interests_profile_id_fkey",
type: :uuid,
prefix: "profiles"
),
null: false

add :interest_id,
references(:interests,
column: :id,
name: "profile_interests_interest_id_fkey",
type: :uuid,
prefix: "interest"
),
null: false
end

create unique_index(:profile_interests, [:profile_id, :interest_id], prefix: "profiles")
end

def down do
drop table(:profile_interests, prefix: "profiles")
drop table(:interests, prefix: "interest")
execute "DROP SCHEMA IF EXISTS interest"
end
end
26 changes: 26 additions & 0 deletions test/cross_schema_many_to_many_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs/contributors>
#
# SPDX-License-Identifier: MIT

defmodule AshPostgres.CrossSchemaManyToManyTest do
@moduledoc """
Regression test for cross-schema many_to_many relationships.

Test setup (two custom schemas):
- Profile lives in "profiles" schema
- Interest lives in "interest" schema
- ProfileInterest (join table) lives in "profiles" schema
"""
use AshPostgres.RepoCase, async: false

alias AshPostgres.Test.{Interest, Profile, ProfileInterest}

test "load many_to_many across custom schemas" do
profile = Ash.create!(Profile, %{description: "Test"})
interest = Ash.create!(Interest, %{name: "eating the dogs"})

Ash.create!(ProfileInterest, %{profile_id: profile.id, interest_id: interest.id})

assert [%{description: "Test"}] = Ash.load!(interest, :profiles).profiles
end
end
2 changes: 2 additions & 0 deletions test/support/domain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ defmodule AshPostgres.Test.Domain do
resource(AshPostgres.Test.Through.Teacher)
resource(AshPostgres.Test.Through.ClassroomTeacher)
resource(AshPostgres.Test.Through.Student)
resource(AshPostgres.Test.Interest)
resource(AshPostgres.Test.ProfileInterest)
end

authorization do
Expand Down
42 changes: 42 additions & 0 deletions test/support/resources/interest.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs/contributors>
#
# SPDX-License-Identifier: MIT

defmodule AshPostgres.Test.Interest do
@moduledoc """
An interest resource in the "interest" schema (e.g., "sports", "music", "coding").

Used to test cross-schema many_to_many relationships:
- Interest lives in "interest" schema
- Profile lives in "profiles" schema
- ProfileInterest (join table) lives in "profiles" schema
"""
use Ash.Resource,
domain: AshPostgres.Test.Domain,
data_layer: AshPostgres.DataLayer

postgres do
table "interests"
schema "interest"
repo AshPostgres.TestRepo
end

attributes do
uuid_primary_key(:id)
attribute(:name, :string, public?: true)
end

actions do
default_accept(:*)
defaults([:read, :destroy, create: :*, update: :*])
end

relationships do
many_to_many :profiles, AshPostgres.Test.Profile do
public?(true)
through(AshPostgres.Test.ProfileInterest)
source_attribute_on_join_resource(:interest_id)
destination_attribute_on_join_resource(:profile_id)
end
end
end
9 changes: 9 additions & 0 deletions test/support/resources/profile.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,14 @@ defmodule AshPostgres.Test.Profile do
belongs_to(:author, AshPostgres.Test.Author) do
public?(true)
end

# Cross-schema many_to_many: Profile (profiles schema) -> Interest (public schema)
# through ProfileInterest (profiles schema)
many_to_many :interests, AshPostgres.Test.Interest do
public?(true)
through(AshPostgres.Test.ProfileInterest)
source_attribute_on_join_resource(:profile_id)
destination_attribute_on_join_resource(:interest_id)
end
end
end
48 changes: 48 additions & 0 deletions test/support/resources/profile_interest.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# SPDX-FileCopyrightText: 2019 ash_postgres contributors <https://github.com/ash-project/ash_postgres/graphs/contributors>
#
# SPDX-License-Identifier: MIT

defmodule AshPostgres.Test.ProfileInterest do
@moduledoc """
A join table in the "profiles" schema linking Profile to Interest.

This tests cross-schema many_to_many relationships between two custom schemas:
- Profile is in "profiles" schema
- Interest is in "interest" schema
- ProfileInterest (this resource) is in "profiles" schema
"""
use Ash.Resource,
domain: AshPostgres.Test.Domain,
data_layer: AshPostgres.DataLayer

postgres do
table "profile_interests"
schema "profiles"
repo AshPostgres.TestRepo
end

attributes do
uuid_primary_key(:id)
end

actions do
default_accept(:*)
defaults([:read, :destroy, create: :*, update: :*])
end

relationships do
belongs_to :profile, AshPostgres.Test.Profile do
public?(true)
allow_nil?(false)
end

belongs_to :interest, AshPostgres.Test.Interest do
public?(true)
allow_nil?(false)
end
end

identities do
identity(:unique_profile_interest, [:profile_id, :interest_id])
end
end
Loading