Skip to content

persistent-postgresql: Newtype conversions in PersistDbSpecific#160

Merged
snoyberg merged 1 commit intoyesodweb:masterfrom
silkapp:dbspecific-novalidation
Sep 20, 2013
Merged

persistent-postgresql: Newtype conversions in PersistDbSpecific#160
snoyberg merged 1 commit intoyesodweb:masterfrom
silkapp:dbspecific-novalidation

Conversation

@bergmark
Copy link
Contributor

As discussed in #153, this adds a newtype around PersistDbSpecific conversions that doesn't do validation. This way native postgresql types that persistent doesn't handle by default can be converted to ByteString.

Example for UUID:

{-# LANGUAGE OverloadedStrings #-}
module UUID where

import Database.Persist
import Database.Persist.Sql
import qualified Data.ByteString.Char8 as B
import qualified Data.UUID as UUID

instance PersistField UUID.UUID where
  toPersistValue u = PersistDbSpecific . B.pack . UUID.toString $ u

  fromPersistValue (PersistDbSpecific t) = case UUID.fromString $ B.unpack t of
    Just x  -> Right x
    Nothing -> Left "Invalid UUID"
  fromPersistValue _ = Left "Not PersistDBSpecific"

instance PersistFieldSql UUID.UUID where
  sqlType _ = SqlOther "UUID"

…rk around postgresql-simple type checking for conversions of internal postgres types
@bergmark
Copy link
Contributor Author

The PersistFieldSql instance should be "uuid" in lowercase

instance PersistFieldSql UUID.UUID where
  sqlType _ = SqlOther "uuid"

otherwise persistent keeps doing a migration.

@gregwebs
Copy link
Member

that looks great! I will let @snoyberg merge this. The name of Unknown should be changed to something slightly more specific if it would ever get exported.

@bergmark
Copy link
Contributor Author

Awesome!

I think this will break the existing Geo example by the way, it shouldn't need to manually escape the bytestring anymore.

snoyberg added a commit that referenced this pull request Sep 20, 2013
persistent-postgresql: Newtype conversions in PersistDbSpecific
@snoyberg snoyberg merged commit 14e25c2 into yesodweb:master Sep 20, 2013
@snoyberg
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