Skip to content

Conversation

@gmcgibbon
Copy link
Member

@gmcgibbon gmcgibbon commented Nov 19, 2025

Motivation / Background

This Pull Request has been created because Active Record currently requires adapter specific syntax for explain queries when using the FORMAT option. MySQL uses FORMAT=JSON whereas Postgres uses FORMAT JSON. We should be able to accept a hash that maps to the right format syntax as an option.

Detail

This Pull Request changes explain clause building for mysql and postgres to map hashes to the correct syntax. Example:

Car.all.explain(format: :json)
# or
Car.connection.explain(query, [], [{format: :json}]

Additional information

I previously tried gsub-ing the SQL string but I think this approach with hashes is cleaner.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@gmcgibbon
Copy link
Member Author

gmcgibbon commented Nov 19, 2025

I think I actually like the hash syntax better. I'll update the PR to use that instead of mangling SQL.

@gmcgibbon gmcgibbon force-pushed the explain_adapter_specific_options branch from 0c87f48 to 0282ea6 Compare November 19, 2025 00:56
@gmcgibbon gmcgibbon changed the title Make explain accept any format syntax Make explain accept hash format syntax Nov 19, 2025
@gmcgibbon gmcgibbon force-pushed the explain_adapter_specific_options branch 3 times, most recently from 0d6fc0d to 23b592b Compare November 19, 2025 19:11
MySQL uses FORMAT=JSON whereas Postgres uses FORMAT JSON. We should be
able to accept a hash that maps to either formats as options.
@gmcgibbon gmcgibbon force-pushed the explain_adapter_specific_options branch from 23b592b to ef8c563 Compare November 19, 2025 21:52
@gmcgibbon gmcgibbon merged commit 990198b into rails:main Nov 19, 2025
4 checks passed
@gmcgibbon gmcgibbon deleted the explain_adapter_specific_options branch November 19, 2025 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant