From 373845629a6a66aabbcc5db4c1a201ccbf075d35 Mon Sep 17 00:00:00 2001 From: Daniel Kukula Date: Mon, 4 Nov 2024 22:46:31 +0100 Subject: [PATCH] fix_regression_on_modify_table --- lib/ecto/adapters/postgres/connection.ex | 12 +++++++++++- test/ecto/adapters/postgres_test.exs | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/ecto/adapters/postgres/connection.ex b/lib/ecto/adapters/postgres/connection.ex index ad39f41d..d38722a3 100644 --- a/lib/ecto/adapters/postgres/connection.ex +++ b/lib/ecto/adapters/postgres/connection.ex @@ -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 != [] @@ -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 diff --git a/test/ecto/adapters/postgres_test.exs b/test/ecto/adapters/postgres_test.exs index 9c157290..5f59dee0 100644 --- a/test/ecto/adapters/postgres_test.exs +++ b/test/ecto/adapters/postgres_test.exs @@ -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}, []}, @@ -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",