From 6e9d8714c4d5642b56b908a3d1f0112243629d69 Mon Sep 17 00:00:00 2001 From: Nikki Kyllonen Date: Thu, 8 May 2025 16:19:48 -0700 Subject: [PATCH 1/2] Update drop_if_exists docs to include constraints --- lib/ecto/migration.ex | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/ecto/migration.ex b/lib/ecto/migration.ex index d49a04544..90f98df00 100644 --- a/lib/ecto/migration.ex +++ b/lib/ecto/migration.ex @@ -718,7 +718,11 @@ defmodule Ecto.Migration do end @doc """ - Drops a table or index if it exists. + Drops one of the following if it exists: + + * an index + * a table + * a constraint Does not raise an error if the specified table or index does not exist. @@ -726,6 +730,7 @@ defmodule Ecto.Migration do drop_if_exists index("posts", [:name]) drop_if_exists table("posts") + drop_if_exists constraint("products", "price_must_be_positive") drop_if_exists index("posts", [:name]), mode: :cascade drop_if_exists table("posts"), mode: :cascade @@ -736,12 +741,12 @@ defmodule Ecto.Migration do on the table. Default is `:restrict` """ - def drop_if_exists(%{} = index_or_table, opts \\ []) when is_list(opts) do + def drop_if_exists(%{} = index_or_table_or_constraint, opts \\ []) when is_list(opts) do Runner.execute( - {:drop_if_exists, __prefix__(index_or_table), Keyword.get(opts, :mode, :restrict)} + {:drop_if_exists, __prefix__(index_or_table_or_constraint), Keyword.get(opts, :mode, :restrict)} ) - index_or_table + index_or_table_or_constraint end @doc """ From 7d5cc0f83687ba62db52952f9421e40e7e00ef2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 9 May 2025 15:27:04 +0200 Subject: [PATCH 2/2] Update migration.ex --- lib/ecto/migration.ex | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/ecto/migration.ex b/lib/ecto/migration.ex index 90f98df00..92f44240b 100644 --- a/lib/ecto/migration.ex +++ b/lib/ecto/migration.ex @@ -742,10 +742,8 @@ defmodule Ecto.Migration do """ def drop_if_exists(%{} = index_or_table_or_constraint, opts \\ []) when is_list(opts) do - Runner.execute( - {:drop_if_exists, __prefix__(index_or_table_or_constraint), Keyword.get(opts, :mode, :restrict)} - ) - + mode = Keyword.get(opts, :mode, :restrict) + Runner.execute({:drop_if_exists, __prefix__(index_or_table_or_constraint), mode}) index_or_table_or_constraint end