How to run a second query inside a read action?

Hey, I have a read query that uses pg_trgm and I want to set word_similarity_threshold before running it.

I tried something like this:

    read :by_entity do
      argument :entity_full_name, :string, allow_nil?: false

      prepare fn query, context ->
        Ash.Query.before_action(query, fn query ->
          Pacman.Repo.query!("set pg_trgm.word_similarity_threshold = 0.99") |> dbg()

          query
        end)
      end

      filter expr(fragment("(? %> ?)", first_grantee_full_name, ^arg(:entity_full_name))
                  and not is_nil(recording_date || instrument_date)
                  and (recording_date || instrument_date) >= ago(2, :year)
                  and not deleted?)

      prepare build(sort: [:recording_date])
    end

But this doesn’t seem to work, I believe because there is no commit query encapsulating both queries inside the same transaction.

What would be the best way to achieve this with Ash?

Oh, I just found out that I can pass transaction? true to the read action and that will make the read action be encapsulated inside a transaction making my preparation work as expected.

Since that is fixed, just another question, would Pacman.Repo.query!("set pg_trgm.word_similarity_threshold = 0.99") be the best way to run such a query or is there a better way to do it with Ash?

That would be the best way :slight_smile:

1 Like