Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 11 additions & 1 deletion lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1580,11 +1580,12 @@ if Code.ensure_loaded?(Postgrex) do
defp column_change(table, {:modify, name, type, opts}) do
column_type = column_type(type, opts)
from_column_type = extract_column_type(opts[:from])
from_opts = extract_opts(opts)

drop_reference_expr = drop_reference_expr(opts[:from], table, name)
any_drop_ref? = drop_reference_expr != []

if column_type == column_type(from_column_type, opts) do
if column_type == column_type(from_column_type, from_opts) do
modify_null = modify_null(name, Keyword.put(opts, :prefix_with_comma, any_drop_ref?))
any_modify_null? = modify_null != []

Expand Down Expand Up @@ -1844,6 +1845,15 @@ if Code.ensure_loaded?(Postgrex) do
[type, generated_expr(generated)]
end

defp extract_opts(opts) do
with {:ok, from} <- Keyword.fetch(opts, :from),
{_type, from_opts} <- from do
from_opts
else
_ -> opts
end
end
Copy link
Member

@greg-rychlewski greg-rychlewski Nov 6, 2024

Choose a reason for hiding this comment

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

Suggested change
defp extract_opts(opts) do
with {:ok, from} <- Keyword.fetch(opts, :from),
{_type, from_opts} <- from do
from_opts
else
_ -> opts
end
end
defp extract_from_opts({_type, opts}), do: opts
defp extract_from_opts(_), do: []

I think we might need to do something like this. Otherwise if someone supplies something like this it will think they are equal:

modify(:field, :string, size: 100, from: :string)


defp extract_column_type({type, _}) when is_atom(type), do: type
defp extract_column_type(type) when is_atom(type), do: type
defp extract_column_type(%Reference{type: type}), do: type
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 @@ -2509,6 +2509,7 @@ defmodule Ecto.Adapters.PostgresTest do
from: %Reference{table: :groups}},
{:modify, :status, :string,
[null: false, size: 100, from: {:integer, null: true, size: 50}]},
{:modify, :email, :string, [size: 512, from: {:string, size: 255}]},
{:remove, :summary},
{:remove, :body, :text, []},
{:remove, :space_id, %Reference{table: :author}, []},
Expand Down Expand Up @@ -2546,6 +2547,7 @@ defmodule Ecto.Adapters.PostgresTest do
ADD CONSTRAINT "posts_group_id_fkey" FOREIGN KEY ("group_id") REFERENCES "groups"("gid"),
ALTER COLUMN "status" TYPE varchar(100),
ALTER COLUMN "status" SET NOT NULL,
ALTER COLUMN "email" TYPE varchar(512),
DROP COLUMN "summary",
DROP COLUMN "body",
DROP CONSTRAINT "posts_space_id_fkey",
Expand Down
Loading