Skip to content

Commit dd1506d

Browse files
authored
Fix migrator version bookkeeping on errors (#750)
1 parent 8fa4390 commit dd1506d

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

‎lib/ecto/migrator.ex‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,6 @@ defmodule Ecto.Migrator do
247247
if version in versions do
248248
:already_up
249249
else
250-
result = do_up(repo, config, version, module, opts)
251-
252250
if version != Enum.max([version | versions]) do
253251
latest = Enum.max(versions)
254252

@@ -271,7 +269,7 @@ defmodule Ecto.Migrator do
271269
end
272270
end
273271

274-
result
272+
do_up(repo, config, version, module, opts)
275273
end
276274
end)
277275
end
@@ -333,9 +331,14 @@ defmodule Ecto.Migrator do
333331
dynamic_repo = repo.get_dynamic_repo()
334332

335333
fun_with_status = fn ->
336-
result = fun.()
337-
apply(SchemaMigration, direction, [repo, config, version, opts])
338-
result
334+
case fun.() do
335+
:ok ->
336+
apply(SchemaMigration, direction, [repo, config, version, opts])
337+
:ok
338+
339+
result ->
340+
result
341+
end
339342
end
340343

341344
fn -> run_maybe_in_transaction(repo, dynamic_repo, module, fun_with_status, opts) end

‎test/ecto/migrator_test.exs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ defmodule Ecto.MigratorTest do
396396
assert_raise Ecto.MigrationError, fn ->
397397
up(TestRepo, 0, Migration, log: false, strict_version_order: true)
398398
end
399+
400+
refute {0, nil} in MigrationsAgent.get()
399401
end
400402

401403
test "up invokes the repository adapter with up commands" do
@@ -418,12 +420,16 @@ defmodule Ecto.MigratorTest do
418420
assert_raise Ecto.MigrationError, fn ->
419421
Ecto.Migrator.up(TestRepo, 10, InvalidMigration, log: false)
420422
end
423+
424+
refute {10, nil} in MigrationsAgent.get()
421425
end
422426

423427
test "down raises error when missing down/0 and change/0" do
424428
assert_raise Ecto.MigrationError, fn ->
425429
Ecto.Migrator.down(TestRepo, 1, InvalidMigration, log: false)
426430
end
431+
432+
assert {1, nil} in MigrationsAgent.get()
427433
end
428434

429435
# TODO: Remove when we require Elixir 1.14

0 commit comments

Comments
 (0)