Skip to content

Commit 45d9db1

Browse files
committed
Move to --log-migrations-sql and --log-migrator-sql
1 parent 0f87606 commit 45d9db1

6 files changed

Lines changed: 34 additions & 66 deletions

File tree

lib/ecto/migration/runner.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,16 @@ defmodule Ecto.Migration.Runner do
343343
{:ok, logs} = repo.__adapter__().execute_ddl(meta, command, timeout: :infinity, log: sql)
344344

345345
Enum.each(logs, fn {level, message, metadata} ->
346-
Logger.log(level, message, metadata)
346+
log(level, message, metadata)
347347
end)
348348

349349
:ok
350350
end
351351

352-
defp log(false, _msg), do: :ok
353-
defp log(level, msg), do: Logger.log(level, msg)
352+
defp log(level, msg, metadata \\ [])
353+
defp log(false, _msg, _metadata), do: :ok
354+
defp log(true, msg, metadata), do: Logger.log(:info, msg, metadata)
355+
defp log(level, msg, metadata), do: Logger.log(level, msg, metadata)
354356

355357
defp command(ddl) when is_binary(ddl) or is_list(ddl),
356358
do: "execute #{inspect ddl}"

lib/ecto/migrator.ex

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ defmodule Ecto.Migrator do
274274
* `:log` - the level to use for logging of migration commands. Defaults to `:info`.
275275
Can be any of `Logger.level/0` values or a boolean.
276276
* `:log_migrations_sql` - the level to use for logging of SQL commands
277-
generated by migrations. Defaults to `false`. Can be any of `Logger.level/0` values
278-
or a boolean.
277+
generated by migrations. Defaults to `false`. Can be any of `Logger.level/0`
278+
values or a boolean.
279279
* `:log_migrator_sql` - the level to use for logging of SQL commands emitted
280280
by the migrator, such as transactions, locks, etc. Defaults to `false`.
281281
Can be any of `Logger.level/0` values or a boolean.
@@ -405,6 +405,14 @@ defmodule Ecto.Migrator do
405405
"""
406406
@spec run(Ecto.Repo.t, String.t | [String.t] | [{integer, module}], atom, Keyword.t) :: [integer]
407407
def run(repo, migration_source, direction, opts) do
408+
opts =
409+
if log_sql = opts[:log_sql] do
410+
IO.warn(":log_sql is deprecated, please use log_migrations_sql instead")
411+
Keyword.put(opts, :log_migrations_sql, log_sql)
412+
else
413+
opts
414+
end
415+
408416
migration_source = List.wrap(migration_source)
409417

410418
pending =

lib/mix/tasks/ecto.migrate.ex

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ defmodule Mix.Tasks.Ecto.Migrate do
1818
prefix: :string,
1919
pool_size: :integer,
2020
log_sql: :boolean,
21-
log_sql_mode: :string,
21+
log_migrations_sql: :boolean,
22+
log_migrator_sql: :boolean,
2223
strict_version_order: :boolean,
2324
repo: [:keep, :string],
2425
no_compile: :boolean,
@@ -71,11 +72,10 @@ defmodule Mix.Tasks.Ecto.Migrate do
7172
7273
* `--all` - run all pending migrations
7374
74-
* `--log-sql` - log the underlying sql statements for migrations
75+
* `--log-migrations-sql` - log SQL generated by migration commands
7576
76-
* `--log-sql-mode` - how much SQL to log. `"migrations"` logs only the SQL
77-
from commands in the migrations. `"all"` also includes transactions, table
78-
locks, and so on. Defaults to `"migrations"`.
77+
* `--log-migrator-sql` - log SQL generated by the migrator, such as
78+
transactions, table locks, etc
7979
8080
* `--migrations-path` - the path to load the migrations from, defaults to
8181
`"priv/repo/migrations"`. This option may be given multiple times in which
@@ -114,8 +114,10 @@ defmodule Mix.Tasks.Ecto.Migrate do
114114
do: opts,
115115
else: Keyword.put(opts, :all, true)
116116

117-
validate_log_sql_mode!(opts)
118-
opts = conform_log_options(opts)
117+
opts =
118+
if opts[:quiet],
119+
do: Keyword.merge(opts, [log: false, log_migrations_sql: false, log_migrator_sql: false]),
120+
else: opts
119121

120122
# Start ecto_sql explicitly before as we don't need
121123
# to restart those apps if migrated.
@@ -141,35 +143,4 @@ defmodule Mix.Tasks.Ecto.Migrate do
141143

142144
:ok
143145
end
144-
145-
@doc false
146-
def validate_log_sql_mode!(opts) do
147-
case Keyword.get(opts, :log_sql_mode) do
148-
nil -> :ok
149-
"migrations" -> :ok
150-
"all" -> :ok
151-
mode ->
152-
Mix.raise("""
153-
#{inspect(mode)} is not a valid log_sql_mode.
154-
Valid options are: "all", "migrations"
155-
""")
156-
end
157-
end
158-
159-
@doc false
160-
def conform_log_options(opts) do
161-
opts =
162-
if opts[:log_sql_mode] == "all",
163-
do: Keyword.merge(opts, [log_migrations_sql: :info, log_migrator_sql: :info, log: :info]),
164-
else: opts
165-
166-
opts =
167-
if opts[:log_sql_mode] == "migrations",
168-
do: Keyword.merge(opts, [log_migrations_sql: :info, log: :info]),
169-
else: opts
170-
171-
if opts[:quiet],
172-
do: Keyword.merge(opts, [log: false]),
173-
else: opts
174-
end
175146
end

lib/mix/tasks/ecto.rollback.ex

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ defmodule Mix.Tasks.Ecto.Rollback do
1818
prefix: :string,
1919
pool_size: :integer,
2020
log_sql: :boolean,
21-
log_sql_mode: :string,
21+
log_migrations_sql: :boolean,
22+
log_migrator_sql: :boolean,
2223
repo: [:keep, :string],
2324
no_compile: :boolean,
2425
no_deps_check: :boolean,
@@ -67,11 +68,10 @@ defmodule Mix.Tasks.Ecto.Rollback do
6768
6869
* `--all` - run all pending migrations
6970
70-
* `--log-sql` - log the underlying sql statements for migrations
71+
* `--log-migrations-sql` - log SQL generated by migration commands
7172
72-
* `--log-sql-mode` - how much SQL to log. `"migrations"` logs only the SQL
73-
from commands in the migrations. `"all"` also includes transactions, table
74-
locks, and so on. Defaults to `"migrations"`.
73+
* `--log-migrator-sql` - log SQL generated by the migrator, such as
74+
transactions, table locks, etc
7575
7676
* `--migrations-path` - the path to load the migrations from, defaults to
7777
`"priv/repo/migrations"`. This option may be given multiple times in which
@@ -103,16 +103,17 @@ defmodule Mix.Tasks.Ecto.Rollback do
103103
@impl true
104104
def run(args, migrator \\ &Ecto.Migrator.run/4) do
105105
repos = parse_repo(args)
106-
{opts, _} = OptionParser.parse! args, strict: @switches, aliases: @aliases
106+
{opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
107107

108108
opts =
109109
if opts[:to] || opts[:step] || opts[:all],
110110
do: opts,
111111
else: Keyword.put(opts, :step, 1)
112112

113-
Mix.Tasks.Ecto.Migrate.validate_log_sql_mode!(opts)
114-
115-
opts = Mix.Tasks.Ecto.Migrate.conform_log_options(opts)
113+
opts =
114+
if opts[:quiet],
115+
do: Keyword.merge(opts, [log: false, log_migrations_sql: false, log_migrator_sql: false]),
116+
else: opts
116117

117118
# Start ecto_sql explicitly before as we don't need
118119
# to restart those apps if migrated.

test/mix/tasks/ecto.migrate_test.exs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,6 @@ defmodule Mix.Tasks.Ecto.MigrateTest do
126126
assert !Process.get(:started)
127127
end
128128

129-
test "raises when invalid log_sql_mode is given" do
130-
assert_raise Mix.Error, fn ->
131-
run ["--log-sql-mode", "invalid"], fn _, _, _, _ -> [] end
132-
end
133-
assert !Process.get(:started)
134-
end
135-
136129
test "uses custom paths" do
137130
path1 = Path.join([unquote(tmp_path()), inspect(Ecto.Migrate), "migrations_1"])
138131
path2 = Path.join([unquote(tmp_path()), inspect(Ecto.Migrate), "migrations_2"])

test/mix/tasks/ecto.rollback_test.exs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,6 @@ defmodule Mix.Tasks.Ecto.RollbackTest do
9090
assert !Process.get(:started)
9191
end
9292

93-
test "raises when invalid log_sql_mode is given" do
94-
assert_raise Mix.Error, fn ->
95-
run ["--log-sql-mode", "invalid"], fn _, _, _, _ -> [] end
96-
end
97-
assert !Process.get(:started)
98-
end
99-
10093
test "uses custom paths" do
10194
path1 = Path.join([unquote(tmp_path()), inspect(Ecto.Migrate), "migrations_1"])
10295
path2 = Path.join([unquote(tmp_path()), inspect(Ecto.Migrate), "migrations_2"])

0 commit comments

Comments
 (0)