Skip to content

Commit 4b5d980

Browse files
committed
chore: undo accidental git stash for previous commit
1 parent 46452ba commit 4b5d980

4 files changed

+39
-85
lines changed

lib/data_layer.ex

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,18 +1105,7 @@ defmodule AshPostgres.DataLayer do
11051105

11061106
source_pkey = Ash.Resource.Info.primary_key(source_query.resource)
11071107

1108-
source_query.resource
1109-
|> Ash.Query.set_context(%{:data_layer => source_query.context[:data_layer]})
1110-
|> Ash.Query.set_tenant(source_query.tenant)
1111-
|> set_lateral_join_prefix(query)
1112-
|> case do
1113-
%{valid?: true} = query ->
1114-
Ash.Query.data_layer_query(query)
1115-
1116-
query ->
1117-
{:error, query}
1118-
end
1119-
|> case do
1108+
case lateral_join_source_query(query, source_query) do
11201109
{:ok, data_layer_query} ->
11211110
source_values = Enum.map(root_data, &Map.get(&1, source_attribute))
11221111

@@ -1164,44 +1153,27 @@ defmodule AshPostgres.DataLayer do
11641153
source_values = Enum.map(root_data, &Map.get(&1, source_attribute))
11651154
source_pkey = Ash.Resource.Info.primary_key(source_query.resource)
11661155

1167-
through_resource
1168-
|> Ash.Query.new()
1169-
|> Ash.Query.set_context(through_relationship.context)
1170-
|> Ash.Query.do_filter(through_relationship.filter)
1171-
|> Ash.Query.set_tenant(source_query.tenant)
1172-
|> Ash.Query.put_context(:data_layer, %{
1173-
start_bindings_at: query.__ash_bindings__.current
1174-
})
1175-
|> set_lateral_join_prefix(query)
1176-
|> case do
1177-
%{valid?: true} = through_query ->
1178-
through_query
1179-
|> Ash.Query.data_layer_query()
1180-
1181-
query ->
1182-
{:error, query}
1183-
end
1184-
|> case do
1185-
{:ok, through_query} ->
1186-
source_query.resource
1156+
case lateral_join_source_query(query, source_query) do
1157+
{:ok, data_layer_query} ->
1158+
through_resource
11871159
|> Ash.Query.new()
1188-
|> Ash.Query.set_context(relationship.context)
1189-
|> Ash.Query.set_context(%{:data_layer => source_query.context[:data_layer]})
11901160
|> Ash.Query.put_context(:data_layer, %{
1191-
start_bindings_at: through_query.__ash_bindings__.current
1161+
start_bindings_at: data_layer_query.__ash_bindings__.current
11921162
})
1163+
|> Ash.Query.set_context(through_relationship.context)
1164+
|> Ash.Query.do_filter(through_relationship.filter)
1165+
|> Ash.Query.set_tenant(source_query.tenant)
11931166
|> set_lateral_join_prefix(query)
1194-
|> Ash.Query.do_filter(relationship.filter)
11951167
|> case do
1196-
%{valid?: true} = query ->
1197-
query
1168+
%{valid?: true} = through_query ->
1169+
through_query
11981170
|> Ash.Query.data_layer_query()
11991171

12001172
query ->
12011173
{:error, query}
12021174
end
12031175
|> case do
1204-
{:ok, data_layer_query} ->
1176+
{:ok, through_query} ->
12051177
if query.__ash_bindings__[:__order__?] do
12061178
subquery =
12071179
subquery(
@@ -1214,14 +1186,14 @@ defmodule AshPostgres.DataLayer do
12141186
source_query,
12151187
relationship.through
12161188
),
1217-
as: ^query.__ash_bindings__.current,
1189+
as: ^data_layer_query.__ash_bindings__.current,
12181190
on:
12191191
field(through, ^destination_attribute_on_join_resource) ==
12201192
field(destination, ^destination_attribute),
12211193
where:
12221194
field(through, ^source_attribute_on_join_resource) ==
12231195
field(
1224-
parent_as(^through_query.__ash_bindings__.current),
1196+
parent_as(^0),
12251197
^source_attribute
12261198
)
12271199
)
@@ -1252,14 +1224,14 @@ defmodule AshPostgres.DataLayer do
12521224
source_query,
12531225
relationship.through
12541226
),
1255-
as: ^query.__ash_bindings__.current,
1227+
as: ^data_layer_query.__ash_bindings__.current,
12561228
on:
12571229
field(through, ^destination_attribute_on_join_resource) ==
12581230
field(destination, ^destination_attribute),
12591231
where:
12601232
field(through, ^source_attribute_on_join_resource) ==
12611233
field(
1262-
parent_as(^through_query.__ash_bindings__.current),
1234+
parent_as(^0),
12631235
^source_attribute
12641236
)
12651237
)
@@ -1289,6 +1261,28 @@ defmodule AshPostgres.DataLayer do
12891261
end
12901262
end
12911263

1264+
defp lateral_join_source_query(
1265+
%{__ash_bindings__: %{lateral_join_source_query: lateral_join_source_query}},
1266+
_
1267+
)
1268+
when not is_nil(lateral_join_source_query) do
1269+
{:ok, lateral_join_source_query}
1270+
end
1271+
1272+
defp lateral_join_source_query(query, source_query) do
1273+
source_query.resource
1274+
|> Ash.Query.set_context(%{:data_layer => source_query.context[:data_layer]})
1275+
|> Ash.Query.set_tenant(source_query.tenant)
1276+
|> set_lateral_join_prefix(query)
1277+
|> case do
1278+
%{valid?: true} = query ->
1279+
Ash.Query.data_layer_query(query)
1280+
1281+
query ->
1282+
{:error, query}
1283+
end
1284+
end
1285+
12921286
@doc false
12931287
def set_subquery_prefix(data_layer_query, source_query, resource) do
12941288
repo = AshPostgres.DataLayer.Info.repo(resource, :mutate)

test/filter_child_relationship_by_parent_relationship_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule AshPostgres.Test.Support.Relationships.FilterChileRelationshipByParentRelationship do
1+
defmodule AshPostgres.Test.Support.Relationships.FilterChileRelationshipByParentRelationshipTest do
22
use AshPostgres.RepoCase, async: false
33
alias AshPostgres.Test.{Comment, Post}
44

test/sort_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ defmodule AshPostgres.SortTest do
218218
|> Ash.create!()
219219

220220
posts_query =
221-
Ash.Query.sort(Post, Ash.Sort.expr_sort(source(post_links.state)))
221+
Ash.Query.sort(Post, Ash.Sort.expr_sort(parent(post_links.state)))
222222

223223
Post
224224
|> Ash.Query.load(linked_posts: posts_query)

test/support/relationships/filter_child_relationship_by_parent_relationsip.ex

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)