From 5c7df39f2aa9dcb00e68feecd81b2ea9f520ee82 Mon Sep 17 00:00:00 2001 From: paolino Date: Sun, 4 Jun 2023 08:06:31 +0000 Subject: [PATCH] Implement MigrationInterface for on file DB. --- .../Cardano/Wallet/DB/Sqlite/MigrationNew.hs | 37 +++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/wallet/src/Cardano/Wallet/DB/Sqlite/MigrationNew.hs b/lib/wallet/src/Cardano/Wallet/DB/Sqlite/MigrationNew.hs index 50d74a18904..e10defb472f 100644 --- a/lib/wallet/src/Cardano/Wallet/DB/Sqlite/MigrationNew.hs +++ b/lib/wallet/src/Cardano/Wallet/DB/Sqlite/MigrationNew.hs @@ -10,12 +10,15 @@ module Cardano.Wallet.DB.Sqlite.MigrationNew , runMigrations , ErrWrongVersion (..) - -- * interfaces + -- * interfaces , newMigrationInterfaceInMemory + , newMigrationInterfaceFile ) where import Prelude +import Cardano.DB.Sqlite + ( DBLog, withConnectionPool ) import Cardano.Wallet.DB.Migration ( ErrWrongVersion (..) , Migration @@ -27,6 +30,12 @@ import Cardano.Wallet.DB.Migration ) import Cardano.Wallet.DB.Sqlite.MigrationOld ( getSchemaVersion, putSchemaVersion ) +import Control.Tracer + ( Tracer ) +import Data.Pool + ( withResource ) +import System.Directory + ( copyFile ) import qualified Cardano.Wallet.DB.Sqlite.MigrationOld as MigrateOld import qualified Database.Sqlite as Sqlite @@ -38,8 +47,24 @@ newMigrationInterfaceInMemory conn = MigrationInterface { backupDatabaseFile = \_ _ -> pure () , withDatabaseFile = \_ f -> f conn - , getVersion = fmap oldToNewSchemaVersion . getSchemaVersion - , setVersion = \conn' -> putSchemaVersion conn' . newToOldSchemaVersion + , getVersion = getVersionNew + , setVersion = setVersionNew + } + +newMigrationInterfaceFile + :: Tracer IO DBLog + -> MigrationInterface IO FilePath Sqlite.Connection +newMigrationInterfaceFile tr = + MigrationInterface + { backupDatabaseFile = \fp v -> do + let backupFile = fp <> ".v" <> show v <> ".bak" + copyFile fp backupFile + , withDatabaseFile = \fp f -> do + withConnectionPool tr fp $ \pool -> + withResource pool $ \(_, conn) -> do + f conn + , getVersion = getVersionNew + , setVersion = setVersionNew } oldToNewSchemaVersion :: MigrateOld.SchemaVersion -> Version @@ -47,3 +72,9 @@ oldToNewSchemaVersion (MigrateOld.SchemaVersion v) = v newToOldSchemaVersion :: Version -> MigrateOld.SchemaVersion newToOldSchemaVersion = MigrateOld.SchemaVersion + +getVersionNew :: Sqlite.Connection -> IO Version +getVersionNew = fmap oldToNewSchemaVersion . getSchemaVersion + +setVersionNew :: Sqlite.Connection -> Version -> IO () +setVersionNew conn = putSchemaVersion conn . newToOldSchemaVersion