Skip to content

Commit

Permalink
Implement MigrationInterface for on file DB.
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Jun 5, 2023
1 parent bb30282 commit 74d57f8
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions lib/wallet/src/Cardano/Wallet/DB/Sqlite/MigrationNew.hs
@@ -1,15 +1,47 @@
module Cardano.Wallet.DB.Sqlite.MigrationNew
(
) where
module Cardano.Wallet.DB.Sqlite.MigrationNew (newMigrationInterface) where

import Prelude

import Cardano.DB.Sqlite
( DBLog, withConnectionPool )
import Cardano.Wallet.DB.Migration
( Version )
( MigrationInterface (..), Version )
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

newMigrationInterface
:: Tracer IO DBLog
-> MigrationInterface IO Sqlite.Connection
newMigrationInterface 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
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

0 comments on commit 74d57f8

Please sign in to comment.