Skip to content

Commit

Permalink
Add Ecto.Migration.remove_if_exists/1 (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust authored Jul 24, 2024
1 parent b1f7386 commit 5a29789
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 9 deletions.
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()}
| {: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 @@ -2503,6 +2503,7 @@ defmodule Ecto.Adapters.PostgresTest 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 @@ -2541,6 +2542,7 @@ defmodule Ecto.Adapters.PostgresTest do
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

0 comments on commit 5a29789

Please sign in to comment.