Merge pull request #1157 from pseudonom/master
Adjustments for split read and write DB
This commit is contained in:
commit
1b5477bc78
@ -17,4 +17,7 @@ packages:
|
|||||||
# Needed for LTS 2
|
# Needed for LTS 2
|
||||||
extra-deps:
|
extra-deps:
|
||||||
- wai-app-static-3.1.4.1
|
- wai-app-static-3.1.4.1
|
||||||
|
- http-api-data-0.2
|
||||||
- yaml-0.8.17
|
- yaml-0.8.17
|
||||||
|
- nonce-1.0.2
|
||||||
|
- persistent-2.5
|
||||||
|
|||||||
@ -499,6 +499,15 @@ class (YesodAuth master, YesodPersist master) => YesodAuthPersist master where
|
|||||||
|
|
||||||
getAuthEntity :: AuthId master -> HandlerT master IO (Maybe (AuthEntity master))
|
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
|
||||||
|
, Key (AuthEntity master) ~ AuthId master
|
||||||
|
, PersistStore backend
|
||||||
|
)
|
||||||
|
=> AuthId master -> HandlerT master IO (Maybe (AuthEntity master))
|
||||||
|
#else
|
||||||
default getAuthEntity
|
default getAuthEntity
|
||||||
:: ( YesodPersistBackend master
|
:: ( YesodPersistBackend master
|
||||||
~ PersistEntityBackend (AuthEntity master)
|
~ PersistEntityBackend (AuthEntity master)
|
||||||
@ -507,6 +516,7 @@ class (YesodAuth master, YesodPersist master) => YesodAuthPersist master where
|
|||||||
, PersistEntity (AuthEntity master)
|
, PersistEntity (AuthEntity master)
|
||||||
)
|
)
|
||||||
=> AuthId master -> HandlerT master IO (Maybe (AuthEntity master))
|
=> AuthId master -> HandlerT master IO (Maybe (AuthEntity master))
|
||||||
|
#endif
|
||||||
getAuthEntity = runDB . get
|
getAuthEntity = runDB . get
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -37,7 +37,7 @@ library
|
|||||||
, unordered-containers
|
, unordered-containers
|
||||||
, yesod-form >= 1.4 && < 1.5
|
, yesod-form >= 1.4 && < 1.5
|
||||||
, transformers >= 0.2.2
|
, transformers >= 0.2.2
|
||||||
, persistent >= 2.1 && < 2.3
|
, persistent >= 2.1 && < 2.6
|
||||||
, persistent-template >= 2.1 && < 2.2
|
, persistent-template >= 2.1 && < 2.2
|
||||||
, http-client
|
, http-client
|
||||||
, http-conduit >= 2.1
|
, http-conduit >= 2.1
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
{-# LANGUAGE ConstraintKinds #-}
|
||||||
{-# LANGUAGE QuasiQuotes #-}
|
{-# LANGUAGE QuasiQuotes #-}
|
||||||
{-# LANGUAGE TypeFamilies #-}
|
{-# LANGUAGE TypeFamilies #-}
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
@ -74,7 +75,11 @@ import Data.Text.Encoding (encodeUtf8, decodeUtf8With)
|
|||||||
import Data.Text.Encoding.Error (lenientDecode)
|
import Data.Text.Encoding.Error (lenientDecode)
|
||||||
import Network.URI (parseURI)
|
import Network.URI (parseURI)
|
||||||
import Database.Persist.Sql (PersistField, PersistFieldSql (..))
|
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))
|
import Database.Persist (Entity (..), SqlType (SqlString))
|
||||||
|
#endif
|
||||||
import Text.HTML.SanitizeXSS (sanitizeBalance)
|
import Text.HTML.SanitizeXSS (sanitizeBalance)
|
||||||
import Control.Monad (when, unless)
|
import Control.Monad (when, unless)
|
||||||
import Data.Either (partitionEithers)
|
import Data.Either (partitionEithers)
|
||||||
@ -645,6 +650,19 @@ optionsEnum = optionsPairs $ map (\x -> (pack $ show x, x)) [minBound..maxBound]
|
|||||||
-- > <$> areq (selectField countries) "Which country do you live in?" Nothing
|
-- > <$> areq (selectField countries) "Which country do you live in?" Nothing
|
||||||
-- > where
|
-- > where
|
||||||
-- > countries = optionsPersist [] [Asc CountryName] countryName
|
-- > countries = optionsPersist [] [Asc CountryName] countryName
|
||||||
|
#if MIN_VERSION_persistent(2,5,0)
|
||||||
|
optionsPersist :: ( YesodPersist site
|
||||||
|
, PersistQueryRead backend
|
||||||
|
, PathPiece (Key a)
|
||||||
|
, RenderMessage site msg
|
||||||
|
, YesodPersistBackend site ~ backend
|
||||||
|
, PersistRecordBackend a backend
|
||||||
|
)
|
||||||
|
=> [Filter a]
|
||||||
|
-> [SelectOpt a]
|
||||||
|
-> (a -> msg)
|
||||||
|
-> HandlerT site IO (OptionList (Entity a))
|
||||||
|
#else
|
||||||
optionsPersist :: ( YesodPersist site, PersistEntity a
|
optionsPersist :: ( YesodPersist site, PersistEntity a
|
||||||
, PersistQuery (PersistEntityBackend a)
|
, PersistQuery (PersistEntityBackend a)
|
||||||
, PathPiece (Key a)
|
, PathPiece (Key a)
|
||||||
@ -655,6 +673,7 @@ optionsPersist :: ( YesodPersist site, PersistEntity a
|
|||||||
-> [SelectOpt a]
|
-> [SelectOpt a]
|
||||||
-> (a -> msg)
|
-> (a -> msg)
|
||||||
-> HandlerT site IO (OptionList (Entity a))
|
-> HandlerT site IO (OptionList (Entity a))
|
||||||
|
#endif
|
||||||
optionsPersist filts ords toDisplay = fmap mkOptionList $ do
|
optionsPersist filts ords toDisplay = fmap mkOptionList $ do
|
||||||
mr <- getMessageRender
|
mr <- getMessageRender
|
||||||
pairs <- runDB $ selectList filts ords
|
pairs <- runDB $ selectList filts ords
|
||||||
@ -668,6 +687,20 @@ optionsPersist filts ords toDisplay = fmap mkOptionList $ do
|
|||||||
-- the entire 'Entity'.
|
-- the entire 'Entity'.
|
||||||
--
|
--
|
||||||
-- Since 1.3.2
|
-- Since 1.3.2
|
||||||
|
#if MIN_VERSION_persistent(2,5,0)
|
||||||
|
optionsPersistKey
|
||||||
|
:: (YesodPersist site
|
||||||
|
, PersistQueryRead backend
|
||||||
|
, PathPiece (Key a)
|
||||||
|
, RenderMessage site msg
|
||||||
|
, backend ~ YesodPersistBackend site
|
||||||
|
, PersistRecordBackend a backend
|
||||||
|
)
|
||||||
|
=> [Filter a]
|
||||||
|
-> [SelectOpt a]
|
||||||
|
-> (a -> msg)
|
||||||
|
-> HandlerT site IO (OptionList (Key a))
|
||||||
|
#else
|
||||||
optionsPersistKey
|
optionsPersistKey
|
||||||
:: (YesodPersist site
|
:: (YesodPersist site
|
||||||
, PersistEntity a
|
, PersistEntity a
|
||||||
@ -680,6 +713,7 @@ optionsPersistKey
|
|||||||
-> [SelectOpt a]
|
-> [SelectOpt a]
|
||||||
-> (a -> msg)
|
-> (a -> msg)
|
||||||
-> HandlerT site IO (OptionList (Key a))
|
-> HandlerT site IO (OptionList (Key a))
|
||||||
|
#endif
|
||||||
|
|
||||||
optionsPersistKey filts ords toDisplay = fmap mkOptionList $ do
|
optionsPersistKey filts ords toDisplay = fmap mkOptionList $ do
|
||||||
mr <- getMessageRender
|
mr <- getMessageRender
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
{-# LANGUAGE ConstraintKinds #-}
|
||||||
{-# LANGUAGE TypeFamilies #-}
|
{-# LANGUAGE TypeFamilies #-}
|
||||||
{-# LANGUAGE CPP #-}
|
{-# LANGUAGE CPP #-}
|
||||||
{-# LANGUAGE FlexibleContexts #-}
|
{-# LANGUAGE FlexibleContexts #-}
|
||||||
@ -20,7 +21,7 @@ module Yesod.Persist.Core
|
|||||||
) where
|
) where
|
||||||
|
|
||||||
import Database.Persist
|
import Database.Persist
|
||||||
import Control.Monad.Trans.Reader (ReaderT, runReaderT)
|
import Control.Monad.Trans.Reader (ReaderT, runReaderT, withReaderT)
|
||||||
|
|
||||||
import Yesod.Core
|
import Yesod.Core
|
||||||
import Data.Conduit
|
import Data.Conduit
|
||||||
@ -80,12 +81,18 @@ newtype DBRunner site = DBRunner
|
|||||||
-- | Helper for implementing 'getDBRunner'.
|
-- | Helper for implementing 'getDBRunner'.
|
||||||
--
|
--
|
||||||
-- Since 1.2.0
|
-- 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
|
defaultGetDBRunner :: YesodPersistBackend site ~ SQL.SqlBackend
|
||||||
=> (site -> Pool SQL.SqlBackend)
|
=> (site -> Pool SQL.SqlBackend)
|
||||||
-> HandlerT site IO (DBRunner site, HandlerT site IO ())
|
-> HandlerT site IO (DBRunner site, HandlerT site IO ())
|
||||||
|
#endif
|
||||||
defaultGetDBRunner getPool = do
|
defaultGetDBRunner getPool = do
|
||||||
pool <- fmap getPool getYesod
|
pool <- fmap getPool getYesod
|
||||||
let withPrep conn f = f conn (SQL.connPrepare conn)
|
let withPrep conn f = f (persistBackend conn) (SQL.connPrepare $ persistBackend conn)
|
||||||
(relKey, (conn, local)) <- allocate
|
(relKey, (conn, local)) <- allocate
|
||||||
(do
|
(do
|
||||||
(conn, local) <- takeResource pool
|
(conn, local) <- takeResource pool
|
||||||
@ -124,9 +131,15 @@ respondSourceDB :: YesodPersistRunner site
|
|||||||
respondSourceDB ctype = respondSource ctype . runDBSource
|
respondSourceDB ctype = respondSource ctype . runDBSource
|
||||||
|
|
||||||
-- | Get the given entity by ID, or return a 404 not found if it doesn't exist.
|
-- | 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)
|
get404 :: (MonadIO m, PersistStore (PersistEntityBackend val), PersistEntity val)
|
||||||
=> Key val
|
=> Key val
|
||||||
-> ReaderT (PersistEntityBackend val) m val
|
-> ReaderT (PersistEntityBackend val) m val
|
||||||
|
#endif
|
||||||
get404 key = do
|
get404 key = do
|
||||||
mres <- get key
|
mres <- get key
|
||||||
case mres of
|
case mres of
|
||||||
@ -135,9 +148,15 @@ get404 key = do
|
|||||||
|
|
||||||
-- | Get the given entity by unique key, or return a 404 not found if it doesn't
|
-- | Get the given entity by unique key, or return a 404 not found if it doesn't
|
||||||
-- exist.
|
-- 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)
|
getBy404 :: (PersistUnique (PersistEntityBackend val), PersistEntity val, MonadIO m)
|
||||||
=> Unique val
|
=> Unique val
|
||||||
-> ReaderT (PersistEntityBackend val) m (Entity val)
|
-> ReaderT (PersistEntityBackend val) m (Entity val)
|
||||||
|
#endif
|
||||||
getBy404 key = do
|
getBy404 key = do
|
||||||
mres <- getBy key
|
mres <- getBy key
|
||||||
case mres of
|
case mres of
|
||||||
|
|||||||
@ -16,7 +16,7 @@ extra-source-files: README.md ChangeLog.md
|
|||||||
library
|
library
|
||||||
build-depends: base >= 4 && < 5
|
build-depends: base >= 4 && < 5
|
||||||
, yesod-core >= 1.4.0 && < 1.5
|
, yesod-core >= 1.4.0 && < 1.5
|
||||||
, persistent >= 2.1 && < 2.3
|
, persistent >= 2.1 && < 2.6
|
||||||
, persistent-template >= 2.1 && < 2.2
|
, persistent-template >= 2.1 && < 2.2
|
||||||
, transformers >= 0.2.2
|
, transformers >= 0.2.2
|
||||||
, blaze-builder
|
, blaze-builder
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user