Using PostgreSQL’s citext extension to compare emails case insensitively

PostgreSQL has an extesion called citext. What it does is comparing all the values in a case insensitve manner. So we can activate and use it in a migration:

execute("CREATE EXTENSION IF NOT EXISTS citext")
create(:users) do
  add :email, :citext
end

And then we can just find it without needing to worry about having to downcase emails ourselves:

{:ok, user} = %User{email: "my@EMAIL.io"} |> Repo.insert()

assert Repo.get_by(email: "my@email.io") == user