multipool-persistent (empty) → 0.1.0.0
raw patch · 7 files changed
+136/−0 lines, 7 filesdep +basedep +mtldep +multipoolsetup-changed
Dependencies added: base, mtl, multipool, multipool-persistent, persistent, unliftio-core, unordered-containers
Files
- ChangeLog.md +3/−0
- LICENSE +30/−0
- README.md +1/−0
- Setup.hs +2/−0
- multipool-persistent.cabal +60/−0
- src/Data/MultiPool/Persist/Sql.hs +38/−0
- test/Spec.hs +2/−0
+ ChangeLog.md view
@@ -0,0 +1,3 @@+# Changelog for multipool-persistent++## Unreleased changes
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Ian Duncan (c) 2018++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Ian Duncan nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,1 @@+# multipool-persistent
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ multipool-persistent.cabal view
@@ -0,0 +1,60 @@+-- This file has been generated from package.yaml by hpack version 0.28.2.+--+-- see: https://github.com/sol/hpack+--+-- hash: 33a046c600cc8d0a53f52d114443a388df18b1bd046b3126a1a7e6671b2aa012++name: multipool-persistent+version: 0.1.0.0+synopsis: Read and write from appropriate persistent sql instances in replicated environments.+description: Please see the README on GitHub at <https://github.com/iand675/multipool-persistent#readme>+homepage: https://github.com/iand675/multipool-persistent#readme+bug-reports: https://github.com/iand675/multipool-persistent/issues+author: Ian Duncan+maintainer: ian@iankduncan.com+copyright: Ian Duncan+license: BSD3+license-file: LICENSE+build-type: Simple+cabal-version: >= 1.10+extra-source-files:+ ChangeLog.md+ README.md++source-repository head+ type: git+ location: https://github.com/iand675/multipool-persistent++library+ exposed-modules:+ Data.MultiPool.Persist.Sql+ other-modules:+ Paths_multipool_persistent+ hs-source-dirs:+ src+ build-depends:+ base >=4.7 && <5+ , mtl+ , multipool+ , persistent+ , unliftio-core+ , unordered-containers+ default-language: Haskell2010++test-suite multipool-persistent-test+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ other-modules:+ Paths_multipool_persistent+ hs-source-dirs:+ test+ ghc-options: -threaded -rtsopts -with-rtsopts=-N+ build-depends:+ base >=4.7 && <5+ , mtl+ , multipool+ , multipool-persistent+ , persistent+ , unliftio-core+ , unordered-containers+ default-language: Haskell2010
+ src/Data/MultiPool/Persist/Sql.hs view
@@ -0,0 +1,38 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE MultiParamTypeClasses #-}+{-# LANGUAGE TypeFamilies #-}+module Data.MultiPool.Persist.Sql+ ( MultiPoolBackend(..)+ , unsafeRead+ ) where++import Control.Exception+import Control.Monad.IO.Unlift+import Control.Monad.Reader+import Data.MultiPool+import Database.Persist.Sql+import Database.Persist.Sql.Types.Internal+import Data.HashMap.Strict (HashMap)+import qualified Data.HashMap.Strict as HM++instance MonadUnliftIO m => MultiPoolBackend m SqlBackend where+ type MasterConnection SqlBackend = SqlWriteBackend+ type ReplicaConnection SqlBackend = SqlReadBackend+ type ReplicaIdentifier SqlBackend = InstanceName SqlReadBackend++ runWriteAny b m = runWrite b () m+ runWrite b () m = runSqlPool m (multiPoolMaster b)++ runReadMaster b () m = runReadAnyMaster b m+ runReadAnyMaster b m = runSqlPool (readToWrite m) (multiPoolMaster b)+ runReadAny b m = do+ mident <- liftIO $ multiPoolAnyReplicaSelector b b+ case mident of+ Nothing -> runReadAnyMaster b m+ Just ident -> runRead b ident m+ runRead b ident m = case HM.lookup ident (multiPoolReplica b) of+ Nothing -> throw (InstanceDoesNotExist ident)+ Just repl -> runSqlPool m repl++unsafeRead :: ReaderT SqlBackend m a -> ReaderT SqlReadBackend m a+unsafeRead = withReaderT persistBackend
+ test/Spec.hs view
@@ -0,0 +1,2 @@+main :: IO ()+main = putStrLn "Test suite not yet implemented"