-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This pull request changes the Deposit Wallet to use the database functionality from `Database.Table.SQLite.Simple`. At the moment, this actually means removing all database usage from the Deposit Wallet — the mock environment does not need it, and also cannot use it, as the `SqlM` monad is not an instance of `MonadSTM`. (For good reason: The `SqlM` monad meant to represent atomic database operations and is **not** an instance of `MonadIO`). However, I want to keep the module `Cardano.Wallet.Deposit.IO.DB` around as a small indirection over `Database.Table.SQLite.Simple`. ### Issue Number ADP-2565
- Loading branch information
Showing
6 changed files
with
68 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 31 additions & 46 deletions
77
lib/customer-deposit-wallet/src/Cardano/Wallet/Deposit/IO/DB.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,61 @@ | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE Rank2Types #-} | ||
module Cardano.Wallet.Deposit.IO.DB | ||
( SqlM | ||
, SqlContext (..) | ||
( Connection | ||
, withSqliteFile | ||
, withSqlContextInMemory | ||
, withSqliteInMemory | ||
|
||
, SqlM | ||
, runSqlM | ||
|
||
, DBLog (..) | ||
) where | ||
|
||
import Prelude | ||
|
||
import Cardano.BM.Extra | ||
( bracketTracer | ||
) | ||
import Cardano.DB.Sqlite | ||
( DBLog (..) | ||
) | ||
import Control.Concurrent.MVar | ||
( newMVar | ||
, withMVar | ||
) | ||
import Control.Monad.Trans.Reader | ||
( ReaderT (..) | ||
) | ||
import Control.Tracer | ||
( Tracer | ||
, contramap | ||
, traceWith | ||
) | ||
|
||
import qualified Database.SQLite.Simple as Sqlite | ||
import Database.Table.SQLite.Simple | ||
( Connection | ||
, SqlM | ||
, runSqlM | ||
, withConnection | ||
) | ||
|
||
{----------------------------------------------------------------------------- | ||
SqlContext | ||
------------------------------------------------------------------------------} | ||
-- | Monad to run SQL queries in. | ||
type SqlM = ReaderT Sqlite.Connection IO | ||
|
||
-- | A facility to run 'SqlM' computations. | ||
-- Importantly, computations are not run in parallel, but sequenced. | ||
newtype SqlContext = SqlContext | ||
{ runSqlM :: forall a. SqlM a -> IO a | ||
} | ||
|
||
-- | Acquire and release an 'SqlContext' in memory. | ||
withSqlContextInMemory | ||
-- | Acquire and release an SQLite 'Connection' in memory. | ||
withSqliteInMemory | ||
:: Tracer IO DBLog | ||
-- ^ Logging | ||
-> (SqlContext -> IO a) | ||
-> (Connection -> IO a) | ||
-- ^ Action to run | ||
-> IO a | ||
withSqlContextInMemory tr = withSqliteFile tr ":memory:" | ||
withSqliteInMemory tr = withSqliteFile tr ":memory:" | ||
|
||
-- | Use sqlite to open a database file | ||
-- and provide an 'SqlContext' for running 'SqlM' actions. | ||
-- | Acquire and release an SQLite 'Connection' from a file. | ||
withSqliteFile | ||
:: Tracer IO DBLog | ||
-- ^ Logging | ||
-> FilePath | ||
-- ^ Database file | ||
-> (SqlContext -> IO a) | ||
-> (Connection -> IO a) | ||
-- ^ Action to run | ||
-> IO a | ||
withSqliteFile tr filepath action = | ||
Sqlite.withConnection filepath $ \connection0 -> do | ||
traceWith tr $ MsgOpenSingleConnection filepath | ||
-- The lock ensures that database operations are sequenced. | ||
lock <- newMVar connection0 | ||
let runSqlM :: SqlM a -> IO a | ||
runSqlM cmd = withMVar lock (observe . runReaderT cmd) | ||
action SqlContext{runSqlM} | ||
where | ||
observe :: IO a -> IO a | ||
observe = bracketTracer (contramap MsgRun tr) | ||
withConnection filepath $ \conn -> do | ||
traceWith tr $ MsgStartConnection filepath | ||
result <- action conn | ||
traceWith tr $ MsgDoneConnection filepath | ||
pure result | ||
|
||
{------------------------------------------------------------------------------- | ||
Logging | ||
-------------------------------------------------------------------------------} | ||
|
||
data DBLog | ||
= MsgStartConnection FilePath | ||
| MsgDoneConnection FilePath | ||
deriving (Show, Eq) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters