{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Database.Persist.Migration.Operation.Types
( ColumnIdentifier
, dotted
, Column(..)
, validateColumn
, ColumnProp(..)
, TableConstraint(..)
, getConstraintColumns
) where
import Control.Monad (when)
import Data.Data (Data)
import Data.Text (Text)
import qualified Data.Text as Text
import Database.Persist.Migration.Utils.Data (hasDuplicateConstrs)
import Database.Persist.Sql (PersistValue(..))
import Database.Persist.Types (SqlType)
type ColumnIdentifier = (Text, Text)
dotted :: ColumnIdentifier -> Text
dotted (tab, col) = Text.concat [tab, ".", col]
data Column = Column
{ colName :: Text
, colType :: SqlType
, colProps :: [ColumnProp]
} deriving (Show)
validateColumn :: Column -> Either String ()
validateColumn col@Column{..} = when (hasDuplicateConstrs colProps) $
Left $ "Duplicate column properties detected: " ++ show col
data ColumnProp
= NotNull
| References ColumnIdentifier
| AutoIncrement
| Default PersistValue
deriving (Show,Eq,Data)
deriving instance Data PersistValue
data TableConstraint
= PrimaryKey [Text]
| Unique Text [Text]
deriving (Show,Data)
getConstraintColumns :: TableConstraint -> [Text]
getConstraintColumns = \case
PrimaryKey cols -> cols
Unique _ cols -> cols