Skip to content

Update drop_if_exists docs to include constraints #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/ecto/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -718,14 +718,19 @@ 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.

## Examples

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

Expand All @@ -736,12 +741,10 @@ defmodule Ecto.Migration do
on the table. Default is `:restrict`

"""
def drop_if_exists(%{} = index_or_table, opts \\ []) when is_list(opts) do
Runner.execute(
{:drop_if_exists, __prefix__(index_or_table), Keyword.get(opts, :mode, :restrict)}
)

index_or_table
def drop_if_exists(%{} = index_or_table_or_constraint, opts \\ []) when is_list(opts) do
mode = Keyword.get(opts, :mode, :restrict)
Runner.execute({:drop_if_exists, __prefix__(index_or_table_or_constraint), mode})
index_or_table_or_constraint
end

@doc """
Expand Down
Loading