fix: update ash & fix subquery sort references

This commit is contained in:
Zach Daniel 2024-05-03 00:02:04 -04:00
parent 56f3a5eb2f
commit 77b824e23d
4 changed files with 25 additions and 12 deletions

View file

@ -21,6 +21,8 @@ if Mix.env() == :test do
config :ash, :validate_domain_resource_inclusion?, false
config :ash, :validate_domain_config_inclusion?, false
config :ash, :policies, show_policy_breakdowns?: true
config :ash_postgres, :ash_domains, [AshPostgres.Test.Domain]
config :ash_postgres, AshPostgres.TestRepo,

View file

@ -1348,7 +1348,7 @@ defmodule AshPostgres.DataLayer do
if needs_to_join? do
root_query =
from(row in query.from.source, [])
from(row in query.from.source, as: ^0)
|> Map.put(:__ash_bindings__, query.__ash_bindings__)
|> Ecto.Query.exclude(:select)
|> Map.put(:limit, query.limit)
@ -1356,7 +1356,7 @@ defmodule AshPostgres.DataLayer do
root_query =
if query.limit || query.offset do
root_query
Map.put(root_query, :order_bys, query.order_bys)
else
Ecto.Query.exclude(root_query, :order_by)
end
@ -1374,11 +1374,12 @@ defmodule AshPostgres.DataLayer do
end)
faked_query =
from(row in root_query,
from(row in query.from.source,
inner_join: limiter in ^subquery(root_query),
as: ^0,
on: ^dynamic
)
|> Map.put(:__ash_bindings__, query.__ash_bindings__)
joins_to_add =
for {%{on: on} = join, ix} <- Enum.with_index(query.joins) do

View file

@ -21,11 +21,17 @@ defmodule AshPostgres.BulkUpdateTest do
end
test "bulk updates can set datetimes" do
Ash.bulk_create!([%{title: "fred"}, %{title: "george"}], Post, :create)
Post
|> Ash.Changeset.for_create(:create, %{title: "fred"})
|> Ash.create!()
Post
|> Ash.Changeset.for_create(:create, %{title: "george"})
|> Ash.create!()
now = DateTime.utc_now()
Ash.bulk_update!(Post, :update, %{datetime: now})
Ash.bulk_update!(Post, :update, %{datetime: now}, strategy: :atomic)
posts = Ash.read!(Post)
@ -81,7 +87,10 @@ defmodule AshPostgres.BulkUpdateTest do
Post
|> Ash.Query.limit(1)
|> Ash.Query.sort(:title)
|> Ash.bulk_update!(:update, %{}, atomic_update: %{title: Ash.Expr.expr(title <> "_stuff")})
|> Ash.bulk_update!(:update, %{},
atomic_update: %{title: Ash.Expr.expr(title <> "_stuff")},
strategy: :atomic
)
titles =
Post

View file

@ -38,12 +38,13 @@ defmodule AshPostgres.Test.MultitenancyTest do
end
test "context multitenancy works with policies", %{org1: org1} do
post =
Post
|> Ash.Changeset.for_create(:create, %{name: "foo"})
|> Ash.Changeset.set_tenant(tenant(org1))
|> Ash.Changeset.for_create(:create, %{name: "foo"}, tenant: tenant(org1))
|> Ash.create!()
|> Ash.Changeset.for_update(:update_with_policy, %{}, authorize?: true)
|> Ash.Changeset.set_tenant(tenant(org1))
post
|> Ash.Changeset.for_update(:update_with_policy, %{}, authorize?: true, tenant: tenant(org1))
|> Ash.update!()
end