Skip to content

Commit 8d43286

Browse files
authored
Improve error messages for type validation in migrations (#484)
* Remove suggestion to use Ecto.Type in migrations * Improve invalid migration type error messages * Fix tests for error message changes
1 parent 76342dc commit 8d43286

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

lib/ecto/migration.ex

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,9 +1395,7 @@ defmodule Ecto.Migration do
13951395
defp validate_type!(type) when is_atom(type) do
13961396
case Atom.to_string(type) do
13971397
"Elixir." <> _ ->
1398-
raise ArgumentError,
1399-
"#{inspect type} is not a valid database type, " <>
1400-
"please use an atom like :string, :text and so on"
1398+
raise_invalid_migration_type!(type)
14011399
_ ->
14021400
:ok
14031401
end
@@ -1416,17 +1414,23 @@ defmodule Ecto.Migration do
14161414
end
14171415

14181416
defp validate_type!(type) do
1417+
raise_invalid_migration_type!(type)
1418+
end
1419+
1420+
defp raise_invalid_migration_type!(type) do
14191421
raise ArgumentError, """
14201422
invalid migration type: #{inspect(type)}. Expected one of:
14211423
14221424
* an atom, such as :string
14231425
* a quoted atom, such as :"integer unsigned"
1424-
* an Ecto.Type, such as Ecto.UUID
1425-
* a tuple of the above, such as {:array, :integer} or {:array, Ecto.UUID}
1426+
* a tuple representing a composite type, such as {:array, :integer} or {:map, :string}
14261427
* a reference, such as references(:users)
14271428
1428-
All Ecto types are allowed and properly translated.
1429-
All other types are sent to the database as is.
1429+
Ecto types are automatically translated to database types. All other types
1430+
are sent to the database as is.
1431+
1432+
Types defined through Ecto.Type or Ecto.ParameterizedType aren't allowed,
1433+
use their underlying types instead.
14301434
"""
14311435
end
14321436

test/ecto/migration_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ defmodule Ecto.MigrationTest do
146146
end
147147

148148
test "chokes on alias types" do
149-
assert_raise ArgumentError, ~r"Ecto.DateTime is not a valid database type", fn ->
149+
assert_raise ArgumentError, ~r"invalid migration type: Ecto.DateTime", fn ->
150150
add(:hello, Ecto.DateTime)
151151
end
152152
end
@@ -413,7 +413,7 @@ defmodule Ecto.MigrationTest do
413413
end
414414

415415
test "forward: column modifications invoke type validations" do
416-
assert_raise ArgumentError, ~r"Ecto.DateTime is not a valid database type", fn ->
416+
assert_raise ArgumentError, ~r"invalid migration type: Ecto.DateTime", fn ->
417417
alter table(:posts) do
418418
modify(:hello, Ecto.DateTime)
419419
end

0 commit comments

Comments
 (0)