Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
* Allow either explain format syntax for EXPLAIN queries.

MySQL uses FORMAT=JSON whereas Postgres uses FORMAT JSON. We should be
able to accept both formats as options.

*Gannon McGibbon*

* On MySQL parallel test database table reset to use `DELETE` instead of `TRUNCATE`.

Truncating on MySQL is very slow even on empty or nearly empty tables.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def explain(arel, binds = [], options = [])
def build_explain_clause(options = [])
return "EXPLAIN" if options.empty?

options = options.flat_map do |option|
option.is_a?(Hash) ? option.to_a.map { |nested| nested.join("=") } : option
end

explain_clause = "EXPLAIN #{options.join(" ").upcase}"

if analyze_without_explain? && explain_clause.include?("ANALYZE")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def high_precision_current_timestamp
def build_explain_clause(options = [])
return "EXPLAIN" if options.empty?

options = options.flat_map do |option|
option.is_a?(Hash) ? option.to_a.map { |nested| nested.join(" ") } : option
end

"EXPLAIN (#{options.join(", ").upcase})"
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ def test_explain_options_with_eager_loading
assert_match %(#{expected_analyze_clause} SELECT `posts`.* FROM `posts` WHERE `posts`.`author_id` = 1), explain
end

def test_explain_format_option
explain = Author.all.explain(format: :json).inspect

assert_match(/\{.*\}/m, explain)
end

private
def explain_option
supports_analyze? || supports_explain_analyze? ? :analyze : :extended
Expand Down
6 changes: 6 additions & 0 deletions activerecord/test/cases/adapters/postgresql/explain_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ def test_explain_options_with_eager_loading
assert_match %r(EXPLAIN \(ANALYZE\) SELECT "authors"\.\* FROM "authors" WHERE "authors"\."id" = (?:\$1 \[\["id", 1\]\]|1)), explain
assert_match %r(EXPLAIN \(ANALYZE\) SELECT "posts"\.\* FROM "posts" WHERE "posts"\."author_id" = (?:\$1 \[\["author_id", 1\]\]|1)), explain
end

def test_explain_format_option
explain = Author.all.explain(format: :json).inspect

assert_match(/\{.*\}/m, explain)
end
end