Skip to content

Commit 0f87606

Browse files
committed
Simplify API and tests
1 parent e968af6 commit 0f87606

12 files changed

Lines changed: 62 additions & 123 deletions

File tree

integration_test/myxql/migrations_test.exs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ defmodule Ecto.Integration.MigrationsTest do
2525
@version_insert ~s[INSERT INTO `schema_migrations`]
2626
@version_delete ~s[DELETE s0.* FROM `schema_migrations`]
2727

28-
test "logs locking and transaction commands when log_all: true" do
28+
test "logs locking and transaction commands" do
2929
num = @base_migration + System.unique_integer([:positive])
3030
up_log =
3131
capture_log(fn ->
32-
Ecto.Migrator.up(PoolRepo, num, NormalMigration, log_all: true, log_sql: :info, log: :info)
32+
Ecto.Migrator.up(PoolRepo, num, NormalMigration, log_migrator_sql: :info, log_migrations_sql: :info, log: :info)
3333
end)
3434

3535
assert up_log =~ "begin []"
@@ -42,7 +42,7 @@ defmodule Ecto.Integration.MigrationsTest do
4242

4343
down_log =
4444
capture_log(fn ->
45-
Ecto.Migrator.down(PoolRepo, num, NormalMigration, log_all: true, log_sql: :info, log: :info)
45+
Ecto.Migrator.down(PoolRepo, num, NormalMigration, log_migrator_sql: :info, log_migrations_sql: :info, log: :info)
4646
end)
4747

4848
assert down_log =~ "begin []"
@@ -54,36 +54,7 @@ defmodule Ecto.Integration.MigrationsTest do
5454
assert down_log =~ "commit []"
5555
end
5656

57-
test "does not log locking and transaction commands when log_sql is true" do
58-
num = @base_migration + System.unique_integer([:positive])
59-
up_log =
60-
capture_log(fn ->
61-
Ecto.Migrator.up(PoolRepo, num, NormalMigration, log_sql: :info, log: :info)
62-
end)
63-
64-
refute up_log =~ "begin []"
65-
refute up_log =~ @get_lock_command
66-
assert up_log =~ @create_table_sql
67-
assert up_log =~ @create_table_log
68-
refute up_log =~ @release_lock_command
69-
refute up_log =~ @version_insert
70-
refute up_log =~ "commit []"
71-
72-
down_log =
73-
capture_log(fn ->
74-
Ecto.Migrator.down(PoolRepo, num, NormalMigration, log_sql: :info, log: :info)
75-
end)
76-
77-
refute down_log =~ "begin []"
78-
refute down_log =~ @get_lock_command
79-
assert down_log =~ @drop_table_sql
80-
assert down_log =~ @drop_table_log
81-
refute down_log =~ @release_lock_command
82-
refute down_log =~ @version_delete
83-
refute down_log =~ "commit []"
84-
end
85-
86-
test ~s(does not log sql when log is default) do
57+
test "does not log sql when log is default" do
8758
num = @base_migration + System.unique_integer([:positive])
8859
up_log =
8960
capture_log(fn ->

integration_test/pg/migrations_test.exs

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ defmodule Ecto.Integration.MigrationsTest do
4343
@version_insert ~s(INSERT INTO "schema_migrations")
4444
@version_delete ~s(DELETE FROM "schema_migrations")
4545

46-
test "logs locking and transaction commands when log_all: true" do
46+
test "logs locking and transaction commands" do
4747
num = @base_migration + System.unique_integer([:positive])
4848
up_log =
4949
capture_log(fn ->
50-
Ecto.Migrator.up(PoolRepo, num, NormalMigration, log_all: true, log_sql: :info, log: :info)
50+
Ecto.Migrator.up(PoolRepo, num, NormalMigration, log_migrator_sql: :info, log_migrations_sql: :info, log: :info)
5151
end)
5252

5353
assert Regex.scan(~r/(begin \[\])/, up_log) |> length() == 2
@@ -59,7 +59,7 @@ defmodule Ecto.Integration.MigrationsTest do
5959

6060
down_log =
6161
capture_log(fn ->
62-
Ecto.Migrator.down(PoolRepo, num, NormalMigration, log_all: true, log_sql: :info, log: :info)
62+
Ecto.Migrator.down(PoolRepo, num, NormalMigration, log_migrator_sql: :info, log_migrations_sql: :info, log: :info)
6363
end)
6464

6565
assert down_log =~ "begin []"
@@ -70,34 +70,7 @@ defmodule Ecto.Integration.MigrationsTest do
7070
assert down_log =~ "commit []"
7171
end
7272

73-
test "does not log locking and transaction commands when log_sql is true" do
74-
num = @base_migration + System.unique_integer([:positive])
75-
up_log =
76-
capture_log(fn ->
77-
Ecto.Migrator.up(PoolRepo, num, NormalMigration, log_sql: :info, log: :info)
78-
end)
79-
80-
refute up_log =~ "begin []"
81-
refute up_log =~ @get_lock_command
82-
assert up_log =~ @create_table_sql
83-
assert up_log =~ @create_table_log
84-
refute up_log =~ @version_insert
85-
refute up_log =~ "commit []"
86-
87-
down_log =
88-
capture_log(fn ->
89-
Ecto.Migrator.down(PoolRepo, num, NormalMigration, log_sql: :info, log: :info)
90-
end)
91-
92-
refute down_log =~ "begin []"
93-
refute down_log =~ @get_lock_command
94-
assert down_log =~ @drop_table_sql
95-
assert down_log =~ @drop_table_log
96-
refute down_log =~ @version_delete
97-
refute down_log =~ "commit []"
98-
end
99-
100-
test ~s(does not log sql when log is default) do
73+
test "does not log sql when log is default" do
10174
num = @base_migration + System.unique_integer([:positive])
10275
up_log =
10376
capture_log(fn ->

integration_test/tds/migrations_test.exs

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ defmodule Ecto.Integration.MigrationsTest do
2424
@version_insert ~s(INSERT INTO [schema_migrations])
2525
@version_delete ~s(DELETE s0 FROM [schema_migrations])
2626

27-
test "logs locking and transaction commands when log_all: true" do
27+
test "logs locking and transaction commands" do
2828
num = @base_migration + System.unique_integer([:positive])
2929
up_log =
3030
capture_log(fn ->
31-
Ecto.Migrator.up(PoolRepo, num, NormalMigration, log_all: true, log_sql: :info, log: :info)
31+
Ecto.Migrator.up(PoolRepo, num, NormalMigration, log_migrator_sql: :info, log_migrations_sql: :info, log: :info)
3232
end)
3333

3434
assert Regex.scan(~r/(begin \[\])/, up_log) |> length() == 2
@@ -40,7 +40,7 @@ defmodule Ecto.Integration.MigrationsTest do
4040

4141
down_log =
4242
capture_log(fn ->
43-
Ecto.Migrator.down(PoolRepo, num, NormalMigration, log_all: true, log_sql: :info, log: :info)
43+
Ecto.Migrator.down(PoolRepo, num, NormalMigration, log_migrator_sql: :info, log_migrations_sql: :info, log: :info)
4444
end)
4545

4646
assert Regex.scan(~r/(begin \[\])/, up_log) |> length() == 2
@@ -51,34 +51,7 @@ defmodule Ecto.Integration.MigrationsTest do
5151
assert Regex.scan(~r/(commit \[\])/, up_log) |> length() == 2
5252
end
5353

54-
test "does not log locking and transaction commands when log_sql is true" do
55-
num = @base_migration + System.unique_integer([:positive])
56-
up_log =
57-
capture_log(fn ->
58-
Ecto.Migrator.up(PoolRepo, num, NormalMigration, log_sql: :info, log: :info)
59-
end)
60-
61-
refute up_log =~ "begin []"
62-
refute up_log =~ @get_lock_command
63-
assert up_log =~ @create_table_sql
64-
assert up_log =~ @create_table_log
65-
refute up_log =~ @version_insert
66-
refute up_log =~ "commit []"
67-
68-
down_log =
69-
capture_log(fn ->
70-
Ecto.Migrator.down(PoolRepo, num, NormalMigration, log_sql: :info, log: :info)
71-
end)
72-
73-
refute down_log =~ "begin []"
74-
refute down_log =~ @get_lock_command
75-
assert down_log =~ @drop_table_sql
76-
assert down_log =~ @drop_table_log
77-
refute down_log =~ @version_delete
78-
refute down_log =~ "commit []"
79-
end
80-
81-
test ~s(does not log sql when log is default) do
54+
test "does not log sql when log is default" do
8255
num = @base_migration + System.unique_integer([:positive])
8356
up_log =
8457
capture_log(fn ->

lib/ecto/adapters/myxql.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ defmodule Ecto.Adapters.MyXQL do
231231
Ecto.Adapters.SQL.raise_migration_pool_size_error()
232232
end
233233

234-
opts = Ecto.Adapters.SQL.log_options(opts) ++ [timeout: :infinity]
234+
opts = Ecto.Adapters.SQL.migrator_log_options(opts) ++ [timeout: :infinity]
235235

236236
{:ok, result} =
237237
transaction(meta, opts, fn ->

lib/ecto/adapters/postgres.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ defmodule Ecto.Adapters.Postgres do
209209
Ecto.Adapters.SQL.raise_migration_pool_size_error()
210210
end
211211

212-
opts = Ecto.Adapters.SQL.log_options(opts) ++ [timeout: :infinity]
212+
opts = Ecto.Adapters.SQL.migrator_log_options(opts) ++ [timeout: :infinity]
213213

214214
{:ok, result} =
215215
transaction(meta, opts, fn ->

lib/ecto/adapters/sql.ex

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -873,12 +873,8 @@ defmodule Ecto.Adapters.SQL do
873873
## Log
874874

875875
@doc false
876-
def log_options(opts) do
877-
if opts[:log_sql] && opts[:log_all] do
878-
[log: opts[:log_sql]]
879-
else
880-
[log: false]
881-
end
876+
def migrator_log_options(opts) do
877+
[log: Keyword.get(opts, :log_migrator_sql, false)]
882878
end
883879

884880
defp with_log(telemetry, params, opts) do

lib/ecto/adapters/tds.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ defmodule Ecto.Adapters.Tds do
284284
Ecto.Adapters.SQL.raise_migration_pool_size_error()
285285
end
286286

287-
opts = Ecto.Adapters.SQL.log_options(opts) ++ [timeout: :infinity]
287+
opts = Ecto.Adapters.SQL.migrator_log_options(opts) ++ [timeout: :infinity]
288288

289289
{:ok, result} =
290290
transaction(meta, opts, fn ->

lib/ecto/migration/runner.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule Ecto.Migration.Runner do
1414
"""
1515
def run(repo, config, version, module, direction, operation, migrator_direction, opts) do
1616
level = Keyword.get(opts, :log, :info)
17-
sql = Keyword.get(opts, :log_sql, false)
17+
sql = Keyword.get(opts, :log_migrations_sql, false)
1818
log = %{level: level, sql: sql}
1919
args = {self(), repo, config, module, direction, migrator_direction, log}
2020

lib/ecto/migration/schema_migration.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ defmodule Ecto.Migration.SchemaMigration do
5656
end
5757

5858
defp default_opts(opts) do
59-
Keyword.merge(@default_opts, [prefix: opts[:prefix]] ++ Ecto.Adapters.SQL.log_options(opts))
59+
Keyword.merge(@default_opts, [prefix: opts[:prefix]] ++ Ecto.Adapters.SQL.migrator_log_options(opts))
6060
end
6161
end

lib/ecto/migrator.ex

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,11 @@ 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-
* `:log_sql` - the level to use for logging of SQL instructions.
207-
Defaults to `false`. Can be any of `Logger.level/0` values or a boolean.
206+
* `:log_migrations_sql` - the level to use for logging of SQL commands
207+
generated by migrations. Defaults to `false`. Can be any of `Logger.level/0` values
208+
or a boolean.
209+
* `:log_migrator_sql` - the level to use for logging of SQL commands emitted
210+
by the migrator, such as transactions, locks, etc. Defaults to `false`.
208211
* `:prefix` - the prefix to run the migrations on
209212
* `:dynamic_repo` - the name of the Repo supervisor process.
210213
See `c:Ecto.Repo.put_dynamic_repo/1`.
@@ -213,6 +216,14 @@ defmodule Ecto.Migrator do
213216
"""
214217
@spec up(Ecto.Repo.t, integer, module, Keyword.t) :: :ok | :already_up
215218
def up(repo, version, module, opts \\ []) do
219+
opts =
220+
if log_sql = opts[:log_sql] do
221+
IO.warn(":log_sql is deprecated, please use log_migrations_sql instead")
222+
Keyword.put(opts, :log_migrations_sql, log_sql)
223+
else
224+
opts
225+
end
226+
216227
conditional_lock_for_migrations module, version, repo, opts, fn config, versions ->
217228
if version in versions do
218229
:already_up
@@ -260,17 +271,29 @@ defmodule Ecto.Migrator do
260271
261272
## Options
262273
263-
* `:log` - the level to use for logging. Defaults to `:info`.
274+
* `:log` - the level to use for logging of migration commands. Defaults to `:info`.
275+
Can be any of `Logger.level/0` values or a boolean.
276+
* `: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.
279+
* `:log_migrator_sql` - the level to use for logging of SQL commands emitted
280+
by the migrator, such as transactions, locks, etc. Defaults to `false`.
264281
Can be any of `Logger.level/0` values or a boolean.
265-
* `:log_sql` - the level to use for logging of SQL instructions.
266-
Defaults to `false`. Can be any of `Logger.level/0` values or a boolean.
267282
* `:prefix` - the prefix to run the migrations on
268283
* `:dynamic_repo` - the name of the Repo supervisor process.
269284
See `c:Ecto.Repo.put_dynamic_repo/1`.
270285
271286
"""
272287
@spec down(Ecto.Repo.t, integer, module) :: :ok | :already_down
273288
def down(repo, version, module, opts \\ []) do
289+
opts =
290+
if log_sql = opts[:log_sql] do
291+
IO.warn(":log_sql is deprecated, please use log_migrations_sql instead")
292+
Keyword.put(opts, :log_migrations_sql, log_sql)
293+
else
294+
opts
295+
end
296+
274297
conditional_lock_for_migrations module, version, repo, opts, fn config, versions ->
275298
if version in versions do
276299
do_down(repo, config, version, module, opts)
@@ -311,7 +334,7 @@ defmodule Ecto.Migrator do
311334
fun.()
312335
else
313336
{:ok, result} =
314-
repo.transaction(fun, Ecto.Adapters.SQL.log_options(opts) ++ [timeout: :infinity])
337+
repo.transaction(fun, Ecto.Adapters.SQL.migrator_log_options(opts) ++ [timeout: :infinity])
315338

316339
result
317340
end

0 commit comments

Comments
 (0)