Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recreate passing tests and file-based and in-memory runQuery #370

Merged
merged 4 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

@paweljakubas paweljakubas Jun 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here will come mechanism of dealing with directories as in DB bench PR

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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is needed for file-based. otherwise :

      1) Cardano.Wallet.DB.SqliteCorruption, Check db opening/closing, opening and closing of db works
           uncaught exception: SqliteException
           SQLite3 returned ErrorBusy while attempting to perform close: unable to close due to unfinalized statements or unfinished backups


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