improvement: upgrade to new exists usage

This commit is contained in:
Zach Daniel 2022-09-25 14:41:29 -04:00
parent e8643d4aa0
commit 6ad57501ff
4 changed files with 54 additions and 6 deletions

View file

@ -732,12 +732,12 @@ defmodule AshPostgres.Expr do
defp do_dynamic_expr(
query,
%Exists{path: [first | rest], expr: expr},
%Exists{at_path: at_path, path: [first | rest], expr: expr},
bindings,
_embedded?,
_type
) do
resource = query.__ash_bindings__.resource
resource = Ash.Resource.Info.related(query.__ash_bindings__.resource, at_path)
first_relationship = Ash.Resource.Info.relationship(resource, first)
filter = %Ash.Filter{expression: expr, resource: first_relationship.destination}
@ -760,7 +760,7 @@ defmodule AshPostgres.Expr do
ref_binding(
%Ref{
attribute: Ash.Resource.Info.attribute(resource, first_relationship.source_attribute),
relationship_path: [],
relationship_path: at_path,
resource: resource
},
bindings
@ -788,8 +788,6 @@ defmodule AshPostgres.Expr do
field(through, ^first_relationship.source_attribute_on_join_resource),
select: 1
)
# )
else
Ecto.Query.from(destination in filtered,
select: [1],

View file

@ -138,7 +138,8 @@ defmodule AshPostgres.MixProject do
{:ecto, "~> 3.8"},
{:jason, "~> 1.0"},
{:postgrex, ">= 0.0.0"},
{:ash, ash_version("~> 2.0.0-rc.9")},
{:ash,
ash_version(github: "ash-project/ash", ref: "3b803d4c552f0df3f8d07a8da9bb1bdd4dfc8fc6")},
{:git_ops, "~> 2.4.5", only: :dev},
{:ex_doc, "~> 0.22", only: :dev, runtime: false},
{:ex_check, "~> 0.14", only: :dev},

View file

@ -622,6 +622,54 @@ defmodule AshPostgres.FilterTest do
|> Api.read!()
end
test "it works with an `at_path`" do
post =
Post
|> Ash.Changeset.new(%{title: "a"})
|> Api.create!()
other_post =
Post
|> Ash.Changeset.new(%{title: "other_a"})
|> Api.create!()
Comment
|> Ash.Changeset.new(%{title: "comment"})
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
|> Api.create!()
Comment
|> Ash.Changeset.new(%{title: "comment"})
|> Ash.Changeset.manage_relationship(:post, other_post, type: :append_and_remove)
|> Api.create!()
Post
|> Ash.Changeset.new(%{title: "b"})
|> Ash.Changeset.manage_relationship(:linked_posts, [post], type: :append_and_remove)
|> Api.create!()
Post
|> Ash.Changeset.new(%{title: "b"})
|> Ash.Changeset.manage_relationship(:linked_posts, [other_post], type: :append_and_remove)
|> Api.create!()
assert [%{title: "b"}] =
Post
|> Ash.Query.filter(
linked_posts.title == "a" and
linked_posts.exists(comments, title == ^"comment")
)
|> Api.read!()
assert [%{title: "b"}] =
Post
|> Ash.Query.filter(
linked_posts.title == "a" and
linked_posts.exists(comments, title == ^"comment")
)
|> Api.read!()
end
test "it works with nested relationships inside of exists" do
post =
Post

View file

@ -1,4 +1,5 @@
defmodule AshPostgres.Test.Money do
@moduledoc false
use Ash.Resource,
data_layer: :embedded