Skip to content

Commit 7ad0b86

Browse files
committed
fix: ensure that tenant is properly used in many-to-many joins
1 parent c9a6e3e commit 7ad0b86

File tree

5 files changed

+33
-18
lines changed

5 files changed

+33
-18
lines changed

lib/data_layer.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,9 @@ defmodule AshPostgres.DataLayer do
12471247
through_query = Ecto.Query.exclude(through_query, :select)
12481248

12491249
through_query =
1250-
if through_query.joins && through_query.joins != [] do
1250+
if (through_query.joins && through_query.joins != []) ||
1251+
(Ash.Resource.Info.multitenancy_strategy(relationship.through) == :context &&
1252+
source_query.tenant) do
12511253
subquery(
12521254
set_subquery_prefix(
12531255
through_query,
@@ -1256,7 +1258,7 @@ defmodule AshPostgres.DataLayer do
12561258
)
12571259
)
12581260
else
1259-
through_query
1261+
set_subquery_prefix(through_query, source_query, relationship.through)
12601262
end
12611263

12621264
if query.__ash_bindings__[:__order__?] do

mix.exs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ defmodule AshPostgres.MixProject do
169169
{:ash, ash_version("~> 3.5 and >= 3.5.35")},
170170
{:spark, "~> 2.3 and >= 2.3.4"},
171171
# TODO: bump to next ash_sql release
172-
# {:ash_sql, ash_sql_version("~> 0.2 and >= 0.2.90")},
173-
{:ash_sql, git: "https://github.com/ash-project/ash_sql.git"},
172+
{:ash_sql, ash_sql_version(git: "https://github.com/ash-project/ash_sql.git")},
174173
{:igniter, "~> 0.6 and >= 0.6.14", optional: true},
175174
{:ecto_sql, "~> 3.13"},
176175
{:ecto, "~> 3.13"},

test/multitenancy_test.exs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ defmodule AshPostgres.Test.MultitenancyTest do
22
use AshPostgres.RepoCase, async: false
33

44
require Ash.Query
5-
alias AshPostgres.MultitenancyTest.{CompositeKeyPost, NamedOrg, Org, Post, User, NonMultitenantPostMultitenantLink}
5+
6+
alias AshPostgres.MultitenancyTest.{
7+
CompositeKeyPost,
8+
NamedOrg,
9+
Org,
10+
Post,
11+
User,
12+
NonMultitenantPostMultitenantLink
13+
}
14+
615
alias AshPostgres.Test.Post, as: GlobalPost
716

817
setup do
@@ -227,23 +236,27 @@ defmodule AshPostgres.Test.MultitenancyTest do
227236
end
228237

229238
test "loading non multitenant resource across a many_to_many works", %{org1: org1} do
230-
post = Post
239+
post =
240+
Post
231241
|> Ash.Changeset.for_create(:create, %{name: "foo"})
232242
|> Ash.Changeset.set_tenant(org1)
233243
|> Ash.create!()
234244

235-
GlobalPost
245+
global_post =
246+
GlobalPost
236247
|> Ash.Changeset.for_create(:create, %{title: "fred"})
237248
|> Ash.create!()
238249

239250
NonMultitenantPostMultitenantLink
240-
|> Ash.Changeset.for_create(:create, %{source_id: post.id, dest_id: global_post.id}, tenant: org1)
241-
|> Ash.create!()
251+
|> Ash.Changeset.for_create(:create, %{source_id: post.id, dest_id: global_post.id},
252+
tenant: org1
253+
)
254+
|> Ash.create!()
242255

243-
post |> Ash.load!([:linked_non_multitenant_posts_through_multitenant_link], tenant: org1) |> IO.inspect()
256+
post
257+
|> Ash.load!([:linked_non_multitenant_posts_through_multitenant_link], tenant: org1)
244258
end
245259

246-
247260
test "manage_relationship from context multitenant resource to attribute multitenant resource doesn't raise an error" do
248261
org = Org |> Ash.Changeset.new() |> Ash.create!()
249262
user = User |> Ash.Changeset.new() |> Ash.create!()

test/support/multitenancy/resources/non_multitenant_post_multitenant_link.ex

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ defmodule AshPostgres.MultitenancyTest.NonMultitenantPostMultitenantLink do
2929
end
3030

3131
attributes do
32-
uuid_primary_key :id
32+
uuid_primary_key(:id)
3333

3434
attribute :state, :atom do
3535
public?(true)
@@ -40,13 +40,13 @@ defmodule AshPostgres.MultitenancyTest.NonMultitenantPostMultitenantLink do
4040

4141
relationships do
4242
belongs_to :source, AshPostgres.MultitenancyTest.Post do
43-
public? true
44-
allow_nil? false
43+
public?(true)
44+
allow_nil?(false)
4545
end
4646

4747
belongs_to :dest, AshPostgres.Test.Post do
48-
public? true
49-
allow_nil? false
48+
public?(true)
49+
allow_nil?(false)
5050
end
5151
end
5252
end

test/support/multitenancy/resources/post.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ defmodule AshPostgres.MultitenancyTest.Post do
5959

6060
# has_many(:non_multitenant_post_links, AshPostgres.MultitenancyTest.NonMultitenantPostLink)
6161

62-
has_many :non_multitenant_post_multitenant_links, AshPostgres.MultitenancyTest.NonMultitenantPostMultitenantLink do
63-
destination_attribute :source_id
62+
has_many :non_multitenant_post_multitenant_links,
63+
AshPostgres.MultitenancyTest.NonMultitenantPostMultitenantLink do
64+
destination_attribute(:source_id)
6465
end
6566

6667
many_to_many :linked_non_multitenant_posts, AshPostgres.Test.Post do

0 commit comments

Comments
 (0)