Skip to content

Commit

Permalink
factor out common code in database migration
Browse files Browse the repository at this point in the history
Turns out that adding column is a pretty common and straighfordward migration. I originally intended to add a new column to the TxMeta table, hence the refactor. In the end, I've used a different table for withdrawals, but the refactor is still worth it as it is IMO.
  • Loading branch information
KtorZ authored and paweljakubas committed Jul 10, 2020
1 parent 11aba2e commit 616c389
Showing 1 changed file with 27 additions and 34 deletions.
61 changes: 27 additions & 34 deletions lib/core/src/Cardano/Wallet/DB/Sqlite.hs
Expand Up @@ -420,25 +420,9 @@ migrateManually tr defaultFieldValues =
-- it is missing.
--
addActiveSlotCoefficientIfMissing :: Sqlite.Connection -> IO ()
addActiveSlotCoefficientIfMissing conn = do
isFieldPresent conn activeSlotCoeff >>= \case
TableMissing ->
traceWith tr $ MsgManualMigrationNotNeeded activeSlotCoeff
ColumnMissing -> do
traceWith tr $ MsgManualMigrationNeeded activeSlotCoeff value
addColumn <- Sqlite.prepare conn $ T.unwords
[ "ALTER TABLE", tableName activeSlotCoeff
, "ADD COLUMN", fieldName activeSlotCoeff
, fieldType activeSlotCoeff, "NOT NULL", "DEFAULT", value
, ";"
]
_ <- Sqlite.step addColumn
Sqlite.finalize addColumn
ColumnPresent ->
traceWith tr $ MsgManualMigrationNotNeeded activeSlotCoeff

addActiveSlotCoefficientIfMissing conn =
addColumn conn (DBField CheckpointActiveSlotCoeff) value
where
activeSlotCoeff = DBField CheckpointActiveSlotCoeff
value = toText
$ W.unActiveSlotCoefficient
$ defaultActiveSlotCoefficient defaultFieldValues
Expand All @@ -448,23 +432,8 @@ migrateManually tr defaultFieldValues =
--
addDesiredPoolNumberIfMissing :: Sqlite.Connection -> IO ()
addDesiredPoolNumberIfMissing conn = do
isFieldPresent conn desiredPoolNumber >>= \case
TableMissing ->
traceWith tr $ MsgManualMigrationNotNeeded desiredPoolNumber
ColumnMissing -> do
traceWith tr $ MsgManualMigrationNeeded desiredPoolNumber value
addColumn <- Sqlite.prepare conn $ T.unwords
[ "ALTER TABLE", tableName desiredPoolNumber
, "ADD COLUMN", fieldName desiredPoolNumber
, fieldType desiredPoolNumber, "NOT NULL", "DEFAULT", value
, ";"
]
_ <- Sqlite.step addColumn
Sqlite.finalize addColumn
ColumnPresent ->
traceWith tr $ MsgManualMigrationNotNeeded desiredPoolNumber
addColumn conn (DBField ProtocolParametersDesiredNumberOfPools) value
where
desiredPoolNumber = DBField ProtocolParametersDesiredNumberOfPools
value = T.pack $ show $ defaultDesiredNumberOfPool defaultFieldValues

-- | This table became @protocol_parameters@.
Expand All @@ -491,6 +460,30 @@ migrateManually tr defaultFieldValues =
| otherwise -> ColumnMissing
_ -> TableMissing

-- | A migration for adding a non-existing column to a table. Factor out as
-- it's a common use-case.
addColumn
:: Sqlite.Connection
-> DBField
-> Text
-> IO ()
addColumn conn field value = do
isFieldPresent conn field >>= \case
TableMissing ->
traceWith tr $ MsgManualMigrationNotNeeded field
ColumnMissing -> do
traceWith tr $ MsgManualMigrationNeeded field value
query <- Sqlite.prepare conn $ T.unwords
[ "ALTER TABLE", tableName field
, "ADD COLUMN", fieldName field
, fieldType field, "NOT NULL", "DEFAULT", value
, ";"
]
_ <- Sqlite.step query
Sqlite.finalize query
ColumnPresent ->
traceWith tr $ MsgManualMigrationNotNeeded field

-- | A set of default field values that can be consulted when performing a
-- database migration.
--
Expand Down

0 comments on commit 616c389

Please sign in to comment.