module Orville.PostgreSQL.Internal.IndexDefinition
( IndexDefinition
, indexCreationStrategy
, setIndexCreationStrategy
, uniqueIndex
, uniqueNamedIndex
, nonUniqueIndex
, nonUniqueNamedIndex
, mkIndexDefinition
, mkNamedIndexDefinition
, IndexMigrationKey (AttributeBasedIndexKey, NamedIndexKey)
, AttributeBasedIndexMigrationKey (AttributeBasedIndexMigrationKey, indexKeyUniqueness, indexKeyColumns)
, NamedIndexMigrationKey
, indexMigrationKey
, indexCreateExpr
, IndexCreationStrategy (Transactional, Concurrent)
)
where
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NEL
import qualified Orville.PostgreSQL.Expr as Expr
import qualified Orville.PostgreSQL.Marshall.FieldDefinition as FieldDefinition
data IndexDefinition = IndexDefinition
{ IndexDefinition
-> IndexCreationStrategy -> Qualified TableName -> CreateIndexExpr
i_indexCreateExpr ::
IndexCreationStrategy ->
Expr.Qualified Expr.TableName ->
Expr.CreateIndexExpr
, IndexDefinition -> IndexMigrationKey
i_indexMigrationKey :: IndexMigrationKey
, IndexDefinition -> IndexCreationStrategy
i_indexCreationStrategy :: IndexCreationStrategy
}
setIndexCreationStrategy ::
IndexCreationStrategy ->
IndexDefinition ->
IndexDefinition
setIndexCreationStrategy :: IndexCreationStrategy -> IndexDefinition -> IndexDefinition
setIndexCreationStrategy IndexCreationStrategy
strategy IndexDefinition
indexDef =
IndexDefinition
indexDef
{ i_indexCreationStrategy :: IndexCreationStrategy
i_indexCreationStrategy = IndexCreationStrategy
strategy
}
indexCreationStrategy ::
IndexDefinition ->
IndexCreationStrategy
indexCreationStrategy :: IndexDefinition -> IndexCreationStrategy
indexCreationStrategy =
IndexDefinition -> IndexCreationStrategy
i_indexCreationStrategy
data IndexCreationStrategy
=
Transactional
|
Concurrent
deriving (IndexCreationStrategy -> IndexCreationStrategy -> Bool
(IndexCreationStrategy -> IndexCreationStrategy -> Bool)
-> (IndexCreationStrategy -> IndexCreationStrategy -> Bool)
-> Eq IndexCreationStrategy
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: IndexCreationStrategy -> IndexCreationStrategy -> Bool
== :: IndexCreationStrategy -> IndexCreationStrategy -> Bool
$c/= :: IndexCreationStrategy -> IndexCreationStrategy -> Bool
/= :: IndexCreationStrategy -> IndexCreationStrategy -> Bool
Eq, Int -> IndexCreationStrategy -> ShowS
[IndexCreationStrategy] -> ShowS
IndexCreationStrategy -> String
(Int -> IndexCreationStrategy -> ShowS)
-> (IndexCreationStrategy -> String)
-> ([IndexCreationStrategy] -> ShowS)
-> Show IndexCreationStrategy
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> IndexCreationStrategy -> ShowS
showsPrec :: Int -> IndexCreationStrategy -> ShowS
$cshow :: IndexCreationStrategy -> String
show :: IndexCreationStrategy -> String
$cshowList :: [IndexCreationStrategy] -> ShowS
showList :: [IndexCreationStrategy] -> ShowS
Show)
data IndexMigrationKey
= AttributeBasedIndexKey AttributeBasedIndexMigrationKey
| NamedIndexKey NamedIndexMigrationKey
deriving (IndexMigrationKey -> IndexMigrationKey -> Bool
(IndexMigrationKey -> IndexMigrationKey -> Bool)
-> (IndexMigrationKey -> IndexMigrationKey -> Bool)
-> Eq IndexMigrationKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: IndexMigrationKey -> IndexMigrationKey -> Bool
== :: IndexMigrationKey -> IndexMigrationKey -> Bool
$c/= :: IndexMigrationKey -> IndexMigrationKey -> Bool
/= :: IndexMigrationKey -> IndexMigrationKey -> Bool
Eq, Eq IndexMigrationKey
Eq IndexMigrationKey
-> (IndexMigrationKey -> IndexMigrationKey -> Ordering)
-> (IndexMigrationKey -> IndexMigrationKey -> Bool)
-> (IndexMigrationKey -> IndexMigrationKey -> Bool)
-> (IndexMigrationKey -> IndexMigrationKey -> Bool)
-> (IndexMigrationKey -> IndexMigrationKey -> Bool)
-> (IndexMigrationKey -> IndexMigrationKey -> IndexMigrationKey)
-> (IndexMigrationKey -> IndexMigrationKey -> IndexMigrationKey)
-> Ord IndexMigrationKey
IndexMigrationKey -> IndexMigrationKey -> Bool
IndexMigrationKey -> IndexMigrationKey -> Ordering
IndexMigrationKey -> IndexMigrationKey -> IndexMigrationKey
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: IndexMigrationKey -> IndexMigrationKey -> Ordering
compare :: IndexMigrationKey -> IndexMigrationKey -> Ordering
$c< :: IndexMigrationKey -> IndexMigrationKey -> Bool
< :: IndexMigrationKey -> IndexMigrationKey -> Bool
$c<= :: IndexMigrationKey -> IndexMigrationKey -> Bool
<= :: IndexMigrationKey -> IndexMigrationKey -> Bool
$c> :: IndexMigrationKey -> IndexMigrationKey -> Bool
> :: IndexMigrationKey -> IndexMigrationKey -> Bool
$c>= :: IndexMigrationKey -> IndexMigrationKey -> Bool
>= :: IndexMigrationKey -> IndexMigrationKey -> Bool
$cmax :: IndexMigrationKey -> IndexMigrationKey -> IndexMigrationKey
max :: IndexMigrationKey -> IndexMigrationKey -> IndexMigrationKey
$cmin :: IndexMigrationKey -> IndexMigrationKey -> IndexMigrationKey
min :: IndexMigrationKey -> IndexMigrationKey -> IndexMigrationKey
Ord)
data AttributeBasedIndexMigrationKey = AttributeBasedIndexMigrationKey
{ AttributeBasedIndexMigrationKey -> IndexUniqueness
indexKeyUniqueness :: Expr.IndexUniqueness
, AttributeBasedIndexMigrationKey -> [FieldName]
indexKeyColumns :: [FieldDefinition.FieldName]
}
deriving (AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool
(AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool)
-> (AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool)
-> Eq AttributeBasedIndexMigrationKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool
== :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool
$c/= :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool
/= :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool
Eq, Eq AttributeBasedIndexMigrationKey
Eq AttributeBasedIndexMigrationKey
-> (AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Ordering)
-> (AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool)
-> (AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool)
-> (AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool)
-> (AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool)
-> (AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey)
-> (AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey)
-> Ord AttributeBasedIndexMigrationKey
AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool
AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Ordering
AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Ordering
compare :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Ordering
$c< :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool
< :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool
$c<= :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool
<= :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool
$c> :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool
> :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool
$c>= :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool
>= :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey -> Bool
$cmax :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey
max :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey
$cmin :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey
min :: AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey
-> AttributeBasedIndexMigrationKey
Ord, Int -> AttributeBasedIndexMigrationKey -> ShowS
[AttributeBasedIndexMigrationKey] -> ShowS
AttributeBasedIndexMigrationKey -> String
(Int -> AttributeBasedIndexMigrationKey -> ShowS)
-> (AttributeBasedIndexMigrationKey -> String)
-> ([AttributeBasedIndexMigrationKey] -> ShowS)
-> Show AttributeBasedIndexMigrationKey
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AttributeBasedIndexMigrationKey -> ShowS
showsPrec :: Int -> AttributeBasedIndexMigrationKey -> ShowS
$cshow :: AttributeBasedIndexMigrationKey -> String
show :: AttributeBasedIndexMigrationKey -> String
$cshowList :: [AttributeBasedIndexMigrationKey] -> ShowS
showList :: [AttributeBasedIndexMigrationKey] -> ShowS
Show)
type NamedIndexMigrationKey = String
indexMigrationKey :: IndexDefinition -> IndexMigrationKey
indexMigrationKey :: IndexDefinition -> IndexMigrationKey
indexMigrationKey = IndexDefinition -> IndexMigrationKey
i_indexMigrationKey
indexCreateExpr :: IndexDefinition -> Expr.Qualified Expr.TableName -> Expr.CreateIndexExpr
indexCreateExpr :: IndexDefinition -> Qualified TableName -> CreateIndexExpr
indexCreateExpr IndexDefinition
indexDef =
IndexDefinition
-> IndexCreationStrategy -> Qualified TableName -> CreateIndexExpr
i_indexCreateExpr
IndexDefinition
indexDef
(IndexDefinition -> IndexCreationStrategy
i_indexCreationStrategy IndexDefinition
indexDef)
nonUniqueIndex :: NonEmpty FieldDefinition.FieldName -> IndexDefinition
nonUniqueIndex :: NonEmpty FieldName -> IndexDefinition
nonUniqueIndex =
IndexUniqueness -> NonEmpty FieldName -> IndexDefinition
mkIndexDefinition IndexUniqueness
Expr.NonUniqueIndex
nonUniqueNamedIndex :: String -> Expr.IndexBodyExpr -> IndexDefinition
nonUniqueNamedIndex :: String -> IndexBodyExpr -> IndexDefinition
nonUniqueNamedIndex =
IndexUniqueness -> String -> IndexBodyExpr -> IndexDefinition
mkNamedIndexDefinition IndexUniqueness
Expr.NonUniqueIndex
uniqueIndex :: NonEmpty FieldDefinition.FieldName -> IndexDefinition
uniqueIndex :: NonEmpty FieldName -> IndexDefinition
uniqueIndex =
IndexUniqueness -> NonEmpty FieldName -> IndexDefinition
mkIndexDefinition IndexUniqueness
Expr.UniqueIndex
uniqueNamedIndex :: String -> Expr.IndexBodyExpr -> IndexDefinition
uniqueNamedIndex :: String -> IndexBodyExpr -> IndexDefinition
uniqueNamedIndex =
IndexUniqueness -> String -> IndexBodyExpr -> IndexDefinition
mkNamedIndexDefinition IndexUniqueness
Expr.UniqueIndex
mkIndexDefinition ::
Expr.IndexUniqueness ->
NonEmpty FieldDefinition.FieldName ->
IndexDefinition
mkIndexDefinition :: IndexUniqueness -> NonEmpty FieldName -> IndexDefinition
mkIndexDefinition IndexUniqueness
uniqueness NonEmpty FieldName
fieldNames =
let
expr :: IndexCreationStrategy -> Qualified TableName -> CreateIndexExpr
expr IndexCreationStrategy
strategy Qualified TableName
tableName =
IndexUniqueness
-> Maybe ConcurrentlyExpr
-> Qualified TableName
-> NonEmpty ColumnName
-> CreateIndexExpr
Expr.createIndexExpr
IndexUniqueness
uniqueness
(IndexCreationStrategy -> Maybe ConcurrentlyExpr
mkMaybeConcurrently IndexCreationStrategy
strategy)
Qualified TableName
tableName
((FieldName -> ColumnName)
-> NonEmpty FieldName -> NonEmpty ColumnName
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FieldName -> ColumnName
FieldDefinition.fieldNameToColumnName NonEmpty FieldName
fieldNames)
migrationKey :: AttributeBasedIndexMigrationKey
migrationKey =
AttributeBasedIndexMigrationKey
{ indexKeyUniqueness :: IndexUniqueness
indexKeyUniqueness = IndexUniqueness
uniqueness
, indexKeyColumns :: [FieldName]
indexKeyColumns = NonEmpty FieldName -> [FieldName]
forall a. NonEmpty a -> [a]
NEL.toList NonEmpty FieldName
fieldNames
}
in
IndexDefinition
{ i_indexCreateExpr :: IndexCreationStrategy -> Qualified TableName -> CreateIndexExpr
i_indexCreateExpr = IndexCreationStrategy -> Qualified TableName -> CreateIndexExpr
expr
, i_indexMigrationKey :: IndexMigrationKey
i_indexMigrationKey = AttributeBasedIndexMigrationKey -> IndexMigrationKey
AttributeBasedIndexKey AttributeBasedIndexMigrationKey
migrationKey
, i_indexCreationStrategy :: IndexCreationStrategy
i_indexCreationStrategy = IndexCreationStrategy
Transactional
}
mkNamedIndexDefinition ::
Expr.IndexUniqueness ->
String ->
Expr.IndexBodyExpr ->
IndexDefinition
mkNamedIndexDefinition :: IndexUniqueness -> String -> IndexBodyExpr -> IndexDefinition
mkNamedIndexDefinition IndexUniqueness
uniqueness String
indexName IndexBodyExpr
bodyExpr =
let
expr :: IndexCreationStrategy -> Qualified TableName -> CreateIndexExpr
expr IndexCreationStrategy
strategy Qualified TableName
tableName =
IndexUniqueness
-> Maybe ConcurrentlyExpr
-> Qualified TableName
-> IndexName
-> IndexBodyExpr
-> CreateIndexExpr
Expr.createNamedIndexExpr
IndexUniqueness
uniqueness
(IndexCreationStrategy -> Maybe ConcurrentlyExpr
mkMaybeConcurrently IndexCreationStrategy
strategy)
Qualified TableName
tableName
(String -> IndexName
Expr.indexName String
indexName)
IndexBodyExpr
bodyExpr
in
IndexDefinition
{ i_indexCreateExpr :: IndexCreationStrategy -> Qualified TableName -> CreateIndexExpr
i_indexCreateExpr = IndexCreationStrategy -> Qualified TableName -> CreateIndexExpr
expr
, i_indexMigrationKey :: IndexMigrationKey
i_indexMigrationKey = String -> IndexMigrationKey
NamedIndexKey String
indexName
, i_indexCreationStrategy :: IndexCreationStrategy
i_indexCreationStrategy = IndexCreationStrategy
Transactional
}
mkMaybeConcurrently :: IndexCreationStrategy -> Maybe Expr.ConcurrentlyExpr
mkMaybeConcurrently :: IndexCreationStrategy -> Maybe ConcurrentlyExpr
mkMaybeConcurrently IndexCreationStrategy
strategy =
case IndexCreationStrategy
strategy of
IndexCreationStrategy
Transactional -> Maybe ConcurrentlyExpr
forall a. Maybe a
Nothing
IndexCreationStrategy
Concurrent -> ConcurrentlyExpr -> Maybe ConcurrentlyExpr
forall a. a -> Maybe a
Just ConcurrentlyExpr
Expr.concurrently