Skip to content

Commit

Permalink
fix_regression_on_modify_table
Browse files Browse the repository at this point in the history
  • Loading branch information
dkuku committed Nov 4, 2024
1 parent 75ddf59 commit 3738456
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
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

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

0 comments on commit 3738456

Please sign in to comment.