Skip to content

Commit 7f7cd02

Browse files
authored
Raise on unknown column opts (#752)
1 parent dd1506d commit 7f7cd02

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

lib/ecto/migration.ex

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,24 @@ defmodule Ecto.Migration do
11761176
Runner.prefix()
11771177
end
11781178

1179+
@add_remove_column_opts [
1180+
:primary_key,
1181+
:default,
1182+
:null,
1183+
:size,
1184+
:precision,
1185+
:scale,
1186+
:comment,
1187+
:collation,
1188+
:after,
1189+
:generated,
1190+
:start_value,
1191+
:increment,
1192+
:fields
1193+
]
1194+
1195+
@modify_column_opts [:from | @add_remove_column_opts]
1196+
11791197
@doc """
11801198
Adds a column when creating or altering a table.
11811199
@@ -1250,6 +1268,7 @@ defmodule Ecto.Migration do
12501268
12511269
"""
12521270
def add(column, type, opts \\ []) when is_atom(column) and is_list(opts) do
1271+
validate_column_opts!(opts, @add_remove_column_opts, "add/3")
12531272
validate_precision_opts!(opts, column)
12541273
validate_type!(type)
12551274
Runner.subcommand({:add, column, type, opts})
@@ -1272,6 +1291,7 @@ defmodule Ecto.Migration do
12721291
12731292
"""
12741293
def add_if_not_exists(column, type, opts \\ []) when is_atom(column) and is_list(opts) do
1294+
validate_column_opts!(opts, @add_remove_column_opts, "add_if_not_exists/3")
12751295
validate_precision_opts!(opts, column)
12761296
validate_type!(type)
12771297
Runner.subcommand({:add_if_not_exists, column, type, opts})
@@ -1433,6 +1453,7 @@ defmodule Ecto.Migration do
14331453
* `:collation` - the collation of the text type.
14341454
"""
14351455
def modify(column, type, opts \\ []) when is_atom(column) and is_list(opts) do
1456+
validate_column_opts!(opts, @modify_column_opts, "modify/3")
14361457
validate_precision_opts!(opts, column)
14371458
validate_type!(type)
14381459
Runner.subcommand({:modify, column, type, opts})
@@ -1471,6 +1492,7 @@ defmodule Ecto.Migration do
14711492
14721493
"""
14731494
def remove(column, type, opts \\ []) when is_atom(column) do
1495+
validate_column_opts!(opts, @add_remove_column_opts, "remove/3")
14741496
validate_type!(type)
14751497
Runner.subcommand({:remove, column, type, opts})
14761498
end
@@ -1747,6 +1769,16 @@ defmodule Ecto.Migration do
17471769
end
17481770
end
17491771

1772+
defp validate_column_opts!(opts, allowed, fun) when is_list(opts) do
1773+
case Enum.find(opts, fn {key, _} -> key not in allowed end) do
1774+
{key, _} ->
1775+
raise ArgumentError, "unknown option #{inspect(key)} given to #{fun}"
1776+
1777+
nil ->
1778+
:ok
1779+
end
1780+
end
1781+
17501782
defp validate_precision_opts!(opts, column) when is_list(opts) do
17511783
if opts[:scale] && !opts[:precision] do
17521784
raise ArgumentError, "column #{Atom.to_string(column)} is missing precision option"

test/ecto/migration_test.exs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,24 @@ defmodule Ecto.MigrationTest do
228228
end
229229
end
230230

231+
test "raises on unknown column options" do
232+
assert_raise ArgumentError, "unknown option :required given to add/3", fn ->
233+
add(:hello, :string, required: true)
234+
end
235+
236+
assert_raise ArgumentError, "unknown option :required given to add_if_not_exists/3", fn ->
237+
add_if_not_exists(:hello, :string, required: true)
238+
end
239+
240+
assert_raise ArgumentError, "unknown option :required given to modify/3", fn ->
241+
modify(:hello, :string, required: true)
242+
end
243+
244+
assert_raise ArgumentError, "unknown option :required given to remove/3", fn ->
245+
remove(:hello, :string, required: true)
246+
end
247+
end
248+
231249
test "flush clears out commands", %{runner: runner} do
232250
execute "TEST"
233251
commands = Agent.get(runner, & &1.commands)

0 commit comments

Comments
 (0)