In my resource, I have a read action that needs to set pg_trgm.word_similarity_threshold
before doing the filter query. To do that I created this preparation:
defmodule Core.Ash.Preparations.WordSimilarityThreshold do
@moduledoc false
use Ash.Resource.Preparation
def prepare(query, opts, _context) do
Ash.Query.before_action(query, fn query ->
repo = Keyword.get(opts, :repo, Core.Repo)
similarity_threshold = Map.get(query.arguments, :similarity_threshold, 0.80)
repo.query!("set pg_trgm.word_similarity_threshold = #{similarity_threshold}", [])
query
end)
end
end
And then i can use it like this
prepare WordSimilarityThreshold
filter expr(fragment("(? %> ?)", full_address_normalized, ^arg(:address)))
This works fine, but if the query fails for some reason (for example, a timeout), instead of getting the timeout error, I will get an rollback
unknown error because the query is inside a transaction.
** (Ash.Error.Unknown)
Bread Crumbs:
> Error returned from: Core.Pacman.Markets.Property.list_by_address
Unknown Error
* unknown error: :rollback
(ash 3.4.60) /var/home/sezdocs/projects/rebuilt/platform/core/deps/splode/lib/splode.ex:344: Ash.Error.to_error/2
(elixir 1.18.1) lib/enum.ex:1714: Enum."-map/2-lists^map/1-1-"/2
(ash 3.4.60) /var/home/sezdocs/projects/rebuilt/platform/core/deps/splode/lib/splode.ex:229: Ash.Error.to_class/2
(ash 3.4.60) lib/ash/error/error.ex:108: Ash.Error.to_error_class/2
(ash 3.4.60) lib/ash/actions/read/read.ex:395: anonymous fn/3 in Ash.Actions.Read.do_run/3
(ash 3.4.60) lib/ash/actions/read/read.ex:324: Ash.Actions.Read.do_run/3
(ash 3.4.60) lib/ash/actions/read/read.ex:82: anonymous fn/3 in Ash.Actions.Read.run/3
(ash 3.4.60) lib/ash/actions/read/read.ex:81: Ash.Actions.Read.run/3
(ash 3.4.60) lib/ash.ex:2014: Ash.read/2
(ash 3.4.60) lib/ash.ex:1972: Ash.read!/2
(core 1.175.0) lib/core_web/components/public_record.ex:488: anonymous fn/2 in CoreWeb.Components.PublicRecord.find_property/2
(phoenix_live_view 1.0.9) lib/phoenix_live_view/async.ex:213: Phoenix.LiveView.Async.do_async/5
(elixir 1.18.1) lib/task/supervised.ex:101: Task.Supervised.invoke_mfa/2
(ash 3.4.60) lib/ash/error/unknown.ex:3: Ash.Error.Unknown."exception (overridable 2)"/1
(ash 3.4.60) /var/home/sezdocs/projects/rebuilt/platform/core/deps/splode/lib/splode.ex:264: Ash.Error.to_class/2
(ash 3.4.60) lib/ash/error/error.ex:108: Ash.Error.to_error_class/2
(ash 3.4.60) lib/ash/actions/read/read.ex:395: anonymous fn/3 in Ash.Actions.Read.do_run/3
(ash 3.4.60) lib/ash/actions/read/read.ex:324: Ash.Actions.Read.do_run/3
(ash 3.4.60) lib/ash/actions/read/read.ex:82: anonymous fn/3 in Ash.Actions.Read.run/3
(ash 3.4.60) lib/ash/actions/read/read.ex:81: Ash.Actions.Read.run/3
(ash 3.4.60) lib/ash.ex:2014: Ash.read/2
(ash 3.4.60) lib/ash.ex:1972: Ash.read!/2
(core 1.175.0) lib/core_web/components/public_record.ex:488: anonymous fn/2 in CoreWeb.Components.PublicRecord.find_property/2
(phoenix_live_view 1.0.9) lib/phoenix_live_view/async.ex:213: Phoenix.LiveView.Async.do_async/5
(elixir 1.18.1) lib/task/supervised.ex:101: Task.Supervised.invoke_mfa/2
Function: #Function<7.1988948/0 in Phoenix.LiveView.Async.run_async_task/5>
Args: []
Is this an Ash bug or is this the expected behavior? Is there some way to make it return the timeout error instead?