Skip to content

Add support in migrations for collations in Postgres #662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 2, 2025

Conversation

Zurga
Copy link
Contributor

@Zurga Zurga commented Apr 28, 2025

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
Member

Yes, a PR is welcome!

@josevalim
Copy link
Member

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

@Zurga
Copy link
Contributor Author

Zurga commented Apr 29, 2025

@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.

" COLLATE \"#{collation_name}\""
end

defp collation_expr(_, _), do: []
Copy link
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
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.

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
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
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
@greg-rychlewski greg-rychlewski merged commit f0cdeda into elixir-ecto:master May 2, 2025
11 checks passed
@greg-rychlewski
Copy link
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