Skip to content

Commit

Permalink
add cmd line docs and update rollback queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Cmdv committed Oct 24, 2024
1 parent 4e51ef9 commit 32f6a5f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 42 deletions.
74 changes: 37 additions & 37 deletions cardano-db/src/Cardano/Db/Operations/Delete.hs
Original file line number Diff line number Diff line change
Expand Up @@ -145,43 +145,6 @@ deleteUsingEpochNo epochN = do
pure [("GovActionProposal Nulled", a + b + c + e)]
pure $ countLogs <> nullLogs

queryDelete ::
forall m record field.
(MonadIO m, PersistEntity record, PersistField field, PersistEntityBackend record ~ SqlBackend) =>
EntityField record field ->
field ->
ReaderT SqlBackend m ()
queryDelete fieldIdField fieldId = do
mRecordId <- queryMinRefId fieldIdField fieldId
case mRecordId of
Nothing -> pure ()
Just recordId -> deleteWhere [persistIdField @record >=. recordId]

queryDeleteAndLog ::
forall m record field.
(MonadIO m, PersistEntity record, PersistField field, PersistEntityBackend record ~ SqlBackend) =>
Text ->
EntityField record field ->
field ->
ReaderT SqlBackend m [(Text, Int64)]
queryDeleteAndLog tableName txIdField fieldId = do
mRecordId <- queryMinRefId txIdField fieldId
case mRecordId of
Nothing -> pure [(tableName, 0)]
Just recordId -> do
count <- deleteWhereCount [persistIdField @record >=. recordId]
pure [(tableName, count)]

onlyDelete ::
forall m record.
(MonadIO m, PersistEntity record, PersistEntityBackend record ~ SqlBackend) =>
Text ->
[Filter record] ->
ReaderT SqlBackend m [(Text, Int64)]
onlyDelete tableName filters = do
count <- deleteWhereCount filters
pure [(tableName, count)]

deleteTablesAfterBlockId ::
MonadIO m =>
TxOutTableType ->
Expand Down Expand Up @@ -334,6 +297,43 @@ deleteTablesAfterTxId txOutTableType mtxId minIdsW = do
-- Return the combined logs of all operations
pure $ minIdsLogs <> txIdLogs

queryDelete ::
forall m record field.
(MonadIO m, PersistEntity record, PersistField field, PersistEntityBackend record ~ SqlBackend) =>
EntityField record field ->
field ->
ReaderT SqlBackend m ()
queryDelete fieldIdField fieldId = do
mRecordId <- queryMinRefId fieldIdField fieldId
case mRecordId of
Nothing -> pure ()
Just recordId -> deleteWhere [persistIdField @record >=. recordId]

queryDeleteAndLog ::
forall m record field.
(MonadIO m, PersistEntity record, PersistField field, PersistEntityBackend record ~ SqlBackend) =>
Text ->
EntityField record field ->
field ->
ReaderT SqlBackend m [(Text, Int64)]
queryDeleteAndLog tableName txIdField fieldId = do
mRecordId <- queryMinRefId txIdField fieldId
case mRecordId of
Nothing -> pure [(tableName, 0)]
Just recordId -> do
count <- deleteWhereCount [persistIdField @record >=. recordId]
pure [(tableName, count)]

onlyDelete ::
forall m record.
(MonadIO m, PersistEntity record, PersistEntityBackend record ~ SqlBackend) =>
Text ->
[Filter record] ->
ReaderT SqlBackend m [(Text, Int64)]
onlyDelete tableName filters = do
count <- deleteWhereCount filters
pure [(tableName, count)]

queryThenNull ::
forall m record field.
(MonadIO m, PersistEntity record, PersistField field, PersistEntityBackend record ~ SqlBackend) =>
Expand Down
27 changes: 23 additions & 4 deletions cardano-db/src/Cardano/Db/Operations/Other/ConsumedTxOut.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ runExtraMigrations trce txOutTableType blockNoDiff pcm = do
DBExtraMigration "runExtraMigrations: The configuration option 'tx_out.use_address_table' was previously set and the database updated. Unfortunately reverting this isn't possible."
-- Has the user given txout address config && the migration wasn't previously set
when (isTxOutVariant && not isTxOutAddressSet) $ do
updateTxOutAndCreateAddress
updateTxOutAndCreateAddress trce
insertExtraMigration TxOutAddressPreviouslySet
-- first check if pruneTxOut flag is missing and it has previously been used
when (isPruneTxOutPreviouslySet migrationValues && not (pcmPruneTxOut pcm)) $
Expand Down Expand Up @@ -415,18 +415,29 @@ createPruneConstraintTxOut = do
exceptHandler e =
liftIO $ throwIO (DBPruneConsumed $ show e)

-- Be very mindfull that these queries can fail silently and make tests fail making it hard to know why.
-- To help mitigate this, logs are printed after each query is ran, so one can know where it stopped.
updateTxOutAndCreateAddress ::
forall m.
( MonadBaseControl IO m
, MonadIO m
) =>
Trace IO Text ->
ReaderT SqlBackend m ()
updateTxOutAndCreateAddress = do
updateTxOutAndCreateAddress trc = do
handle exceptHandler $ rawExecute dropViewsQuery []
liftIO $ logInfo trc "updateTxOutAndCreateAddress: Dropped views"
handle exceptHandler $ rawExecute alterTxOutQuery []
liftIO $ logInfo trc "updateTxOutAndCreateAddress: Altered tx_out"
handle exceptHandler $ rawExecute alterCollateralTxOutQuery []
liftIO $ logInfo trc "updateTxOutAndCreateAddress: Altered collateral_tx_out"
handle exceptHandler $ rawExecute createAddressTableQuery []
liftIO $ logInfo trc "updateTxOutAndCreateAddress: Created address table"
handle exceptHandler $ rawExecute createIndexPaymentCredQuery []
liftIO $ logInfo trc "updateTxOutAndCreateAddress: Created index payment_cred"
handle exceptHandler $ rawExecute createIndexRawQuery []
liftIO $ logInfo trc "updateTxOutAndCreateAddress: Created index raw"
liftIO $ logInfo trc "updateTxOutAndCreateAddress: Completed"
where
dropViewsQuery =
Text.unlines
Expand All @@ -440,8 +451,16 @@ updateTxOutAndCreateAddress = do
, " ADD COLUMN \"address_id\" INT8 NOT NULL,"
, " DROP COLUMN \"address\","
, " DROP COLUMN \"address_has_script\","
, " DROP COLUMN \"payment_cred\","
, " DROP COLUMN \"stake_address_id\""
, " DROP COLUMN \"payment_cred\""
]

alterCollateralTxOutQuery =
Text.unlines
[ "ALTER TABLE \"collateral_tx_out\""
, " ADD COLUMN \"address_id\" INT8 NOT NULL,"
, " DROP COLUMN \"address\","
, " DROP COLUMN \"address_has_script\","
, " DROP COLUMN \"payment_cred\""
]

createAddressTableQuery =
Expand Down
2 changes: 1 addition & 1 deletion cardano-db/test/Test/IO/Cardano/Db/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ assertBool msg bool =
deleteAllBlocks :: MonadIO m => ReaderT SqlBackend m ()
deleteAllBlocks = do
mblkId <- queryMinBlock
whenJust mblkId $ \(blkId, epochN) -> deleteBlocksForTests TxOutCore blkId epochN
whenJust mblkId $ uncurry (deleteBlocksForTests TxOutCore)

dummyUTCTime :: UTCTime
dummyUTCTime = UTCTime (ModifiedJulianDay 0) 0
Expand Down
36 changes: 36 additions & 0 deletions doc/command-line-options
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
### Command Line Options

- **General Options:**
- `--config FILEPATH`: Path to the db-sync node config file
- `--socket-path FILEPATH`: Path to a cardano-node socket
- `--state-dir FILEPATH`: The directory for persisting ledger state.
- `--schema-dir FILEPATH`: The directory containing the migrations.
- `--pg-pass-env ENV`: Alternative env variable to use, defaults to `PGPASSFILE` env variable.
- `--disable-epoch`: Makes epoch table remain empty.
- `--skip-fix`: Disables the db-sync fix procedure for the wrong datum and redeemer_data bytes.
- `--force-indexes`: Forces the Index creation at the start of db-sync. Normally they're created later.
- `--fix-only`: Runs only the db-sync fix procedure for the wrong datum, redeemer_data, and plutus script bytes and exits.
- `--disable-cache`: Disables the db-sync caches. Reduces memory usage but it takes longer to sync.
- `--rollback-to-slot SLOTNO`: Force a rollback to the specified slot, if the given slot doesn't exist it will use the next greater slot.
- `--disable-in-out`: Disables the `tx_in` and `tx_out` table.

- **Deprecated Options (for historical reference only):**
- For more details, refer to [Configuration Documentation](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/configuration.md)
- `--disable-offline-data`
- `--disable-ledger`
- `--dont-use-ledger`
- `--keep-tx-metadata`
- `--disable-shelley`
- `--disable-multiassets`
- `--disable-metadata`
- `--disable-plutus-extra`
- `--disable-gov`
- `--disable-offchain-pool-data`
- `--force-tx-in`
- `--disable-all`
- `--full`
- `--only-utxo`
- `--only-gov`
- `--consumed-tx-out`
- `--prune-tx-out`
- `--bootstrap-tx-out`

0 comments on commit 32f6a5f

Please sign in to comment.