Skip to content

Commit daeed39

Browse files
committed
fix syntax error bug when setting default value
1 parent fc2e173 commit daeed39

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

integration_test/sql/migration.exs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ defmodule Ecto.Integration.MigrationTest do
5353

5454
add :from_default_to_no_default, :integer, default: 0
5555
add :from_no_default_to_default, :integer
56+
57+
add :another_from_default_to_no_default, :integer, default: 0
58+
add :another_from_no_default_to_default, :integer
5659
end
5760

5861
alter table(:alter_col_migration) do
@@ -62,6 +65,9 @@ defmodule Ecto.Integration.MigrationTest do
6265

6366
modify :from_default_to_no_default, :integer, default: nil
6467
modify :from_no_default_to_default, :integer, default: 0
68+
69+
modify :another_from_default_to_no_default, :integer, default: nil, from: {:integer, default: 0}
70+
modify :another_from_no_default_to_default, :integer, default: 0, from: {:integer, default: nil}
6571
end
6672

6773
execute "INSERT INTO alter_col_migration (from_null_to_not_null, another_from_null_to_not_null) VALUES ('foo', 'baz')"
@@ -143,7 +149,7 @@ defmodule Ecto.Integration.MigrationTest do
143149
end
144150

145151
alter table(:alter_fk_comments) do
146-
modify :alter_fk_user_id, references(:alter_fk_users, on_delete: :delete_all), from: references(:alter_fk_users, on_delete: :nothing)
152+
modify :alter_fk_user_id, references(:alter_fk_users, on_delete: :nilify_all), from: references(:alter_fk_users, on_delete: :nothing)
147153
end
148154
end
149155

@@ -566,12 +572,16 @@ defmodule Ecto.Integration.MigrationTest do
566572

567573
assert ["foo"] ==
568574
PoolRepo.all from p in "alter_col_migration", select: p.from_null_to_not_null
575+
assert ["baz"] ==
576+
PoolRepo.all from p in "alter_col_migration", select: p.another_from_null_to_not_null
569577
assert [nil] ==
570578
PoolRepo.all from p in "alter_col_migration", select: p.from_not_null_to_null
571579
assert [nil] ==
572580
PoolRepo.all from p in "alter_col_migration", select: p.from_default_to_no_default
581+
assert [nil] ==
582+
PoolRepo.all from p in "alter_col_migration", select: p.another_from_default_to_no_default
573583
assert [0] ==
574-
PoolRepo.all from p in "alter_col_migration", select: p.from_no_default_to_default
584+
PoolRepo.all from p in "alter_col_migration", select: p.another_from_no_default_to_default
575585

576586
query = "INSERT INTO `alter_col_migration` (\"from_not_null_to_null\") VALUES ('foo')"
577587
assert catch_error(PoolRepo.query!(query))
@@ -607,8 +617,10 @@ defmodule Ecto.Integration.MigrationTest do
607617
assert [id] = PoolRepo.all from p in "alter_fk_users", select: p.id
608618

609619
PoolRepo.insert_all("alter_fk_posts", [[alter_fk_user_id: id]])
620+
PoolRepo.insert_all("alter_fk_comments", [[alter_fk_user_id: id]])
610621
PoolRepo.delete_all("alter_fk_users")
611622
assert [nil] == PoolRepo.all from p in "alter_fk_posts", select: p.alter_fk_user_id
623+
assert [nil] == PoolRepo.all from p in "alter_fk_comments", select: p.alter_fk_user_id
612624

613625
:ok = down(PoolRepo, num, AlterForeignKeyOnDeleteMigration, log: false)
614626
end

lib/ecto/adapters/postgres/connection.ex

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,18 +1582,25 @@ if Code.ensure_loaded?(Postgrex) do
15821582
from_column_type = extract_column_type(opts[:from])
15831583

15841584
drop_reference_expr = drop_reference_expr(opts[:from], table, name)
1585-
prefix_with_comma? = drop_reference_expr != []
1585+
any_drop_ref? = drop_reference_expr != []
15861586

15871587
if column_type == column_type(from_column_type, opts) do
1588+
modify_null = modify_null(name, Keyword.put(opts, :prefix_with_comma, any_drop_ref?))
1589+
any_modify_null? = modify_null != []
1590+
15881591
[
15891592
drop_reference_expr,
1590-
modify_null(name, Keyword.put(opts, :prefix_with_comma, prefix_with_comma?)),
1591-
modify_default(name, type, opts)
1593+
modify_null,
1594+
modify_default(
1595+
name,
1596+
type,
1597+
Keyword.put(opts, :prefix_with_comma, any_drop_ref? or any_modify_null?)
1598+
)
15921599
]
15931600
else
15941601
[
15951602
drop_reference_expr,
1596-
(prefix_with_comma? && ", ") || "",
1603+
(any_drop_ref? && ", ") || "",
15971604
"ALTER COLUMN ",
15981605
quote_name(name),
15991606
" TYPE ",

0 commit comments

Comments
 (0)