Skip to content

Commit 389f855

Browse files
authored
Silence DDL log / notices when migration :log option is false (#478)
1 parent c936e6d commit 389f855

5 files changed

Lines changed: 35 additions & 7 deletions

File tree

integration_test/pg/migrations_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ defmodule Ecto.Integration.MigrationsTest do
4444
log =
4545
capture_log(fn ->
4646
num = @base_migration + System.unique_integer([:positive])
47-
Ecto.Migrator.up(PoolRepo, num, DuplicateTableMigration, log: false)
47+
Ecto.Migrator.up(PoolRepo, num, DuplicateTableMigration, log: :info)
4848
end)
4949

5050
assert log =~ ~s(relation "duplicate_table" already exists, skipping)

lib/ecto/migration/runner.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,16 @@ defmodule Ecto.Migration.Runner do
325325
meta = Ecto.Adapter.lookup_meta(repo.get_dynamic_repo())
326326
{:ok, logs} = repo.__adapter__().execute_ddl(meta, command, timeout: :infinity, log: sql)
327327

328-
Enum.each(logs, fn {level, message, metadata} ->
329-
log(level, message, metadata)
328+
Enum.each(logs, fn {ddl_log_level, message, metadata} ->
329+
ddl_log(ddl_log_level, level, message, metadata)
330330
end)
331331

332332
:ok
333333
end
334334

335+
defp ddl_log(_level, false, _msg, _metadata), do: :ok
336+
defp ddl_log(level, _, msg, metadata), do: log(level, msg, metadata)
337+
335338
defp log(level, msg, metadata \\ [])
336339
defp log(false, _msg, _metadata), do: :ok
337340
defp log(true, msg, metadata), do: Logger.log(:info, msg, metadata)

lib/ecto/migrator.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ defmodule Ecto.Migrator do
203203
204204
* `:log` - the level to use for logging of migration instructions.
205205
Defaults to `:info`. Can be any of `Logger.level/0` values or a boolean.
206+
If `false`, it also avoids logging messages from the database.
206207
* `:log_migrations_sql` - the level to use for logging of SQL commands
207208
generated by migrations. Can be any of the `Logger.level/0` values
208209
or a boolean. If `false`, logging is disabled. If `true`, uses the configured

test/ecto/migrator_test.exs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ defmodule Ecto.MigratorTest do
240240
lines = String.split(output, "\n")
241241
assert Enum.at(lines, 1) =~ "== Running #{num} #{inspect(module)}.change/0"
242242
assert Enum.at(lines, 3) =~ ~s[execute "select 'This is a first part of ecto.#{name}';"]
243-
assert Enum.at(lines, 5) =~ "select 'In the middle of ecto.#{name}';"
244-
assert Enum.at(lines, 7) =~ ~s[execute "select 'This is a second part of ecto.#{name}';"]
245-
assert Enum.at(lines, 9) =~ ~r"Migrated #{num} in \d.\ds"
243+
assert Enum.at(lines, 7) =~ "select 'In the middle of ecto.#{name}';"
244+
assert Enum.at(lines, 9) =~ ~s[execute "select 'This is a second part of ecto.#{name}';"]
245+
assert Enum.at(lines, 13) =~ ~r"Migrated #{num} in \d.\ds"
246246
end
247247
end
248248

@@ -333,6 +333,30 @@ defmodule Ecto.MigratorTest do
333333
assert output =~ ~r"== Migrated 12 in \d.\ds"
334334
end
335335

336+
test "logs ddl notices" do
337+
output = capture_log fn ->
338+
:ok = up(TestRepo, 10, ChangeMigration)
339+
end
340+
assert output =~ "execute ddl"
341+
342+
output = capture_log fn ->
343+
:ok = down(TestRepo, 10, ChangeMigration)
344+
end
345+
assert output =~ "execute ddl"
346+
end
347+
348+
test "silences ddl notices when log is set to false" do
349+
output = capture_log fn ->
350+
:ok = up(TestRepo, 10, ChangeMigration, log: false)
351+
end
352+
refute output =~ "execute ddl"
353+
354+
output = capture_log fn ->
355+
:ok = down(TestRepo, 10, ChangeMigration, log: false)
356+
end
357+
refute output =~ "execute ddl"
358+
end
359+
336360
test "up raises error in strict mode" do
337361
assert_raise Ecto.MigrationError, fn ->
338362
up(TestRepo, 0, Migration, log: false, strict_version_order: true)

test/test_repo.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ defmodule EctoSQL.TestAdapter do
9191

9292
def execute_ddl(_, command, _) do
9393
Process.put(:last_command, command)
94-
{:ok, []}
94+
{:ok, [{:info, "execute ddl", %{command: command}}]}
9595
end
9696

9797
def supports_ddl_transaction? do

0 commit comments

Comments
 (0)