Skip to content
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

Add Ecto.Migration.remove_if_exists/1 #624

Merged
merged 3 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ locals_without_parens = [
remove: 1,
remove: 2,
remove: 3,
remove_if_exists: 1,
remove_if_exists: 2,
rename: 2,
rename: 3,
Expand Down
3 changes: 2 additions & 1 deletion lib/ecto/adapter/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ defmodule Ecto.Adapter.Migration do
| {:remove, field :: atom, type :: Ecto.Type.t() | Reference.t() | binary(),
Keyword.t()}
| {:remove, field :: atom}
| {:remove_if_exists, type :: Ecto.Type.t() | Reference.t() | binary()}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spec was missing field

| {:remove_if_exists, field :: atom, type :: Ecto.Type.t() | Reference.t() | binary()}
| {:remove_if_exists, field :: atom}

@typedoc """
A struct that represents a table or index in a database schema.
Expand Down
5 changes: 4 additions & 1 deletion lib/ecto/adapters/myxql/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,10 @@ if Code.ensure_loaded?(MyXQL) do
[drop_constraint_if_exists_expr(ref, table, name), "DROP IF EXISTS ", quote_name(name)]
end

defp column_change(_table, {:remove_if_exists, name, _type}),
defp column_change(table, {:remove_if_exists, name, _type}),
do: column_change(table, {:remove_if_exists, name})

defp column_change(_table, {:remove_if_exists, name}),
do: ["DROP IF EXISTS ", quote_name(name)]

defp column_options(opts) do
Expand Down
5 changes: 4 additions & 1 deletion lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,10 @@ if Code.ensure_loaded?(Postgrex) do
]
end

defp column_change(_table, {:remove_if_exists, name, _type}),
defp column_change(table, {:remove_if_exists, name, _type}),
do: column_change(table, {:remove_if_exists, name})

defp column_change(_table, {:remove_if_exists, name}),
do: ["DROP COLUMN IF EXISTS ", quote_name(name)]

defp modify_null(name, opts) do
Expand Down
5 changes: 4 additions & 1 deletion lib/ecto/adapters/tds/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1470,10 +1470,13 @@ if Code.ensure_loaded?(Tds) do
defp column_change(statement_prefix, _table, {:remove, name, _type, _opts}),
do: [statement_prefix, "DROP COLUMN ", quote_name(name)]

defp column_change(statement_prefix, table, {:remove_if_exists, column_name, _}),
do: column_change(statement_prefix, table, {:remove_if_exists, column_name})

defp column_change(
statement_prefix,
%{name: table, prefix: prefix},
{:remove_if_exists, column_name, _}
{:remove_if_exists, column_name}
) do
[
[
Expand Down
25 changes: 21 additions & 4 deletions lib/ecto/migration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1358,15 +1358,32 @@ defmodule Ecto.Migration do
end

@doc """
Removes a column only if the column exists when altering the constraint if the reference type is passed
once it only has the constraint name on reference structure.
Removes a column if the column exists.

This command is not reversible as Ecto does not know about column existence before the removal attempt.
This command is not reversible as Ecto does not know whether or not the column existed before the removal attempt.

## Examples

alter table("posts") do
remove_if_exists :title, :string
remove_if_exists :title
end

"""
def remove_if_exists(column) when is_atom(column) do
Runner.subcommand({:remove_if_exists, column})
end

@doc """
Removes a column if the column exists.

If the type is a reference, removes the foreign key constraint for the reference first, if it exists.

This command is not reversible as Ecto does not know whether or not the column existed before the removal attempt.

## Examples

alter table("posts") do
remove_if_exists :author_id, references(:authors)
end

"""
Expand Down
2 changes: 2 additions & 0 deletions test/ecto/adapters/myxql_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,7 @@ defmodule Ecto.Adapters.MyXQLTest do
{:remove, :body, :text, []},
{:remove, :space_id, %Reference{table: :author}, []},
{:remove_if_exists, :body, :text},
{:remove_if_exists, :body},
{:remove_if_exists, :space_id, %Reference{table: :author}}
]}

Expand Down Expand Up @@ -2015,6 +2016,7 @@ defmodule Ecto.Adapters.MyXQLTest do
DROP FOREIGN KEY `posts_space_id_fkey`,
DROP `space_id`,
DROP IF EXISTS `body`,
DROP IF EXISTS `body`,
DROP FOREIGN KEY IF EXISTS `posts_space_id_fkey`,
DROP IF EXISTS `space_id`
"""
Expand Down
2 changes: 2 additions & 0 deletions test/ecto/adapters/postgres_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@
query = "schema" |> where(foo: "abc") |> select([], true) |> plan()
assert all(query) == ~s{SELECT TRUE FROM "schema" AS s0 WHERE (s0."foo" = 'abc')}

query = "schema" |> where(foo: <<0, ?a, ?b, ?c>>) |> select([], true) |> plan()

Check warning on line 862 in test/ecto/adapters/postgres_test.exs

View workflow job for this annotation

GitHub Actions / unittest (1.15.6, 24.3.4.13)

this check/guard will always yield the same result

Check warning on line 862 in test/ecto/adapters/postgres_test.exs

View workflow job for this annotation

GitHub Actions / unittest (1.15.6, 26.1.2, lint)

this check/guard will always yield the same result

assert all(query) ==
~s{SELECT TRUE FROM "schema" AS s0 WHERE (s0."foo" = '\\x00616263'::bytea)}
Expand Down Expand Up @@ -2503,6 +2503,7 @@
{:remove, :body, :text, []},
{:remove, :space_id, %Reference{table: :author}, []},
{:remove_if_exists, :body, :text},
{:remove_if_exists, :body},
{:remove_if_exists, :space_id, %Reference{table: :author}}
]}

Expand Down Expand Up @@ -2541,6 +2542,7 @@
DROP CONSTRAINT "posts_space_id_fkey",
DROP COLUMN "space_id",
DROP COLUMN IF EXISTS "body",
DROP COLUMN IF EXISTS "body",
DROP CONSTRAINT IF EXISTS "posts_space_id_fkey",
DROP COLUMN IF EXISTS "space_id"
"""
Expand Down
4 changes: 3 additions & 1 deletion test/ecto/migration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ defmodule Ecto.MigrationTest do
remove :views
remove :status, :string
remove_if_exists :status, :string
remove_if_exists :status
end

flush()
Expand All @@ -481,7 +482,8 @@ defmodule Ecto.MigrationTest do
{:modify, :title, :text, []},
{:remove, :views},
{:remove, :status, :string, []},
{:remove_if_exists, :status, :string}
{:remove_if_exists, :status, :string},
{:remove_if_exists, :status}
]}
end

Expand Down
Loading