@@ -176,7 +176,7 @@ defmodule Mix.Tasks.Ecto.QueryTest do
176176 assert Inspect.Opts . default_inspect_fun ( ) == previous_fun
177177 end
178178
179- test "prints SQL and params with --sql" do
179+ test "prints SQL and params with --to- sql" do
180180 Process . put (
181181 :test_repo_to_sql ,
182182 { "SELECT p0.\" id\" FROM \" posts\" AS p0 WHERE (p0.\" id\" = $1)" , [ 1 ] }
@@ -188,7 +188,7 @@ defmodule Mix.Tasks.Ecto.QueryTest do
188188 run ( [
189189 "-r" ,
190190 to_string ( __MODULE__ . PostgresRepo ) ,
191- "--sql" ,
191+ "--to- sql" ,
192192 "from(p in Post, where: p.id == ^1)"
193193 ] )
194194
@@ -202,6 +202,17 @@ defmodule Mix.Tasks.Ecto.QueryTest do
202202 end )
203203 end
204204
205+ test "prints explain plan with --explain" do
206+ Process . put ( :test_repo_explain , "Seq Scan on posts p0" )
207+
208+ run ( [ "-r" , to_string ( __MODULE__ . PostgresRepo ) , "--explain" , inspect ( Post ) ] )
209+
210+ assert_received { :explain , :all , % Ecto.Query { } , [ ] }
211+ refute_received { :all , _query }
212+ refute_received { :read_only_transaction , __MODULE__ . PostgresRepo , _fun , _opts }
213+ assert_received { :mix_shell , :info , [ "Seq Scan on posts p0" ] }
214+ end
215+
205216 test "runs MySQL queries in a read-only transaction" do
206217 Process . put ( :test_repo_all_results , [ [ 1 , "first" , "hunter2" ] ] )
207218
@@ -216,7 +227,7 @@ defmodule Mix.Tasks.Ecto.QueryTest do
216227 test "prints MySQL SQL without starting a transaction" do
217228 Process . put ( :test_repo_to_sql , { "SELECT p0.`id` FROM `posts` AS p0" , [ ] } )
218229
219- run ( [ "-r" , to_string ( __MODULE__ . MyXQLRepo ) , "--sql" , inspect ( Post ) ] )
230+ run ( [ "-r" , to_string ( __MODULE__ . MyXQLRepo ) , "--to- sql" , inspect ( Post ) ] )
220231
221232 refute_received { :myxql_read_only_transaction , __MODULE__ . MyXQLRepo , _fun , _opts }
222233 assert_received { :myxql_to_sql , :all , % Ecto.Query { } , [ ] }
@@ -269,6 +280,20 @@ defmodule Mix.Tasks.Ecto.QueryTest do
269280 end
270281 end
271282
283+ test "raises when explain and to_sql are given together" do
284+ assert_raise Mix.Error ,
285+ "ecto.query expects only one of --explain or --to-sql to be given" ,
286+ fn ->
287+ run ( [
288+ "-r" ,
289+ to_string ( __MODULE__ . PostgresRepo ) ,
290+ "--explain" ,
291+ "--to-sql" ,
292+ inspect ( Post )
293+ ] )
294+ end
295+ end
296+
272297 test "raises when the adapter does not support read-only transactions" do
273298 assert_raise ArgumentError , ~r/ read-only transactions are not supported/ , fn ->
274299 run ( [ "-r" , to_string ( __MODULE__ . NoReadOnlyRepo ) , inspect ( Post ) ] )
@@ -323,6 +348,11 @@ defmodule Mix.Tasks.Ecto.QueryTest do
323348 send ( self ( ) , { :to_sql , operation , queryable , opts } )
324349 Process . get ( :test_repo_to_sql , { "SELECT s0.\" id\" FROM \" posts\" AS s0" , [ ] } )
325350 end
351+
352+ def explain ( operation , queryable , opts \\ [ ] ) do
353+ send ( self ( ) , { :explain , operation , queryable , opts } )
354+ Process . get ( :test_repo_explain , "Seq Scan on posts p0" )
355+ end
326356 end
327357
328358 defmodule PostgresAdapter do
0 commit comments