-
Notifications
You must be signed in to change notification settings - Fork 217
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
556aa7b
Recreate passing tests and file-based and in-memory runQuery
paweljakubas c994e61
export 'SqlBackend' instead of 'SqlConnection'
KtorZ 7a9a458
refactor SqliteFileModeSpec to enhance readability & use close'
KtorZ b5aeff8
remove unused 'DummyState' and its 'PersistState' instance
KtorZ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Ignore everything in this directory | ||
* | ||
# Except this file | ||
!.gitignore | ||
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
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 #-} | ||
|
@@ -18,7 +17,7 @@ | |
|
||
module Cardano.Wallet.DB.Sqlite | ||
( newDBLayer | ||
, DummyState(..) | ||
, PersistState (..) | ||
) where | ||
|
||
import Prelude | ||
|
@@ -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 | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is needed for file-based. otherwise :
|
||
|
||
createSqliteBackend | ||
:: Maybe FilePath | ||
-> LogFunc | ||
-> IO SqlBackend | ||
createSqliteBackend fp logFunc = do | ||
conn <- Sqlite.open (sqliteConnStr fp) | ||
enableForeignKeys conn | ||
|
@@ -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. | ||
|
@@ -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 | ||
|
@@ -311,7 +308,7 @@ newDBLayer fp = do | |
|
||
, withLock = \action -> | ||
ExceptT $ withMVar lock $ \() -> runExceptT action | ||
} | ||
}) | ||
|
||
---------------------------------------------------------------------------- | ||
-- SQLite database setup | ||
|
@@ -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] |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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