Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement MigrationInterface for on file DB.
- Loading branch information
Showing
1 changed file
with
36 additions
and
4 deletions.
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 |
---|---|---|
@@ -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 |