packages feed

persistent-sqlite 2.1.1.2 → 2.1.3

raw patch · 4 files changed

+71/−12 lines, 4 filesdep ~base

Dependency ranges changed: base

Files

ChangeLog.md view
@@ -1,3 +1,13 @@+## 2.1.3++* Added a `Show` instance for `SqliteConf`.+* Use `SqliteException` instead of calling `fail` [#364](https://github.com/yesodweb/persistent/issues/364)++## 2.1.2++* Turn on write-ahead log [#363](https://github.com/yesodweb/persistent/issues/363)+    * Prepending `WAL=off ` to your connection string will recover the previous behavior.+ ## 2.1.1.1  Fix rendering of `UTCTime` to match SQLite requirements (see [issue
Database/Persist/Sqlite.hs view
@@ -3,7 +3,12 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE PatternGuards #-} -- | A sqlite backend for persistent.+--+-- Note: If you prepend @WAL=off @ to your connection string, it will disable+-- the write-ahead log. For more information, see+-- <https://github.com/yesodweb/persistent/issues/363>. module Database.Persist.Sqlite     ( withSqlitePool     , withSqliteConn@@ -35,6 +40,7 @@ import Data.Monoid ((<>)) import Control.Monad.Trans.Control (MonadBaseControl) import Control.Monad.Trans.Resource (ResourceT, runResourceT)+import Control.Monad (when)  createSqlitePool :: (MonadIO m, MonadLogger m, MonadBaseControl IO m) => Text -> Int -> m ConnectionPool createSqlitePool s = createSqlPool $ open' s@@ -50,13 +56,36 @@ withSqliteConn = withSqlConn . open'  open' :: Text -> LogFunc -> IO SqlBackend-open' connStr logFunc = Sqlite.open connStr >>= flip wrapConnection logFunc+open' connStr logFunc = do+    let (connStr', enableWal) = case () of+          ()+            | Just cs <- T.stripPrefix "WAL=on "  connStr -> (cs, True)+            | Just cs <- T.stripPrefix "WAL=off " connStr -> (cs, False)+            | otherwise                                   -> (connStr, True) +    conn <- Sqlite.open connStr'+    wrapConnectionWal enableWal conn logFunc+ -- | Wrap up a raw 'Sqlite.Connection' as a Persistent SQL 'Connection'. -- -- Since 1.1.5 wrapConnection :: Sqlite.Connection -> LogFunc -> IO SqlBackend-wrapConnection conn logFunc = do+wrapConnection = wrapConnectionWal True++-- | Allow control of WAL settings when wrapping+wrapConnectionWal :: Bool -- ^ enable WAL?+                  -> Sqlite.Connection+                  -> LogFunc+                  -> IO SqlBackend+wrapConnectionWal enableWal conn logFunc = do+    when enableWal $ do+        -- Turn on the write-ahead log+        -- https://github.com/yesodweb/persistent/issues/363+        turnOnWal <- Sqlite.prepare conn "PRAGMA journal_mode=WAL;"+        _ <- Sqlite.step turnOnWal+        Sqlite.reset conn turnOnWal+        Sqlite.finalize turnOnWal+     smap <- newIORef $ Map.empty     return SqlBackend         { connPrepare = prepare' conn@@ -343,7 +372,7 @@ data SqliteConf = SqliteConf     { sqlDatabase :: Text     , sqlPoolSize :: Int-    }+    } deriving Show  instance FromJSON SqliteConf where     parseJSON = withObject "SqliteConf" $ \o -> SqliteConf
Database/Sqlite.hs view
@@ -7,6 +7,7 @@                          Connection,                          Statement,                          Error(..),+                         SqliteException(..),                          StepResult(Row,                                     Done),                          open,@@ -36,6 +37,7 @@ import qualified Data.ByteString.Internal as BSI import Foreign import Foreign.C+import Control.Exception (Exception, throwIO) import Database.Persist (PersistValue (..), listToJSON, mapToJSON) import Data.Text (Text, pack, unpack) import Data.Text.Encoding (encodeUtf8, decodeUtf8With)@@ -44,6 +46,7 @@ import Data.IORef (IORef, newIORef, readIORef, writeIORef) import Data.Fixed (Pico) import Data.Time (formatTime, UTCTime)+import Data.Typeable (Typeable)  #if MIN_VERSION_time(1,5,0) import Data.Time (defaultTimeLocale)@@ -55,6 +58,25 @@ newtype Connection' = Connection' (Ptr ()) newtype Statement = Statement (Ptr ()) +-- | A custom exception type to make it easier to catch exceptions.+--+-- Since 2.1.3+data SqliteException = SqliteException+    { seError        :: !Error+    , seFunctionName :: !Text+    , seDetails      :: !Text+    }+    deriving (Typeable)+instance Show SqliteException where+    show (SqliteException error functionName details) = unpack $ mconcat+        ["SQLite3 returned "+        , pack $ show error+        , " while attempting to perform "+        , functionName+        , details+        ]+instance Exception SqliteException+ data Error = ErrorOK            | ErrorError            | ErrorInternal@@ -150,13 +172,11 @@                  details <- errmsg database                  return $ ": " `mappend` details                Nothing -> return "."-  fail $ unpack $ mconcat-    ["SQLite3 returned "-    , pack $ show error-    , " while attempting to perform "-    , functionName-    , details-    ]+  throwIO SqliteException+    { seError = error+    , seFunctionName = functionName+    , seDetails = details+    }  foreign import ccall "sqlite3_open"   openC :: CString -> Ptr (Ptr ()) -> IO Int
persistent-sqlite.cabal view
@@ -1,5 +1,5 @@ name:            persistent-sqlite-version:         2.1.1.2+version:         2.1.3 license:         MIT license-file:    LICENSE author:          Michael Snoyman <michael@snoyman.com>@@ -22,7 +22,7 @@   default: False  library-    build-depends:   base                    >= 4         && < 5+    build-depends:   base                    >= 4.6         && < 5                    , bytestring              >= 0.9.1                    , transformers            >= 0.2.1                    , persistent              >= 2.1       && < 3