Skip to content
Closed
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
17 changes: 9 additions & 8 deletions lib/ecto/adapters/postgres/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,7 @@ if Code.ensure_loaded?(Postgrex) do

defp column_change(table, {:modify, name, %Reference{} = ref, opts}) do
reference_column_type = reference_column_type(ref.type, opts)
from_column_type = extract_column_type(opts[:from])
{from_column_type, from_opts} = from_column_type_and_opts(opts[:from])

drop_reference_expr = drop_reference_expr(opts[:from], table, name)
prefix_with_comma = (drop_reference_expr != [] && ", ") || ""
Expand All @@ -1558,7 +1558,7 @@ if Code.ensure_loaded?(Postgrex) do
modify_default(name, ref.type, opts)
]

if reference_column_type == reference_column_type(from_column_type, opts) do
if reference_column_type == reference_column_type(from_column_type, from_opts) do
[
drop_reference_expr,
prefix_with_comma,
Expand All @@ -1579,12 +1579,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_column_type, from_opts} = from_column_type_and_opts(opts[:from])

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,10 +1844,11 @@ if Code.ensure_loaded?(Postgrex) do
[type, generated_expr(generated)]
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
defp extract_column_type(_), do: nil
defp from_column_type_and_opts({type, opts}) when is_atom(type), do: {type, opts}
defp from_column_type_and_opts({%Reference{type: type}, opts}), do: {type, opts}
defp from_column_type_and_opts(type) when is_atom(type), do: {type, []}
defp from_column_type_and_opts(%Reference{type: type}), do: {type, []}
defp from_column_type_and_opts(_), do: {nil, []}

defp generated_expr(nil), do: []

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