Skip to content

Commit

Permalink
add renameColumnName migration
Browse files Browse the repository at this point in the history
proper order of Rule migration commands
  • Loading branch information
paweljakubas committed Nov 19, 2020
1 parent 9e8a18d commit 564a853
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Expand Up @@ -376,7 +376,9 @@ migrateManually tr proxy defaultFieldValues =

addSeqStateDerivationPrefixIfMissing conn

renameRole conn
renameRoleColumn conn

renameRoleFields conn
where
-- NOTE
-- Wallets created before the 'PassphraseScheme' was introduced have no
Expand Down Expand Up @@ -566,24 +568,45 @@ migrateManually tr proxy defaultFieldValues =
-- which is pretty lame. This was changed later on, but already
-- serialized data may subsist on for quite a while. Hence this little
-- pirouette here.
renameRole :: Sqlite.Connection -> IO ()
renameRole conn = do
renameColumn conn (DBField SeqStateAddressRole)
renameRoleFields :: Sqlite.Connection -> IO ()
renameRoleFields conn = do
renameColumnField conn (DBField SeqStateAddressRole)
"u_tx_o_internal" "utxo_internal"
renameColumn conn (DBField SeqStateAddressRole)
renameColumnField conn (DBField SeqStateAddressRole)
"u_tx_o_external" "utxo_external"

-- | Rename column table of SeqStateAddress from 'accounting_style' to `role`
-- if needed.
renameRoleColumn :: Sqlite.Connection -> IO ()
renameRoleColumn conn =
isFieldPresent conn roleField >>= \case
TableMissing ->
traceWith tr $ MsgManualMigrationNotNeeded roleField
ColumnMissing -> do
traceWith tr $ MsgManualMigrationNotNeeded roleField
query <- Sqlite.prepare conn $ T.unwords
[ "ALTER TABLE", tableName roleField
, "RENAME COLUMN accounting_style TO"
, fieldName roleField
, ";"
]
Sqlite.step query *> Sqlite.finalize query
ColumnPresent ->
traceWith tr $ MsgManualMigrationNotNeeded roleField
where
roleField = DBField SeqStateAddressRole

-- | Determines whether a field is present in its parent table.
isFieldPresent :: Sqlite.Connection -> DBField -> IO SqlColumnStatus
isFieldPresent conn field = do
getCheckpointTableInfo <- Sqlite.prepare conn $ mconcat
getTableInfo <- Sqlite.prepare conn $ mconcat
[ "SELECT sql FROM sqlite_master "
, "WHERE type = 'table' "
, "AND name = '" <> tableName field <> "';"
]
row <- Sqlite.step getCheckpointTableInfo
>> Sqlite.columns getCheckpointTableInfo
Sqlite.finalize getCheckpointTableInfo
row <- Sqlite.step getTableInfo
>> Sqlite.columns getTableInfo
Sqlite.finalize getTableInfo
pure $ case row of
[PersistText t]
| fieldName field `T.isInfixOf` t -> ColumnPresent
Expand Down Expand Up @@ -625,13 +648,13 @@ migrateManually tr proxy defaultFieldValues =
ColumnPresent ->
traceWith tr $ MsgManualMigrationNotNeeded field

renameColumn
renameColumnField
:: Sqlite.Connection
-> DBField
-> Text -- Old Value
-> Text -- New Value
-> IO ()
renameColumn conn field old new = do
renameColumnField conn field old new = do
isFieldPresent conn field >>= \case
TableMissing ->
traceWith tr $ MsgManualMigrationNotNeeded field
Expand Down

0 comments on commit 564a853

Please sign in to comment.