Skip to content

Commit

Permalink
new prologue migration 2
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Oct 13, 2023
1 parent 168609d commit e3481ec
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
11 changes: 7 additions & 4 deletions lib/wallet/src/Cardano/Wallet/DB/Sqlite/Migration/New.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ getVersionNew = fmap oldToNewSchemaVersion . getSchemaVersion
setVersionNew :: Connection -> Version -> IO ()
setVersionNew conn = putSchemaVersion conn . newToOldSchemaVersion

noMigrations :: Migration m 2 2
noMigrations = id
noMigrations2 :: Migration m 2 2
noMigrations2 = id

noMigrations3 :: Migration m 3 3
noMigrations3 = id

_useSqlBackend
:: Migration (SqlPersistT m) from to
Expand All @@ -73,6 +76,6 @@ _useSqlBackend = hoistMigration $ withReaderT dbBackend
runNewStyleMigrations :: Tracer IO DBLog -> FilePath -> IO ()
runNewStyleMigrations tr fp = do
runMigrations (newMigrationInterface tr) fp
$ migrateDelegations . noMigrations
$ migrateDelegations . noMigrations2
runMigrations (newMigrationInterface tr) fp
$ migratePrologue . noMigrations
$ migratePrologue . noMigrations3
73 changes: 70 additions & 3 deletions lib/wallet/src/Cardano/Wallet/DB/Store/WalletState/Migration.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,76 @@ module Cardano.Wallet.DB.Store.WalletState.Migration
import Prelude

import Cardano.DB.Sqlite
( ReadDBHandle )
( DBField (..)
, ReadDBHandle
, dbBackend
, dbConn
, fieldName
, fieldType
, tableName
)
import Cardano.Wallet.DB.Migration
( Migration, mkMigration )
import Cardano.Wallet.DB.Sqlite.Migration.Old
( SqlColumnStatus (..), isFieldPresent )
import Cardano.Wallet.DB.Sqlite.Schema
( EntityField (..) )
import Control.Monad
( void )
import Control.Monad.Reader
( asks, liftIO, withReaderT )
import Data.Text
( Text )

migratePrologue :: Migration (ReadDBHandle IO) 2 3
migratePrologue = mkMigration $ undefined
import qualified Data.Text as T
import qualified Database.Sqlite as Sqlite

migratePrologue :: Migration (ReadDBHandle IO) 3 4
migratePrologue = mkMigration oneChangeAddrMigration
where
oneChangeAddrMigration = do
conn <- asks dbConn
r1 <- liftIO $ isFieldPresent conn $ DBField SeqStateWalletId
r2 <- liftIO $ isFieldPresent conn $ DBField SharedStateWalletId
let defaultValue = "FALSE"
case (r1, r2) of
(ColumnPresent, ColumnMissing) -> withReaderT dbBackend $ do
liftIO $ addColumn_ conn True (DBField SeqStateOneChangeAddrMode) defaultValue
undefined
(ColumnMissing, ColumnPresent) -> withReaderT dbBackend $ do
liftIO $ addColumn_ conn True (DBField SharedStateOneChangeAddrMode) defaultValue
undefined
_ ->
return ()

addColumn_
:: Sqlite.Connection
-> Bool
-> DBField
-> Text
-> IO ()
addColumn_ a b c =
void . addColumn a b c
where
addColumn
:: Sqlite.Connection
-> Bool
-> DBField
-> Text
-> IO SqlColumnStatus
addColumn conn notNull field value = do
isFieldPresent conn field >>= \st -> st <$ case st of
TableMissing ->
return ()
ColumnMissing -> do
query <- Sqlite.prepare conn $ T.unwords
[ "ALTER TABLE", tableName field
, "ADD COLUMN", fieldName field
, fieldType field, if notNull then "NOT NULL" else ""
, "DEFAULT", value
, ";"
]
_ <- Sqlite.step query
Sqlite.finalize query
ColumnPresent ->
return ()

0 comments on commit e3481ec

Please sign in to comment.