yesodweb / yesodweb/persistent

Better support for case-insensitive text?

Open
#732 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Haskell
Stars
486
Forks
306
PR merge metrics
No merged PRs in 30d

Description

A persistent user on Reddit recently asked about support for case insensitive text: https://www.reddit.com/r/haskell/comments/7erfio/persistmarshalerror_when_using_text_haskell_value/

Postgres has it's own citext type, and I came up with this code to use it: https://gist.github.com/MaxGabriel/9e757f2da60ac53b45bb06b2b097d86b

The core of which is these instances:

instance PersistField (CI Text) where
  toPersistValue ciText = PersistDbSpecific $ TE.encodeUtf8 (CI.original ciText)
  fromPersistValue (PersistDbSpecific bs) = Right $ CI.mk (TE.decodeUtf8 bs)
  fromPersistValue x = Left $ T.pack $ "Expected PersistDbSpecific, received: " ++ show x

instance PersistFieldSql (CI Text) where
  sqlType _ = SqlOther "citext"

instance (ToJSON (CI Text)) where
  toJSON a = J.String (CI.original a)

instance (FromJSON (CI Text)) where
  parseJSON (J.String text) = pure $ CI.mk text
  parseJSON v          = fail $ "Expected String, encountered " ++ (show v)

Any thoughts on if this is the best approach, and if the code should be incorporated into Persistent proper? I would say definitely not for the aeson instances. And only possibly for the PersistFieldSql instances, since they'd be orphans and only right for persistent-postgresql (for MySQL and SQLite, you would want to use a normal string type with a case insensitive collation (this is actually the default for MySQL)).

My instinct is that the best option would be some new package with extra instances for all the different Postgres types (I know of the geography types and citext; I'm sure they're more), which may or may not be part of persistent's repo. Anyone know if such a package already exists?

cc @parsonsmatt

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked gist and the issue's discussion of PostgreSQL's citext type and PersistField instances. Compare the proposed PostgreSQL approach with the stated MySQL and SQLite differences. Done requires an agreed scope for Persistent support or a separate package, but the issue does not specify files, tests, or a concrete implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell, postgresql
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.