Skip to content

Add support in migrations for collations in Postgres#662

Merged
greg-rychlewski merged 8 commits into
elixir-ecto:masterfrom
Zurga:master
May 2, 2025
Merged

Add support in migrations for collations in Postgres#662
greg-rychlewski merged 8 commits into
elixir-ecto:masterfrom
Zurga:master

Conversation

@Zurga

@Zurga Zurga commented Apr 28, 2025

Copy link
Copy Markdown
Contributor

I found this was missing when I added fractional indexes in my application. The default en_US-UTF8 collation will sort "Zy" after "a0". After setting the collation on the columns manually to "POSIX" the sorting worked as expected.

If there is interest to pull this into ecto_sql I will add documentation and support for MySQL and MSSQL as well.

@josevalim

Copy link
Copy Markdown
Member

Yes, a PR is welcome!

@josevalim

Copy link
Copy Markdown
Member

Sorry, to be clear, yes please update this PR :)

@Zurga

Zurga commented Apr 29, 2025

Copy link
Copy Markdown
Contributor Author

@josevalim I think this PR is ready for review. I did not add tests for modifying the collation on columns that have references to other columns. If you want I can look into that, but it requires some more SQL in between the DDL commands.

Comment thread lib/ecto/adapters/myxql/connection.ex Outdated
" COLLATE \"#{collation_name}\""
end

defp collation_expr(_, _), do: []

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should either raise or not check the type and let the underlying SQL operation fail. Perhaps the second option is best, because I don’t know if Postgres supports collations for custom types (which would then be impossible to check).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Postgres does support collations for custom types, so letting the SQL fail is the best option.

Comment thread lib/ecto/migration.ex Outdated
are not known by `ecto_sql` and specifying an incorrect collation might cause
a migration to fail.

### N.B. be sure to match the collation on any columns that reference other text columns. See example below

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s convert this to a regular paragraph or admonition. Please also avoid abbreviations such as NB, i.e. and similar :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do!

@Zurga Zurga requested a review from josevalim May 1, 2025 18:15
Comment thread integration_test/sql/migration.exs Outdated
Comment thread lib/ecto/migration.ex Outdated
@greg-rychlewski greg-rychlewski merged commit f0cdeda into elixir-ecto:master May 2, 2025
11 checks passed
@greg-rychlewski

Copy link
Copy Markdown
Member

thanks! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants