Skip to content

Commit

Permalink
Recreate passing tests and file-based and in-memory runQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Jun 5, 2019
1 parent 5e809e0 commit 4916de6
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 11 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
2 changes: 2 additions & 0 deletions lib/core/cardano-wallet-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ test-suite unit
, http-api-data
, lens
, memory
, persistent-sqlite
, QuickCheck
, quickcheck-instances
, quickcheck-state-machine >= 0.6.0
Expand All @@ -154,6 +155,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
46 changes: 35 additions & 11 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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

Expand Down Expand Up @@ -112,6 +113,7 @@ import Database.Persist.Sql
, selectKeysList
, selectList
, updateWhere
, withSqlConn
, (<-.)
, (=.)
, (==.)
Expand Down Expand Up @@ -139,13 +141,21 @@ 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;"
enableForeignKeys conn = do
stmt <- Sqlite.prepare conn "PRAGMA foreign_keys = ON;"
_ <- Sqlite.step stmt
Sqlite.finalize stmt

createSqliteBackend :: Maybe FilePath -> LogFunc -> IO SqlBackend
createSqliteBackend :: Maybe FilePath -> LogFunc -> IO (Sqlite.Connection, SqlBackend)
createSqliteBackend fp logFunc = do
conn <- Sqlite.open (sqliteConnStr fp)
enableForeignKeys conn
backend <- wrapConnection conn logFunc
pure (conn, backend)

createSqliteBackend1 :: Maybe FilePath -> LogFunc -> IO SqlBackend
createSqliteBackend1 fp logFunc = do
conn <- Sqlite.open (sqliteConnStr fp)
enableForeignKeys conn
wrapConnection conn logFunc
Expand Down Expand Up @@ -176,29 +186,43 @@ handleConstraint e = handleJust select handler . fmap Right

----------------------------------------------------------------------------
-- Database layer methods
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)
newDBLayer = fmap snd . newDBLayer'


-- | Sets up a connection to the SQLite database.
--
-- Database migrations are run to create tables if necessary.
--
-- If the given file path does not exist, it will be created by the sqlite
-- library.
newDBLayer
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)
newDBLayer fp = do
-> IO (Sqlite.Connection, DBLayer IO s t)
newDBLayer' fp = do
lock <- newMVar ()
bigLock <- newMVar ()
conn <- createSqliteBackend fp (dbLogs [LevelError])
(conn, backend) <- createSqliteBackend fp (dbLogs [LevelError])
let runQuery' :: SqlPersistM a -> IO a
runQuery' = withMVar bigLock . const . runQuery conn
runQuery' cmd = case fp of
Nothing ->
withMVar bigLock $ const $ runQuery backend cmd
_ ->
withMVar bigLock $ const $ runResourceT $ runNoLoggingT $ withSqlConn (createSqliteBackend1 fp)
(\b -> flip runSqlConn b $ do
cmd
)

runQuery' $ void $ runMigrationSilent migrateAll
runQuery' addIndexes

return $ DBLayer
return (conn, DBLayer

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

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

----------------------------------------------------------------------------
-- SQLite database setup
Expand Down
Loading

0 comments on commit 4916de6

Please sign in to comment.