From 6ad57501ff97ad66813bcddd6136347425a446da Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Sun, 25 Sep 2022 14:41:29 -0400 Subject: [PATCH] improvement: upgrade to new `exists` usage --- lib/expr.ex | 8 +++---- mix.exs | 3 ++- test/filter_test.exs | 48 +++++++++++++++++++++++++++++++++++++ test/support/types/money.ex | 1 + 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/lib/expr.ex b/lib/expr.ex index 149dffa..390f29b 100644 --- a/lib/expr.ex +++ b/lib/expr.ex @@ -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], diff --git a/mix.exs b/mix.exs index 763d1fa..52d3d05 100644 --- a/mix.exs +++ b/mix.exs @@ -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}, diff --git a/test/filter_test.exs b/test/filter_test.exs index 65caac9..6de1bbb 100644 --- a/test/filter_test.exs +++ b/test/filter_test.exs @@ -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 diff --git a/test/support/types/money.ex b/test/support/types/money.ex index 2c829e3..fde16df 100644 --- a/test/support/types/money.ex +++ b/test/support/types/money.ex @@ -1,4 +1,5 @@ defmodule AshPostgres.Test.Money do + @moduledoc false use Ash.Resource, data_layer: :embedded