Skip to content

Commit 79e6479

Browse files
committed
minor refactor
1 parent daeed39 commit 79e6479

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

integration_test/sql/migration.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,8 @@ defmodule Ecto.Integration.MigrationTest do
580580
PoolRepo.all from p in "alter_col_migration", select: p.from_default_to_no_default
581581
assert [nil] ==
582582
PoolRepo.all from p in "alter_col_migration", select: p.another_from_default_to_no_default
583+
assert [0] ==
584+
PoolRepo.all from p in "alter_col_migration", select: p.from_no_default_to_default
583585
assert [0] ==
584586
PoolRepo.all from p in "alter_col_migration", select: p.another_from_no_default_to_default
585587

lib/ecto/adapters/postgres/connection.ex

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,14 +1552,17 @@ if Code.ensure_loaded?(Postgrex) do
15521552
drop_reference_expr = drop_reference_expr(opts[:from], table, name)
15531553
prefix_with_comma = (drop_reference_expr != [] && ", ") || ""
15541554

1555+
common_suffix = [
1556+
reference_expr(ref, table, name),
1557+
modify_null(name, opts),
1558+
modify_default(name, ref.type, opts)
1559+
]
1560+
15551561
if reference_column_type == column_type(from_column_type, opts) do
15561562
[
15571563
drop_reference_expr,
15581564
prefix_with_comma,
1559-
"ADD ",
1560-
reference_expr(ref, table, name),
1561-
modify_null(name, opts),
1562-
modify_default(name, ref.type, opts)
1565+
"ADD " | common_suffix
15631566
]
15641567
else
15651568
[
@@ -1569,10 +1572,7 @@ if Code.ensure_loaded?(Postgrex) do
15691572
quote_name(name),
15701573
" TYPE ",
15711574
reference_column_type,
1572-
", ADD ",
1573-
reference_expr(ref, table, name),
1574-
modify_null(name, opts),
1575-
modify_default(name, ref.type, opts)
1575+
", ADD " | common_suffix
15761576
]
15771577
end
15781578
end
@@ -1588,15 +1588,14 @@ if Code.ensure_loaded?(Postgrex) do
15881588
modify_null = modify_null(name, Keyword.put(opts, :prefix_with_comma, any_drop_ref?))
15891589
any_modify_null? = modify_null != []
15901590

1591-
[
1592-
drop_reference_expr,
1593-
modify_null,
1591+
modify_default =
15941592
modify_default(
15951593
name,
15961594
type,
15971595
Keyword.put(opts, :prefix_with_comma, any_drop_ref? or any_modify_null?)
15981596
)
1599-
]
1597+
1598+
[drop_reference_expr, modify_null, modify_default]
16001599
else
16011600
[
16021601
drop_reference_expr,

0 commit comments

Comments
 (0)