From d99de615547d0dd8bbc6f766b1749024403379a4 Mon Sep 17 00:00:00 2001 From: Eric Easley Date: Mon, 18 Apr 2016 10:03:39 -0700 Subject: [PATCH] Use CPP to maintain backward compat --- stack.yaml | 1 + yesod-auth/Yesod/Auth.hs | 11 +++++++++ yesod-auth/yesod-auth.cabal | 2 +- yesod-form/Yesod/Form/Fields.hs | 32 +++++++++++++++++++++++++ yesod-persistent/Yesod/Persist/Core.hs | 18 ++++++++++++++ yesod-persistent/yesod-persistent.cabal | 2 +- 6 files changed, 64 insertions(+), 2 deletions(-) diff --git a/stack.yaml b/stack.yaml index fb3b7b71..29b90e10 100644 --- a/stack.yaml +++ b/stack.yaml @@ -19,4 +19,5 @@ extra-deps: - wai-app-static-3.1.4.1 - http-api-data-0.2 - yaml-0.8.17 +- nonce-1.0.2 - persistent-2.5 diff --git a/yesod-auth/Yesod/Auth.hs b/yesod-auth/Yesod/Auth.hs index 7b72dbd8..9fe96010 100644 --- a/yesod-auth/Yesod/Auth.hs +++ b/yesod-auth/Yesod/Auth.hs @@ -499,6 +499,7 @@ class (YesodAuth master, YesodPersist master) => YesodAuthPersist master where getAuthEntity :: AuthId master -> HandlerT master IO (Maybe (AuthEntity master)) +#if MIN_VERSION_persistent(2,5,0) default getAuthEntity :: ( YesodPersistBackend master ~ backend , PersistRecordBackend (AuthEntity master) backend @@ -506,6 +507,16 @@ class (YesodAuth master, YesodPersist master) => YesodAuthPersist master where , PersistStore backend ) => AuthId master -> HandlerT master IO (Maybe (AuthEntity master)) +#else + default getAuthEntity + :: ( YesodPersistBackend master + ~ PersistEntityBackend (AuthEntity master) + , Key (AuthEntity master) ~ AuthId master + , PersistStore (YesodPersistBackend master) + , PersistEntity (AuthEntity master) + ) + => AuthId master -> HandlerT master IO (Maybe (AuthEntity master)) +#endif getAuthEntity = runDB . get diff --git a/yesod-auth/yesod-auth.cabal b/yesod-auth/yesod-auth.cabal index e817ba0e..1c0cb4f5 100644 --- a/yesod-auth/yesod-auth.cabal +++ b/yesod-auth/yesod-auth.cabal @@ -37,7 +37,7 @@ library , unordered-containers , yesod-form >= 1.4 && < 1.5 , transformers >= 0.2.2 - , persistent >= 2.5 && < 2.6 + , persistent >= 2.1 && < 2.6 , persistent-template >= 2.1 && < 2.2 , http-client , http-conduit >= 2.1 diff --git a/yesod-form/Yesod/Form/Fields.hs b/yesod-form/Yesod/Form/Fields.hs index f9cee889..e080062a 100644 --- a/yesod-form/Yesod/Form/Fields.hs +++ b/yesod-form/Yesod/Form/Fields.hs @@ -75,7 +75,11 @@ import Data.Text.Encoding (encodeUtf8, decodeUtf8With) import Data.Text.Encoding.Error (lenientDecode) import Network.URI (parseURI) import Database.Persist.Sql (PersistField, PersistFieldSql (..)) +#if MIN_VERSION_persistent(2,5,0) import Database.Persist (Entity (..), SqlType (SqlString), PersistRecordBackend, PersistQueryRead) +#else +import Database.Persist (Entity (..), SqlType (SqlString)) +#endif import Text.HTML.SanitizeXSS (sanitizeBalance) import Control.Monad (when, unless) import Data.Either (partitionEithers) @@ -646,6 +650,7 @@ optionsEnum = optionsPairs $ map (\x -> (pack $ show x, x)) [minBound..maxBound] -- > <$> areq (selectField countries) "Which country do you live in?" Nothing -- > where -- > countries = optionsPersist [] [Asc CountryName] countryName +#if MIN_VERSION_persistent(2,5,0) optionsPersist :: ( YesodPersist site , PersistQueryRead backend , PathPiece (Key a) @@ -657,6 +662,18 @@ optionsPersist :: ( YesodPersist site -> [SelectOpt a] -> (a -> msg) -> HandlerT site IO (OptionList (Entity a)) +#else +optionsPersist :: ( YesodPersist site, PersistEntity a + , PersistQuery (PersistEntityBackend a) + , PathPiece (Key a) + , RenderMessage site msg + , YesodPersistBackend site ~ PersistEntityBackend a + ) + => [Filter a] + -> [SelectOpt a] + -> (a -> msg) + -> HandlerT site IO (OptionList (Entity a)) +#endif optionsPersist filts ords toDisplay = fmap mkOptionList $ do mr <- getMessageRender pairs <- runDB $ selectList filts ords @@ -670,6 +687,7 @@ optionsPersist filts ords toDisplay = fmap mkOptionList $ do -- the entire 'Entity'. -- -- Since 1.3.2 +#if MIN_VERSION_persistent(2,5,0) optionsPersistKey :: (YesodPersist site , PersistQueryRead backend @@ -682,6 +700,20 @@ optionsPersistKey -> [SelectOpt a] -> (a -> msg) -> HandlerT site IO (OptionList (Key a)) +#else +optionsPersistKey + :: (YesodPersist site + , PersistEntity a + , PersistQuery (PersistEntityBackend a) + , PathPiece (Key a) + , RenderMessage site msg + , YesodPersistBackend site ~ PersistEntityBackend a + ) + => [Filter a] + -> [SelectOpt a] + -> (a -> msg) + -> HandlerT site IO (OptionList (Key a)) +#endif optionsPersistKey filts ords toDisplay = fmap mkOptionList $ do mr <- getMessageRender diff --git a/yesod-persistent/Yesod/Persist/Core.hs b/yesod-persistent/Yesod/Persist/Core.hs index 6cde856d..2b6904f6 100644 --- a/yesod-persistent/Yesod/Persist/Core.hs +++ b/yesod-persistent/Yesod/Persist/Core.hs @@ -81,9 +81,15 @@ newtype DBRunner site = DBRunner -- | Helper for implementing 'getDBRunner'. -- -- Since 1.2.0 +#if MIN_VERSION_persistent(2,5,0) defaultGetDBRunner :: (SQL.IsSqlBackend backend, YesodPersistBackend site ~ backend) => (site -> Pool backend) -> HandlerT site IO (DBRunner site, HandlerT site IO ()) +#else +defaultGetDBRunner :: YesodPersistBackend site ~ SQL.SqlBackend + => (site -> Pool SQL.SqlBackend) + -> HandlerT site IO (DBRunner site, HandlerT site IO ()) +#endif defaultGetDBRunner getPool = do pool <- fmap getPool getYesod let withPrep conn f = f (persistBackend conn) (SQL.connPrepare $ persistBackend conn) @@ -125,9 +131,15 @@ respondSourceDB :: YesodPersistRunner site respondSourceDB ctype = respondSource ctype . runDBSource -- | Get the given entity by ID, or return a 404 not found if it doesn't exist. +#if MIN_VERSION_persistent(2,5,0) get404 :: (MonadIO m, PersistStore backend, PersistRecordBackend val backend) => Key val -> ReaderT backend m val +#else +get404 :: (MonadIO m, PersistStore (PersistEntityBackend val), PersistEntity val) + => Key val + -> ReaderT (PersistEntityBackend val) m val +#endif get404 key = do mres <- get key case mres of @@ -136,9 +148,15 @@ get404 key = do -- | Get the given entity by unique key, or return a 404 not found if it doesn't -- exist. +#if MIN_VERSION_persistent(2,5,0) getBy404 :: (PersistUnique backend, PersistRecordBackend val backend, MonadIO m) => Unique val -> ReaderT backend m (Entity val) +#else +getBy404 :: (PersistUnique (PersistEntityBackend val), PersistEntity val, MonadIO m) + => Unique val + -> ReaderT (PersistEntityBackend val) m (Entity val) +#endif getBy404 key = do mres <- getBy key case mres of diff --git a/yesod-persistent/yesod-persistent.cabal b/yesod-persistent/yesod-persistent.cabal index 3054a1d6..f32221a3 100644 --- a/yesod-persistent/yesod-persistent.cabal +++ b/yesod-persistent/yesod-persistent.cabal @@ -16,7 +16,7 @@ extra-source-files: README.md ChangeLog.md library build-depends: base >= 4 && < 5 , yesod-core >= 1.4.0 && < 1.5 - , persistent >= 2.5 && < 2.6 + , persistent >= 2.1 && < 2.6 , persistent-template >= 2.1 && < 2.2 , transformers >= 0.2.2 , blaze-builder