Skip to content

Commit b67cdae

Browse files
committed
Default to :restrict
1 parent c91a10a commit b67cdae

File tree

10 files changed

+88
-104
lines changed

10 files changed

+88
-104
lines changed

lib/ecto/adapter/migration.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ defmodule Ecto.Adapter.Migration do
1616
| {:create, Table.t(), [table_subcommand]}
1717
| {:create_if_not_exists, Table.t(), [table_subcommand]}
1818
| {:alter, Table.t(), [table_subcommand]}
19-
| {:drop, Table.t(), :restrict | :cascade | nil}
20-
| {:drop_if_exists, Table.t(), :restrict | :cascade | nil}
19+
| {:drop, Table.t(), :restrict | :cascade}
20+
| {:drop_if_exists, Table.t(), :restrict | :cascade}
2121
| {:create, Index.t()}
2222
| {:create_if_not_exists, Index.t()}
23-
| {:drop, Index.t(), :restrict | :cascade | nil}
24-
| {:drop_if_exists, Index.t(), :restrict | :cascade | nil}
23+
| {:drop, Index.t(), :restrict | :cascade}
24+
| {:drop_if_exists, Index.t(), :restrict | :cascade}
2525

2626
@typedoc "All commands allowed within the block passed to `table/2`"
2727
@type table_subcommand ::

lib/ecto/adapters/myxql/connection.ex

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -730,14 +730,10 @@ if Code.ensure_loaded?(MyXQL) do
730730
engine_expr(table.engine), options_expr(table.options)]]
731731
end
732732

733-
def execute_ddl({command, %Table{} = table, :cascade}) when command in [:drop, :drop_if_exists] do
734-
[cmd | _] = execute_ddl({command, %Table{} = table, nil})
735-
[cmd ++ [" CASCADE"]]
736-
end
737733

738-
def execute_ddl({command, %Table{} = table, _}) when command in [:drop, :drop_if_exists] do
734+
def execute_ddl({command, %Table{} = table, mode}) when command in [:drop, :drop_if_exists] do
739735
[["DROP TABLE ", if_do(command == :drop_if_exists, "IF EXISTS "),
740-
quote_table(table.prefix, table.name)]]
736+
quote_table(table.prefix, table.name), drop_mode(mode)]]
741737
end
742738

743739
def execute_ddl({:alter, %Table{} = table, changes}) do
@@ -770,7 +766,7 @@ if Code.ensure_loaded?(MyXQL) do
770766
def execute_ddl({:drop, %Index{}, :cascade}),
771767
do: error!(nil, "MySQL adapter does not support cascade in drop index")
772768

773-
def execute_ddl({:drop, %Index{} = index, _}) do
769+
def execute_ddl({:drop, %Index{} = index, :restrict}) do
774770
[["DROP INDEX ",
775771
quote_name(index.name),
776772
" ON ", quote_table(index.prefix, index.table),
@@ -809,6 +805,9 @@ if Code.ensure_loaded?(MyXQL) do
809805
{"SELECT true FROM information_schema.tables WHERE table_name = ? AND table_schema = DATABASE() LIMIT 1", [table]}
810806
end
811807

808+
defp drop_mode(:cascade), do: " CASCADE"
809+
defp drop_mode(:restrict), do: []
810+
812811
defp pk_definitions(columns, prefix) do
813812
pks =
814813
for {_, name, _, opts} <- columns,

lib/ecto/adapters/postgres/connection.ex

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -845,14 +845,9 @@ if Code.ensure_loaded?(Postgrex) do
845845
comments_for_columns(table_name, columns)
846846
end
847847

848-
def execute_ddl({command, %Table{} = table, :cascade}) when command in @drops do
849-
[cmd | _] = execute_ddl({command, table, nil})
850-
[cmd ++ [" CASCADE"]]
851-
end
852-
853-
def execute_ddl({command, %Table{} = table, _}) when command in @drops do
848+
def execute_ddl({command, %Table{} = table, mode}) when command in @drops do
854849
[["DROP TABLE ", if_do(command == :drop_if_exists, "IF EXISTS "),
855-
quote_table(table.prefix, table.name)]]
850+
quote_table(table.prefix, table.name), drop_mode(mode)]]
856851
end
857852

858853
def execute_ddl({:alter, %Table{} = table, changes}) do
@@ -885,16 +880,12 @@ if Code.ensure_loaded?(Postgrex) do
885880
queries ++ comments_on("INDEX", quote_table(index.prefix, index.name), index.comment)
886881
end
887882

888-
def execute_ddl({command, %Index{} = index, :cascade}) when command in @drops do
889-
[cmd | _] = execute_ddl({command, index, nil})
890-
[cmd ++ [" CASCADE"]]
891-
end
892-
893-
def execute_ddl({command, %Index{} = index, _}) when command in @drops do
883+
def execute_ddl({command, %Index{} = index, mode}) when command in @drops do
894884
[["DROP INDEX ",
895885
if_do(index.concurrently, "CONCURRENTLY "),
896886
if_do(command == :drop_if_exists, "IF EXISTS "),
897-
quote_table(index.prefix, index.name)]]
887+
quote_table(index.prefix, index.name),
888+
drop_mode(mode)]]
898889
end
899890

900891
def execute_ddl({:rename, %Table{} = current_table, %Table{} = new_table}) do
@@ -915,14 +906,12 @@ if Code.ensure_loaded?(Postgrex) do
915906
queries ++ comments_on("CONSTRAINT", constraint.name, constraint.comment, table_name)
916907
end
917908

918-
def execute_ddl({:drop, %Constraint{} = constraint, _}) do
919-
[["ALTER TABLE ", quote_table(constraint.prefix, constraint.table),
920-
" DROP CONSTRAINT ", quote_name(constraint.name)]]
921-
end
909+
def execute_ddl({command, %Constraint{}, :cascade}) when command in @drops,
910+
do: error!(nil, "PostgreSQL does not support `CASCADE` in DROP CONSTRAINT commands")
922911

923-
def execute_ddl({:drop_if_exists, %Constraint{} = constraint, _}) do
912+
def execute_ddl({command, %Constraint{} = constraint, :restrict}) when command in @drops do
924913
[["ALTER TABLE ", quote_table(constraint.prefix, constraint.table),
925-
" DROP CONSTRAINT IF EXISTS ", quote_name(constraint.name)]]
914+
" DROP CONSTRAINT ", if_do(command == :drop_if_exists, "IF EXISTS "), quote_name(constraint.name)]]
926915
end
927916

928917
def execute_ddl(string) when is_binary(string), do: [string]
@@ -946,6 +935,9 @@ if Code.ensure_loaded?(Postgrex) do
946935
{"SELECT true FROM information_schema.tables WHERE table_name = $1 AND table_schema = current_schema() LIMIT 1", [table]}
947936
end
948937

938+
defp drop_mode(:cascade), do: " CASCADE"
939+
defp drop_mode(:restrict), do: []
940+
949941
# From https://www.postgresql.org/docs/current/protocol-error-fields.html.
950942
defp ddl_log_level("DEBUG"), do: :debug
951943
defp ddl_log_level("LOG"), do: :info

lib/ecto/adapters/tds/connection.ex

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -974,9 +974,11 @@ if Code.ensure_loaded?(Tds) do
974974
# DDL
975975
alias Ecto.Migration.{Table, Index, Reference, Constraint}
976976

977+
@creates [:create, :create_if_not_exists]
978+
@drops [:drop, :drop_if_exists]
979+
977980
@impl true
978-
def execute_ddl({command, %Table{} = table, columns})
979-
when command in [:create, :create_if_not_exists] do
981+
def execute_ddl({command, %Table{} = table, columns}) when command in @creates do
980982
prefix = table.prefix
981983

982984
pk_name =
@@ -1009,10 +1011,10 @@ if Code.ensure_loaded?(Tds) do
10091011
]
10101012
end
10111013

1012-
def execute_ddl({command, %Table{}, :cascade}) when command in [:drop, :drop_if_exists],
1014+
def execute_ddl({command, %Table{}, :cascade}) when command in @drops,
10131015
do: error!(nil, "MSSQL does not support `CASCADE` in DROP TABLE commands")
10141016

1015-
def execute_ddl({command, %Table{} = table, _}) when command in [:drop, :drop_if_exists] do
1017+
def execute_ddl({command, %Table{} = table, :restrict}) when command in @drops do
10161018
prefix = table.prefix
10171019

10181020
[
@@ -1047,8 +1049,7 @@ if Code.ensure_loaded?(Tds) do
10471049
]
10481050
end
10491051

1050-
def execute_ddl({command, %Index{} = index})
1051-
when command in [:create, :create_if_not_exists] do
1052+
def execute_ddl({command, %Index{} = index}) when command in @creates do
10521053
prefix = index.prefix
10531054

10541055
if index.using do
@@ -1127,10 +1128,10 @@ if Code.ensure_loaded?(Tds) do
11271128
]
11281129
end
11291130

1130-
def execute_ddl({command, %Index{}, :cascade}) when command in [:drop, :drop_if_exists],
1131+
def execute_ddl({command, %Index{}, :cascade}) when command in @drops,
11311132
do: error!(nil, "MSSQL does not support `CASCADE` in DROP INDEX commands")
11321133

1133-
def execute_ddl({command, %Index{} = index, _}) when command in [:drop, :drop_if_exists] do
1134+
def execute_ddl({command, %Index{} = index, :restrict}) when command in @drops do
11341135
prefix = index.prefix
11351136

11361137
[
@@ -1150,11 +1151,10 @@ if Code.ensure_loaded?(Tds) do
11501151
]
11511152
end
11521153

1153-
def execute_ddl({command, %Constraint{}, :cascade}) when command in [:drop, :drop_if_exists],
1154+
def execute_ddl({command, %Constraint{}, :cascade}) when command in @drops,
11541155
do: error!(nil, "MSSQL does not support `CASCADE` in DROP CONSTRAINT commands")
11551156

1156-
def execute_ddl({command, %Constraint{} = constraint, _})
1157-
when command in [:drop, :drop_if_exists] do
1157+
def execute_ddl({command, %Constraint{} = constraint, _}) when command in @drops do
11581158
table_name = quote_table(constraint.prefix, constraint.table)
11591159

11601160
[

lib/ecto/migration.ex

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -575,20 +575,18 @@ defmodule Ecto.Migration do
575575
drop index("posts", [:name])
576576
drop table("posts")
577577
drop constraint("products", "price_must_be_positive")
578-
drop index("posts", [:name]), cascade: true
579-
drop table("posts"), cascade: true
578+
drop index("posts", [:name]), mode: :cascade
579+
drop table("posts"), mode: :cascade
580580
581581
## Options
582582
583-
* `:cascade` - when `true`, automatically drop objects that depend
584-
- on the index, and in turn all objects that depend on those objects
585-
- on the table
586-
Default is `false`
583+
* `:mode` - when set to `:cascade`, automatically drop objects that depend
584+
on the index, and in turn all objects that depend on those objects
585+
on the table. Default is `:restrict`
587586
588587
"""
589588
def drop(%{} = index_or_table_or_constraint, opts \\ []) when is_list(opts) do
590-
Runner.execute {:drop, __prefix__(index_or_table_or_constraint), Keyword.get(opts, :mode)}
591-
589+
Runner.execute {:drop, __prefix__(index_or_table_or_constraint), Keyword.get(opts, :mode, :restrict)}
592590
index_or_table_or_constraint
593591
end
594592

@@ -607,12 +605,12 @@ defmodule Ecto.Migration do
607605
## Options
608606
609607
* `:mode` - when set to `:cascade`, automatically drop objects that depend
610-
- on the index, and in turn all objects that depend on those objects
611-
- on the table
612-
Default is `:restrict`
608+
on the index, and in turn all objects that depend on those objects
609+
on the table. Default is `:restrict`
610+
613611
"""
614612
def drop_if_exists(%{} = index_or_table, opts \\ []) when is_list(opts) do
615-
Runner.execute {:drop_if_exists, __prefix__(index_or_table), Keyword.get(opts, :mode)}
613+
Runner.execute {:drop_if_exists, __prefix__(index_or_table), Keyword.get(opts, :mode, :restrict)}
616614

617615
index_or_table
618616
end

lib/ecto/migration/runner.ex

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,18 @@ defmodule Ecto.Migration.Runner do
217217
end
218218

219219
defp reverse({:create, %Index{} = index}),
220-
do: {:drop, index, nil}
220+
do: {:drop, index, :restrict}
221221
defp reverse({:create_if_not_exists, %Index{} = index}),
222-
do: {:drop_if_exists, index, nil}
222+
do: {:drop_if_exists, index, :restrict}
223223
defp reverse({:drop, %Index{} = index, _}),
224224
do: {:create, index}
225225
defp reverse({:drop_if_exists, %Index{} = index, _}),
226226
do: {:create_if_not_exists, index}
227227

228228
defp reverse({:create, %Table{} = table, _columns}),
229-
do: {:drop, table, nil}
229+
do: {:drop, table, :restrict}
230230
defp reverse({:create_if_not_exists, %Table{} = table, _columns}),
231-
do: {:drop_if_exists, table, nil}
231+
do: {:drop_if_exists, table, :restrict}
232232
defp reverse({:rename, %Table{} = table_current, %Table{} = table_new}),
233233
do: {:rename, table_new, table_current}
234234
defp reverse({:rename, %Table{} = table, current_column, new_column}),
@@ -242,9 +242,9 @@ defmodule Ecto.Migration.Runner do
242242
# It is not a good idea to reverse constraints because
243243
# we can't guarantee data integrity when applying them back.
244244
defp reverse({:create_if_not_exists, %Constraint{} = constraint}),
245-
do: {:drop_if_exists, constraint, nil}
245+
do: {:drop_if_exists, constraint, :restrict}
246246
defp reverse({:create, %Constraint{} = constraint}),
247-
do: {:drop, constraint, nil}
247+
do: {:drop, constraint, :restrict}
248248

249249
defp reverse(_command), do: false
250250

@@ -361,27 +361,19 @@ defmodule Ecto.Migration.Runner do
361361
do: "create table if not exists #{quote_name(table.prefix, table.name)}"
362362
defp command({:alter, %Table{} = table, _}),
363363
do: "alter table #{quote_name(table.prefix, table.name)}"
364-
defp command({:drop, %Table{} = table, :cascade}),
365-
do: command({:drop, table, nil}) <> " cascade"
366-
defp command({:drop, %Table{} = table, _}),
367-
do: "drop table #{quote_name(table.prefix, table.name)}"
368-
defp command({:drop_if_exists, %Table{} = table, :cascade}),
369-
do: command({:drop_if_exists, table, nil}) <> " cascade"
370-
defp command({:drop_if_exists, %Table{} = table, _}),
371-
do: "drop table if exists #{quote_name(table.prefix, table.name)}"
364+
defp command({:drop, %Table{} = table, mode}),
365+
do: "drop table #{quote_name(table.prefix, table.name)}#{drop_mode(mode)}"
366+
defp command({:drop_if_exists, %Table{} = table, mode}),
367+
do: "drop table if exists #{quote_name(table.prefix, table.name)}#{drop_mode(mode)}"
372368

373369
defp command({:create, %Index{} = index}),
374370
do: "create index #{quote_name(index.prefix, index.name)}"
375371
defp command({:create_if_not_exists, %Index{} = index}),
376372
do: "create index if not exists #{quote_name(index.prefix, index.name)}"
377-
defp command({:drop, %Index{} = index, :cascade}),
378-
do: command({:drop, index, nil}) <> " cascade"
379-
defp command({:drop, %Index{} = index, _}),
380-
do: "drop index #{quote_name(index.prefix, index.name)}"
381-
defp command({:drop_if_exists, %Index{} = index, :cascade}),
382-
do: command({:drop_if_exists, index, nil}) <> " cascade"
383-
defp command({:drop_if_exists, %Index{} = index, _}),
384-
do: "drop index if exists #{quote_name(index.prefix, index.name)}"
373+
defp command({:drop, %Index{} = index, mode}),
374+
do: "drop index #{quote_name(index.prefix, index.name)}#{drop_mode(mode)}"
375+
defp command({:drop_if_exists, %Index{} = index, mode}),
376+
do: "drop index if exists #{quote_name(index.prefix, index.name)}#{drop_mode(mode)}"
385377
defp command({:rename, %Table{} = current_table, %Table{} = new_table}),
386378
do: "rename table #{quote_name(current_table.prefix, current_table.name)} to #{quote_name(new_table.prefix, new_table.name)}"
387379
defp command({:rename, %Table{} = table, current_column, new_column}),
@@ -400,6 +392,9 @@ defmodule Ecto.Migration.Runner do
400392
defp command({:drop_if_exists, %Constraint{} = constraint, _}),
401393
do: "drop constraint if exists #{constraint.name} from table #{quote_name(constraint.prefix, constraint.table)}"
402394

395+
defp drop_mode(:restrict), do: ""
396+
defp drop_mode(:cascade), do: " cascade"
397+
403398
defp quote_name(nil, name), do: quote_name(name)
404399
defp quote_name(prefix, name), do: quote_name(prefix) <> "." <> quote_name(name)
405400
defp quote_name(name) when is_atom(name), do: quote_name(Atom.to_string(name))

test/ecto/adapters/myxql_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,24 +1270,24 @@ defmodule Ecto.Adapters.MyXQLTest do
12701270
end
12711271

12721272
test "drop table" do
1273-
drop = {:drop, table(:posts), nil}
1273+
drop = {:drop, table(:posts), :restrict}
12741274
assert execute_ddl(drop) == [~s|DROP TABLE `posts`|]
12751275
end
12761276

12771277
test "drop table with prefixes" do
1278-
drop = {:drop, table(:posts, prefix: :foo), nil}
1278+
drop = {:drop, table(:posts, prefix: :foo), :restrict}
12791279
assert execute_ddl(drop) == [~s|DROP TABLE `foo`.`posts`|]
12801280
end
12811281

12821282
test "drop constraint" do
12831283
assert_raise ArgumentError, ~r/MySQL adapter does not support constraints/, fn ->
1284-
execute_ddl({:drop, constraint(:products, "price_must_be_positive", prefix: :foo), nil})
1284+
execute_ddl({:drop, constraint(:products, "price_must_be_positive", prefix: :foo), :restrict})
12851285
end
12861286
end
12871287

12881288
test "drop_if_exists constraint" do
12891289
assert_raise ArgumentError, ~r/MySQL adapter does not support constraints/, fn ->
1290-
execute_ddl({:drop_if_exists, constraint(:products, "price_must_be_positive", prefix: :foo), nil})
1290+
execute_ddl({:drop_if_exists, constraint(:products, "price_must_be_positive", prefix: :foo), :restrict})
12911291
end
12921292
end
12931293

@@ -1425,12 +1425,12 @@ defmodule Ecto.Adapters.MyXQLTest do
14251425
end
14261426

14271427
test "drop index" do
1428-
drop = {:drop, index(:posts, [:id], name: "posts$main"), nil}
1428+
drop = {:drop, index(:posts, [:id], name: "posts$main"), :restrict}
14291429
assert execute_ddl(drop) == [~s|DROP INDEX `posts$main` ON `posts`|]
14301430
end
14311431

14321432
test "drop index with prefix" do
1433-
drop = {:drop, index(:posts, [:id], name: "posts$main", prefix: :foo), nil}
1433+
drop = {:drop, index(:posts, [:id], name: "posts$main", prefix: :foo), :restrict}
14341434
assert execute_ddl(drop) == [~s|DROP INDEX `posts$main` ON `foo`.`posts`|]
14351435
end
14361436

0 commit comments

Comments
 (0)