{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE CPP #-}
module Database.Beam.Migrate.Backend
( BeamMigrationBackend(..)
, DdlError
, HaskellPredicateConverter(..)
, sql92HsPredicateConverters
, hasColumnConverter
, trivialHsConverter, hsPredicateConverter
, SomeBeamMigrationBackend(..), SomeCheckedDatabaseSettings(..) )
where
import Database.Beam
import Database.Beam.Backend.SQL
import Database.Beam.Migrate.Actions
import Database.Beam.Migrate.Checks
import Database.Beam.Migrate.Serialization
import Database.Beam.Migrate.SQL
import Database.Beam.Migrate.Types
( SomeDatabasePredicate(..), CheckedDatabaseSettings )
import Database.Beam.Haskell.Syntax
import Control.Applicative
import qualified Control.Monad.Fail as Fail
#if ! MIN_VERSION_base(4,11,0)
import Data.Semigroup
#endif
import Data.Text (Text)
import Data.Time
import Data.Typeable
type DdlError = String
data BeamMigrationBackend be m where
BeamMigrationBackend ::
( MonadBeam be m
, Fail.MonadFail m
, HasQBuilder be
, BeamMigrateSqlBackend be
, HasDataTypeCreatedCheck (BeamMigrateSqlBackendDataTypeSyntax be)
, BeamSqlBackendCanSerialize be LocalTime
, BeamSqlBackendCanSerialize be (Maybe LocalTime)
, BeamSqlBackendCanSerialize be Text
, BeamSqlBackendCanSerialize be SqlNull
, Sql92ReasonableMarshaller be ) =>
{ backendName :: String
, backendConnStringExplanation :: String
, backendGetDbConstraints :: m [ SomeDatabasePredicate ]
, backendPredicateParsers :: BeamDeserializers be
, backendRenderSyntax :: BeamSqlBackendSyntax be -> String
, backendFileExtension :: String
, backendConvertToHaskell :: HaskellPredicateConverter
, backendActionProvider :: ActionProvider be
, backendTransact :: forall a. String -> m a -> IO (Either DdlError a)
} -> BeamMigrationBackend be m
data SomeBeamMigrationBackend where
SomeBeamMigrationBackend :: Typeable be
=> BeamMigrationBackend be m
-> SomeBeamMigrationBackend
data SomeCheckedDatabaseSettings where
SomeCheckedDatabaseSettings :: Database be db => CheckedDatabaseSettings be db
-> SomeCheckedDatabaseSettings
newtype HaskellPredicateConverter
= HaskellPredicateConverter (SomeDatabasePredicate -> Maybe SomeDatabasePredicate)
instance Semigroup HaskellPredicateConverter where
(<>) = mappend
instance Monoid HaskellPredicateConverter where
mempty = HaskellPredicateConverter $ \_ -> Nothing
mappend (HaskellPredicateConverter a) (HaskellPredicateConverter b) =
HaskellPredicateConverter $ \r -> a r <|> b r
sql92HsPredicateConverters :: forall fromBe
. Typeable fromBe
=> (BeamMigrateSqlBackendDataTypeSyntax fromBe -> Maybe HsDataType)
-> HaskellPredicateConverter
sql92HsPredicateConverters convType =
trivialHsConverter @TableExistsPredicate <>
trivialHsConverter @TableHasPrimaryKey <>
hasColumnConverter @fromBe convType
hasColumnConverter :: forall fromBe
. Typeable fromBe
=> (BeamMigrateSqlBackendDataTypeSyntax fromBe -> Maybe HsDataType)
-> HaskellPredicateConverter
hasColumnConverter convType =
hsPredicateConverter $
\(TableHasColumn tbl col ty :: TableHasColumn fromBe) ->
fmap SomeDatabasePredicate (TableHasColumn tbl col <$> convType ty :: Maybe (TableHasColumn HsMigrateBackend))
trivialHsConverter :: forall pred. Typeable pred => HaskellPredicateConverter
trivialHsConverter =
HaskellPredicateConverter $ \orig@(SomeDatabasePredicate p') ->
case cast p' of
Nothing -> Nothing
Just (_ :: pred) -> Just orig
hsPredicateConverter :: Typeable pred => (pred -> Maybe SomeDatabasePredicate) -> HaskellPredicateConverter
hsPredicateConverter f =
HaskellPredicateConverter $ \(SomeDatabasePredicate p') ->
case cast p' of
Nothing -> Nothing
Just p'' -> f p''