Skip to content
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

Change the data type of some varchar and text columns to be citext #23

Closed
L-Mario564 opened this issue Mar 17, 2024 · 3 comments · Fixed by #29
Closed

Change the data type of some varchar and text columns to be citext #23

L-Mario564 opened this issue Mar 17, 2024 · 3 comments · Fixed by #29
Labels
database Database schema related issues

Comments

@L-Mario564
Copy link
Contributor

L-Mario564 commented Mar 17, 2024

Why?

citext is a Postgres extension that stores strings in a similar fashion to text but when comparing values, it's case insensitive. This could be useful on some fields for either searching capabilities or to avoid strings that only have one letter changed.

For searching capabilities, this would make it so for example, searching for mario564 will make it so the user Mario564 pops up, instead of having to search for Mario564. This is already possible using Postgres' ilike operator but I think it would simplify things a little bit more.

The more important case is the second one. Putting another example, right now, there's a unique constraint on the name field on the tournament table, which means there can't be two tournaments named osu! World Cup 2023, but if someone were to create one named osu! world cup 2023 then it would be allowed since the casing for W and C are different, but using the citext data type makes it so the latter string violates the unique constraint as citext checks a string's uniqueness case insensitively.

How?

We'd need to create a custom migration to add the extension to the database and then create a custom data type with Drizzle to maintain typesafety.

The affected tables and columns would be:

  • OsuUser: username.
  • DiscordUser: username.
  • Tournament: name.
  • Round: name.
  • StaffRole: name.

Citext Postgres docs: https://www.postgresql.org/docs/current/citext.html
Drizzle custom types docs: https://orm.drizzle.team/docs/custom-types

@L-Mario564 L-Mario564 changed the title Change the data type of some varchar and text to be citext Change the data type of some varchar and text columns to be citext Mar 17, 2024
@L-Mario564 L-Mario564 added the database Database schema related issues label Mar 17, 2024
@ArtemOsuskyi
Copy link
Collaborator

According to some Drizzle docs I've found, we can't currently apply extension types for Drizzle, so sticking to the good old ILIKE seems to be the best approach for now

@L-Mario564
Copy link
Contributor Author

The extension can be applied to the DB by inserting create extension if not exists citext; in a migration file, and then creating the custom type as listed on their website (https://orm.drizzle.team/docs/custom-types). I believe the docs you listed are one's that are outdated and they website are the (mostly) up to date docs.

@ArtemOsuskyi
Copy link
Collaborator

ArtemOsuskyi commented Mar 19, 2024

In that case we'll have to drop length constraints from mentioned varchar fields, because citext - just like text - doesn't accept any params

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
database Database schema related issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants