Skip to content

Commit 11dafbb

Browse files
authored
Warn for migration files that end in ".ex" (#600)
1 parent 5f144a9 commit 11dafbb

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

lib/ecto/migrator.ex

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ defmodule Ecto.Migrator do
662662
migration_source
663663
|> Enum.flat_map(fn
664664
directory when is_binary(directory) ->
665-
Path.join([directory, "**", "*.exs"])
665+
Path.join([directory, "**", "*.{ex,exs}"])
666666
|> Path.wildcard()
667667
|> Enum.map(&extract_migration_info/1)
668668
|> Enum.filter(& &1)
@@ -677,8 +677,25 @@ defmodule Ecto.Migrator do
677677
base = Path.basename(file)
678678

679679
case Integer.parse(Path.rootname(base)) do
680-
{integer, "_" <> name} -> {integer, name, file}
681-
_ -> nil
680+
{integer, "_" <> name} ->
681+
if Path.extname(base) == ".ex" do
682+
# See: https://github.com/elixir-ecto/ecto_sql/issues/599
683+
IO.warn(
684+
"""
685+
file looks like a migration but ends in .ex. \
686+
Migration files should end in .exs. Use "mix ecto.gen.migration" to generate \
687+
migration files with the correct extension.\
688+
""",
689+
file: file
690+
)
691+
692+
nil
693+
else
694+
{integer, name, file}
695+
end
696+
697+
_ ->
698+
nil
682699
end
683700
end
684701

test/ecto/migrator_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule Ecto.MigratorTest do
33

44
import Support.FileHelpers
55
import Ecto.Migrator
6+
import ExUnit.CaptureIO
67
import ExUnit.CaptureLog
78

89
alias EctoSQL.TestRepo
@@ -405,6 +406,19 @@ defmodule Ecto.MigratorTest do
405406
end
406407
end
407408

409+
test "warns for .ex files that look like migrations" do
410+
in_tmp(fn path ->
411+
output =
412+
capture_io(:stderr, fn ->
413+
create_migration("123_looks_like_migration.ex")
414+
assert run(TestRepo, path, :up, all: true, log: false) == []
415+
end)
416+
417+
assert output =~ "file looks like a migration but ends in .ex"
418+
assert output =~ "123_looks_like_migration.ex"
419+
end)
420+
end
421+
408422
describe "lock for migrations" do
409423
setup do
410424
put_test_adapter_config(test_process: self())

0 commit comments

Comments
 (0)