Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Suggestions for integrating the new migrations into the `withDBOpenFromFile` function
  • Loading branch information
HeinrichApfelmus committed Jun 2, 2023
1 parent 32e0a52 commit 7702574
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
17 changes: 15 additions & 2 deletions lib/wallet/src/Cardano/Wallet/DB/Layer.hs
Expand Up @@ -387,6 +387,20 @@ withDBOpenFromFile
-- ^ Action to run.
-> IO a
withDBOpenFromFile walletF tr defaultFieldValues dbFile action = do
let trDB = contramap MsgDB tr
runOldMigrations trDB dbFile defaultFieldValues
runMigrationSteps trDB dbFile $
newMigrations
withConnectionPool trDB dbFile $ \pool -> do
res <- newSqliteContext trDB pool [] (pure ())
case res of
Left err -> throwIO err
Right sqliteContext -> action =<< newQueryLock sqliteContext

runOldMigrations
:: FilePath
-> IO ()
runOldMigrations dbFile = do
let trDB = contramap MsgDB tr
let manualMigrations =
maybe [] (migrateManually trDB $ keyOfWallet walletF)
Expand All @@ -396,8 +410,7 @@ withDBOpenFromFile walletF tr defaultFieldValues dbFile action = do
res <- newSqliteContext trDB pool manualMigrations autoMigrations
case res of
Left err -> throwIO err
Right sqliteContext -> action =<< newQueryLock sqliteContext

Right _ -> pure ()

-- | Open an SQLite database in-memory.
--
Expand Down
6 changes: 3 additions & 3 deletions lib/wallet/src/Cardano/Wallet/DB/Migration.hs
Expand Up @@ -70,12 +70,12 @@ instance Category (Migration m) where
Migration s2 . Migration s1 = Migration (s1 <> s2)

-- | Functions to interact with the database.
data MigrationInterface m handle = MigrationInterface
{ backupDatabaseFile :: FilePath -> Version -> m ()
data MigrationInterface m filePath handle = MigrationInterface
{ backupDatabaseFile :: filePath -> Version -> m ()
-- ^ Back up the (closed) database file.
-- The 'Version' argument can be used to find a new file path
-- for the backup copy.
, withDatabaseFile :: forall r. FilePath -> (handle -> m r) -> m r
, withDatabaseFile :: forall r. filePath -> (handle -> m r) -> m r
-- ^ Open a database file and close it after use or on exception.
, getVersion :: handle -> m Version
-- ^ Get the current version.
Expand Down
26 changes: 26 additions & 0 deletions lib/wallet/src/Cardano/Wallet/DB/Sqlite/MigrationNew.hs
@@ -0,0 +1,26 @@
{-
1. implement
mkMigrationInterfaceFile
:: MigrationInterface IO FilePath Sqlite.Connection
Duplicate raw SQL statements form `getVersion` and `setVersion`
withDatabaseFile = do
withConnectionPool $ \pool ->
2. implement
newMigrationInterfaceInMemory
:: Sqlite.Connection
-> MigrationInterface IO () Sqlite.Connection
backupDatabaseFile = nop
withDatabaseFile = \_ -> …
-- in memory,
-- do not close the database context on exiting this function
-}

0 comments on commit 7702574

Please sign in to comment.