Skip to content

Commit b134114

Browse files
committed
update docmentation for db-tool for address table schema
1 parent 1b5b4fa commit b134114

File tree

9 files changed

+145
-72
lines changed

9 files changed

+145
-72
lines changed

cardano-db/app/gen-schema-docs.hs

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{-# LANGUAGE OverloadedStrings #-}
22

33
import Cardano.Db (schemaDocs)
4+
import Cardano.Db.Schema.Core.TxOut (schemaDocsTxOutCore)
5+
import Cardano.Db.Schema.Variant.TxOut (schemaDocsTxOutVariant)
46
import Data.Text (Text)
57
import qualified Data.Text as Text
68
import qualified Data.Text.IO as Text
@@ -67,10 +69,18 @@ docHeader branchName =
6769
]
6870

6971
docBody :: Text
70-
docBody =
71-
Text.replace "ID:" "Id:"
72-
. Text.replace "#" "###"
73-
$ render markdownTableRenderer schemaDocs
72+
docBody = do
73+
coreDocBody <> variantDivider <> variantDocBody
74+
where
75+
coreDocBody = cleanUp $ render markdownTableRenderer (schemaDocs <> schemaDocsTxOutCore)
76+
variantDocBody = cleanUp $ render markdownTableRenderer schemaDocsTxOutVariant
77+
cleanUp = Text.replace "ID:" "Id:" . Text.replace "#" "###"
78+
variantDivider =
79+
mconcat
80+
[ "# Variant Schema\n\n"
81+
, "When using the `use_address_table` [configuration](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/configuration.md#tx-out), the `tx_out` table is split into two tables: `tx_out` and `address`.\n"
82+
, "Bellow are the table documentation for this variaton. \n\n"
83+
]
7484

7585
readGitBranch :: IO Text
7686
readGitBranch = do

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

+14-15
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,19 @@ runExtraMigrations trce txOutTableType blockNoDiff pcm = do
101101
ems <- queryAllExtraMigrations
102102
isTxOutNull <- queryTxOutIsNull txOutTableType
103103
let migrationValues = processMigrationValues ems pcm
104+
isTxOutVariant = isTxOutVariantAddress txOutTableType
105+
isTxOutAddressSet = isTxOutAddressPreviouslySet migrationValues
104106

105-
-- can only run "use_address_table" on a non populated database
106-
when (isTxOutVariantAddress txOutTableType && not isTxOutNull) $
107+
-- can only run "use_address_table" on a non populated database but don't throw if the migration was previously set
108+
when (isTxOutVariant && not isTxOutNull && not isTxOutAddressSet) $
107109
throw $
108-
DBExtraMigration "runExtraMigrations: The use the config 'tx_out.use_address_table' can only be caried out on a non populated database."
110+
DBExtraMigration "runExtraMigrations: The use of the config 'tx_out.use_address_table' can only be caried out on a non populated database."
109111
-- Make sure the config "use_address_table" is there if the migration wasn't previously set in the past
110-
when (not (isTxOutVariantAddress txOutTableType) && isTxOutAddressPreviouslySet migrationValues) $
112+
when (not isTxOutVariant && isTxOutAddressSet) $
111113
throw $
112114
DBExtraMigration "runExtraMigrations: The configuration option 'tx_out.use_address_table' was previously set and the database updated. Unfortunately reverting this isn't possible."
113115
-- Has the user given txout address config && the migration wasn't previously set
114-
when (isTxOutVariantAddress txOutTableType && not (isTxOutAddressPreviouslySet migrationValues)) $ do
116+
when (isTxOutVariant && not isTxOutAddressSet) $ do
115117
updateTxOutAndCreateAddress
116118
insertExtraMigration TxOutAddressPreviouslySet
117119
-- first check if pruneTxOut flag is missing and it has previously been used
@@ -239,10 +241,8 @@ migrateTxOut ::
239241
ReaderT SqlBackend m ()
240242
migrateTxOut trce txOutTableType mMvs = do
241243
whenJust mMvs $ \mvs -> do
242-
liftIO $ logInfo trce $ "migrateTxOut: previously set: " <> textShow (not (isTxOutAddressPreviouslySet mvs))
243-
liftIO $ logInfo trce $ "migrateTxOut: pcmConsumedTxOut: " <> textShow (pcmConsumedTxOut (pruneConsumeMigration mvs))
244244
when (pcmConsumedTxOut (pruneConsumeMigration mvs) && not (isTxOutAddressPreviouslySet mvs)) $ do
245-
liftIO $ logInfo trce "migrateTxOut: addeding consumed-by-id Index"
245+
liftIO $ logInfo trce "migrateTxOut: adding consumed-by-id Index"
246246
void createConsumedIndexTxOut
247247
when (pcmPruneTxOut (pruneConsumeMigration mvs)) $ do
248248
liftIO $ logInfo trce "migrateTxOut: adding prune contraint on tx_out table"
@@ -259,12 +259,6 @@ migrateNextPageTxOut mTrce txOutTableType offst = do
259259
migrateNextPageTxOut mTrce txOutTableType $!
260260
(offst + pageSize)
261261

262-
-- TODO: cmdv put into tools or something
263-
migrateTxOutDbTool :: (MonadIO m, MonadBaseControl IO m) => TxOutTableType -> ReaderT SqlBackend m ()
264-
migrateTxOutDbTool txOutTableType = do
265-
_ <- createConsumedIndexTxOut
266-
migrateNextPageTxOut Nothing txOutTableType 0
267-
268262
--------------------------------------------------------------------------------------------------
269263
-- Delete + Update
270264
--------------------------------------------------------------------------------------------------
@@ -458,7 +452,7 @@ updateTxOutAndCreateAddress = do
458452
"CREATE INDEX IF NOT EXISTS idx_address_payment_cred ON address(payment_cred);"
459453

460454
createIndexRawQuery =
461-
"CREATE INDEX IF NOT EXISTS idx_address_raw ON address(raw);"
455+
"CREATE INDEX IF NOT EXISTS idx_address_raw ON address USING HASH (raw);"
462456

463457
exceptHandler :: SqlError -> ReaderT SqlBackend m a
464458
exceptHandler e =
@@ -490,6 +484,11 @@ deleteConsumedBeforeTx trce txOutTableType txId = do
490484
--------------------------------------------------------------------------------------------------
491485
-- Helpers
492486
--------------------------------------------------------------------------------------------------
487+
migrateTxOutDbTool :: (MonadIO m, MonadBaseControl IO m) => TxOutTableType -> ReaderT SqlBackend m ()
488+
migrateTxOutDbTool txOutTableType = do
489+
_ <- createConsumedIndexTxOut
490+
migrateNextPageTxOut Nothing txOutTableType 0
491+
493492
findMaxTxInId :: forall m. MonadIO m => Word64 -> ReaderT SqlBackend m (Either Text TxId)
494493
findMaxTxInId blockNoDiff = do
495494
mBlockHeight <- queryBlockHeight

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Database.Persist.TH
3030
share
3131
[ mkPersist sqlSettings
3232
, mkMigrate "migrateCoreTxOutCardanoDb"
33-
, mkEntityDefList "entityDefs"
33+
, mkEntityDefList "entityDefsTxOutCore"
3434
, deriveShowFields
3535
]
3636
[persistLowerCase|
@@ -62,13 +62,14 @@ share
6262

6363
|]
6464

65-
schemaDocs :: [EntityDef]
66-
schemaDocs =
67-
document entityDefs $ do
65+
schemaDocsTxOutCore :: [EntityDef]
66+
schemaDocsTxOutCore =
67+
document entityDefsTxOutCore $ do
6868
TxOut --^ do
6969
"A table for transaction outputs."
7070
TxOutAddress # "The human readable encoding of the output address. Will be Base58 for Byron era addresses and Bech32 for Shelley era."
7171
TxOutAddressHasScript # "Flag which shows if this address is locked by a script."
72+
TxOutConsumedByTxId # "The Tx table index of the transaction that consumes this transaction output. Not populated by default, can be activated via tx-out configs."
7273
TxOutDataHash # "The hash of the transaction output datum. (NULL for Txs without scripts)."
7374
TxOutIndex # "The index of this transaction output with the transaction."
7475
TxOutInlineDatumId # "The inline datum of the output, if it has one. New in v13."

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Database.Persist.TH
3030
share
3131
[ mkPersist sqlSettings
3232
, mkMigrate "migrateVariantAddressCardanoDb"
33-
, mkEntityDefList "entityDefs"
33+
, mkEntityDefList "entityDefsTxOutVariant"
3434
, deriveShowFields
3535
]
3636
[persistLowerCase|
@@ -65,12 +65,13 @@ share
6565
deriving Show
6666
|]
6767

68-
schemaDocs :: [EntityDef]
69-
schemaDocs =
70-
document entityDefs $ do
68+
schemaDocsTxOutVariant :: [EntityDef]
69+
schemaDocsTxOutVariant =
70+
document entityDefsTxOutVariant $ do
7171
TxOut --^ do
7272
"A table for transaction outputs."
7373
TxOutAddressId # "The human readable encoding of the output address. It is Base58 for Byron era addresses and Bech32 for Shelley era."
74+
TxOutConsumedByTxId # "The Tx table index of the transaction that consumes this transaction output. Not populated by default, can be activated via tx-out configs."
7475
TxOutDataHash # "The hash of the transaction output datum. (NULL for Txs without scripts)."
7576
TxOutIndex # "The index of this transaction output with the transaction."
7677
TxOutInlineDatumId # "The inline datum of the output, if it has one. New in v13."

doc/configuration.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,7 @@ can be changed.
280280

281281
* Type: `boolean`
282282

283-
This value defaults to false.
284-
<!-- TODO: Need to add more of a description as to what it does -->
285-
283+
When using `consumed` configuration `tx_in` will not be populated. That behaviour can be overridden by setting this value to `true`.
286284

287285
### Address Table
288286

doc/schema-management.md

+16-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ indexes during syncing slows down db-sync and so they are added later. Index
1515
creation is idempotent and the `schema_version.stage_tree` field is ignored.
1616
These files cannot be modified but they can be extended, in case users want to
1717
introduce their own indexes from the begining.
18-
- `stage 4`: introduces all the other indexes. By default these are the indexes
19-
that were created by previous db-sync versions. This stage is executed when
20-
db-sync has reached 30mins before the tip of the chain. It is advised to increase
21-
the `maintenance_work_mem` from Postgres config to 0.5GB - 1GB to speed this
22-
process (default is 64MB). Also use the default (2) or higher
23-
`max_parallel_maintenance_workers`. These files can be modified or extended
24-
by users.
18+
- `stage 4`: introduces all the other indexes. By default these are the indexes that were created by previous db-sync versions. This stage is executed when
19+
db-sync has reached 30mins before the tip of the chain. It is advised to increase the `maintenance_work_mem` from Postgres config to 0.5GB - 1GB to speed this
20+
process (default is 64MB).
21+
Also use the default (2) or higher `max_parallel_maintenance_workers`. These files can be modified or extended
22+
by users.
2523

2624
All of the schema migrations in these three stages are written to be idempotent (so that they
2725
"know" if they have already been applied).
@@ -34,6 +32,11 @@ where the `1` denotes "stage 1" of the SQL migration, the `0000` is the migratio
3432
last number is the date. Listing the directory containing the schema and sorting the list will
3533
order them in the correct order for applying to the database.
3634

35+
Since the introduction of `use_address_table` [config](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/configuration.md#tx-out). The file `migration-4-001-*` when indexing will not be ran when the this configuration is active.
36+
37+
There is also a flag you can use in cardano-db-tool `--use-tx-out-address` which handles the alternate variation of the schema, one might be using.
38+
39+
3740
## Creating a Migration
3841

3942
Whenever the Haskell schema definition in `Cardano.Db.Schema` is updated, a schema migration will need to be migrated.
@@ -52,6 +55,12 @@ cabal run cardano-db-tool -- create-migration --mdir schema/
5255
```
5356
This will generate a migration if one is needed.
5457

58+
There is an alternate way to create/run a migration when using the `use_txout_address` configuration as previously mentioned, this uses a different variation of the schema.
59+
To do this, you simply add the flag `--use-tx-out-address` like so:
60+
```
61+
PGPASSFILE=config/pgpass-mainnet cabal run cardano-db-tool -- create-migration --use-tx-out-address --mdir schema/
62+
```
63+
5564
Once this has completed it's good practice to rebuild `cardano-db-sync` due to how it caches schema files when built, this can be done using the following documentation [Build and Install](./installing.md#build-and-install)
5665

5766
**Note:** For extra reassurance one can run the test suite to check that the new migration hasn't broken any tests:

doc/schema.md

+88-33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Schema Documentation for cardano-db-sync
22

3+
Schema version: 13.5.0.2 (from branch **1333-new-AddressDetail-table** which may not accurately reflect the version number)
4+
**Note:** This file is auto-generated from the documentation in cardano-db/src/Cardano/Db/Schema/BaseSchema.hs by the command `cabal run -- gen-schema-docs doc/schema.md`. This document should only be updated during the release process and updated on the release branch.
5+
36
### `schema_version`
47

58
The version of the database schema. Schema versioning is split into three stages as detailed below. This table should only ever have a single row.
@@ -122,26 +125,6 @@ A table of unique stake addresses. Can be an actual address or a script hash. T
122125
| `view` | string | The Bech32 encoded version of the stake address. |
123126
| `script_hash` | hash28type | The script hash, in case this address is locked by a script. |
124127

125-
### `tx_out`
126-
127-
A table for transaction outputs.
128-
129-
* Primary Id: `id`
130-
131-
| Column name | Type | Description |
132-
|-|-|-|
133-
| `id` | integer (64) | |
134-
| `tx_id` | integer (64) | The Tx table index of the transaction that contains this transaction output. |
135-
| `index` | txindex | The index of this transaction output with the transaction. |
136-
| `address` | string | The human readable encoding of the output address. Will be Base58 for Byron era addresses and Bech32 for Shelley era. |
137-
| `address_has_script` | boolean | Flag which shows if this address is locked by a script. |
138-
| `payment_cred` | hash28type | The payment credential part of the Shelley address. (NULL for Byron addresses). For a script-locked address, this is the script hash. |
139-
| `stake_address_id` | integer (64) | The StakeAddress table index for the stake address part of the Shelley address. (NULL for Byron addresses). |
140-
| `value` | lovelace | The output value (in Lovelace) of the transaction output. |
141-
| `data_hash` | hash32type | The hash of the transaction output datum. (NULL for Txs without scripts). |
142-
| `inline_datum_id` | integer (64) | The inline datum of the output, if it has one. New in v13. |
143-
| `reference_script_id` | integer (64) | The reference script of the output, if it has one. New in v13. |
144-
145128
### `collateral_tx_out`
146129

147130
A table for transaction collateral outputs. New in v13.
@@ -545,19 +528,6 @@ A table containing Multi-Asset mint events.
545528
| `quantity` | int65type | The amount of the Multi Asset to mint (can be negative to "burn" assets). |
546529
| `tx_id` | integer (64) | The Tx table index for the transaction that contains this minting event. |
547530

548-
### `ma_tx_out`
549-
550-
A table containing Multi-Asset transaction outputs.
551-
552-
* Primary Id: `id`
553-
554-
| Column name | Type | Description |
555-
|-|-|-|
556-
| `id` | integer (64) | |
557-
| `ident` | integer (64) | The MultiAsset table index specifying the asset. |
558-
| `quantity` | word64type | The Multi Asset transaction output amount (denominated in the Multi Asset). |
559-
| `tx_out_id` | integer (64) | The TxOut table index for the transaction that this Multi Asset transaction output. |
560-
561531
### `redeemer`
562532

563533
A table containing redeemers. A redeemer is provided for all items that are validated by a script.
@@ -1196,4 +1166,89 @@ A table containing pools that have been delisted.
11961166
| `id` | integer (64) | |
11971167
| `hash_raw` | hash28type | The pool hash |
11981168

1169+
### `tx_out`
1170+
1171+
A table for transaction outputs.
1172+
1173+
* Primary Id: `id`
1174+
1175+
| Column name | Type | Description |
1176+
|-|-|-|
1177+
| `id` | integer (64) | |
1178+
| `address` | string | The human readable encoding of the output address. Will be Base58 for Byron era addresses and Bech32 for Shelley era. |
1179+
| `address_has_script` | boolean | Flag which shows if this address is locked by a script. |
1180+
| `data_hash` | hash32type | The hash of the transaction output datum. (NULL for Txs without scripts). |
1181+
| `consumed_by_tx_id` | integer (64) | The Tx table index of the transaction that consumes this transaction output. Not populated by default, can be activated via tx-out configs. |
1182+
| `index` | txindex | The index of this transaction output with the transaction. |
1183+
| `inline_datum_id` | integer (64) | The inline datum of the output, if it has one. New in v13. |
1184+
| `payment_cred` | hash28type | The payment credential part of the Shelley address. (NULL for Byron addresses). For a script-locked address, this is the script hash. |
1185+
| `reference_script_id` | integer (64) | The reference script of the output, if it has one. New in v13. |
1186+
| `stake_address_id` | integer (64) | The StakeAddress table index for the stake address part of the Shelley address. (NULL for Byron addresses). |
1187+
| `tx_id` | integer (64) | The Tx table index of the transaction that contains this transaction output. |
1188+
| `value` | lovelace | The output value (in Lovelace) of the transaction output. |
1189+
1190+
### `ma_tx_out`
1191+
1192+
A table containing Multi-Asset transaction outputs.
1193+
1194+
* Primary Id: `id`
1195+
1196+
| Column name | Type | Description |
1197+
|-|-|-|
1198+
| `id` | integer (64) | |
1199+
| `ident` | integer (64) | The MultiAsset table index specifying the asset. |
1200+
| `quantity` | word64type | The Multi Asset transaction output amount (denominated in the Multi Asset). |
1201+
| `tx_out_id` | integer (64) | The TxOut table index for the transaction that this Multi Asset transaction output. |
1202+
1203+
# Variant Schema
1204+
1205+
When using the `use_address_table` [configuration](https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/configuration.md#tx-out), the `tx_out` table is split into two tables: `tx_out` and `address`.
1206+
Bellow are the table documentation for this variaton.
1207+
1208+
### `tx_out`
1209+
1210+
A table for transaction outputs.
1211+
1212+
* Primary Id: `id`
1213+
1214+
| Column name | Type | Description |
1215+
|-|-|-|
1216+
| `id` | integer (64) | |
1217+
| `address_id` | integer (64) | The human readable encoding of the output address. It is Base58 for Byron era addresses and Bech32 for Shelley era. |
1218+
| `consumed_by_tx_id` | integer (64) | The Tx table index of the transaction that consumes this transaction output. Not populated by default, can be activated via tx-out configs. |
1219+
| `data_hash` | hash32type | The hash of the transaction output datum. (NULL for Txs without scripts). |
1220+
| `index` | txindex | The index of this transaction output with the transaction. |
1221+
| `inline_datum_id` | integer (64) | The inline datum of the output, if it has one. New in v13. |
1222+
| `reference_script_id` | integer (64) | The reference script of the output, if it has one. New in v13. |
1223+
| `tx_id` | integer (64) | The Tx table index of the transaction that contains this transaction output. |
1224+
| `value` | lovelace | The output value (in Lovelace) of the transaction output. |
1225+
1226+
### `address`
1227+
1228+
A table for addresses that appear in outputs.
1229+
1230+
* Primary Id: `id`
1231+
1232+
| Column name | Type | Description |
1233+
|-|-|-|
1234+
| `id` | integer (64) | |
1235+
| `address` | string | The human readable encoding of the output address. Will be Base58 for Byron era addresses and Bech32 for Shelley era. |
1236+
| `raw` | blob | The raw binary address. |
1237+
| `has_script` | boolean | Flag which shows if this address is locked by a script. |
1238+
| `payment_cred` | hash28type | The payment credential part of the Shelley address. (NULL for Byron addresses). For a script-locked address, this is the script hash. |
1239+
| `stake_address_id` | integer (64) | The StakeAddress table index for the stake address part of the Shelley address. (NULL for Byron addresses). |
1240+
1241+
### `ma_tx_out`
1242+
1243+
A table containing Multi-Asset transaction outputs.
1244+
1245+
* Primary Id: `id`
1246+
1247+
| Column name | Type | Description |
1248+
|-|-|-|
1249+
| `id` | integer (64) | |
1250+
| `ident` | integer (64) | The MultiAsset table index specifying the asset. |
1251+
| `quantity` | word64type | The Multi Asset transaction output amount (denominated in the Multi Asset). |
1252+
| `tx_out_id` | integer (64) | The TxOut table index for the transaction that this Multi Asset transaction output. |
1253+
11991254

flake.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
packages.cardano-db.package.extraSrcFiles =
203203
["../config/pgpass-testnet"];
204204
packages.cardano-db.components.tests.schema-rollback.extraSrcFiles =
205-
[ "src/Cardano/Db/Schema.hs" "src/Cardano/Db/Operations/Delete.hs" ];
205+
[ "src/Cardano/Db/BaseSchema.hs" "src/Cardano/Db/Operations/Delete.hs" ];
206206
packages.cardano-db.components.tests.test-db.extraSrcFiles =
207207
[ "../config/pgpass-mainnet" ];
208208
packages.cardano-chain-gen.package.extraSrcFiles =

schema/migration-4-0001-20200702.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
--
55

66
CREATE INDEX IF NOT EXISTS idx_tx_out_payment_cred ON tx_out(payment_cred);
7-
CREATE INDEX IF NOT EXISTS idx_tx_out_stake_address_id ON tx_out(stake_address_id) ;
7+
CREATE INDEX IF NOT EXISTS idx_tx_out_stake_address_id ON tx_out(stake_address_id);
88

99
-- Left here for reference, it's removed to speed up restoring from a snapshot as this index is very slow to create.
1010
-- CREATE INDEX IF NOT EXISTS idx_tx_out_address ON tx_out USING hash (address);

0 commit comments

Comments
 (0)