Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn for migration files that end in ".ex" #600

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions lib/ecto/migrator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ defmodule Ecto.Migrator do
migration_source
|> Enum.flat_map(fn
directory when is_binary(directory) ->
Path.join([directory, "**", "*.exs"])
Path.join([directory, "**", "*.{ex,exs}"])
|> Path.wildcard()
|> Enum.map(&extract_migration_info/1)
|> Enum.filter(& &1)
Expand All @@ -677,8 +677,25 @@ defmodule Ecto.Migrator do
base = Path.basename(file)

case Integer.parse(Path.rootname(base)) do
{integer, "_" <> name} -> {integer, name, file}
_ -> nil
{integer, "_" <> name} ->
if Path.extname(base) == ".ex" do
# See: https://github.com/elixir-ecto/ecto_sql/issues/599
IO.warn(
"""
file 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.\
""",
file: file
)

nil
else
{integer, name, file}
end

_ ->
nil
end
end

Expand Down
14 changes: 14 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,19 @@ 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 looks like a migration but ends in .ex"
assert output =~ "123_looks_like_migration.ex"
end)
end

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