Skip to content

Commit

Permalink
Merge pull request #370 from input-output-hk/paweljakubas/154/file-ba…
Browse files Browse the repository at this point in the history
…sed-and-inmemory

Recreate passing tests and file-based and in-memory runQuery
  • Loading branch information
KtorZ authored Jun 6, 2019
2 parents 24eb7a7 + b5aeff8 commit 4d897a7
Show file tree
Hide file tree
Showing 7 changed files with 312 additions and 29 deletions.
4 changes: 4 additions & 0 deletions lib/core/backup/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
3 changes: 3 additions & 0 deletions lib/core/cardano-wallet-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ test-suite unit
, http-api-data
, lens
, memory
, persistent
, persistent-sqlite
, QuickCheck
, quickcheck-instances
, quickcheck-state-machine >= 0.6.0
Expand All @@ -154,6 +156,7 @@ test-suite unit
Cardano.Wallet.DB.MVarSpec
Cardano.Wallet.DB.StateMachine
Cardano.Wallet.DB.SqliteSpec
Cardano.Wallet.DB.SqliteFileModeSpec
Cardano.Wallet.DBSpec
Cardano.Wallet.NetworkSpec
Cardano.Wallet.Primitive.AddressDerivationSpec
Expand Down
42 changes: 15 additions & 27 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
Expand All @@ -18,7 +17,7 @@

module Cardano.Wallet.DB.Sqlite
( newDBLayer
, DummyState(..)
, PersistState (..)
) where

import Prelude
Expand Down Expand Up @@ -120,8 +119,6 @@ import Database.Persist.Sqlite
( SqlBackend, SqlPersistM, SqlPersistT, wrapConnection )
import Database.Sqlite
( Error (ErrorConstraint), SqliteException (SqliteException) )
import GHC.Generics
( Generic )
import System.IO
( stderr )
import System.Log.FastLogger
Expand All @@ -141,10 +138,15 @@ import qualified Database.Sqlite as Sqlite
-- Sqlite connection set up

enableForeignKeys :: Sqlite.Connection -> IO ()
enableForeignKeys conn = stmt >>= void . Sqlite.step
where stmt = Sqlite.prepare conn "PRAGMA foreign_keys = ON;"

createSqliteBackend :: Maybe FilePath -> LogFunc -> IO SqlBackend
enableForeignKeys conn = do
stmt <- Sqlite.prepare conn "PRAGMA foreign_keys = ON;"
_ <- Sqlite.step stmt
Sqlite.finalize stmt

createSqliteBackend
:: Maybe FilePath
-> LogFunc
-> IO SqlBackend
createSqliteBackend fp logFunc = do
conn <- Sqlite.open (sqliteConnStr fp)
enableForeignKeys conn
Expand Down Expand Up @@ -174,9 +176,6 @@ handleConstraint e = handleJust select handler . fmap Right
select _ = Nothing
handler = const . pure . Left $ e

----------------------------------------------------------------------------
-- Database layer methods

-- | Sets up a connection to the SQLite database.
--
-- Database migrations are run to create tables if necessary.
Expand All @@ -187,18 +186,16 @@ newDBLayer
:: forall s t. (W.IsOurs s, NFData s, Show s, PersistState s, W.TxId t)
=> Maybe FilePath
-- ^ Database file location, or Nothing for in-memory database
-> IO (DBLayer IO s t)
-> IO (SqlBackend, DBLayer IO s t)
newDBLayer fp = do
lock <- newMVar ()
bigLock <- newMVar ()
conn <- createSqliteBackend fp (dbLogs [LevelError])
backend <- createSqliteBackend fp (dbLogs [LevelError])
let runQuery' :: SqlPersistM a -> IO a
runQuery' = withMVar bigLock . const . runQuery conn

runQuery' cmd = withMVar bigLock $ const $ runQuery backend cmd
runQuery' $ void $ runMigrationSilent migrateAll
runQuery' addIndexes

return $ DBLayer
return (backend, DBLayer

{-----------------------------------------------------------------------
Wallets
Expand Down Expand Up @@ -311,7 +308,7 @@ newDBLayer fp = do

, withLock = \action ->
ExceptT $ withMVar lock $ \() -> runExceptT action
}
})

----------------------------------------------------------------------------
-- SQLite database setup
Expand Down Expand Up @@ -766,12 +763,3 @@ selectSeqStatePendingIxs ssid =
[Desc SeqStatePendingIxIndex]
where
fromRes = fmap (W.Index . seqStatePendingIxIndex . entityVal)

data DummyState = DummyState
deriving (Show, Eq, Generic)

instance PersistState DummyState where
insertState (wid, sl) _ = insert_ (SeqState wid sl)
selectState (wid, sl) = fmap (const DummyState) <$>
selectFirst [SeqStateTableWalletId ==. wid, SeqStateTableCheckpointSlot ==. sl] []
deleteState wid = deleteWhere [SeqStateTableWalletId ==. wid]
2 changes: 1 addition & 1 deletion lib/core/test/bench/db/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ withDB bm = envWithCleanup setup cleanup (\ ~(_, db) -> bm db)
where
setup = do
f <- emptySystemTempFile "bench.db"
db <- newDBLayer (Just f)
(_, db) <- newDBLayer (Just f)
pure (f, db)
cleanup (f, _) = mapM_ remove [f, f <> "-shm", f <> "-wal"]
remove f = doesFileExist f >>= \case
Expand Down
Loading

0 comments on commit 4d897a7

Please sign in to comment.