Skip to content

Commit 1534ae3

Browse files
committed
Add support for collations in Postgres
1 parent 96415e3 commit 1534ae3

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

integration_test/sql/migration.exs

+41
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,21 @@ defmodule Ecto.Integration.MigrationTest do
463463
end
464464
end
465465

466+
467+
defmodule CollateMigration do
468+
use Ecto.Migration
469+
470+
def change do
471+
create table(:collate) do
472+
add :string, :string, collation: "POSIX"
473+
add :char, :char, collation: "POSIX"
474+
add :varchar, :varchar, collation: "POSIX"
475+
add :text, :text, collation: "POSIX"
476+
add :integer, :integer, collation: "POSIX"
477+
end
478+
end
479+
end
480+
466481
import Ecto.Query, only: [from: 2]
467482
import Ecto.Migrator, only: [up: 4, down: 4]
468483

@@ -683,4 +698,30 @@ defmodule Ecto.Integration.MigrationTest do
683698

684699
:ok = down(PoolRepo, num, OnDeleteNilifyColumnsMigration, log: false)
685700
end
701+
702+
@tag :add_column
703+
test "collate can set on a column", %{migration_number: num} do
704+
assert :ok = up(PoolRepo, num, CollateMigration, log: true)
705+
query = fn column -> """
706+
SELECT column_name, data_type, collation_name
707+
FROM information_schema.columns
708+
WHERE table_name = 'collate' AND column_name = '#{column}';
709+
"""
710+
end
711+
for type <- ~w/string varchar/ do
712+
assert %{
713+
rows: [[^type, "character varying", "POSIX"]]
714+
} = Ecto.Adapters.SQL.query!(PoolRepo, query.(type) , [])
715+
end
716+
assert %{
717+
rows: [["char", "character", "POSIX"]]
718+
} = Ecto.Adapters.SQL.query!(PoolRepo, query.("char") , [])
719+
720+
assert %{
721+
rows: [["text", "text", "POSIX"]]
722+
} = Ecto.Adapters.SQL.query!(PoolRepo, query.("text") , [])
723+
assert %{
724+
rows: [["integer", "integer", nil]]
725+
} = Ecto.Adapters.SQL.query!(PoolRepo, query.("integer") , [])
726+
end
686727
end

lib/ecto/adapters/postgres/connection.ex

+9-1
Original file line numberDiff line numberDiff line change
@@ -1624,14 +1624,22 @@ if Code.ensure_loaded?(Postgrex) do
16241624
defp column_options(type, opts) do
16251625
default = Keyword.fetch(opts, :default)
16261626
null = Keyword.get(opts, :null)
1627+
collation = Keyword.fetch(opts, :collation)
16271628

1628-
[default_expr(default, type), null_expr(null)]
1629+
[default_expr(default, type), null_expr(null), collation(collation, type)]
16291630
end
16301631

16311632
defp null_expr(false), do: " NOT NULL"
16321633
defp null_expr(true), do: " NULL"
16331634
defp null_expr(_), do: []
16341635

1636+
def collation({:ok, collation_name}, text_type)
1637+
when text_type in ~w/string text char varchar/a do
1638+
" COLLATE \"#{collation_name}\""
1639+
end
1640+
1641+
def collation(_, _), do: []
1642+
16351643
defp new_constraint_expr(%Constraint{check: check} = constraint) when is_binary(check) do
16361644
[
16371645
"CONSTRAINT ",

0 commit comments

Comments
 (0)