Skip to content

[codex] Finalize returned SQLite queries - #4

Draft
wtsnz wants to merge 2 commits into
mainfrom
codex/data-layer-return-query
Draft

[codex] Finalize returned SQLite queries#4
wtsnz wants to merge 2 commits into
mainfrom
codex/data-layer-return-query

Conversation

@wtsnz

@wtsnz wtsnz commented May 19, 2026

Copy link
Copy Markdown
Owner

Summary

This adds AshSqlite.DataLayer.return_query/2 and delegates returned Ecto queries through the shared AshSql.Query.return_query/2 path after applying the normal Ash SQL bindings.

Why

I ran into this while looking into generated SQLite queries from Ash. Normal reads go through run_query/2, where AshSqlite has a chance to finalize the query before it executes. But callers that ask Ash for the data-layer query directly do not go through that execution path.

That means some query state that has been collected in Ash SQL bindings, such as sort/finalization state, can be left unapplied on the returned Ecto query.

I check and AshPostgres already has this same callback shape, so I figured it makes sense to bring AshSqlite into line with that behaviour.

Validation

  • MIX_ENV=test mix test
  • mix format --check-formatted lib/data_layer.ex test/sort_test.exs

wtsnz added 2 commits May 19, 2026 16:31
Implement the Ash data-layer return_query/2 callback for AshSqlite by delegating returned Ecto queries through AshSql.Query.return_query/2 after applying default SQL bindings. Normal reads already finalize queries in run_query/2 before execution, but callers that ask Ash for the data-layer query need the same finalization so stored sorts and related query rewrites are visible in the returned Ecto query.

Add a regression test that a sorted Ash query returned through Ash.Query.data_layer_query/1 includes the expected ORDER BY clause.
Drop the dedicated SQL-shape regression test from the return_query callback PR. AshPostgres has the same callback without a direct return_query test, so keep this PR focused on mirroring the data-layer behavior and leave broader returned-query coverage to follow-up aggregate work.
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.

1 participant