Skip to content

Add mix ecto.query task#4731

Closed
cdvx wants to merge 1 commit into
elixir-ecto:masterfrom
cdvx:add-mix-ecto-query-task
Closed

Add mix ecto.query task#4731
cdvx wants to merge 1 commit into
elixir-ecto:masterfrom
cdvx:add-mix-ecto-query-task

Conversation

@cdvx

@cdvx cdvx commented May 29, 2026

Copy link
Copy Markdown

Refs #4719

  • adds mix ecto.query to evaluate an Ecto query expression, run it through the selected/default repo inside a read-only transaction, and print schema level data.

Refs elixir-ecto#4719
- adds mix ecto.query to evaluate an Ecto query expression, run it through the
  selected/default repo inside a read-only transaction, and print schema level data.
Comment on lines +147 to +151
defmodule Mix.Tasks.Ecto.Query.Schema do
@moduledoc false

defstruct [:schema, :fields]
end

@v0idpwn v0idpwn Jun 1, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why does this struct need to exist? I think it would probably be nicer to rely on the existing inspect implementation for the structs...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This is only there to keep schema shaped output after dropping __meta__, redacted fields, and association fields. Without it, the cleaned value becomes a map and loses the schema name.
I found the SchemaName{...} shape easier to read when testing, but I can simplify to maps or regular inspect output if preferred.

defp clean_entry(%{__struct__: schema} = struct) do
if function_exported?(schema, :__schema__, 1) do
drop_fields = [
:__meta__ | schema.__schema__(:associations) ++ schema.__schema__(:redact_fields)

@v0idpwn v0idpwn Jun 1, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if associations should only be deleted if they aren't preloaded

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

That's a good point. I interpreted the issue as dropping all association fields, but keeping preloaded associations is more useful. I’ll change it to keep preloaded associations.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Inspect has a default_inspect_fun. I wonder if we can override it during the execution of this task, so we don't have to traverse everything and create a fake Mix.Tasks.Ecto.Query.Schema. Basically we intercept all structs with a __meta__ field pointing to Ecto, otherwise we render them as is.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'll look into overriding default_inspect_fun for this

Comment on lines +14 to +16

If the `:read_only` option is true, the adapter must run the transaction
in read-only mode or raise if read-only transactions are not supported.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I like this option conceptually, but the need to add it makes me wonder if the whole thing should be in ecto_sql 🤔

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yeah, I had the same concern. I started here because the command evaluates an Ecto query and calls Repo.all, so the schema-level output felt like the more natural first pass.
The read_only: true part is the bit that crosses into adapter behavior. My thinking was to document and pass the option through in Ecto, then handle the actual SQL adapter support in an ecto_sql follow-up, together with the --sql option.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Agreed on moving it to ecto_sql!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, in that case I'll close this PR and handle this in ecto_sql

@cdvx

cdvx commented Jun 8, 2026

Copy link
Copy Markdown
Author

to be handled in ecto_sql

@cdvx cdvx closed this Jun 8, 2026
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.

3 participants