2020-09-11 12:26:47 +12:00
|
|
|
defmodule AshPostgres.Test.Comment do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
Update test to demo issue with policy on aggregate
The issue occurs when an aggregate is loaded, and the aggregated
resource itself has a relates_to_actor_via policy.
The following error is produced at test/aggregate_test.exs:42
1) test relates to actor via has_many and with an aggregate (AshPostgres.AggregateTest)
test/aggregate_test.exs:8
** (RuntimeError) Could not determined related for `exists/2` expression.
Context Resource: %{aggregates: %{count_of_comments: #count<comments from #Ash.Query<resource: AshPostgres.Test.Comment>>}, calculations: %{}, data_layer: AshPostgres.DataLayer, public?: false, relationship_path: [], resource: AshPostgres.Test.Post, root_resource: AshPostgres.Test.Post}
Context Relationship Path: []
At Path: []
Path: [:post, :organization, :users]
Related: nil
Expression: exists(post.organization.users, [id: "236f08bc-6a3a-4a78-9911-2c24189dafac"])
code: |> Api.read_one!()
stacktrace:
(ash 2.5.12) lib/ash/filter/filter.ex:2121: Ash.Filter.add_expression_part/3
(ash 2.5.12) lib/ash/filter/filter.ex:2037: anonymous fn/3 in Ash.Filter.parse_expression/2
(elixir 1.14.2) lib/enum.ex:4751: Enumerable.List.reduce/3
(elixir 1.14.2) lib/enum.ex:2514: Enum.reduce_while/3
(ash 2.5.12) lib/ash/filter/filter.ex:295: Ash.Filter.parse/5
(ash 2.5.12) lib/ash/filter/filter.ex:995: Ash.Filter.add_to_filter/6
(ash 2.5.12) lib/ash/query/query.ex:1797: Ash.Query.do_filter/2
(elixir 1.14.2) lib/map.ex:883: Map.update!/3
(ash 2.5.12) lib/ash/engine/request.ex:650: Ash.Engine.Request.apply_filter/4
(ash 2.5.12) lib/ash/engine/request.ex:557: Ash.Engine.Request.do_strict_check/3
(ash 2.5.12) lib/ash/engine/request.ex:518: anonymous fn/2 in Ash.Engine.Request.strict_check/2
(elixir 1.14.2) lib/enum.ex:4751: Enumerable.List.reduce/3
(elixir 1.14.2) lib/enum.ex:2514: Enum.reduce_while/3
(ash 2.5.12) lib/ash/engine/request.ex:255: Ash.Engine.Request.do_next/1
(ash 2.5.12) lib/ash/engine/request.ex:211: Ash.Engine.Request.next/1
(ash 2.5.12) lib/ash/engine/engine.ex:650: Ash.Engine.advance_request/2
(ash 2.5.12) lib/ash/engine/engine.ex:556: Ash.Engine.fully_advance_request/2
(ash 2.5.12) lib/ash/engine/engine.ex:497: Ash.Engine.do_run_iteration/2
(elixir 1.14.2) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
(ash 2.5.12) lib/ash/engine/engine.ex:257: Ash.Engine.run_to_completion/1
(ash 2.5.12) lib/ash/engine/engine.ex:202: Ash.Engine.do_run/2
(ash 2.5.12) lib/ash/engine/engine.ex:141: Ash.Engine.run/2
(ash 2.5.12) lib/ash/actions/read.ex:170: Ash.Actions.Read.do_run/3
(ash 2.5.12) lib/ash/actions/read.ex:90: Ash.Actions.Read.run/3
(ash 2.5.12) lib/ash/api/api.ex:1005: Ash.Api.read_one/3
(ash 2.5.12) lib/ash/api/api.ex:998: Ash.Api.read_one!/3
test/aggregate_test.exs:43: (test)
2023-01-30 19:05:52 +13:00
|
|
|
data_layer: AshPostgres.DataLayer,
|
|
|
|
authorizers: [
|
|
|
|
Ash.Policy.Authorizer
|
|
|
|
]
|
|
|
|
|
|
|
|
policies do
|
|
|
|
bypass action_type(:read) do
|
|
|
|
# Check that the comment is in the same org (via post) as actor
|
|
|
|
authorize_if(relates_to_actor_via([:post, :organization, :users]))
|
|
|
|
end
|
|
|
|
end
|
2020-09-11 12:26:47 +12:00
|
|
|
|
|
|
|
postgres do
|
|
|
|
table "comments"
|
2022-08-19 06:56:36 +12:00
|
|
|
repo(AshPostgres.TestRepo)
|
2021-04-01 19:19:30 +13:00
|
|
|
|
|
|
|
references do
|
|
|
|
reference(:post, on_delete: :delete, on_update: :update, name: "special_name_fkey")
|
|
|
|
end
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2022-04-20 03:08:28 +12:00
|
|
|
defaults([:read, :update, :destroy])
|
2021-02-01 10:39:59 +13:00
|
|
|
|
|
|
|
create :create do
|
2022-05-09 00:08:06 +12:00
|
|
|
primary?(true)
|
2021-02-06 12:59:33 +13:00
|
|
|
argument(:rating, :map)
|
2021-02-01 10:39:59 +13:00
|
|
|
|
2021-02-23 17:53:18 +13:00
|
|
|
change(manage_relationship(:rating, :ratings, on_missing: :ignore, on_match: :create))
|
2021-02-01 10:39:59 +13:00
|
|
|
end
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 14:22:28 +13:00
|
|
|
uuid_primary_key(:id)
|
|
|
|
attribute(:title, :string)
|
2021-04-05 08:05:41 +12:00
|
|
|
attribute(:likes, :integer)
|
2021-11-14 07:55:49 +13:00
|
|
|
attribute(:arbitrary_timestamp, :utc_datetime_usec)
|
2022-02-10 05:49:19 +13:00
|
|
|
create_timestamp(:created_at, writable?: true)
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|
|
|
|
|
2021-12-18 07:25:14 +13:00
|
|
|
aggregates do
|
|
|
|
first(:post_category, :post, :category)
|
2022-05-22 18:12:07 +12:00
|
|
|
count(:co_popular_comments, [:post, :popular_comments])
|
2022-09-14 08:27:39 +12:00
|
|
|
count(:count_of_comments_containing_title, [:post, :comments_containing_title])
|
|
|
|
list(:posts_for_comments_containing_title, [:post, :comments_containing_title, :post], :title)
|
2021-12-18 07:25:14 +13:00
|
|
|
end
|
|
|
|
|
2020-09-11 12:26:47 +12:00
|
|
|
relationships do
|
2021-01-13 14:22:28 +13:00
|
|
|
belongs_to(:post, AshPostgres.Test.Post)
|
2021-06-04 17:48:35 +12:00
|
|
|
belongs_to(:author, AshPostgres.Test.Author)
|
2021-01-29 13:42:55 +13:00
|
|
|
|
|
|
|
has_many(:ratings, AshPostgres.Test.Rating,
|
2022-08-19 06:56:36 +12:00
|
|
|
destination_attribute: :resource_id,
|
2021-05-14 17:20:10 +12:00
|
|
|
relationship_context: %{data_layer: %{table: "comment_ratings"}}
|
2021-01-29 13:42:55 +13:00
|
|
|
)
|
2022-02-15 11:44:17 +13:00
|
|
|
|
|
|
|
has_many(:popular_ratings, AshPostgres.Test.Rating,
|
2022-08-19 06:56:36 +12:00
|
|
|
destination_attribute: :resource_id,
|
2022-02-15 11:44:17 +13:00
|
|
|
relationship_context: %{data_layer: %{table: "comment_ratings"}},
|
|
|
|
filter: expr(score > 5)
|
|
|
|
)
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|
|
|
|
end
|