{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE CPP #-}
module Database.Beam.Migrate.Checks where
import Database.Beam.Backend.SQL.SQL92
import Database.Beam.Migrate.SQL.SQL92
import Database.Beam.Migrate.SQL.Types
import Database.Beam.Migrate.Serialization
import Database.Beam.Migrate.Types.Predicates
import Data.Aeson ((.:), (.=), withObject, object)
import Data.Aeson.Types (Parser, Value)
import Data.Hashable (Hashable(..))
import Data.Text (Text)
import Data.Typeable (Typeable, cast)
#if !MIN_VERSION_base(4, 11, 0)
import Data.Semigroup
#endif
import GHC.Generics (Generic)
data TableExistsPredicate = TableExistsPredicate QualifiedName
deriving (Show, Eq, Ord, Typeable, Generic)
instance Hashable TableExistsPredicate
instance DatabasePredicate TableExistsPredicate where
englishDescription (TableExistsPredicate t) =
"Table " <> show t <> " must exist"
serializePredicate (TableExistsPredicate t) =
object [ "table-exists" .= t ]
predicateSpecificity _ = PredicateSpecificityAllBackends
class HasDataTypeCreatedCheck dataType where
dataTypeHasBeenCreated :: dataType -> (forall preCondition. Typeable preCondition => [ preCondition ]) -> Bool
data TableHasColumn be where
TableHasColumn
:: ( HasDataTypeCreatedCheck (BeamMigrateSqlBackendDataTypeSyntax be) )
=> { hasColumn_table :: QualifiedName
, hasColumn_column :: Text
, hasColumn_type :: BeamMigrateSqlBackendDataTypeSyntax be
}
-> TableHasColumn be
instance Hashable (BeamMigrateSqlBackendDataTypeSyntax be) => Hashable (TableHasColumn be) where
hashWithSalt salt (TableHasColumn t c s) = hashWithSalt salt (t, c, s)
instance Eq (BeamMigrateSqlBackendDataTypeSyntax be) => Eq (TableHasColumn be) where
TableHasColumn aTbl aCol aDt == TableHasColumn bTbl bCol bDt =
aTbl == bTbl && aCol == bCol && aDt == bDt
instance ( Typeable be
, BeamMigrateOnlySqlBackend be
, Hashable (BeamMigrateSqlBackendDataTypeSyntax be) ) =>
DatabasePredicate (TableHasColumn be) where
englishDescription (TableHasColumn tbl col type_) =
"Table " <> show tbl <> " must have a column " <> show col <> " of " <> displaySyntax type_
predicateSpecificity _ = PredicateSpecificityAllBackends
serializePredicate (TableHasColumn tbl col type_) =
object [ "has-column" .= object [ "table" .= tbl, "column" .= col
, "type" .= serializeDataType type_ ]]
predicateCascadesDropOn (TableHasColumn tblNm _ _) p'
| Just (TableExistsPredicate tblNm') <- cast p' = tblNm' == tblNm
| otherwise = False
data TableColumnHasConstraint be
= TableColumnHasConstraint
{ hasConstraint_table :: QualifiedName
, hasConstraint_column :: Text
, hasConstraint_defn :: BeamSqlBackendColumnConstraintDefinitionSyntax be
} deriving Generic
instance Hashable (BeamSqlBackendColumnConstraintDefinitionSyntax be) => Hashable (TableColumnHasConstraint be)
deriving instance Eq (BeamSqlBackendColumnConstraintDefinitionSyntax be) => Eq (TableColumnHasConstraint be)
instance ( Typeable be, BeamMigrateOnlySqlBackend be
, Hashable (BeamSqlBackendColumnConstraintDefinitionSyntax be) ) =>
DatabasePredicate (TableColumnHasConstraint be) where
englishDescription (TableColumnHasConstraint tbl col cns) =
"Column " <> show tbl <> "." <> show col <> " has constraint " <> displaySyntax cns
predicateSpecificity _ = PredicateSpecificityAllBackends
serializePredicate (TableColumnHasConstraint tbl col cns) =
object [ "has-column-constraint" .= object [ "table" .= tbl, "column" .= col
, "constraint" .= serializeConstraint cns ] ]
predicateCascadesDropOn (TableColumnHasConstraint tblNm colNm _) p'
| Just (TableExistsPredicate tblNm') <- cast p' = tblNm' == tblNm
| Just (TableHasColumn tblNm' colNm' _ :: TableHasColumn be) <- cast p' = tblNm' == tblNm && colNm' == colNm
| otherwise = False
data TableHasPrimaryKey
= TableHasPrimaryKey
{ hasPrimaryKey_table :: QualifiedName
, hasPrimaryKey_cols :: [Text]
} deriving (Show, Eq, Generic)
instance Hashable TableHasPrimaryKey
instance DatabasePredicate TableHasPrimaryKey where
englishDescription (TableHasPrimaryKey tblName colNames) =
"Table " <> show tblName <> " has primary key " <> show colNames
predicateSpecificity _ = PredicateSpecificityAllBackends
serializePredicate (TableHasPrimaryKey tbl cols) =
object [ "has-primary-key" .= object [ "table" .= tbl
, "columns" .= cols ] ]
predicateCascadesDropOn (TableHasPrimaryKey tblNm _) p'
| Just (TableExistsPredicate tblNm') <- cast p' = tblNm' == tblNm
| otherwise = False
beamCheckDeserializers
:: forall be
. ( Typeable be, BeamMigrateOnlySqlBackend be
, HasDataTypeCreatedCheck (BeamMigrateSqlBackendDataTypeSyntax be) )
=> BeamDeserializers be
beamCheckDeserializers = mconcat
[ beamDeserializer (const deserializeTableExistsPredicate)
, beamDeserializer (const deserializeTableHasPrimaryKeyPredicate)
, beamDeserializer deserializeTableHasColumnPredicate
, beamDeserializer deserializeTableColumnHasConstraintPredicate
]
where
deserializeTableExistsPredicate :: Value -> Parser SomeDatabasePredicate
deserializeTableExistsPredicate =
withObject "TableExistPredicate" $ \v ->
SomeDatabasePredicate <$> (TableExistsPredicate <$> v .: "table-exists")
deserializeTableHasPrimaryKeyPredicate :: Value -> Parser SomeDatabasePredicate
deserializeTableHasPrimaryKeyPredicate =
withObject "TableHasPrimaryKey" $ \v ->
v .: "has-primary-key" >>=
(withObject "TableHasPrimaryKey" $ \v' ->
SomeDatabasePredicate <$> (TableHasPrimaryKey <$> v' .: "table" <*> v' .: "columns"))
deserializeTableHasColumnPredicate :: BeamDeserializers be'
-> Value -> Parser SomeDatabasePredicate
deserializeTableHasColumnPredicate d =
withObject "TableHasColumn" $ \v ->
v .: "has-column" >>=
(withObject "TableHasColumn" $ \v' ->
SomeDatabasePredicate <$>
fmap (id @(TableHasColumn be))
(TableHasColumn <$> v' .: "table" <*> v' .: "column"
<*> (beamDeserialize d =<< v' .: "type")))
deserializeTableColumnHasConstraintPredicate :: BeamDeserializers be'
-> Value -> Parser SomeDatabasePredicate
deserializeTableColumnHasConstraintPredicate d =
withObject "TableColumnHasConstraint" $ \v ->
v .: "has-column-constraint" >>=
(withObject "TableColumnHasConstraint" $ \v' ->
SomeDatabasePredicate <$>
fmap (id @(TableColumnHasConstraint be))
(TableColumnHasConstraint <$> v' .: "table" <*> v' .: "column"
<*> (beamDeserialize d =<< v' .: "constraint")))