Skip to content

Commit 44f561b

Browse files
authored
Fix invalid postgres SQL related to collation (#756)
1 parent 86234e7 commit 44f561b

3 files changed

Lines changed: 109 additions & 17 deletions

File tree

‎lib/ecto/adapters/postgres/connection.ex‎

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,15 +1532,15 @@ if Code.ensure_loaded?(Postgrex) do
15321532
[
15331533
quote_name(name),
15341534
?\s,
1535-
reference_column_type(ref.type, opts),
1535+
add_reference_column_type(ref.type, opts),
15361536
column_options(ref.type, opts),
15371537
", ",
15381538
reference_expr(ref, table, name)
15391539
]
15401540
end
15411541

15421542
defp column_definition(_table, {:add, name, type, opts}) do
1543-
[quote_name(name), ?\s, column_type(type, opts), column_options(type, opts)]
1543+
[quote_name(name), ?\s, add_column_type(type, opts), column_options(type, opts)]
15441544
end
15451545

15461546
defp column_changes(table, columns) do
@@ -1552,23 +1552,29 @@ if Code.ensure_loaded?(Postgrex) do
15521552
"ADD COLUMN ",
15531553
quote_name(name),
15541554
?\s,
1555-
reference_column_type(ref.type, opts),
1555+
add_reference_column_type(ref.type, opts),
15561556
column_options(ref.type, opts),
15571557
", ADD ",
15581558
reference_expr(ref, table, name)
15591559
]
15601560
end
15611561

15621562
defp column_change(_table, {:add, name, type, opts}) do
1563-
["ADD COLUMN ", quote_name(name), ?\s, column_type(type, opts), column_options(type, opts)]
1563+
[
1564+
"ADD COLUMN ",
1565+
quote_name(name),
1566+
?\s,
1567+
add_column_type(type, opts),
1568+
column_options(type, opts)
1569+
]
15641570
end
15651571

15661572
defp column_change(table, {:add_if_not_exists, name, %Reference{} = ref, opts}) do
15671573
[
15681574
"ADD COLUMN IF NOT EXISTS ",
15691575
quote_name(name),
15701576
?\s,
1571-
reference_column_type(ref.type, opts),
1577+
add_reference_column_type(ref.type, opts),
15721578
column_options(ref.type, opts),
15731579
", ADD ",
15741580
reference_expr(ref, table, name)
@@ -1580,7 +1586,7 @@ if Code.ensure_loaded?(Postgrex) do
15801586
"ADD COLUMN IF NOT EXISTS ",
15811587
quote_name(name),
15821588
?\s,
1583-
column_type(type, opts),
1589+
add_column_type(type, opts),
15841590
column_options(type, opts)
15851591
]
15861592
end
@@ -1594,11 +1600,11 @@ if Code.ensure_loaded?(Postgrex) do
15941600
quote_name(name),
15951601
" TYPE ",
15961602
reference_column_type(ref.type, opts),
1603+
collation_expr(collation),
15971604
", ADD ",
15981605
reference_expr(ref, table, name),
15991606
modify_null(name, opts),
1600-
modify_default(name, ref.type, opts),
1601-
collation_expr(collation)
1607+
modify_default(name, ref.type, opts)
16021608
]
16031609
end
16041610

@@ -1611,9 +1617,9 @@ if Code.ensure_loaded?(Postgrex) do
16111617
quote_name(name),
16121618
" TYPE ",
16131619
modify_column_type(type, opts),
1620+
collation_expr(collation),
16141621
modify_null(name, opts),
16151622
modify_default(name, type, opts),
1616-
collation_expr(collation),
16171623
modify_identity(name, type, opts)
16181624
]
16191625
end
@@ -1675,18 +1681,23 @@ if Code.ensure_loaded?(Postgrex) do
16751681
defp column_options(type, opts) do
16761682
default = Keyword.fetch(opts, :default)
16771683
null = Keyword.get(opts, :null)
1678-
collation = Keyword.fetch(opts, :collation)
16791684

1680-
[default_expr(default, type), null_expr(null), collation_expr(collation)]
1685+
[default_expr(default, type), null_expr(null)]
16811686
end
16821687

16831688
defp null_expr(false), do: " NOT NULL"
16841689
defp null_expr(true), do: " NULL"
16851690
defp null_expr(_), do: []
16861691

1687-
defp collation_expr({:ok, collation_name}), do: " COLLATE \"#{collation_name}\""
1692+
defp collation_expr({:ok, name}), do: [" COLLATE ", quote_collation_name(name)]
16881693
defp collation_expr(_), do: []
16891694

1695+
defp quote_collation_name(name) when is_atom(name),
1696+
do: quote_collation_name(Atom.to_string(name))
1697+
1698+
defp quote_collation_name(name) when is_binary(name),
1699+
do: [?", String.replace(name, "\"", "\"\""), ?"]
1700+
16901701
defp new_constraint_expr(%Constraint{check: check} = constraint) when is_binary(check) do
16911702
[
16921703
"CONSTRAINT ",
@@ -1815,18 +1826,28 @@ if Code.ensure_loaded?(Postgrex) do
18151826
defp options_expr(options),
18161827
do: [?\s, options]
18171828

1818-
defp column_type(type, opts) do
1829+
defp add_column_type(type, opts) do
1830+
column_type(type, opts, collation_expr(Keyword.fetch(opts, :collation)))
1831+
end
1832+
1833+
defp add_reference_column_type(type, opts) when type in [:serial, :bigserial, :identity] do
1834+
[reference_column_type(type, opts), collation_expr(Keyword.fetch(opts, :collation))]
1835+
end
1836+
1837+
defp add_reference_column_type(type, opts), do: add_column_type(type, opts)
1838+
1839+
defp column_type(type, opts, collation \\ []) do
18191840
type_name = column_type_name(type, opts)
18201841

18211842
case Keyword.get(opts, :generated) do
18221843
nil when type == :identity ->
1823-
[type_name, identity_generated_expr(opts)]
1844+
[type_name, collation, identity_generated_expr(opts)]
18241845

18251846
nil ->
1826-
type_name
1847+
[type_name, collation]
18271848

18281849
expr when is_binary(expr) ->
1829-
[type_name, " GENERATED ", expr]
1850+
[type_name, collation, " GENERATED ", expr]
18301851

18311852
other ->
18321853
raise ArgumentError,

‎lib/ecto/migration.ex‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,9 @@ defmodule Ecto.Migration do
14501450
specified.
14511451
* `:scale` - the scale of a numeric type. Defaults to `0`.
14521452
* `:comment` - adds a comment to the modified column.
1453-
* `:collation` - the collation of the text type.
1453+
* `:collation` - the collation of the text type. PostgreSQL resets the
1454+
collation to the type's default when modifying a column without this option,
1455+
even if the type is unchanged. Specify the current collation to preserve it.
14541456
"""
14551457
def modify(column, type, opts \\ []) when is_atom(column) and is_list(opts) do
14561458
validate_column_opts!(opts, @modify_column_opts, "modify/3")

‎test/ecto/adapters/postgres_test.exs‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2799,6 +2799,75 @@ defmodule Ecto.Adapters.PostgresTest do
27992799
]
28002800
end
28012801

2802+
test "alter column collation stays with the type when modifying null or default" do
2803+
assert execute_ddl(
2804+
{:alter, table(:posts), [{:modify, :name, :text, collation: "C", null: false}]}
2805+
) ==
2806+
[
2807+
~s|ALTER TABLE "posts" ALTER COLUMN "name" TYPE text COLLATE "C", ALTER COLUMN "name" SET NOT NULL|
2808+
]
2809+
2810+
assert execute_ddl(
2811+
{:alter, table(:posts), [{:modify, :name, :text, collation: "C", default: "x"}]}
2812+
) ==
2813+
[
2814+
~s|ALTER TABLE "posts" ALTER COLUMN "name" TYPE text COLLATE "C", ALTER COLUMN "name" SET DEFAULT 'x'|
2815+
]
2816+
2817+
assert execute_ddl(
2818+
{:alter, table(:posts),
2819+
[
2820+
{:modify, :name, %Reference{table: :names, type: :text},
2821+
collation: "C", null: false, default: "x"}
2822+
]}
2823+
) ==
2824+
[
2825+
~s|ALTER TABLE "posts" ALTER COLUMN "name" TYPE text COLLATE "C", ADD CONSTRAINT "posts_name_fkey" FOREIGN KEY ("name") REFERENCES "names"("id"), ALTER COLUMN "name" SET NOT NULL, ALTER COLUMN "name" SET DEFAULT 'x'|
2826+
]
2827+
end
2828+
2829+
test "column collation precedes defaults and constraints when adding columns" do
2830+
opts = [collation: "C", default: "x", null: false]
2831+
2832+
assert execute_ddl({:create, table(:posts), [{:add, :name, :text, opts}]}) == [
2833+
~s|CREATE TABLE "posts" ("name" text COLLATE "C" DEFAULT 'x' NOT NULL)|
2834+
]
2835+
2836+
assert execute_ddl({:alter, table(:posts), [{:add, :name, :text, opts}]}) == [
2837+
~s|ALTER TABLE "posts" ADD COLUMN "name" text COLLATE "C" DEFAULT 'x' NOT NULL|
2838+
]
2839+
2840+
assert execute_ddl({:alter, table(:posts), [{:add_if_not_exists, :name, :text, opts}]}) == [
2841+
~s|ALTER TABLE "posts" ADD COLUMN IF NOT EXISTS "name" text COLLATE "C" DEFAULT 'x' NOT NULL|
2842+
]
2843+
2844+
assert execute_ddl(
2845+
{:alter, table(:posts),
2846+
[{:add, :name, %Reference{table: :names, type: :text}, opts}]}
2847+
) ==
2848+
[
2849+
~s|ALTER TABLE "posts" ADD COLUMN "name" text COLLATE "C" DEFAULT 'x' NOT NULL, ADD CONSTRAINT "posts_name_fkey" FOREIGN KEY ("name") REFERENCES "names"("id")|
2850+
]
2851+
2852+
assert execute_ddl(
2853+
{:alter, table(:posts),
2854+
[{:add, :computed, :text, collation: "C", generated: "ALWAYS AS (name) STORED"}]}
2855+
) ==
2856+
[
2857+
~s|ALTER TABLE "posts" ADD COLUMN "computed" text COLLATE "C" GENERATED ALWAYS AS (name) STORED|
2858+
]
2859+
end
2860+
2861+
test "collation strings are quoted as single identifiers" do
2862+
assert execute_ddl({:alter, table(:posts), [{:modify, :name, :text, collation: "odd\"name"}]}) ==
2863+
[~s|ALTER TABLE "posts" ALTER COLUMN "name" TYPE text COLLATE "odd""name"|]
2864+
2865+
assert execute_ddl(
2866+
{:alter, table(:posts), [{:add, :name, :text, collation: "name.with.dot"}]}
2867+
) ==
2868+
[~s|ALTER TABLE "posts" ADD COLUMN "name" text COLLATE "name.with.dot"|]
2869+
end
2870+
28022871
test "alter table with comments on table and columns" do
28032872
alter =
28042873
{:alter, table(:posts, comment: "table comment"),

0 commit comments

Comments
 (0)