Skip to content

Commit

Permalink
Warn for migration files that end in ".ex"
Browse files Browse the repository at this point in the history
Closes 599.
  • Loading branch information
whatyouhide committed Mar 28, 2024
1 parent 5f144a9 commit a17d567
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/ecto/migrator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,8 @@ defmodule Ecto.Migrator do
migration_source
|> Enum.flat_map(fn
directory when is_binary(directory) ->
warn_for_misnamed_files(directory)

Path.join([directory, "**", "*.exs"])
|> Path.wildcard()
|> Enum.map(&extract_migration_info/1)
Expand All @@ -673,6 +675,22 @@ defmodule Ecto.Migrator do
|> Enum.sort()
end

# See: https://github.com/elixir-ecto/ecto_sql/issues/599
defp warn_for_misnamed_files(directory) do
[directory, "**", "*.ex"]
|> Path.join()
|> Path.wildcard()
|> Enum.each(fn path ->
if Path.basename(path) =~ ~r/\d+_/ do
IO.warn("""
File #{Path.relative_to_cwd(path)} looks like a migration but ends in .ex. \
Migration files should end in .exs. Use "mix ecto.gen.migration" to generate \
migration files with the correct extension.\
""")
end
end)
end

defp extract_migration_info(file) do
base = Path.basename(file)

Expand Down
13 changes: 13 additions & 0 deletions test/ecto/migrator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Ecto.MigratorTest do

import Support.FileHelpers
import Ecto.Migrator
import ExUnit.CaptureIO
import ExUnit.CaptureLog

alias EctoSQL.TestRepo
Expand Down Expand Up @@ -405,6 +406,18 @@ defmodule Ecto.MigratorTest do
end
end

test "warns for .ex files that look like migrations" do
in_tmp(fn path ->
output =
capture_io(:stderr, fn ->
create_migration("123_looks_like_migration.ex")
assert run(TestRepo, path, :up, all: true, log: false) == []
end)

assert output =~ "File 123_looks_like_migration.ex looks like a migration"
end)
end

describe "lock for migrations" do
setup do
put_test_adapter_config(test_process: self())
Expand Down

0 comments on commit a17d567

Please sign in to comment.