Skip to content

Commit de67165

Browse files
committed
feature(cardano-db): Add a migration that removes "consumed_by_tx_id"
1 parent 344555f commit de67165

File tree

5 files changed

+38
-5
lines changed

5 files changed

+38
-5
lines changed

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Config/TxOutConsumed.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ consumeTx =
5151

5252
consumeTxConsumed :: IOManager -> [(Text, Text)] -> Assertion
5353
consumeTxConsumed =
54-
withCustomConfig cmdLineArgs cfg conwayConfigDir testLabel $ \interpreter mockServer dbSync -> do
54+
withCustomConfigAndDropDB cmdLineArgs cfg conwayConfigDir testLabel $ \interpreter mockServer dbSync -> do
5555
startDBSync dbSync
5656

5757
-- Forge a payment transaction

cardano-db/src/Cardano/Db/Operations/Other/ConsumedTxOut.hs

+32-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ runExtraMigrations trce txOutTableType blockNoDiff pcm = do
137137
case (isConsumeTxOutPreviouslySet, pcmConsumedTxOut, pcmPruneTxOut) of
138138
-- No Migration Needed
139139
(False, False, False) -> do
140-
liftIO $ logInfo trce "runExtraMigrations: No extra migration specified"
140+
liftIO $ logInfo trce "runExtraMigrations: Running extra migration no_consumed_tx_out"
141+
insertExtraMigration NoConsumeTxOut
142+
migrateTxOut trce txOutTableType (Just migrationValues)
141143
-- Already migrated
142144
(True, True, False) -> do
143145
liftIO $ logInfo trce "runExtraMigrations: Extra migration consumed_tx_out already executed"
@@ -255,7 +257,12 @@ migrateTxOut trce txOutTableType mMvs = do
255257
when (pcmPruneTxOut (pruneConsumeMigration mvs)) $ do
256258
liftIO $ logInfo trce "migrateTxOut: adding prune contraint on tx_out table"
257259
void createPruneConstraintTxOut
258-
migrateNextPageTxOut (Just trce) txOutTableType 0
260+
if pcmConsumedTxOut (pruneConsumeMigration mvs)
261+
then do
262+
migrateNextPageTxOut (Just trce) txOutTableType 0
263+
else do
264+
liftIO $ logInfo trce "migrateTxOut: removing column consumed_by_tx from tx_out table"
265+
void (dropColumnConsumedByTxOut trce)
259266

260267
migrateNextPageTxOut :: MonadIO m => Maybe (Trace IO Text) -> TxOutTableType -> Word64 -> ReaderT SqlBackend m ()
261268
migrateNextPageTxOut mTrce txOutTableType offst = do
@@ -415,6 +422,29 @@ createPruneConstraintTxOut = do
415422
exceptHandler e =
416423
liftIO $ throwIO (DBPruneConsumed $ show e)
417424

425+
dropColumnConsumedByTxOut ::
426+
forall io.
427+
(MonadBaseControl IO io, MonadIO io) =>
428+
Trace IO Text ->
429+
ReaderT SqlBackend io ()
430+
dropColumnConsumedByTxOut trace = do
431+
handle exceptionHandler (rawExecute dropViewsQuery [])
432+
liftIO $ logInfo trace "dropColumnConsumedByTxOut: Dropped views"
433+
434+
handle exceptionHandler (rawExecute dropColumn [])
435+
liftIO $ logInfo trace "dropColumnConsumedByTxOut: Altered tx_out"
436+
where
437+
dropColumn = "ALTER TABLE tx_out DROP COLUMN IF EXISTS consumed_by_tx_id;"
438+
439+
dropViewsQuery =
440+
Text.unlines
441+
[ "DROP VIEW IF EXISTS utxo_byron_view;"
442+
, "DROP VIEW IF EXISTS utxo_view;"
443+
]
444+
445+
exceptionHandler :: SqlError -> ReaderT SqlBackend io a
446+
exceptionHandler = liftIO . throwIO . DBPruneConsumed . show
447+
418448
-- Be very mindfull that these queries can fail silently and make tests fail making it hard to know why.
419449
-- To help mitigate this, logs are printed after each query is ran, so one can know where it stopped.
420450
updateTxOutAndCreateAddress ::

cardano-db/src/Cardano/Db/Schema/Core/TxOut.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ share
4141
address Text
4242
addressHasScript Bool
4343
dataHash ByteString Maybe sqltype=hash32type
44-
consumedByTxId TxId Maybe noreference
44+
consumedByTxId TxId Maybe noreference default=null
4545
index Word64 sqltype=txindex
4646
inlineDatumId DatumId Maybe noreference
4747
paymentCred ByteString Maybe sqltype=hash28type

cardano-db/src/Cardano/Db/Schema/Variant/TxOut.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ share
3939
----------------------------------------------
4040
TxOut
4141
addressId AddressId noreference
42-
consumedByTxId TxId Maybe noreference
42+
consumedByTxId TxId Maybe noreference default=null
4343
dataHash ByteString Maybe sqltype=hash32type
4444
index Word64 sqltype=txindex
4545
inlineDatumId DatumId Maybe noreference

cardano-db/src/Cardano/Db/Types.hs

+3
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ data ExtraMigration
199199
| BootstrapFinished
200200
| ConsumeTxOutPreviouslySet
201201
| TxOutAddressPreviouslySet
202+
| NoConsumeTxOut
202203
deriving (Eq, Show, Read)
203204

204205
data MigrationValues = MigrationValues
@@ -259,6 +260,8 @@ extraDescription = \case
259260
"The --consume-tx-out flag has previously been enabled"
260261
TxOutAddressPreviouslySet ->
261262
"The addition of a Address table for TxOuts was previously set"
263+
NoConsumeTxOut ->
264+
"The --consume-tx-out flag has previously been disabled"
262265
instance Ord PoolCert where
263266
compare a b = compare (pcCertNo a) (pcCertNo b)
264267

0 commit comments

Comments
 (0)