diff --git a/src/migrations/1591291822107_mempool_txs.ts b/src/migrations/1591291822107_mempool_txs.ts index 277a32bce5..b28b8c6147 100644 --- a/src/migrations/1591291822107_mempool_txs.ts +++ b/src/migrations/1591291822107_mempool_txs.ts @@ -90,6 +90,11 @@ export async function up(pgm: MigrationBuilder): Promise { // `coinbase` tx types coinbase_payload: 'bytea', + tx_size: { + type: 'integer', + notNull: true, + expressionGenerated: 'length(raw_tx)' + } }); pgm.createIndex('mempool_txs', 'tx_id', { method: 'hash' }); @@ -100,6 +105,9 @@ export async function up(pgm: MigrationBuilder): Promise { pgm.createIndex('mempool_txs', 'sponsor_address', { method: 'hash' }); pgm.createIndex('mempool_txs', 'token_transfer_recipient_address', { method: 'hash' }); pgm.createIndex('mempool_txs', [{ name: 'receipt_time', sort: 'DESC' }]); + pgm.createIndex('mempool_txs', ['type_id', 'receipt_block_height'], { where: 'pruned = false'}); + pgm.createIndex('mempool_txs', ['type_id', 'fee_rate'], { where: 'pruned = false'}); + pgm.createIndex('mempool_txs', ['type_id', 'tx_size'], { where: 'pruned = false'}); pgm.addConstraint('mempool_txs', 'unique_tx_id', `UNIQUE(tx_id)`); diff --git a/src/migrations/1621511823100_token_metadata_queue.ts b/src/migrations/1621511823100_token_metadata_queue.ts index 0a550849fd..b83f819f35 100644 --- a/src/migrations/1621511823100_token_metadata_queue.ts +++ b/src/migrations/1621511823100_token_metadata_queue.ts @@ -28,6 +28,11 @@ export async function up(pgm: MigrationBuilder): Promise { processed: { type: 'boolean', notNull: true, + }, + retry_count: { + type: 'integer', + notNull: true, + default: 0, } }); diff --git a/src/migrations/1636130197558_nft_custody.ts b/src/migrations/1636130197558_nft_custody.ts index a47124b6a4..aca78753b8 100644 --- a/src/migrations/1636130197558_nft_custody.ts +++ b/src/migrations/1636130197558_nft_custody.ts @@ -26,5 +26,6 @@ export async function up(pgm: MigrationBuilder): Promise { `); pgm.createIndex('nft_custody', ['recipient', 'asset_identifier']); - pgm.createIndex('nft_custody', 'asset_identifier'); + pgm.createIndex('nft_custody', ['asset_identifier', 'value'], { unique: true }); + pgm.createIndex('nft_custody', 'value'); } diff --git a/src/migrations/1640037852136_nft_custody_unanchored.ts b/src/migrations/1640037852136_nft_custody_unanchored.ts index a3d12f8659..92e38fd905 100644 --- a/src/migrations/1640037852136_nft_custody_unanchored.ts +++ b/src/migrations/1640037852136_nft_custody_unanchored.ts @@ -30,5 +30,6 @@ export async function up(pgm: MigrationBuilder): Promise { `); pgm.createIndex('nft_custody_unanchored', ['recipient', 'asset_identifier']); - pgm.createIndex('nft_custody_unanchored', 'asset_identifier'); + pgm.createIndex('nft_custody_unanchored', ['asset_identifier', 'value'], { unique: true }); + pgm.createIndex('nft_custody_unanchored', 'value'); } diff --git a/src/migrations/1643755236533_chain_tip.ts b/src/migrations/1643755236533_chain_tip.ts index b29fa2f8d3..5d55ba89a2 100644 --- a/src/migrations/1643755236533_chain_tip.ts +++ b/src/migrations/1643755236533_chain_tip.ts @@ -40,5 +40,8 @@ export async function up(pgm: MigrationBuilder): Promise { LEFT JOIN microblock_count ON TRUE LEFT JOIN tx_count ON TRUE LEFT JOIN tx_count_unanchored ON TRUE + LIMIT 1 `); + + pgm.createIndex('chain_tip', 'block_height', { unique: true }); } diff --git a/src/migrations/1647573802137_mempool_digest.ts b/src/migrations/1647573802137_mempool_digest.ts index 2ed4f0c8fd..81392d54b9 100644 --- a/src/migrations/1647573802137_mempool_digest.ts +++ b/src/migrations/1647573802137_mempool_digest.ts @@ -31,8 +31,11 @@ export async function up(pgm: MigrationBuilder): Promise { // extension which might not be possible for some users. pgm.createMaterializedView('mempool_digest', {}, `SELECT NULL AS digest`); } + + pgm.createIndex('mempool_digest', 'digest', { unique: true }); } export async function down(pgm: MigrationBuilder): Promise { + pgm.dropIndex('mempool_digest', 'digest', { unique: true, ifExists: true }); pgm.dropMaterializedView('mempool_digest'); } diff --git a/src/migrations/1652304516306_token_metadata_queue_retry_count.ts b/src/migrations/1652304516306_token_metadata_queue_retry_count.ts deleted file mode 100644 index 88b3ed79e3..0000000000 --- a/src/migrations/1652304516306_token_metadata_queue_retry_count.ts +++ /dev/null @@ -1,14 +0,0 @@ -/* eslint-disable @typescript-eslint/naming-convention */ -import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate'; - -export const shorthands: ColumnDefinitions | undefined = undefined; - -export async function up(pgm: MigrationBuilder): Promise { - pgm.addColumn('token_metadata_queue', { - retry_count: { - type: 'integer', - notNull: true, - default: 0, - } - }); -} diff --git a/src/migrations/1655235863682_nft_custody_value_index.ts b/src/migrations/1655235863682_nft_custody_value_index.ts deleted file mode 100644 index 17cb3da963..0000000000 --- a/src/migrations/1655235863682_nft_custody_value_index.ts +++ /dev/null @@ -1,9 +0,0 @@ -/* eslint-disable @typescript-eslint/naming-convention */ -import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate'; - -export const shorthands: ColumnDefinitions | undefined = undefined; - -export async function up(pgm: MigrationBuilder): Promise { - pgm.createIndex('nft_custody', 'value', { method: 'hash' }); - pgm.createIndex('nft_custody_unanchored', 'value', { method: 'hash' }); -} diff --git a/src/migrations/1659435823368_mempool_indexes.ts b/src/migrations/1659435823368_mempool_indexes.ts deleted file mode 100644 index 38c50b2da3..0000000000 --- a/src/migrations/1659435823368_mempool_indexes.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate'; - -export async function up(pgm: MigrationBuilder): Promise { - pgm.addColumn('mempool_txs', { - tx_size: { - type: 'integer', - notNull: true, - expressionGenerated: 'length(raw_tx)' - } - }); - - pgm.createIndex('mempool_txs', ['type_id', 'receipt_block_height'], { where: 'pruned = false'}); - pgm.createIndex('mempool_txs', ['type_id', 'fee_rate'], { where: 'pruned = false'}); - pgm.createIndex('mempool_txs', ['type_id', 'tx_size'], { where: 'pruned = false'}); - -} diff --git a/src/migrations/1660595195398_materialized_view_indexes.ts b/src/migrations/1660595195398_materialized_view_indexes.ts deleted file mode 100644 index 68db1bfd20..0000000000 --- a/src/migrations/1660595195398_materialized_view_indexes.ts +++ /dev/null @@ -1,98 +0,0 @@ -/* eslint-disable @typescript-eslint/camelcase */ -import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate'; - -export const shorthands: ColumnDefinitions | undefined = undefined; - -export async function up(pgm: MigrationBuilder): Promise { - // Add LIMIT 1 to chain_tip view so we can add the uniqueness index for `block_height`. - pgm.dropMaterializedView('chain_tip'); - pgm.createMaterializedView('chain_tip', {}, ` - WITH block_tip AS ( - SELECT block_height, block_hash, index_block_hash - FROM blocks - WHERE block_height = (SELECT MAX(block_height) FROM blocks WHERE canonical = TRUE) - ), - microblock_tip AS ( - SELECT microblock_hash, microblock_sequence - FROM microblocks, block_tip - WHERE microblocks.parent_index_block_hash = block_tip.index_block_hash - AND microblock_canonical = true AND canonical = true - ORDER BY microblock_sequence DESC - LIMIT 1 - ), - microblock_count AS ( - SELECT COUNT(*)::INTEGER AS microblock_count - FROM microblocks - WHERE canonical = TRUE AND microblock_canonical = TRUE - ), - tx_count AS ( - SELECT COUNT(*)::INTEGER AS tx_count - FROM txs - WHERE canonical = TRUE AND microblock_canonical = TRUE - AND block_height <= (SELECT MAX(block_height) FROM blocks WHERE canonical = TRUE) - ), - tx_count_unanchored AS ( - SELECT COUNT(*)::INTEGER AS tx_count_unanchored - FROM txs - WHERE canonical = TRUE AND microblock_canonical = TRUE - ) - SELECT *, block_tip.block_height AS block_count - FROM block_tip - LEFT JOIN microblock_tip ON TRUE - LEFT JOIN microblock_count ON TRUE - LEFT JOIN tx_count ON TRUE - LEFT JOIN tx_count_unanchored ON TRUE - LIMIT 1 - `); - - pgm.addIndex('chain_tip', 'block_height', { unique: true }); - pgm.addIndex('mempool_digest', 'digest', { unique: true }); - pgm.addIndex('nft_custody', ['asset_identifier', 'value'], { unique: true }); - pgm.addIndex('nft_custody_unanchored', ['asset_identifier', 'value'], { unique: true }); -} - -export async function down(pgm: MigrationBuilder): Promise { - pgm.dropIndex('chain_tip', 'block_height', { unique: true, ifExists: true }); - pgm.dropIndex('mempool_digest', 'digest', { unique: true, ifExists: true }); - pgm.dropIndex('nft_custody', ['asset_identifier', 'value'], { unique: true, ifExists: true }); - pgm.dropIndex('nft_custody_unanchored', ['asset_identifier', 'value'], { unique: true, ifExists: true }); - - pgm.dropMaterializedView('chain_tip'); - pgm.createMaterializedView('chain_tip', {}, ` - WITH block_tip AS ( - SELECT block_height, block_hash, index_block_hash - FROM blocks - WHERE block_height = (SELECT MAX(block_height) FROM blocks WHERE canonical = TRUE) - ), - microblock_tip AS ( - SELECT microblock_hash, microblock_sequence - FROM microblocks, block_tip - WHERE microblocks.parent_index_block_hash = block_tip.index_block_hash - AND microblock_canonical = true AND canonical = true - ORDER BY microblock_sequence DESC - LIMIT 1 - ), - microblock_count AS ( - SELECT COUNT(*)::INTEGER AS microblock_count - FROM microblocks - WHERE canonical = TRUE AND microblock_canonical = TRUE - ), - tx_count AS ( - SELECT COUNT(*)::INTEGER AS tx_count - FROM txs - WHERE canonical = TRUE AND microblock_canonical = TRUE - AND block_height <= (SELECT MAX(block_height) FROM blocks WHERE canonical = TRUE) - ), - tx_count_unanchored AS ( - SELECT COUNT(*)::INTEGER AS tx_count_unanchored - FROM txs - WHERE canonical = TRUE AND microblock_canonical = TRUE - ) - SELECT *, block_tip.block_height AS block_count - FROM block_tip - LEFT JOIN microblock_tip ON TRUE - LEFT JOIN microblock_count ON TRUE - LEFT JOIN tx_count ON TRUE - LEFT JOIN tx_count_unanchored ON TRUE - `); -}