Skip to content

Commit

Permalink
Integrate new migrations into the db layer inmemory operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Jun 5, 2023
1 parent 5c7df39 commit 00e2ea3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 28 deletions.
9 changes: 6 additions & 3 deletions lib/wallet/src/Cardano/DB/Sqlite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ import Database.Persist.Sql
import Database.Persist.Sqlite
( SqlBackend, wrapConnection )
import Database.Sqlite
( Error (ErrorConstraint), SqliteException (SqliteException) )
( Connection, Error (ErrorConstraint), SqliteException (SqliteException) )
import Fmt
( fmt, ordinalF, (+|), (+||), (|+), (||+) )
import GHC.Generics
Expand Down Expand Up @@ -171,14 +171,17 @@ newInMemorySqliteContext
:: Tracer IO DBLog
-> [ManualMigration]
-> Migration
-> (Connection -> IO ()) -- ^ New style migrations
-> ForeignKeysSetting
-> IO (IO (), SqliteContext)
newInMemorySqliteContext tr manualMigrations autoMigration disableFK = do
newInMemorySqliteContext tr manualMigrations autoMigration newMigrations
disableFK = do
conn <- Sqlite.open ":memory:"
mapM_ (`executeManualMigration` conn) manualMigrations
unsafeBackend <- wrapConnection conn (queryLogFunc tr)
void $ runSqlConn (runMigrationUnsafeQuiet autoMigration) unsafeBackend

newMigrations conn
-- runMigrations (newMigrationInterfaceInMemory
let observe :: forall a. IO a -> IO a
observe = bracketTracer (contramap MsgRun tr)

Expand Down
7 changes: 6 additions & 1 deletion lib/wallet/src/Cardano/Pool/DB/Sqlite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ import Cardano.Pool.Metadata.Types
( StakePoolMetadata (..), StakePoolMetadataHash )
import Cardano.Pool.Types
( PoolId (..) )
import Cardano.Wallet.DB.Sqlite.MigrationNew
( runInMemoryNewStyleMigrations )
import Cardano.Wallet.DB.Sqlite.Types
( BlockId (..), fromMaybeHash, toMaybeHash )
import Cardano.Wallet.Logging
Expand Down Expand Up @@ -209,7 +211,10 @@ withDecoratedDBLayer dbDecorator tr mDatabaseDir ti action = do
case mDatabaseDir of
Nothing -> bracket
(newInMemorySqliteContext tr'
createViews migrateAll ForeignKeysEnabled)
createViews migrateAll
runInMemoryNewStyleMigrations
ForeignKeysEnabled
)
fst
(action . decorateDBLayer dbDecorator . newDBLayer tr ti . snd)

Expand Down
5 changes: 4 additions & 1 deletion lib/wallet/src/Cardano/Wallet/DB/Layer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import Cardano.Wallet.DB
, mkDBLayerFromParts
, transactionsStore
)
import Cardano.Wallet.DB.Sqlite.MigrationNew
import Cardano.Wallet.DB.Sqlite.MigrationOld
( DefaultFieldValues (..), migrateManually )
import Cardano.Wallet.DB.Sqlite.Schema
Expand Down Expand Up @@ -413,7 +414,9 @@ newDBOpenInMemory
newDBOpenInMemory tr = do
let tr' = contramap MsgDB tr
(destroy, sqliteContext) <-
newInMemorySqliteContext tr' [] migrateAll ForeignKeysEnabled
newInMemorySqliteContext tr' [] migrateAll
runInMemoryNewStyleMigrations
ForeignKeysEnabled
db <- newQueryLock sqliteContext
pure (destroy, db)

Expand Down
40 changes: 18 additions & 22 deletions lib/wallet/src/Cardano/Wallet/DB/Sqlite/MigrationNew.hs
Original file line number Diff line number Diff line change
@@ -1,35 +1,21 @@
module Cardano.Wallet.DB.Sqlite.MigrationNew
( -- * Build migrations
Migration
, mkMigration
, VersionT

-- * Run migrations
, Version
, MigrationInterface (..)
, runMigrations
, ErrWrongVersion (..)
{-# LANGUAGE DataKinds #-}

-- * interfaces
, newMigrationInterfaceInMemory
module Cardano.Wallet.DB.Sqlite.MigrationNew
( runInMemoryNewStyleMigrations
, newMigrationInterfaceFile
) where

import Prelude
import Prelude hiding
( id )

import Cardano.DB.Sqlite
( DBLog, withConnectionPool )
import Cardano.Wallet.DB.Migration
( ErrWrongVersion (..)
, Migration
, MigrationInterface (..)
, Version
, VersionT
, mkMigration
, runMigrations
)
( Migration, MigrationInterface (..), Version, runMigrations )
import Cardano.Wallet.DB.Sqlite.MigrationOld
( getSchemaVersion, putSchemaVersion )
import Control.Category
( id )
import Control.Tracer
( Tracer )
import Data.Pool
Expand Down Expand Up @@ -78,3 +64,13 @@ getVersionNew = fmap oldToNewSchemaVersion . getSchemaVersion

setVersionNew :: Sqlite.Connection -> Version -> IO ()
setVersionNew conn = putSchemaVersion conn . newToOldSchemaVersion

noMigrations :: Migration m 2 2
noMigrations = id

runInMemoryNewStyleMigrations :: Sqlite.Connection -> IO ()
runInMemoryNewStyleMigrations conn =
runMigrations
(newMigrationInterfaceInMemory conn)
()
noMigrations
4 changes: 3 additions & 1 deletion lib/wallet/test/unit/Cardano/Wallet/DB/Fixtures.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import Prelude

import Cardano.DB.Sqlite
( ForeignKeysSetting, SqliteContext, newInMemorySqliteContext, runQuery )
import Cardano.Wallet.DB.Sqlite.MigrationNew
( runInMemoryNewStyleMigrations )
import Cardano.Wallet.DB.Sqlite.Schema
( Wallet (..), migrateAll )
import Cardano.Wallet.DB.Sqlite.Types
Expand Down Expand Up @@ -75,7 +77,6 @@ import Test.QuickCheck.Monadic
import UnliftIO.Exception
( bracket )


import qualified Cardano.Wallet.DB.Sqlite.Schema as TH


Expand All @@ -87,6 +88,7 @@ withDBInMemory disableFK action = bracket (newDBInMemory disableFK) fst (action

newDBInMemory :: ForeignKeysSetting -> IO (IO (), SqliteContext)
newDBInMemory = newInMemorySqliteContext nullTracer [] migrateAll
runInMemoryNewStyleMigrations

initializeWallet :: WalletId -> SqlPersistT IO ()
initializeWallet wid = do
Expand Down

0 comments on commit 00e2ea3

Please sign in to comment.