Skip to content

Commit 475cf8c

Browse files
committed
avoid alter column type on modify if not needed
1 parent 70a375f commit 475cf8c

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

integration_test/sql/migration.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ defmodule Ecto.Integration.MigrationTest do
4848
def up do
4949
create table(:alter_col_migration) do
5050
add :from_null_to_not_null, :integer
51+
add :another_from_null_to_not_null, :string
5152
add :from_not_null_to_null, :integer, null: false
5253

5354
add :from_default_to_no_default, :integer, default: 0
@@ -56,13 +57,14 @@ defmodule Ecto.Integration.MigrationTest do
5657

5758
alter table(:alter_col_migration) do
5859
modify :from_null_to_not_null, :string, null: false
60+
modify :another_from_null_to_not_null, :string, null: false, from: {:string, null: true}
5961
modify :from_not_null_to_null, :string, null: true
6062

6163
modify :from_default_to_no_default, :integer, default: nil
6264
modify :from_no_default_to_default, :integer, default: 0
6365
end
6466

65-
execute "INSERT INTO alter_col_migration (from_null_to_not_null) VALUES ('foo')"
67+
execute "INSERT INTO alter_col_migration (from_null_to_not_null, another_from_null_to_not_null) VALUES ('foo', 'baz')"
6668
end
6769

6870
def down do

lib/ecto/adapters/postgres/connection.ex

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,15 +1556,33 @@ if Code.ensure_loaded?(Postgrex) do
15561556
end
15571557

15581558
defp column_change(table, {:modify, name, type, opts}) do
1559-
[
1560-
drop_reference_expr(opts[:from], table, name),
1561-
"ALTER COLUMN ",
1562-
quote_name(name),
1563-
" TYPE ",
1564-
column_type(type, opts),
1565-
modify_null(name, opts),
1566-
modify_default(name, type, opts)
1567-
]
1559+
column_type = column_type(type, opts)
1560+
1561+
extract_type = fn
1562+
{type, _} when is_atom(type) -> type
1563+
type when is_atom(type) -> type
1564+
_ -> nil
1565+
end
1566+
1567+
from_type = extract_type.(opts[:from])
1568+
1569+
if column_type == column_type(from_type, opts) do
1570+
[
1571+
drop_reference_expr(opts[:from], table, name),
1572+
modify_null(name, Keyword.put(opts, :prefix_with_comma, false)),
1573+
modify_default(name, type, opts)
1574+
]
1575+
else
1576+
[
1577+
drop_reference_expr(opts[:from], table, name),
1578+
"ALTER COLUMN ",
1579+
quote_name(name),
1580+
" TYPE ",
1581+
column_type,
1582+
modify_null(name, opts),
1583+
modify_default(name, type, opts)
1584+
]
1585+
end
15681586
end
15691587

15701588
defp column_change(_table, {:remove, name}), do: ["DROP COLUMN ", quote_name(name)]
@@ -1591,9 +1609,12 @@ if Code.ensure_loaded?(Postgrex) do
15911609
do: ["DROP COLUMN IF EXISTS ", quote_name(name)]
15921610

15931611
defp modify_null(name, opts) do
1612+
prefix_with_comma = Keyword.get(opts, :prefix_with_comma, true)
1613+
prefix = if prefix_with_comma, do: ", ", else: ""
1614+
15941615
case Keyword.get(opts, :null) do
1595-
true -> [", ALTER COLUMN ", quote_name(name), " DROP NOT NULL"]
1596-
false -> [", ALTER COLUMN ", quote_name(name), " SET NOT NULL"]
1616+
true -> [prefix, "ALTER COLUMN ", quote_name(name), " DROP NOT NULL"]
1617+
false -> [prefix, "ALTER COLUMN ", quote_name(name), " SET NOT NULL"]
15971618
nil -> []
15981619
end
15991620
end

0 commit comments

Comments
 (0)