@@ -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"
0 commit comments