-- | Instances for the Data.UUID UUID type to be mapped to postgresql's -- custom builtin uuid type. -- -- Unfortunately, this breaks compatability with other SQL databases -- (though uuids aren't really supported by most anyways) module PersistOrphans () where import Data.Either.Combinators (maybeToRight) import qualified Data.Text as T import Data.UUID (UUID) import Data.UUID as UUID import Data.UUID.V4 import Database.Persist (PersistField (..), PersistValue (PersistLiteralEscaped), SqlType (SqlOther)) import Database.Persist.Sql (PersistFieldSql (..), SqlBackend, migrate, runMigration) import Web.PathPieces (PathPiece (..)) instance PersistField UUID where toPersistValue = PersistLiteralEscaped . UUID.toASCIIBytes fromPersistValue (PersistLiteralEscaped buf) = maybeToRight "not a uuid (cannot decode)" $ UUID.fromASCIIBytes buf fromPersistValue v = Left $ "not a uuid (wrong type in database): " <> T.pack (show v) -- postgres is type-safe, so this should /hopefully/ never happen instance PersistFieldSql UUID where sqlType = const $ SqlOther "uuid" instance PathPiece UUID where fromPathPiece = UUID.fromText toPathPiece = UUID.toText