diff --git a/package.yaml b/package.yaml index 5dcd91b..dcaa2de 100644 --- a/package.yaml +++ b/package.yaml @@ -26,13 +26,13 @@ library: - monad-logger >= 0.3.29 && < 0.4 - mtl >= 2.2.2 && < 2.3 - persistent >= 2.8.2 && < 2.14 - - resource-pool >= 0.2.3.2 && < 0.3 + - resource-pool >= 0.2.3.2 && < 0.5 - resourcet >= 1.2.1 && < 1.3 - text >= 1.2.3.0 && < 1.3 - transformers >= 0.5.2.0 && < 0.6 - unliftio >= 0.2.7.0 && < 0.3 - unliftio-core >= 0.1.2.0 && < 0.3 - - unliftio-pool >= 0.2.0.0 && < 0.3 + - unliftio-pool >= 0.2.0.0 && < 0.5 tests: persistent-mtl-test: diff --git a/persistent-mtl.cabal b/persistent-mtl.cabal index 2629bc8..27164ef 100644 --- a/persistent-mtl.cabal +++ b/persistent-mtl.cabal @@ -1,6 +1,6 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.34.4. +-- This file has been generated from package.yaml by hpack version 0.35.2. -- -- see: https://github.com/sol/hpack @@ -35,6 +35,8 @@ library Control.Monad.IO.Rerunnable Database.Persist.Monad Database.Persist.Monad.Class + Database.Persist.Monad.Orphan.QueryLift + Database.Persist.Monad.Orphan.TransactionLift Database.Persist.Monad.Shim Database.Persist.Monad.SqlQueryRep Database.Persist.Monad.TestUtils @@ -51,13 +53,13 @@ library , monad-logger >=0.3.29 && <0.4 , mtl >=2.2.2 && <2.3 , persistent >=2.8.2 && <2.14 - , resource-pool >=0.2.3.2 && <0.3 + , resource-pool >=0.2.3.2 && <0.5 , resourcet >=1.2.1 && <1.3 , text >=1.2.3.0 && <1.3 , transformers >=0.5.2.0 && <0.6 , unliftio >=0.2.7.0 && <0.3 , unliftio-core >=0.1.2.0 && <0.3 - , unliftio-pool >=0.2.0.0 && <0.3 + , unliftio-pool >=0.2.0.0 && <0.5 default-language: Haskell2010 test-suite persistent-mtl-test diff --git a/scripts/generate/persistent-api.yaml b/scripts/generate/persistent-api.yaml index 8038165..fe58475 100644 --- a/scripts/generate/persistent-api.yaml +++ b/scripts/generate/persistent-api.yaml @@ -479,11 +479,6 @@ - 'Migration' result: '[Text]' -- name: runMigrationSilent - args: - - 'Migration' - result: '[Text]' - - name: runMigrationUnsafe args: - 'Migration' diff --git a/scripts/generate/templates/Shim.mustache b/scripts/generate/templates/Shim.mustache index f696cba..a39c8a9 100644 --- a/scripts/generate/templates/Shim.mustache +++ b/scripts/generate/templates/Shim.mustache @@ -1,7 +1,9 @@ {-| Module: Database.Persist.Monad.Shim -Defines all the @persistent@ functions lifted into 'MonadSqlQuery'. +Defines all the @persistent@ functions lifted into 'MonadSqlQuery', and +re-exports a version of the @Database.Persist.Sql@ module replacing each +function with the lifted version. This file is autogenerated, to keep it in sync with @Database.Persist.Monad.SqlQueryRep@. @@ -15,7 +17,15 @@ This file is autogenerated, to keep it in sync with {-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE RankNTypes #-} -module Database.Persist.Monad.Shim where +module Database.Persist.Monad.Shim ( + module Database.Persist.Sql, + {{#functions}} + {{#withCondition}} + {{{name}}}, + {{/withCondition}} + {{/functions}} + unsafeLiftSql, + ) where import Control.Monad.IO.Class (MonadIO) import Control.Monad.Trans.Class (lift) @@ -27,10 +37,17 @@ import Data.Map (Map) import Data.Text (Text) import Data.Typeable (Typeable) import Data.Void (Void) -import Database.Persist.Sql hiding (pattern Update) +import Database.Persist.Sql hiding ( + {{#functions}} + {{#withCondition}} + {{{name}}}, + {{/withCondition}} + {{/functions}} + pattern Update + ) import GHC.Stack (HasCallStack) -import Database.Persist.Monad.Class (MonadSqlQuery(..)) +import Database.Persist.Monad.Class (MonadSqlQuery, runCompatibleQueryRep) import Database.Persist.Monad.SqlQueryRep (SqlQueryRep(..)) {-# ANN module "HLint: ignore" #-} @@ -42,10 +59,10 @@ import Database.Persist.Monad.SqlQueryRep (SqlQueryRep(..)) :: ({{#constraints}}{{{type}}}, {{/constraints}}{{#recordTypeVars}}Typeable {{.}}, {{/recordTypeVars}}MonadSqlQuery m) => {{#args}}{{{type}}} -> {{/args}}{{result}} {{#conduitFrom?}} -{{{name}}} {{#args}}a{{index}} {{/args}}= fromAcquire $ runQueryRep $ {{{conduitFrom.nameCapital}}}{{#args}} a{{index}}{{/args}} +{{{name}}} {{#args}}a{{index}} {{/args}}= fromAcquire $ runCompatibleQueryRep $ {{{conduitFrom.nameCapital}}}{{#args}} a{{index}}{{/args}} {{/conduitFrom?}} {{^conduitFrom?}} -{{{name}}} {{#args}}a{{index}} {{/args}}= runQueryRep $ {{{nameCapital}}}{{#args}} a{{index}}{{/args}} +{{{name}}} {{#args}}a{{index}} {{/args}}= runCompatibleQueryRep $ {{{nameCapital}}}{{#args}} a{{index}}{{/args}} {{/conduitFrom?}} {{/withCondition}} @@ -64,7 +81,7 @@ import Database.Persist.Monad.SqlQueryRep (SqlQueryRep(..)) -- select q = unsafeLiftSql "esqueleto-select" (E.select q) -- @ unsafeLiftSql :: MonadSqlQuery m => Text -> (forall m2. MonadIO m2 => SqlPersistT m2 a) -> m a -unsafeLiftSql label action = runQueryRep $ UnsafeLiftSql label action +unsafeLiftSql label action = runCompatibleQueryRep $ UnsafeLiftSql label action {- Helpers -} diff --git a/scripts/generate/templates/SqlQueryRep.mustache b/scripts/generate/templates/SqlQueryRep.mustache index f2cf623..af08dd8 100644 --- a/scripts/generate/templates/SqlQueryRep.mustache +++ b/scripts/generate/templates/SqlQueryRep.mustache @@ -12,20 +12,25 @@ This file is autogenerated, to keep it in sync with {-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE LambdaCase #-} +{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE TypeSynonymInstances #-} module Database.Persist.Monad.SqlQueryRep - ( SqlQueryRep(..) + ( QueryRepCompatible(..) + , withCompatibleQueryRep + , SqlQueryRep(..) , runSqlQueryRep ) where import Control.Monad.IO.Class (MonadIO) -import Control.Monad.IO.Unlift (MonadUnliftIO) import Data.Acquire (Acquire) import Data.Conduit (ConduitM) import Data.Int (Int64) @@ -40,6 +45,33 @@ import GHC.Stack (HasCallStack) {-# ANN module "HLint: ignore" #-} +-- | Class of compatible query representations. +-- +-- If @'QueryRepCompatible' sup sub@, then queries in @sup@ can also +-- represented as queries in @sub@ - so @sub@ must be at least as detailed +-- as @sup@ in representing queries. +-- +-- This is the equivalent to Persistent's 'BackendCompatible', but for query +-- representations +class QueryRepCompatible sup sub where + projectQueryRep :: sup record a -> sub record a + +instance QueryRepCompatible q q where + projectQueryRep = id + +-- | Run a function expecting a particular 'QueryRep' type using a compatible type +-- instead. This allows for "lifting" functions from the base @sub@ 'QueryRep' to +-- the more-powerful @sup@ 'QueryRep'. +-- +-- This is the equivalent to Persistent's 'withCompatibleBackend', using our +-- notion of compatiblity ('QueryRepCompatible'). +withCompatibleQueryRep + :: QueryRepCompatible sup sub + => (sub record a -> b) + -> sup record a + -> b +withCompatibleQueryRep = (. projectQueryRep) + -- | The data type containing a constructor for each persistent function we'd -- like to lift into 'Database.Persist.Monad.MonadSqlQuery'. -- @@ -81,7 +113,7 @@ instance Typeable record => Show (SqlQueryRep record a) where -- | A helper to execute the actual @persistent@ function corresponding to -- each 'SqlQueryRep' data constructor. -runSqlQueryRep :: MonadUnliftIO m => SqlQueryRep record a -> Persist.SqlPersistT m a +runSqlQueryRep :: MonadIO m => SqlQueryRep record a -> Persist.SqlPersistT m a runSqlQueryRep = \case {{#sqlQueryRepConstructors}} {{#withCondition}} diff --git a/src/Database/Persist/Monad.hs b/src/Database/Persist/Monad.hs index 6b87a22..9626a7c 100644 --- a/src/Database/Persist/Monad.hs +++ b/src/Database/Persist/Monad.hs @@ -52,7 +52,10 @@ myFunction = do module Database.Persist.Monad ( -- * Type class for executing database queries - MonadSqlQuery + MonadQuery + , MonadSqlQuery + , MonadTransaction + , MonadSqlTransaction , withTransaction -- * SqlQueryT monad transformer @@ -81,7 +84,6 @@ import Control.Monad.Reader.Class (MonadReader(..)) import Control.Monad.Trans.Class (MonadTrans(..)) import Control.Monad.Trans.Resource (MonadResource) import Data.Pool (Pool) -import Database.Persist.Sql (SqlBackend, SqlPersistT, runSqlConn) import qualified GHC.TypeLits as GHC import UnliftIO.Concurrent (threadDelay) import UnliftIO.Exception (Exception, SomeException, catchJust, throwIO) @@ -117,14 +119,16 @@ instance => MonadIO (SqlTransaction m) where liftIO = undefined -instance (MonadSqlQuery m, MonadUnliftIO m) => MonadSqlQuery (SqlTransaction m) where +instance MonadTransaction m => MonadTransaction (SqlTransaction m) where type TransactionM (SqlTransaction m) = TransactionM m - runQueryRep = SqlTransaction . runSqlQueryRep - -- Delegate to 'm', since 'm' is in charge of starting/stopping transactions. -- 'SqlTransaction' is ONLY in charge of executing queries. - withTransaction = SqlTransaction . withTransaction + withTransaction = SqlTransaction . lift . withTransaction + +instance (MonadSqlQuery m, MonadUnliftIO m) => MonadQuery (SqlTransaction m) where + type QueryRep (SqlTransaction m) = SqlQueryRep + runQueryRep = SqlTransaction . runSqlQueryRep runSqlTransaction :: MonadUnliftIO m => SqlBackend -> SqlTransaction m a -> m a runSqlTransaction conn = (`runSqlConn` conn) . unSqlTransaction @@ -199,12 +203,9 @@ newtype SqlQueryT m a = SqlQueryT , MonadLogger ) -instance MonadUnliftIO m => MonadSqlQuery (SqlQueryT m) where +instance MonadUnliftIO m => MonadTransaction (SqlQueryT m) where type TransactionM (SqlQueryT m) = SqlTransaction (SqlQueryT m) - -- Running a query directly in SqlQueryT will create a one-off transaction. - runQueryRep = withTransaction . runQueryRep - -- Start a new transaction and run the given 'SqlTransaction' withTransaction m = do SqlQueryEnv{..} <- SqlQueryT ask @@ -218,6 +219,13 @@ instance MonadUnliftIO m => MonadSqlQuery (SqlQueryT m) where else throwIO RetryLimitExceeded in loop 0 + +instance MonadUnliftIO m => MonadQuery (SqlQueryT m) where + type QueryRep (SqlQueryT m) = SqlQueryRep + + -- Running a query directly in SqlQueryT will create a one-off transaction. + runQueryRep = withTransaction . runQueryRep + instance MonadUnliftIO m => MonadUnliftIO (SqlQueryT m) where withRunInIO = wrappedWithRunInIO SqlQueryT unSqlQueryT diff --git a/src/Database/Persist/Monad/Class.hs b/src/Database/Persist/Monad/Class.hs index f435f00..491dddd 100644 --- a/src/Database/Persist/Monad/Class.hs +++ b/src/Database/Persist/Monad/Class.hs @@ -6,88 +6,82 @@ in order to interpret how to run a 'Database.Persist.Monad.SqlQueryRep.SqlQueryRep' sent by a lifted function from @Database.Persist.Monad.Shim@. -} +{-# LANGUAGE ConstraintKinds #-} +{-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE GeneralizedNewtypeDeriving #-} +{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE UndecidableSuperClasses #-} module Database.Persist.Monad.Class - ( MonadSqlQuery(..) + ( MonadTransaction(..) + , MonadQuery(..) + , MonadSqlTransaction + , MonadSqlQuery + , runCompatibleQueryRep + , Via(..) ) where -import Control.Monad.Trans.Class (lift) -import qualified Control.Monad.Trans.Except as Except -import qualified Control.Monad.Trans.Identity as Identity -import qualified Control.Monad.Trans.Maybe as Maybe -import qualified Control.Monad.Trans.RWS.Lazy as RWS.Lazy -import qualified Control.Monad.Trans.RWS.Strict as RWS.Strict -import qualified Control.Monad.Trans.Reader as Reader -import qualified Control.Monad.Trans.State.Lazy as State.Lazy -import qualified Control.Monad.Trans.State.Strict as State.Strict -import qualified Control.Monad.Trans.Writer.Lazy as Writer.Lazy -import qualified Control.Monad.Trans.Writer.Strict as Writer.Strict import Data.Kind (Type) import Data.Typeable (Typeable) -import Database.Persist.Monad.SqlQueryRep (SqlQueryRep) +import Database.Persist.Monad.SqlQueryRep (QueryRepCompatible(..), SqlQueryRep) --- | The type-class for monads that can run persistent database queries. -class (Monad m, MonadSqlQuery (TransactionM m)) => MonadSqlQuery m where +-- | The type-class for monads that can execute queries in a single transaction +class (Monad m, MonadQuery (TransactionM m)) => MonadTransaction m where type TransactionM m :: Type -> Type - -- | Interpret the given SQL query operation. - runQueryRep :: Typeable record => SqlQueryRep record a -> m a - -- | Run all queries in the given action using the same database connection. withTransaction :: TransactionM m a -> m a -{- Instances for common monad transformers -} - -instance MonadSqlQuery m => MonadSqlQuery (Reader.ReaderT r m) where - type TransactionM (Reader.ReaderT r m) = TransactionM m - runQueryRep = lift . runQueryRep - withTransaction = lift . withTransaction - -instance MonadSqlQuery m => MonadSqlQuery (Except.ExceptT e m) where - type TransactionM (Except.ExceptT e m) = TransactionM m - runQueryRep = lift . runQueryRep - withTransaction = lift . withTransaction - -instance MonadSqlQuery m => MonadSqlQuery (Identity.IdentityT m) where - type TransactionM (Identity.IdentityT m) = TransactionM m - runQueryRep = lift . runQueryRep - withTransaction = lift . withTransaction - -instance MonadSqlQuery m => MonadSqlQuery (Maybe.MaybeT m) where - type TransactionM (Maybe.MaybeT m) = TransactionM m - runQueryRep = lift . runQueryRep - withTransaction = lift . withTransaction +type MonadSqlTransaction m = (MonadTransaction m, QueryRepCompatible SqlQueryRep (QueryRep (TransactionM m))) -instance (Monoid w, MonadSqlQuery m) => MonadSqlQuery (RWS.Lazy.RWST r w s m) where - type TransactionM (RWS.Lazy.RWST r w s m) = TransactionM m - runQueryRep = lift . runQueryRep - withTransaction = lift . withTransaction - -instance (Monoid w, MonadSqlQuery m) => MonadSqlQuery (RWS.Strict.RWST r w s m) where - type TransactionM (RWS.Strict.RWST r w s m) = TransactionM m - runQueryRep = lift . runQueryRep - withTransaction = lift . withTransaction - -instance MonadSqlQuery m => MonadSqlQuery (State.Lazy.StateT s m) where - type TransactionM (State.Lazy.StateT s m) = TransactionM m - runQueryRep = lift . runQueryRep - withTransaction = lift . withTransaction - -instance MonadSqlQuery m => MonadSqlQuery (State.Strict.StateT s m) where - type TransactionM (State.Strict.StateT s m) = TransactionM m - runQueryRep = lift . runQueryRep - withTransaction = lift . withTransaction - -instance (Monoid w, MonadSqlQuery m) => MonadSqlQuery (Writer.Lazy.WriterT w m) where - type TransactionM (Writer.Lazy.WriterT w m) = TransactionM m - runQueryRep = lift . runQueryRep - withTransaction = lift . withTransaction - -instance (Monoid w, MonadSqlQuery m) => MonadSqlQuery (Writer.Strict.WriterT w m) where - type TransactionM (Writer.Strict.WriterT w m) = TransactionM m - runQueryRep = lift . runQueryRep - withTransaction = lift . withTransaction +-- | The type-class for monads that can run persistent database queries. +class (Monad m) => MonadQuery m where + type QueryRep m :: Type -> Type -> Type + + -- | Interpret the given query operation. + runQueryRep :: Typeable record => QueryRep m record a -> m a + +type MonadSqlQuery m = (MonadQuery m, QueryRepCompatible SqlQueryRep (QueryRep m)) + +runCompatibleQueryRep + :: (MonadQuery m, QueryRepCompatible rep (QueryRep m), Typeable record) + => rep record a + -> m a +runCompatibleQueryRep = runQueryRep . projectQueryRep + +-- | A helpful monad wrapper for running a particular DB function "via" a +-- compatible backend, rather than the current one. +-- +-- 'MonadQuery' specifies a specific 'QueryRep' type for each monad. But +-- classy DB functions (like ones that constrain by 'MonadSqlQuery') allow for +-- any query representation, provided it is 'QueryRepCompatible' with the +-- query representation they actually want to use - like, say, 'SqlQueryRep'. +-- +-- So in a function that wants +-- @('MonadQuery m', 'QueryRepCompatible' MyQueryRep ('QueryRep' m))@ +-- if we try to call a function that constrains by +-- @('MonadQuery m', 'QueryRepCompatible' MyOtherRep ('QueryRep' m))@ +-- we will be told that the compiler cannot determine +-- @'QueryRepCompatible' MyOtherRep ('QueryRep' m)@ +-- even if 'MyQueryRep' and 'MyOtherRep' *are* compatible. In this case, +-- polymorphism is a problem: the type doesn't know that it should first +-- convert @'QueryRep' m@ to a @MyQueryRep@, and then @MyQueryRep@ to a +-- @MyOtherRep@. We have to guide it there, which 'runVia' lets us do. +-- +-- In the above example, we would use @'runVia' \@MyQueryRep$ ...@ to call +-- the second function, to tell the compiler that we want to use the +-- compatibility between @MyQueryRep@ and @MyOtherRep@. +newtype Via (sub :: Type -> Type -> Type) m a = Via { runVia :: m a } + deriving newtype (Functor, Applicative, Monad) + +instance + ( MonadQuery m + , QueryRepCompatible sub (QueryRep m) + ) => MonadQuery (Via sub m) where + type QueryRep (Via sub m) = sub + runQueryRep = Via . runCompatibleQueryRep diff --git a/src/Database/Persist/Monad/Orphan/QueryLift.hs b/src/Database/Persist/Monad/Orphan/QueryLift.hs new file mode 100644 index 0000000..a71e80b --- /dev/null +++ b/src/Database/Persist/Monad/Orphan/QueryLift.hs @@ -0,0 +1,62 @@ +{-| +Module: Database.Persist.Monad.Orphan.QueryLift + +Instances for common monad transformers -} +{-# OPTIONS_GHC -Wno-orphans #-} +{-# LANGUAGE TypeFamilies #-} + +module Database.Persist.Monad.Orphan.QueryLift () where + +import Control.Monad.Trans.Class (lift) +import qualified Control.Monad.Trans.Except as Except +import qualified Control.Monad.Trans.Identity as Identity +import qualified Control.Monad.Trans.Maybe as Maybe +import qualified Control.Monad.Trans.RWS.Lazy as RWS.Lazy +import qualified Control.Monad.Trans.RWS.Strict as RWS.Strict +import qualified Control.Monad.Trans.Reader as Reader +import qualified Control.Monad.Trans.State.Lazy as State.Lazy +import qualified Control.Monad.Trans.State.Strict as State.Strict +import qualified Control.Monad.Trans.Writer.Lazy as Writer.Lazy +import qualified Control.Monad.Trans.Writer.Strict as Writer.Strict + +import Database.Persist.Monad.Class + +instance MonadQuery m => MonadQuery (Reader.ReaderT r m) where + type QueryRep (Reader.ReaderT r m) = QueryRep m + runQueryRep = lift . runQueryRep + +instance MonadQuery m => MonadQuery (Except.ExceptT e m) where + type QueryRep (Except.ExceptT e m) = QueryRep m + runQueryRep = lift . runQueryRep + +instance MonadQuery m => MonadQuery (Identity.IdentityT m) where + type QueryRep (Identity.IdentityT m) = QueryRep m + runQueryRep = lift . runQueryRep + +instance MonadQuery m => MonadQuery (Maybe.MaybeT m) where + type QueryRep (Maybe.MaybeT m) = QueryRep m + runQueryRep = lift . runQueryRep + +instance (Monoid w, MonadQuery m) => MonadQuery (RWS.Lazy.RWST r w s m) where + type QueryRep (RWS.Lazy.RWST r w s m) = QueryRep m + runQueryRep = lift . runQueryRep + +instance (Monoid w, MonadQuery m) => MonadQuery (RWS.Strict.RWST r w s m) where + type QueryRep (RWS.Strict.RWST r w s m) = QueryRep m + runQueryRep = lift . runQueryRep + +instance MonadQuery m => MonadQuery (State.Lazy.StateT s m) where + type QueryRep (State.Lazy.StateT s m) = QueryRep m + runQueryRep = lift . runQueryRep + +instance MonadQuery m => MonadQuery (State.Strict.StateT s m) where + type QueryRep (State.Strict.StateT s m) = QueryRep m + runQueryRep = lift . runQueryRep + +instance (Monoid w, MonadQuery m) => MonadQuery (Writer.Lazy.WriterT w m) where + type QueryRep (Writer.Lazy.WriterT w m) = QueryRep m + runQueryRep = lift . runQueryRep + +instance (Monoid w, MonadQuery m) => MonadQuery (Writer.Strict.WriterT w m) where + type QueryRep (Writer.Strict.WriterT w m) = QueryRep m + runQueryRep = lift . runQueryRep diff --git a/src/Database/Persist/Monad/Orphan/TransactionLift.hs b/src/Database/Persist/Monad/Orphan/TransactionLift.hs new file mode 100644 index 0000000..5213d40 --- /dev/null +++ b/src/Database/Persist/Monad/Orphan/TransactionLift.hs @@ -0,0 +1,74 @@ +{-| +Module: Database.Persist.Monad.Orphan.TransactionLift + +Module of orphan instances which "lift" 'MonadTransaction' through various +monad transformers by simply not allowing those transformers' effects to +occur inside the transaction. + +Most monad transformers cannot safely be made to lift 'MonadTransaction', +because they will allow for breaking the transaction semantics or transaction +re-running. + +However, thanks to 'TransactionM' being defined per-instance, it's possible +to automatically lift "running a transaction" through any monad transformer +by limiting the effects inside the transaction, so 'TransactionM' is *not* +lifted. +-} +{-# OPTIONS_GHC -Wno-orphans #-} +{-# LANGUAGE TypeFamilies #-} + +module Database.Persist.Monad.Orphan.TransactionLift () where + +import Control.Monad.Trans.Class (lift) +import qualified Control.Monad.Trans.Except as Except +import qualified Control.Monad.Trans.Identity as Identity +import qualified Control.Monad.Trans.Maybe as Maybe +import qualified Control.Monad.Trans.RWS.Lazy as RWS.Lazy +import qualified Control.Monad.Trans.RWS.Strict as RWS.Strict +import qualified Control.Monad.Trans.Reader as Reader +import qualified Control.Monad.Trans.State.Lazy as State.Lazy +import qualified Control.Monad.Trans.State.Strict as State.Strict +import qualified Control.Monad.Trans.Writer.Lazy as Writer.Lazy +import qualified Control.Monad.Trans.Writer.Strict as Writer.Strict + +import Database.Persist.Monad.Class + +instance MonadTransaction m => MonadTransaction (Reader.ReaderT r m) where + type TransactionM (Reader.ReaderT r m) = TransactionM m + withTransaction = lift . withTransaction + +instance MonadTransaction m => MonadTransaction (Except.ExceptT e m) where + type TransactionM (Except.ExceptT e m) = TransactionM m + withTransaction = lift . withTransaction + +instance MonadTransaction m => MonadTransaction (Identity.IdentityT m) where + type TransactionM (Identity.IdentityT m) = TransactionM m + withTransaction = lift . withTransaction + +instance MonadTransaction m => MonadTransaction (Maybe.MaybeT m) where + type TransactionM (Maybe.MaybeT m) = TransactionM m + withTransaction = lift . withTransaction + +instance (Monoid w, MonadTransaction m) => MonadTransaction (RWS.Lazy.RWST r w s m) where + type TransactionM (RWS.Lazy.RWST r w s m) = TransactionM m + withTransaction = lift . withTransaction + +instance (Monoid w, MonadTransaction m) => MonadTransaction (RWS.Strict.RWST r w s m) where + type TransactionM (RWS.Strict.RWST r w s m) = TransactionM m + withTransaction = lift . withTransaction + +instance MonadTransaction m => MonadTransaction (State.Lazy.StateT s m) where + type TransactionM (State.Lazy.StateT s m) = TransactionM m + withTransaction = lift . withTransaction + +instance MonadTransaction m => MonadTransaction (State.Strict.StateT s m) where + type TransactionM (State.Strict.StateT s m) = TransactionM m + withTransaction = lift . withTransaction + +instance (Monoid w, MonadTransaction m) => MonadTransaction (Writer.Lazy.WriterT w m) where + type TransactionM (Writer.Lazy.WriterT w m) = TransactionM m + withTransaction = lift . withTransaction + +instance (Monoid w, MonadTransaction m) => MonadTransaction (Writer.Strict.WriterT w m) where + type TransactionM (Writer.Strict.WriterT w m) = TransactionM m + withTransaction = lift . withTransaction diff --git a/src/Database/Persist/Monad/Shim.hs b/src/Database/Persist/Monad/Shim.hs index 2520c83..48780a6 100644 --- a/src/Database/Persist/Monad/Shim.hs +++ b/src/Database/Persist/Monad/Shim.hs @@ -1,7 +1,9 @@ {-| Module: Database.Persist.Monad.Shim -Defines all the @persistent@ functions lifted into 'MonadSqlQuery'. +Defines all the @persistent@ functions lifted into 'MonadSqlQuery', and +re-exports a version of the @Database.Persist.Sql@ module replacing each +function with the lifted version. This file is autogenerated, to keep it in sync with @Database.Persist.Monad.SqlQueryRep@. @@ -15,7 +17,116 @@ This file is autogenerated, to keep it in sync with {-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE RankNTypes #-} -module Database.Persist.Monad.Shim where +module Database.Persist.Monad.Shim ( + module Database.Persist.Sql, + get, + getMany, + getJust, + getJustEntity, + getEntity, + belongsTo, + belongsToJust, + insert, + insert_, + insertMany, + insertMany_, + insertEntityMany, + insertKey, + repsert, + repsertMany, + replace, + delete, + update, + updateGet, + insertEntity, + insertRecord, + getBy, +#if MIN_VERSION_persistent(2,10,0) + getByValue, +#endif +#if !MIN_VERSION_persistent(2,10,0) + getByValue, +#endif + checkUnique, +#if MIN_VERSION_persistent(2,11,0) + checkUniqueUpdateable, +#endif + deleteBy, + insertUnique, +#if MIN_VERSION_persistent(2,10,0) + upsert, +#endif +#if !MIN_VERSION_persistent(2,10,0) + upsert, +#endif + upsertBy, + putMany, +#if MIN_VERSION_persistent(2,10,0) + insertBy, +#endif +#if !MIN_VERSION_persistent(2,10,0) + insertBy, +#endif + insertUniqueEntity, + replaceUnique, +#if MIN_VERSION_persistent(2,10,0) + onlyUnique, +#endif +#if !MIN_VERSION_persistent(2,10,0) + onlyUnique, +#endif + selectSourceRes, + selectFirst, + selectKeysRes, + count, +#if MIN_VERSION_persistent(2,11,0) + exists, +#endif + selectSource, + selectKeys, + selectList, + selectKeysList, + updateWhere, + deleteWhere, + deleteWhereCount, + updateWhereCount, +#if !MIN_VERSION_persistent(2,13,0) + deleteCascade, +#endif +#if !MIN_VERSION_persistent(2,13,0) + deleteCascadeWhere, +#endif + parseMigration, + parseMigration', + printMigration, + showMigration, + getMigration, + runMigration, +#if MIN_VERSION_persistent(2,10,2) + runMigrationQuiet, +#endif + runMigrationUnsafe, +#if MIN_VERSION_persistent(2,10,2) + runMigrationUnsafeQuiet, +#endif + getFieldName, + getTableName, + withRawQuery, + rawQueryRes, + rawQuery, + rawExecute, + rawExecuteCount, + rawSql, + transactionSave, +#if MIN_VERSION_persistent(2,9,0) + transactionSaveWithIsolation, +#endif + transactionUndo, +#if MIN_VERSION_persistent(2,9,0) + transactionUndoWithIsolation, +#endif + unsafeLiftSql, + ) where import Control.Monad.IO.Class (MonadIO) import Control.Monad.Trans.Class (lift) @@ -27,10 +138,118 @@ import Data.Map (Map) import Data.Text (Text) import Data.Typeable (Typeable) import Data.Void (Void) -import Database.Persist.Sql hiding (pattern Update) +import Database.Persist.Sql hiding ( + get, + getMany, + getJust, + getJustEntity, + getEntity, + belongsTo, + belongsToJust, + insert, + insert_, + insertMany, + insertMany_, + insertEntityMany, + insertKey, + repsert, + repsertMany, + replace, + delete, + update, + updateGet, + insertEntity, + insertRecord, + getBy, +#if MIN_VERSION_persistent(2,10,0) + getByValue, +#endif +#if !MIN_VERSION_persistent(2,10,0) + getByValue, +#endif + checkUnique, +#if MIN_VERSION_persistent(2,11,0) + checkUniqueUpdateable, +#endif + deleteBy, + insertUnique, +#if MIN_VERSION_persistent(2,10,0) + upsert, +#endif +#if !MIN_VERSION_persistent(2,10,0) + upsert, +#endif + upsertBy, + putMany, +#if MIN_VERSION_persistent(2,10,0) + insertBy, +#endif +#if !MIN_VERSION_persistent(2,10,0) + insertBy, +#endif + insertUniqueEntity, + replaceUnique, +#if MIN_VERSION_persistent(2,10,0) + onlyUnique, +#endif +#if !MIN_VERSION_persistent(2,10,0) + onlyUnique, +#endif + selectSourceRes, + selectFirst, + selectKeysRes, + count, +#if MIN_VERSION_persistent(2,11,0) + exists, +#endif + selectSource, + selectKeys, + selectList, + selectKeysList, + updateWhere, + deleteWhere, + deleteWhereCount, + updateWhereCount, +#if !MIN_VERSION_persistent(2,13,0) + deleteCascade, +#endif +#if !MIN_VERSION_persistent(2,13,0) + deleteCascadeWhere, +#endif + parseMigration, + parseMigration', + printMigration, + showMigration, + getMigration, + runMigration, +#if MIN_VERSION_persistent(2,10,2) + runMigrationQuiet, +#endif + runMigrationUnsafe, +#if MIN_VERSION_persistent(2,10,2) + runMigrationUnsafeQuiet, +#endif + getFieldName, + getTableName, + withRawQuery, + rawQueryRes, + rawQuery, + rawExecute, + rawExecuteCount, + rawSql, + transactionSave, +#if MIN_VERSION_persistent(2,9,0) + transactionSaveWithIsolation, +#endif + transactionUndo, +#if MIN_VERSION_persistent(2,9,0) + transactionUndoWithIsolation, +#endif + pattern Update + ) import GHC.Stack (HasCallStack) -import Database.Persist.Monad.Class (MonadSqlQuery(..)) +import Database.Persist.Monad.Class (MonadSqlQuery, runCompatibleQueryRep) import Database.Persist.Monad.SqlQueryRep (SqlQueryRep(..)) {-# ANN module "HLint: ignore" #-} @@ -39,140 +258,140 @@ import Database.Persist.Monad.SqlQueryRep (SqlQueryRep(..)) get :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => Key record -> m (Maybe record) -get a1 = runQueryRep $ Get a1 +get a1 = runCompatibleQueryRep $ Get a1 -- | The lifted version of 'Database.Persist.Sql.getMany' getMany :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [Key record] -> m (Map (Key record) record) -getMany a1 = runQueryRep $ GetMany a1 +getMany a1 = runCompatibleQueryRep $ GetMany a1 -- | The lifted version of 'Database.Persist.Sql.getJust' getJust :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => Key record -> m record -getJust a1 = runQueryRep $ GetJust a1 +getJust a1 = runCompatibleQueryRep $ GetJust a1 -- | The lifted version of 'Database.Persist.Sql.getJustEntity' getJustEntity :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => Key record -> m (Entity record) -getJustEntity a1 = runQueryRep $ GetJustEntity a1 +getJustEntity a1 = runCompatibleQueryRep $ GetJustEntity a1 -- | The lifted version of 'Database.Persist.Sql.getEntity' getEntity :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => Key record -> m (Maybe (Entity record)) -getEntity a1 = runQueryRep $ GetEntity a1 +getEntity a1 = runCompatibleQueryRep $ GetEntity a1 -- | The lifted version of 'Database.Persist.Sql.belongsTo' belongsTo :: (PersistEntity record1, PersistRecordBackend record2 SqlBackend, Typeable record1, Typeable record2, MonadSqlQuery m) => (record1 -> Maybe (Key record2)) -> record1 -> m (Maybe record2) -belongsTo a1 a2 = runQueryRep $ BelongsTo a1 a2 +belongsTo a1 a2 = runCompatibleQueryRep $ BelongsTo a1 a2 -- | The lifted version of 'Database.Persist.Sql.belongsToJust' belongsToJust :: (PersistEntity record1, PersistRecordBackend record2 SqlBackend, Typeable record1, Typeable record2, MonadSqlQuery m) => (record1 -> Key record2) -> record1 -> m record2 -belongsToJust a1 a2 = runQueryRep $ BelongsToJust a1 a2 +belongsToJust a1 a2 = runCompatibleQueryRep $ BelongsToJust a1 a2 -- | The lifted version of 'Database.Persist.Sql.insert' insert :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => record -> m (Key record) -insert a1 = runQueryRep $ Insert a1 +insert a1 = runCompatibleQueryRep $ Insert a1 -- | The lifted version of 'Database.Persist.Sql.insert_' insert_ :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => record -> m () -insert_ a1 = runQueryRep $ Insert_ a1 +insert_ a1 = runCompatibleQueryRep $ Insert_ a1 -- | The lifted version of 'Database.Persist.Sql.insertMany' insertMany :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [record] -> m [Key record] -insertMany a1 = runQueryRep $ InsertMany a1 +insertMany a1 = runCompatibleQueryRep $ InsertMany a1 -- | The lifted version of 'Database.Persist.Sql.insertMany_' insertMany_ :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [record] -> m () -insertMany_ a1 = runQueryRep $ InsertMany_ a1 +insertMany_ a1 = runCompatibleQueryRep $ InsertMany_ a1 -- | The lifted version of 'Database.Persist.Sql.insertEntityMany' insertEntityMany :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [Entity record] -> m () -insertEntityMany a1 = runQueryRep $ InsertEntityMany a1 +insertEntityMany a1 = runCompatibleQueryRep $ InsertEntityMany a1 -- | The lifted version of 'Database.Persist.Sql.insertKey' insertKey :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => Key record -> record -> m () -insertKey a1 a2 = runQueryRep $ InsertKey a1 a2 +insertKey a1 a2 = runCompatibleQueryRep $ InsertKey a1 a2 -- | The lifted version of 'Database.Persist.Sql.repsert' repsert :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => Key record -> record -> m () -repsert a1 a2 = runQueryRep $ Repsert a1 a2 +repsert a1 a2 = runCompatibleQueryRep $ Repsert a1 a2 -- | The lifted version of 'Database.Persist.Sql.repsertMany' repsertMany :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [(Key record, record)] -> m () -repsertMany a1 = runQueryRep $ RepsertMany a1 +repsertMany a1 = runCompatibleQueryRep $ RepsertMany a1 -- | The lifted version of 'Database.Persist.Sql.replace' replace :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => Key record -> record -> m () -replace a1 a2 = runQueryRep $ Replace a1 a2 +replace a1 a2 = runCompatibleQueryRep $ Replace a1 a2 -- | The lifted version of 'Database.Persist.Sql.delete' delete :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => Key record -> m () -delete a1 = runQueryRep $ Delete a1 +delete a1 = runCompatibleQueryRep $ Delete a1 -- | The lifted version of 'Database.Persist.Sql.update' update :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => Key record -> [Update record] -> m () -update a1 a2 = runQueryRep $ Update a1 a2 +update a1 a2 = runCompatibleQueryRep $ Update a1 a2 -- | The lifted version of 'Database.Persist.Sql.updateGet' updateGet :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => Key record -> [Update record] -> m record -updateGet a1 a2 = runQueryRep $ UpdateGet a1 a2 +updateGet a1 a2 = runCompatibleQueryRep $ UpdateGet a1 a2 -- | The lifted version of 'Database.Persist.Sql.insertEntity' insertEntity :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => record -> m (Entity record) -insertEntity a1 = runQueryRep $ InsertEntity a1 +insertEntity a1 = runCompatibleQueryRep $ InsertEntity a1 -- | The lifted version of 'Database.Persist.Sql.insertRecord' insertRecord :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => record -> m record -insertRecord a1 = runQueryRep $ InsertRecord a1 +insertRecord a1 = runCompatibleQueryRep $ InsertRecord a1 -- | The lifted version of 'Database.Persist.Sql.getBy' getBy :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => Unique record -> m (Maybe (Entity record)) -getBy a1 = runQueryRep $ GetBy a1 +getBy a1 = runCompatibleQueryRep $ GetBy a1 #if MIN_VERSION_persistent(2,10,0) -- | The lifted version of 'Database.Persist.Sql.getByValue' getByValue :: (PersistRecordBackend record SqlBackend, AtLeastOneUniqueKey record, Typeable record, MonadSqlQuery m) => record -> m (Maybe (Entity record)) -getByValue a1 = runQueryRep $ GetByValue a1 +getByValue a1 = runCompatibleQueryRep $ GetByValue a1 #endif #if !MIN_VERSION_persistent(2,10,0) @@ -180,41 +399,41 @@ getByValue a1 = runQueryRep $ GetByValue a1 getByValue :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => record -> m (Maybe (Entity record)) -getByValue a1 = runQueryRep $ GetByValue a1 +getByValue a1 = runCompatibleQueryRep $ GetByValue a1 #endif -- | The lifted version of 'Database.Persist.Sql.checkUnique' checkUnique :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => record -> m (Maybe (Unique record)) -checkUnique a1 = runQueryRep $ CheckUnique a1 +checkUnique a1 = runCompatibleQueryRep $ CheckUnique a1 #if MIN_VERSION_persistent(2,11,0) -- | The lifted version of 'Database.Persist.Sql.checkUniqueUpdateable' checkUniqueUpdateable :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => Entity record -> m (Maybe (Unique record)) -checkUniqueUpdateable a1 = runQueryRep $ CheckUniqueUpdateable a1 +checkUniqueUpdateable a1 = runCompatibleQueryRep $ CheckUniqueUpdateable a1 #endif -- | The lifted version of 'Database.Persist.Sql.deleteBy' deleteBy :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => Unique record -> m () -deleteBy a1 = runQueryRep $ DeleteBy a1 +deleteBy a1 = runCompatibleQueryRep $ DeleteBy a1 -- | The lifted version of 'Database.Persist.Sql.insertUnique' insertUnique :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => record -> m (Maybe (Key record)) -insertUnique a1 = runQueryRep $ InsertUnique a1 +insertUnique a1 = runCompatibleQueryRep $ InsertUnique a1 #if MIN_VERSION_persistent(2,10,0) -- | The lifted version of 'Database.Persist.Sql.upsert' upsert :: (PersistRecordBackend record SqlBackend, OnlyOneUniqueKey record, Typeable record, MonadSqlQuery m) => record -> [Update record] -> m (Entity record) -upsert a1 a2 = runQueryRep $ Upsert a1 a2 +upsert a1 a2 = runCompatibleQueryRep $ Upsert a1 a2 #endif #if !MIN_VERSION_persistent(2,10,0) @@ -222,27 +441,27 @@ upsert a1 a2 = runQueryRep $ Upsert a1 a2 upsert :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => record -> [Update record] -> m (Entity record) -upsert a1 a2 = runQueryRep $ Upsert a1 a2 +upsert a1 a2 = runCompatibleQueryRep $ Upsert a1 a2 #endif -- | The lifted version of 'Database.Persist.Sql.upsertBy' upsertBy :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => Unique record -> record -> [Update record] -> m (Entity record) -upsertBy a1 a2 a3 = runQueryRep $ UpsertBy a1 a2 a3 +upsertBy a1 a2 a3 = runCompatibleQueryRep $ UpsertBy a1 a2 a3 -- | The lifted version of 'Database.Persist.Sql.putMany' putMany :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [record] -> m () -putMany a1 = runQueryRep $ PutMany a1 +putMany a1 = runCompatibleQueryRep $ PutMany a1 #if MIN_VERSION_persistent(2,10,0) -- | The lifted version of 'Database.Persist.Sql.insertBy' insertBy :: (PersistRecordBackend record SqlBackend, AtLeastOneUniqueKey record, Typeable record, MonadSqlQuery m) => record -> m (Either (Entity record) (Key record)) -insertBy a1 = runQueryRep $ InsertBy a1 +insertBy a1 = runCompatibleQueryRep $ InsertBy a1 #endif #if !MIN_VERSION_persistent(2,10,0) @@ -250,27 +469,27 @@ insertBy a1 = runQueryRep $ InsertBy a1 insertBy :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => record -> m (Either (Entity record) (Key record)) -insertBy a1 = runQueryRep $ InsertBy a1 +insertBy a1 = runCompatibleQueryRep $ InsertBy a1 #endif -- | The lifted version of 'Database.Persist.Sql.insertUniqueEntity' insertUniqueEntity :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => record -> m (Maybe (Entity record)) -insertUniqueEntity a1 = runQueryRep $ InsertUniqueEntity a1 +insertUniqueEntity a1 = runCompatibleQueryRep $ InsertUniqueEntity a1 -- | The lifted version of 'Database.Persist.Sql.replaceUnique' replaceUnique :: (PersistRecordBackend record SqlBackend, Eq (Unique record), Eq record, Typeable record, MonadSqlQuery m) => Key record -> record -> m (Maybe (Unique record)) -replaceUnique a1 a2 = runQueryRep $ ReplaceUnique a1 a2 +replaceUnique a1 a2 = runCompatibleQueryRep $ ReplaceUnique a1 a2 #if MIN_VERSION_persistent(2,10,0) -- | The lifted version of 'Database.Persist.Sql.onlyUnique' onlyUnique :: (PersistRecordBackend record SqlBackend, OnlyOneUniqueKey record, Typeable record, MonadSqlQuery m) => record -> m (Unique record) -onlyUnique a1 = runQueryRep $ OnlyUnique a1 +onlyUnique a1 = runCompatibleQueryRep $ OnlyUnique a1 #endif #if !MIN_VERSION_persistent(2,10,0) @@ -278,95 +497,95 @@ onlyUnique a1 = runQueryRep $ OnlyUnique a1 onlyUnique :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => record -> m (Unique record) -onlyUnique a1 = runQueryRep $ OnlyUnique a1 +onlyUnique a1 = runCompatibleQueryRep $ OnlyUnique a1 #endif -- | The lifted version of 'Database.Persist.Sql.selectSourceRes' selectSourceRes :: (MonadIO m2, PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [Filter record] -> [SelectOpt record] -> m (Acquire (ConduitM () (Entity record) m2 ())) -selectSourceRes a1 a2 = runQueryRep $ SelectSourceRes a1 a2 +selectSourceRes a1 a2 = runCompatibleQueryRep $ SelectSourceRes a1 a2 -- | The lifted version of 'Database.Persist.Sql.selectFirst' selectFirst :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [Filter record] -> [SelectOpt record] -> m (Maybe (Entity record)) -selectFirst a1 a2 = runQueryRep $ SelectFirst a1 a2 +selectFirst a1 a2 = runCompatibleQueryRep $ SelectFirst a1 a2 -- | The lifted version of 'Database.Persist.Sql.selectKeysRes' selectKeysRes :: (MonadIO m2, PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [Filter record] -> [SelectOpt record] -> m (Acquire (ConduitM () (Key record) m2 ())) -selectKeysRes a1 a2 = runQueryRep $ SelectKeysRes a1 a2 +selectKeysRes a1 a2 = runCompatibleQueryRep $ SelectKeysRes a1 a2 -- | The lifted version of 'Database.Persist.Sql.count' count :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [Filter record] -> m Int -count a1 = runQueryRep $ Count a1 +count a1 = runCompatibleQueryRep $ Count a1 #if MIN_VERSION_persistent(2,11,0) -- | The lifted version of 'Database.Persist.Sql.exists' exists :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [Filter record] -> m Bool -exists a1 = runQueryRep $ Exists a1 +exists a1 = runCompatibleQueryRep $ Exists a1 #endif -- | The lifted version of 'Database.Persist.Sql.selectSource' selectSource :: (PersistRecordBackend record SqlBackend, MonadResource m, Typeable record, MonadSqlQuery m) => [Filter record] -> [SelectOpt record] -> ConduitM () (Entity record) m () -selectSource a1 a2 = fromAcquire $ runQueryRep $ SelectSourceRes a1 a2 +selectSource a1 a2 = fromAcquire $ runCompatibleQueryRep $ SelectSourceRes a1 a2 -- | The lifted version of 'Database.Persist.Sql.selectKeys' selectKeys :: (PersistRecordBackend record SqlBackend, MonadResource m, Typeable record, MonadSqlQuery m) => [Filter record] -> [SelectOpt record] -> ConduitM () (Key record) m () -selectKeys a1 a2 = fromAcquire $ runQueryRep $ SelectKeysRes a1 a2 +selectKeys a1 a2 = fromAcquire $ runCompatibleQueryRep $ SelectKeysRes a1 a2 -- | The lifted version of 'Database.Persist.Sql.selectList' selectList :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [Filter record] -> [SelectOpt record] -> m [Entity record] -selectList a1 a2 = runQueryRep $ SelectList a1 a2 +selectList a1 a2 = runCompatibleQueryRep $ SelectList a1 a2 -- | The lifted version of 'Database.Persist.Sql.selectKeysList' selectKeysList :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [Filter record] -> [SelectOpt record] -> m [Key record] -selectKeysList a1 a2 = runQueryRep $ SelectKeysList a1 a2 +selectKeysList a1 a2 = runCompatibleQueryRep $ SelectKeysList a1 a2 -- | The lifted version of 'Database.Persist.Sql.updateWhere' updateWhere :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [Filter record] -> [Update record] -> m () -updateWhere a1 a2 = runQueryRep $ UpdateWhere a1 a2 +updateWhere a1 a2 = runCompatibleQueryRep $ UpdateWhere a1 a2 -- | The lifted version of 'Database.Persist.Sql.deleteWhere' deleteWhere :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [Filter record] -> m () -deleteWhere a1 = runQueryRep $ DeleteWhere a1 +deleteWhere a1 = runCompatibleQueryRep $ DeleteWhere a1 -- | The lifted version of 'Database.Persist.Sql.deleteWhereCount' deleteWhereCount :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [Filter record] -> m Int64 -deleteWhereCount a1 = runQueryRep $ DeleteWhereCount a1 +deleteWhereCount a1 = runCompatibleQueryRep $ DeleteWhereCount a1 -- | The lifted version of 'Database.Persist.Sql.updateWhereCount' updateWhereCount :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => [Filter record] -> [Update record] -> m Int64 -updateWhereCount a1 a2 = runQueryRep $ UpdateWhereCount a1 a2 +updateWhereCount a1 a2 = runCompatibleQueryRep $ UpdateWhereCount a1 a2 #if !MIN_VERSION_persistent(2,13,0) -- | The lifted version of 'Database.Persist.Sql.deleteCascade' deleteCascade :: (DeleteCascade record SqlBackend, Typeable record, MonadSqlQuery m) => Key record -> m () -deleteCascade a1 = runQueryRep $ DeleteCascade a1 +deleteCascade a1 = runCompatibleQueryRep $ DeleteCascade a1 #endif #if !MIN_VERSION_persistent(2,13,0) @@ -374,147 +593,141 @@ deleteCascade a1 = runQueryRep $ DeleteCascade a1 deleteCascadeWhere :: (DeleteCascade record SqlBackend, Typeable record, MonadSqlQuery m) => [Filter record] -> m () -deleteCascadeWhere a1 = runQueryRep $ DeleteCascadeWhere a1 +deleteCascadeWhere a1 = runCompatibleQueryRep $ DeleteCascadeWhere a1 #endif -- | The lifted version of 'Database.Persist.Sql.parseMigration' parseMigration :: (HasCallStack, MonadSqlQuery m) => Migration -> m (Either [Text] CautiousMigration) -parseMigration a1 = runQueryRep $ ParseMigration a1 +parseMigration a1 = runCompatibleQueryRep $ ParseMigration a1 -- | The lifted version of 'Database.Persist.Sql.parseMigration'' parseMigration' :: (HasCallStack, MonadSqlQuery m) => Migration -> m CautiousMigration -parseMigration' a1 = runQueryRep $ ParseMigration' a1 +parseMigration' a1 = runCompatibleQueryRep $ ParseMigration' a1 -- | The lifted version of 'Database.Persist.Sql.printMigration' printMigration :: (HasCallStack, MonadSqlQuery m) => Migration -> m () -printMigration a1 = runQueryRep $ PrintMigration a1 +printMigration a1 = runCompatibleQueryRep $ PrintMigration a1 -- | The lifted version of 'Database.Persist.Sql.showMigration' showMigration :: (HasCallStack, MonadSqlQuery m) => Migration -> m [Text] -showMigration a1 = runQueryRep $ ShowMigration a1 +showMigration a1 = runCompatibleQueryRep $ ShowMigration a1 -- | The lifted version of 'Database.Persist.Sql.getMigration' getMigration :: (HasCallStack, MonadSqlQuery m) => Migration -> m [Sql] -getMigration a1 = runQueryRep $ GetMigration a1 +getMigration a1 = runCompatibleQueryRep $ GetMigration a1 -- | The lifted version of 'Database.Persist.Sql.runMigration' runMigration :: (MonadSqlQuery m) => Migration -> m () -runMigration a1 = runQueryRep $ RunMigration a1 +runMigration a1 = runCompatibleQueryRep $ RunMigration a1 #if MIN_VERSION_persistent(2,10,2) -- | The lifted version of 'Database.Persist.Sql.runMigrationQuiet' runMigrationQuiet :: (MonadSqlQuery m) => Migration -> m [Text] -runMigrationQuiet a1 = runQueryRep $ RunMigrationQuiet a1 +runMigrationQuiet a1 = runCompatibleQueryRep $ RunMigrationQuiet a1 #endif --- | The lifted version of 'Database.Persist.Sql.runMigrationSilent' -runMigrationSilent - :: (MonadSqlQuery m) - => Migration -> m [Text] -runMigrationSilent a1 = runQueryRep $ RunMigrationSilent a1 - -- | The lifted version of 'Database.Persist.Sql.runMigrationUnsafe' runMigrationUnsafe :: (MonadSqlQuery m) => Migration -> m () -runMigrationUnsafe a1 = runQueryRep $ RunMigrationUnsafe a1 +runMigrationUnsafe a1 = runCompatibleQueryRep $ RunMigrationUnsafe a1 #if MIN_VERSION_persistent(2,10,2) -- | The lifted version of 'Database.Persist.Sql.runMigrationUnsafeQuiet' runMigrationUnsafeQuiet :: (HasCallStack, MonadSqlQuery m) => Migration -> m [Text] -runMigrationUnsafeQuiet a1 = runQueryRep $ RunMigrationUnsafeQuiet a1 +runMigrationUnsafeQuiet a1 = runCompatibleQueryRep $ RunMigrationUnsafeQuiet a1 #endif -- | The lifted version of 'Database.Persist.Sql.getFieldName' getFieldName :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => EntityField record typ -> m Text -getFieldName a1 = runQueryRep $ GetFieldName a1 +getFieldName a1 = runCompatibleQueryRep $ GetFieldName a1 -- | The lifted version of 'Database.Persist.Sql.getTableName' getTableName :: (PersistRecordBackend record SqlBackend, Typeable record, MonadSqlQuery m) => record -> m Text -getTableName a1 = runQueryRep $ GetTableName a1 +getTableName a1 = runCompatibleQueryRep $ GetTableName a1 -- | The lifted version of 'Database.Persist.Sql.withRawQuery' withRawQuery :: (MonadSqlQuery m) => Text -> [PersistValue] -> ConduitM [PersistValue] Void IO a -> m a -withRawQuery a1 a2 a3 = runQueryRep $ WithRawQuery a1 a2 a3 +withRawQuery a1 a2 a3 = runCompatibleQueryRep $ WithRawQuery a1 a2 a3 -- | The lifted version of 'Database.Persist.Sql.rawQueryRes' rawQueryRes :: (MonadIO m2, MonadSqlQuery m) => Text -> [PersistValue] -> m (Acquire (ConduitM () [PersistValue] m2 ())) -rawQueryRes a1 a2 = runQueryRep $ RawQueryRes a1 a2 +rawQueryRes a1 a2 = runCompatibleQueryRep $ RawQueryRes a1 a2 -- | The lifted version of 'Database.Persist.Sql.rawQuery' rawQuery :: (MonadResource m, MonadSqlQuery m) => Text -> [PersistValue] -> ConduitM () [PersistValue] m () -rawQuery a1 a2 = fromAcquire $ runQueryRep $ RawQueryRes a1 a2 +rawQuery a1 a2 = fromAcquire $ runCompatibleQueryRep $ RawQueryRes a1 a2 -- | The lifted version of 'Database.Persist.Sql.rawExecute' rawExecute :: (MonadSqlQuery m) => Text -> [PersistValue] -> m () -rawExecute a1 a2 = runQueryRep $ RawExecute a1 a2 +rawExecute a1 a2 = runCompatibleQueryRep $ RawExecute a1 a2 -- | The lifted version of 'Database.Persist.Sql.rawExecuteCount' rawExecuteCount :: (MonadSqlQuery m) => Text -> [PersistValue] -> m Int64 -rawExecuteCount a1 a2 = runQueryRep $ RawExecuteCount a1 a2 +rawExecuteCount a1 a2 = runCompatibleQueryRep $ RawExecuteCount a1 a2 -- | The lifted version of 'Database.Persist.Sql.rawSql' rawSql :: (RawSql a, MonadSqlQuery m) => Text -> [PersistValue] -> m [a] -rawSql a1 a2 = runQueryRep $ RawSql a1 a2 +rawSql a1 a2 = runCompatibleQueryRep $ RawSql a1 a2 -- | The lifted version of 'Database.Persist.Sql.transactionSave' transactionSave :: (MonadSqlQuery m) => m () -transactionSave = runQueryRep $ TransactionSave +transactionSave = runCompatibleQueryRep $ TransactionSave #if MIN_VERSION_persistent(2,9,0) -- | The lifted version of 'Database.Persist.Sql.transactionSaveWithIsolation' transactionSaveWithIsolation :: (MonadSqlQuery m) => IsolationLevel -> m () -transactionSaveWithIsolation a1 = runQueryRep $ TransactionSaveWithIsolation a1 +transactionSaveWithIsolation a1 = runCompatibleQueryRep $ TransactionSaveWithIsolation a1 #endif -- | The lifted version of 'Database.Persist.Sql.transactionUndo' transactionUndo :: (MonadSqlQuery m) => m () -transactionUndo = runQueryRep $ TransactionUndo +transactionUndo = runCompatibleQueryRep $ TransactionUndo #if MIN_VERSION_persistent(2,9,0) -- | The lifted version of 'Database.Persist.Sql.transactionUndoWithIsolation' transactionUndoWithIsolation :: (MonadSqlQuery m) => IsolationLevel -> m () -transactionUndoWithIsolation a1 = runQueryRep $ TransactionUndoWithIsolation a1 +transactionUndoWithIsolation a1 = runCompatibleQueryRep $ TransactionUndoWithIsolation a1 #endif -- | Lift an arbitrary 'SqlPersistT' action into 'MonadSqlQuery'. @@ -531,7 +744,7 @@ transactionUndoWithIsolation a1 = runQueryRep $ TransactionUndoWithIsolation a1 -- select q = unsafeLiftSql "esqueleto-select" (E.select q) -- @ unsafeLiftSql :: MonadSqlQuery m => Text -> (forall m2. MonadIO m2 => SqlPersistT m2 a) -> m a -unsafeLiftSql label action = runQueryRep $ UnsafeLiftSql label action +unsafeLiftSql label action = runCompatibleQueryRep $ UnsafeLiftSql label action {- Helpers -} diff --git a/src/Database/Persist/Monad/SqlQueryRep.hs b/src/Database/Persist/Monad/SqlQueryRep.hs index 6521e67..ebd7806 100644 --- a/src/Database/Persist/Monad/SqlQueryRep.hs +++ b/src/Database/Persist/Monad/SqlQueryRep.hs @@ -12,20 +12,25 @@ This file is autogenerated, to keep it in sync with {-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE LambdaCase #-} +{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE PatternSynonyms #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE TypeSynonymInstances #-} module Database.Persist.Monad.SqlQueryRep - ( SqlQueryRep(..) + ( QueryRepCompatible(..) + , withCompatibleQueryRep + , SqlQueryRep(..) , runSqlQueryRep ) where import Control.Monad.IO.Class (MonadIO) -import Control.Monad.IO.Unlift (MonadUnliftIO) import Data.Acquire (Acquire) import Data.Conduit (ConduitM) import Data.Int (Int64) @@ -40,6 +45,33 @@ import GHC.Stack (HasCallStack) {-# ANN module "HLint: ignore" #-} +-- | Class of compatible query representations. +-- +-- If @'QueryRepCompatible' sup sub@, then queries in @sup@ can also +-- represented as queries in @sub@ - so @sub@ must be at least as detailed +-- as @sup@ in representing queries. +-- +-- This is the equivalent to Persistent's 'BackendCompatible', but for query +-- representations +class QueryRepCompatible sup sub where + projectQueryRep :: sup record a -> sub record a + +instance QueryRepCompatible q q where + projectQueryRep = id + +-- | Run a function expecting a particular 'QueryRep' type using a compatible type +-- instead. This allows for "lifting" functions from the base @sub@ 'QueryRep' to +-- the more-powerful @sup@ 'QueryRep'. +-- +-- This is the equivalent to Persistent's 'withCompatibleBackend', using our +-- notion of compatiblity ('QueryRepCompatible'). +withCompatibleQueryRep + :: QueryRepCompatible sup sub + => (sub record a -> b) + -> sup record a + -> b +withCompatibleQueryRep = (. projectQueryRep) + -- | The data type containing a constructor for each persistent function we'd -- like to lift into 'Database.Persist.Monad.MonadSqlQuery'. -- @@ -366,11 +398,6 @@ data SqlQueryRep record a where => Migration -> SqlQueryRep Void [Text] #endif - -- | Constructor corresponding to 'Persist.runMigrationSilent' - RunMigrationSilent - :: () - => Migration -> SqlQueryRep Void [Text] - -- | Constructor corresponding to 'Persist.runMigrationUnsafe' RunMigrationUnsafe :: () @@ -532,7 +559,6 @@ instance Typeable record => Show (SqlQueryRep record a) where #if MIN_VERSION_persistent(2,10,2) RunMigrationQuiet{} -> "RunMigrationQuiet{..}" ++ record #endif - RunMigrationSilent{} -> "RunMigrationSilent{..}" ++ record RunMigrationUnsafe{} -> "RunMigrationUnsafe{..}" ++ record #if MIN_VERSION_persistent(2,10,2) RunMigrationUnsafeQuiet{} -> "RunMigrationUnsafeQuiet{..}" ++ record @@ -563,7 +589,7 @@ instance Typeable record => Show (SqlQueryRep record a) where -- | A helper to execute the actual @persistent@ function corresponding to -- each 'SqlQueryRep' data constructor. -runSqlQueryRep :: MonadUnliftIO m => SqlQueryRep record a -> Persist.SqlPersistT m a +runSqlQueryRep :: MonadIO m => SqlQueryRep record a -> Persist.SqlPersistT m a runSqlQueryRep = \case Get a1 -> Persist.get a1 GetMany a1 -> Persist.getMany a1 @@ -649,7 +675,6 @@ runSqlQueryRep = \case #if MIN_VERSION_persistent(2,10,2) RunMigrationQuiet a1 -> Persist.runMigrationQuiet a1 #endif - RunMigrationSilent a1 -> Persist.runMigrationSilent a1 RunMigrationUnsafe a1 -> Persist.runMigrationUnsafe a1 #if MIN_VERSION_persistent(2,10,2) RunMigrationUnsafeQuiet a1 -> Persist.runMigrationUnsafeQuiet a1 diff --git a/src/Database/Persist/Monad/TestUtils.hs b/src/Database/Persist/Monad/TestUtils.hs index b2ac762..c930ec7 100644 --- a/src/Database/Persist/Monad/TestUtils.hs +++ b/src/Database/Persist/Monad/TestUtils.hs @@ -8,6 +8,7 @@ Defines 'MockSqlQueryT', which one can use in tests in order to mock out {-# LANGUAGE GADTs #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE LambdaCase #-} +{-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} @@ -44,7 +45,7 @@ import Data.Typeable (Typeable, eqT, (:~:)(..)) import Database.Persist.Sql (Entity, Filter, Key, PersistValue, SelectOpt, rawSqlProcessRow) -import Database.Persist.Monad.Class (MonadSqlQuery(..)) +import Database.Persist.Monad.Class (MonadQuery(..), MonadTransaction(..)) import Database.Persist.Monad.SqlQueryRep (SqlQueryRep(..)) -- | A monad transformer for testing functions that use 'MonadSqlQuery'. @@ -89,9 +90,12 @@ newtype MockSqlQueryT m a = MockSqlQueryT runMockSqlQueryT :: MockSqlQueryT m a -> [MockQuery] -> m a runMockSqlQueryT action mockQueries = (`runReaderT` mockQueries) . unMockSqlQueryT $ action -instance MonadIO m => MonadSqlQuery (MockSqlQueryT m) where +instance MonadIO m => MonadTransaction (MockSqlQueryT m) where type TransactionM (MockSqlQueryT m) = MockSqlQueryT m + withTransaction = id +instance MonadIO m => MonadQuery (MockSqlQueryT m) where + type QueryRep (MockSqlQueryT m) = SqlQueryRep runQueryRep rep = do mockQueries <- MockSqlQueryT ask maybe (error $ "Could not find mock for query: " ++ show rep) liftIO @@ -99,8 +103,6 @@ instance MonadIO m => MonadSqlQuery (MockSqlQueryT m) where where tryMockQuery (MockQuery f) = f rep - withTransaction = id - -- | A mocked query to use in 'runMockSqlQueryT'. -- -- Use 'withRecord' or another helper to create a 'MockQuery'. diff --git a/test/Basic.hs b/test/Basic.hs index 385bd6e..515e984 100644 --- a/test/Basic.hs +++ b/test/Basic.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE TypeApplications #-} @@ -18,7 +19,7 @@ tests = testGroup "Basic functionality tests" foo = insert_ $ person "Alice" bar :: MonadSqlQuery m => m () bar = insert_ $ person "Bob" - fooAndBar :: MonadSqlQuery m => m () + fooAndBar :: MonadSqlTransaction m => m () fooAndBar = withTransaction $ foo >> bar runMockSqlQueryT fooAndBar [ withRecord @Person $ \case diff --git a/test/Example.hs b/test/Example.hs index a4ce2c1..7cb3006 100644 --- a/test/Example.hs +++ b/test/Example.hs @@ -1,6 +1,7 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DerivingStrategies #-} +{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} @@ -42,8 +43,6 @@ module Example import Control.Arrow ((&&&)) import Control.Monad.IO.Class (MonadIO(..)) import Control.Monad.Trans.Resource (MonadResource, ResourceT, runResourceT) -import Database.Persist.Sql - (Entity(..), EntityField, Key, SelectOpt(..), Unique, toSqlKey) import Database.Persist.TH (mkMigrate, mkPersist, persistLowerCase, share, sqlSettings) #if !MIN_VERSION_persistent(2,13,0) @@ -102,7 +101,8 @@ newtype TestApp a = TestApp , Monad , MonadIO , MonadRerunnableIO - , MonadSqlQuery + , MonadQuery + , MonadTransaction , MonadResource ) @@ -113,7 +113,7 @@ runTestApp :: BackendType -> TestApp a -> IO a runTestApp backendType m = withTestDB backendType $ \pool -> runResourceT . runSqlQueryT pool . unTestApp $ do - _ <- runMigrationSilent migration + _ <- runMigrationQuiet migration m runTestAppWith :: BackendType -> (SqlQueryEnv -> SqlQueryEnv) -> TestApp a -> IO a @@ -121,7 +121,7 @@ runTestAppWith backendType f m = withTestDB backendType $ \pool -> do let env = mkSqlQueryEnv pool f runResourceT . runSqlQueryTWith env . unTestApp $ do - _ <- runMigrationSilent migration + _ <- runMigrationQuiet migration m {- Person functions -} diff --git a/test/Generated.hs b/test/Generated.hs index 40a12b9..b567fbc 100644 --- a/test/Generated.hs +++ b/test/Generated.hs @@ -105,7 +105,6 @@ allSqlQueryRepShowRepresentations = #if MIN_VERSION_persistent(2,10,2) , show (RunMigrationQuiet undefined :: SqlQueryRep Void [Text]) #endif - , show (RunMigrationSilent undefined :: SqlQueryRep Void [Text]) , show (RunMigrationUnsafe undefined :: SqlQueryRep Void ()) #if MIN_VERSION_persistent(2,10,2) , show (RunMigrationUnsafeQuiet undefined :: SqlQueryRep Void [Text]) diff --git a/test/Integration.hs b/test/Integration.hs index 0f7362a..d7558db 100644 --- a/test/Integration.hs +++ b/test/Integration.hs @@ -1,4 +1,5 @@ {-# LANGUAGE CPP #-} +{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeApplications #-} @@ -20,21 +21,6 @@ import qualified Database.Esqueleto.Experimental as E #else import qualified Database.Esqueleto as E #endif -import Database.Persist.Sql - ( Entity(..) - , Migration - , PersistField - , PersistRecordBackend - , PersistValue - , Single(..) - , SqlBackend - , fromPersistValue - , (=.) - , (==.) - ) -#if MIN_VERSION_persistent(2,9,0) -import Database.Persist.Sql (IsolationLevel(..)) -#endif import Test.Predicates (anything, elemsAre, eq, right) import Test.Predicates.HUnit ((@?~)) import Test.Tasty @@ -684,27 +670,15 @@ testPersistentAPI backendType = testGroup "Persistent API" #if MIN_VERSION_persistent(2,10,2) , testCase "runMigrationQuiet" $ do - (withQuiet, cols) <- runTestApp backendType $ do - setupSafeMigration - sql <- runMigrationQuiet migration - cols <- getSchemaColumnNames backendType "person" - return (sql, cols) - withSilent <- runTestApp backendType $ do - setupSafeMigration - runMigrationSilent migration - assertNotIn "removed_column" cols - withQuiet @?= withSilent -#endif - - , testCase "runMigrationSilent" $ do (sqlPlanned, sqlExecuted, cols) <- runTestApp backendType $ do setupSafeMigration sqlPlanned <- getMigration migration - sqlExecuted <- runMigrationSilent migration + sqlExecuted <- runMigrationQuiet migration cols <- getSchemaColumnNames backendType "person" return (sqlPlanned, sqlExecuted, cols) assertNotIn "removed_column" cols sqlExecuted @?= sqlPlanned +#endif , testCase "runMigrationUnsafe" $ do result <- runTestApp backendType $ do diff --git a/test/Mocked.hs b/test/Mocked.hs index 7efbfae..cc7ffc3 100644 --- a/test/Mocked.hs +++ b/test/Mocked.hs @@ -10,8 +10,6 @@ import Conduit (runConduit, runResourceT, (.|)) import qualified Conduit import qualified Data.Acquire as Acquire import qualified Data.Map.Strict as Map -import Database.Persist.Sql - (Entity(..), Single(..), toPersistValue, (=.), (==.)) import Test.Tasty import Test.Tasty.HUnit diff --git a/test/TestUtils/Esqueleto.hs b/test/TestUtils/Esqueleto.hs index 08c7385..5aebd19 100644 --- a/test/TestUtils/Esqueleto.hs +++ b/test/TestUtils/Esqueleto.hs @@ -1,5 +1,7 @@ {-# LANGUAGE CPP #-} +{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeFamilies #-} module TestUtils.Esqueleto ( esqueletoSelect diff --git a/test/goldens/persistent-2.13/sqlqueryrep_show_representation.golden b/test/goldens/persistent-2.13/sqlqueryrep_show_representation.golden index a090293..a8368de 100644 --- a/test/goldens/persistent-2.13/sqlqueryrep_show_representation.golden +++ b/test/goldens/persistent-2.13/sqlqueryrep_show_representation.golden @@ -50,7 +50,6 @@ ShowMigration{..} GetMigration{..} RunMigration{..} RunMigrationQuiet{..} -RunMigrationSilent{..} RunMigrationUnsafe{..} RunMigrationUnsafeQuiet{..} GetFieldName{..}