-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate new migrations into the db layer on disk operations.
- Loading branch information
Showing
7 changed files
with
176 additions
and
58 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
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
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
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
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,47 +1,88 @@ | ||
module Cardano.Wallet.DB.Sqlite.MigrationNew (newMigrationInterface) where | ||
{-# LANGUAGE DataKinds #-} | ||
|
||
import Prelude | ||
module Cardano.Wallet.DB.Sqlite.MigrationNew | ||
( runNewStyleMigrations | ||
, DBHandle(..)) where | ||
|
||
import Prelude hiding | ||
( id, (.) ) | ||
|
||
import Cardano.DB.Sqlite | ||
( DBLog, withConnectionPool ) | ||
import Cardano.Wallet.DB.Migration | ||
( MigrationInterface (..), Version ) | ||
( Migration | ||
, MigrationInterface (..) | ||
, Version (..) | ||
, hoistMigration | ||
, runMigrations | ||
) | ||
import Cardano.Wallet.DB.Sqlite.MigrationOld | ||
( getSchemaVersion, putSchemaVersion ) | ||
import Control.Category | ||
( Category (id), (.) ) | ||
import Control.Monad.Reader | ||
( withReaderT ) | ||
import Control.Monad.Trans.Reader | ||
( ReaderT ) | ||
import Control.Tracer | ||
( Tracer ) | ||
import Data.Pool | ||
( withResource ) | ||
import Database.Persist.Sql | ||
( SqlBackend ) | ||
import Database.Persist.Sqlite | ||
( SqlPersistT ) | ||
import Database.Sqlite | ||
( Connection ) | ||
import System.Directory | ||
( copyFile ) | ||
|
||
import qualified Cardano.Wallet.DB.Sqlite.MigrationOld as MigrateOld | ||
import qualified Database.Sqlite as Sqlite | ||
|
||
data DBHandle = DBHandle | ||
{ dbConn :: Connection | ||
, dbBackend :: SqlBackend | ||
} | ||
|
||
mkDBHandle :: (SqlBackend, Connection) -> DBHandle | ||
mkDBHandle (backend, conn) = DBHandle conn backend | ||
|
||
newMigrationInterface | ||
:: Tracer IO DBLog | ||
-> MigrationInterface IO Sqlite.Connection | ||
-> MigrationInterface IO DBHandle | ||
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 | ||
withResource pool $ f . mkDBHandle | ||
, getVersion = getVersionNew . dbConn | ||
, setVersion = setVersionNew . dbConn | ||
} | ||
|
||
oldToNewSchemaVersion :: MigrateOld.SchemaVersion -> Version | ||
oldToNewSchemaVersion (MigrateOld.SchemaVersion v) = v | ||
oldToNewSchemaVersion (MigrateOld.SchemaVersion v) = Version v | ||
|
||
newToOldSchemaVersion :: Version -> MigrateOld.SchemaVersion | ||
newToOldSchemaVersion = MigrateOld.SchemaVersion | ||
newToOldSchemaVersion (Version v) = MigrateOld.SchemaVersion v | ||
|
||
getVersionNew :: Sqlite.Connection -> IO Version | ||
getVersionNew = fmap oldToNewSchemaVersion . getSchemaVersion | ||
|
||
setVersionNew :: Sqlite.Connection -> Version -> IO () | ||
setVersionNew conn = putSchemaVersion conn . newToOldSchemaVersion | ||
|
||
noMigrations :: Migration m 2 2 | ||
noMigrations = id | ||
|
||
_useSqlBackend | ||
:: Migration (SqlPersistT m) from to | ||
-> Migration (ReaderT DBHandle m) from to | ||
_useSqlBackend = hoistMigration $ withReaderT dbBackend | ||
|
||
runNewStyleMigrations :: Tracer IO DBLog -> FilePath -> IO () | ||
runNewStyleMigrations tr fp = runMigrations (newMigrationInterface tr) fp | ||
noMigrations |
Oops, something went wrong.