Skip to content

Commit 89326d4

Browse files
committed
chore: update for ash_sql/ash compatibility
1 parent 5004faa commit 89326d4

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

lib/data_layer.ex

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,10 +1159,12 @@ defmodule AshPostgres.DataLayer do
11591159
{:ok, data_layer_query} ->
11601160
data_layer_query = Ecto.Query.exclude(data_layer_query, :select)
11611161

1162+
through_binding = Map.get(query, :__ash_bindings__)[:current]
1163+
11621164
through_resource
11631165
|> Ash.Query.new()
11641166
|> Ash.Query.put_context(:data_layer, %{
1165-
start_bindings_at: Map.get(data_layer_query, :__ash_bindings__)[:current]
1167+
start_bindings_at: through_binding
11661168
})
11671169
|> Ash.Query.set_context(through_relationship.context)
11681170
|> Ash.Query.do_filter(through_relationship.filter)
@@ -1192,7 +1194,7 @@ defmodule AshPostgres.DataLayer do
11921194
source_query,
11931195
relationship.through
11941196
),
1195-
as: ^Map.get(data_layer_query, :__ash_bindings__)[:current],
1197+
as: ^through_binding,
11961198
on:
11971199
field(through, ^destination_attribute_on_join_resource) ==
11981200
field(destination, ^destination_attribute),
@@ -1230,7 +1232,7 @@ defmodule AshPostgres.DataLayer do
12301232
source_query,
12311233
relationship.through
12321234
),
1233-
as: ^Map.get(data_layer_query, :__ash_bindings__)[:current],
1235+
as: ^through_binding,
12341236
on:
12351237
field(through, ^destination_attribute_on_join_resource) ==
12361238
field(destination, ^destination_attribute),
@@ -1295,10 +1297,8 @@ defmodule AshPostgres.DataLayer do
12951297
:no_inner_join?,
12961298
true
12971299
)
1300+
|> Map.delete(:lateral_join_source)
12981301
})
1299-
# This is a hack, but surely there is just no way someone writes
1300-
# a 500 binding query as the base of a lateral join query...
1301-
|> Ash.Query.set_context(%{data_layer: %{start_bindings_at: 500}})
13021302
|> Ash.Query.set_tenant(source_query.tenant)
13031303
|> filter_for_records(root_data)
13041304
|> set_lateral_join_prefix(query)
@@ -3259,12 +3259,20 @@ defmodule AshPostgres.DataLayer do
32593259
def filter(query, filter, resource, opts \\ []) do
32603260
used_aggregates = Ash.Filter.used_aggregates(filter, [])
32613261

3262+
query =
3263+
AshSql.Bindings.default_bindings(query, resource, AshPostgres.SqlImplementation)
3264+
32623265
query
32633266
|> AshSql.Join.join_all_relationships(filter, opts)
32643267
|> case do
32653268
{:ok, query} ->
32663269
query
3267-
|> AshSql.Aggregate.add_aggregates(used_aggregates, resource, false, 0)
3270+
|> AshSql.Aggregate.add_aggregates(
3271+
used_aggregates,
3272+
resource,
3273+
false,
3274+
query.__ash_bindings__.root_binding
3275+
)
32683276
|> case do
32693277
{:ok, query} ->
32703278
{:ok, AshSql.Filter.add_filter_expression(query, filter)}
@@ -3280,12 +3288,24 @@ defmodule AshPostgres.DataLayer do
32803288

32813289
@impl true
32823290
def add_aggregates(query, aggregates, resource) do
3283-
AshSql.Aggregate.add_aggregates(query, aggregates, resource, true, 0)
3291+
AshSql.Aggregate.add_aggregates(
3292+
query,
3293+
aggregates,
3294+
resource,
3295+
true,
3296+
query.__ash_bindings__.root_binding
3297+
)
32843298
end
32853299

32863300
@impl true
32873301
def add_calculations(query, calculations, resource, select? \\ true) do
3288-
AshSql.Calculation.add_calculations(query, calculations, resource, 0, select?)
3302+
AshSql.Calculation.add_calculations(
3303+
query,
3304+
calculations,
3305+
resource,
3306+
query.__ash_bindings__.root_binding,
3307+
select?
3308+
)
32893309
end
32903310

32913311
def add_known_binding(query, data, known_binding) do

test/load_test.exs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,11 @@ defmodule AshPostgres.Test.LoadTest do
990990
|> Ash.Query.load(posts: paginated_posts)
991991
|> Ash.read!(page: [limit: 1, count: true])
992992

993-
assert %Ash.Page.Offset{count: 5, results: [%{followers: %Ash.Page.Offset{count: 3}}]} =
994-
author1.posts
993+
assert %Ash.Page.Offset{} = page = author1.posts
994+
assert [result] = page.results
995+
assert %Ash.Page.Offset{} = nested_page = result.followers
996+
assert length(nested_page.results) == 1
997+
assert nested_page.count == 3
995998
end
996999

9971000
test "doesn't leak the internal count aggregate when counting" do

0 commit comments

Comments
 (0)