Skip to content

Conversation

stevebrambilla
Copy link
Contributor

Instead of splitting the code across AshPostgres and AshSql for this, I realized that we can keep it entirely self-contained in AshPostgres using AshSql's expr callback.

Contributor checklist

Leave anything that you believe does not apply unchecked.

  • I accept the AI Policy, or AI was not used in the creation of this PR.
  • Bug fixes include regression tests
  • Chores
  • Documentation changes
  • Features include unit/acceptance tests
  • Refactoring
  • Update dependencies

Comment on lines +89 to +131
if Ash.Expr.expr?(input) do
frag_parts =
Enum.flat_map(input, fn {key, value} ->
if Ash.Expr.expr?(value) do
[
expr: to_string(key),
raw: "::text, ",
expr: value,
raw: ", "
]
else
[
expr: to_string(key),
raw: "::text, ",
expr: value,
raw: "::jsonb, "
]
end
end)

frag_parts =
List.update_at(frag_parts, -1, fn {:raw, text} ->
{:raw, String.trim_trailing(text, ", ") <> "))"}
end)

AshSql.Expr.dynamic_expr(
query,
%Ash.Query.Function.Fragment{
embedded?: false,
arguments:
[
raw: "jsonb_build_object('exception', ",
expr: inspect(exception),
raw: "::text, 'input', jsonb_build_object("
] ++
frag_parts
},
bindings,
embedded?,
nil,
acc
)
else
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit has a bit of duplication with AshSql for now, but some of this will be reworked soon specifically for immutable raise errors so it's good that it's separate

end

# Returns a row-dependent token to prevent constant-folding for immutable functions.
defp immutable_error_expr_token(query, bindings) do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is moved from AshSql, will revert the code I added there last week

[
{:ash, ash_version("~> 3.5 and >= 3.5.35")},
{:spark, "~> 2.3 and >= 2.3.4"},
# TODO: bump to next ash_sql release
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this in the previous PR, and since the immutable error expressions are self-contained within AshPostgres now, there's no hard requirement to bump the AshSql version for this

Though you may still want to for that other typing bugfix

@zachdaniel zachdaniel merged commit cf0d1df into ash-project:main Oct 9, 2025
61 of 64 checks passed
@zachdaniel
Copy link
Contributor

🚀 Thank you for your contribution! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants