Specify DB migrations in terms of your persistent model
[[ 🗃
^0rd3E persistent-migration
]] ::
[📥 Inbox]
[📤 Outbox]
[🐤 Followers]
[🤝 Collaborators]
[🛠 Changes]
Clone
HTTPS:
darcs clone https://vervis.peers.community/repos/0rd3E
SSH:
darcs clone USERNAME@vervis.peers.community:0rd3E
Tags
TODO
Types.hs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | {- This file is part of persistent-migration.
-
- Written in 2016, 2018 by fr33domlover <fr33domlover@riseup.net>.
-
- ♡ Copying is an act of love. Please copy, reuse and share.
-
- The author(s) have dedicated all copyright and related and neighboring
- rights to this software to the public domain worldwide. This software is
- distributed without any warranty.
-
- You should have received a copy of the CC0 Public Domain Dedication along
- with this software. If not, see
- <http://creativecommons.org/publicdomain/zero/1.0/>.
-}
{-# LANGUAGE OverloadedStrings #-}
-- | Types for describing schemas and migrations.
module Database.Persist.Schema.Types
( FieldName (..)
, EntityName (..)
, UniqueName (..)
, FieldType (..)
, FieldMaybe (..)
, Field (..)
, Unique (..)
, Entity (..)
)
where
import Data.Char (isAsciiLower, isAsciiUpper)
import Data.String (IsString (..))
import Data.Text (Text)
import qualified Data.Text as T (uncons, all, stripPrefix)
import Database.Persist.BackendDataType
isAsciiLetter :: Char -> Bool
isAsciiLetter c = isAsciiLower c || isAsciiUpper c
newtype FieldName = FieldName { unFieldName :: Text }
instance IsString FieldName where
fromString s =
let t = fromString s
in case T.uncons t of
Nothing -> error "empty field name"
Just (c, r) ->
if isAsciiLower c
then
if T.all isAsciiLetter r
then FieldName t
else
error "non ascii-letter char in field name"
else
error
"field name doesn't start with lowercase \
\ascii letter"
newtype EntityName = EntityName { unEntityName :: Text }
instance IsString EntityName where
fromString s =
let t = fromString s
in case T.uncons t of
Nothing -> error "empty entity name"
Just (c, r) ->
if isAsciiUpper c
then
if T.all isAsciiLetter r
then EntityName t
else
error
"non ascii-letter char in entity name"
else
error
"entity name doesn't start with uppercase \
\ascii letter"
newtype UniqueName = UniqueName { unUniqueName :: Text }
instance IsString UniqueName where
fromString s =
let t = fromString s
in case T.stripPrefix "Unique" t of
Nothing -> error "unique name doesn't start with \"Unique\""
Just u ->
case T.uncons u of
Nothing -> error "unique name is just \"Unique\""
Just (c, r) ->
if isAsciiUpper c
then
if T.all isAsciiLetter r
then UniqueName t
else
error
"non ascii-letter char in \
\unique name"
else
error
"unique name doesn't follow with \
\uppercase ascii letter after Unique"
data FieldType backend = FTPrim (BackendDataType backend) | FTRef EntityName
data FieldMaybe = FieldMaybe | FieldRequired
data Field backend = Field
{ fieldName :: FieldName
, fieldType :: FieldType backend
, fieldMaybe :: FieldMaybe
}
data Unique = Unique
{ uniqueName :: UniqueName
, uniqueFields :: [FieldName]
}
data Entity backend = Entity
{ entityName :: EntityName
, entityFields :: [Field backend]
, entityUniques :: [Unique]
}
|