|
| 1 | +/* eslint-disable @typescript-eslint/camelcase */ |
| 2 | +import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate'; |
| 3 | + |
| 4 | +export const shorthands: ColumnDefinitions | undefined = undefined; |
| 5 | + |
| 6 | +export async function up(pgm: MigrationBuilder): Promise<void> { |
| 7 | + // Add LIMIT 1 to chain_tip view so we can add the uniqueness index for `block_height`. |
| 8 | + pgm.dropMaterializedView('chain_tip'); |
| 9 | + pgm.createMaterializedView('chain_tip', {}, ` |
| 10 | + WITH block_tip AS ( |
| 11 | + SELECT block_height, block_hash, index_block_hash |
| 12 | + FROM blocks |
| 13 | + WHERE block_height = (SELECT MAX(block_height) FROM blocks WHERE canonical = TRUE) |
| 14 | + ), |
| 15 | + microblock_tip AS ( |
| 16 | + SELECT microblock_hash, microblock_sequence |
| 17 | + FROM microblocks, block_tip |
| 18 | + WHERE microblocks.parent_index_block_hash = block_tip.index_block_hash |
| 19 | + AND microblock_canonical = true AND canonical = true |
| 20 | + ORDER BY microblock_sequence DESC |
| 21 | + LIMIT 1 |
| 22 | + ), |
| 23 | + microblock_count AS ( |
| 24 | + SELECT COUNT(*)::INTEGER AS microblock_count |
| 25 | + FROM microblocks |
| 26 | + WHERE canonical = TRUE AND microblock_canonical = TRUE |
| 27 | + ), |
| 28 | + tx_count AS ( |
| 29 | + SELECT COUNT(*)::INTEGER AS tx_count |
| 30 | + FROM txs |
| 31 | + WHERE canonical = TRUE AND microblock_canonical = TRUE |
| 32 | + AND block_height <= (SELECT MAX(block_height) FROM blocks WHERE canonical = TRUE) |
| 33 | + ), |
| 34 | + tx_count_unanchored AS ( |
| 35 | + SELECT COUNT(*)::INTEGER AS tx_count_unanchored |
| 36 | + FROM txs |
| 37 | + WHERE canonical = TRUE AND microblock_canonical = TRUE |
| 38 | + ) |
| 39 | + SELECT *, block_tip.block_height AS block_count |
| 40 | + FROM block_tip |
| 41 | + LEFT JOIN microblock_tip ON TRUE |
| 42 | + LEFT JOIN microblock_count ON TRUE |
| 43 | + LEFT JOIN tx_count ON TRUE |
| 44 | + LEFT JOIN tx_count_unanchored ON TRUE |
| 45 | + LIMIT 1 |
| 46 | + `); |
| 47 | + |
| 48 | + pgm.addIndex('chain_tip', 'block_height', { unique: true }); |
| 49 | + pgm.addIndex('mempool_digest', 'digest', { unique: true }); |
| 50 | + pgm.addIndex('nft_custody', ['asset_identifier', 'value'], { unique: true }); |
| 51 | + pgm.addIndex('nft_custody_unanchored', ['asset_identifier', 'value'], { unique: true }); |
| 52 | +} |
| 53 | + |
| 54 | +export async function down(pgm: MigrationBuilder): Promise<void> { |
| 55 | + pgm.dropIndex('chain_tip', 'block_height', { unique: true, ifExists: true }); |
| 56 | + pgm.dropIndex('mempool_digest', 'digest', { unique: true, ifExists: true }); |
| 57 | + pgm.dropIndex('nft_custody', ['asset_identifier', 'value'], { unique: true, ifExists: true }); |
| 58 | + pgm.dropIndex('nft_custody_unanchored', ['asset_identifier', 'value'], { unique: true, ifExists: true }); |
| 59 | + |
| 60 | + pgm.dropMaterializedView('chain_tip'); |
| 61 | + pgm.createMaterializedView('chain_tip', {}, ` |
| 62 | + WITH block_tip AS ( |
| 63 | + SELECT block_height, block_hash, index_block_hash |
| 64 | + FROM blocks |
| 65 | + WHERE block_height = (SELECT MAX(block_height) FROM blocks WHERE canonical = TRUE) |
| 66 | + ), |
| 67 | + microblock_tip AS ( |
| 68 | + SELECT microblock_hash, microblock_sequence |
| 69 | + FROM microblocks, block_tip |
| 70 | + WHERE microblocks.parent_index_block_hash = block_tip.index_block_hash |
| 71 | + AND microblock_canonical = true AND canonical = true |
| 72 | + ORDER BY microblock_sequence DESC |
| 73 | + LIMIT 1 |
| 74 | + ), |
| 75 | + microblock_count AS ( |
| 76 | + SELECT COUNT(*)::INTEGER AS microblock_count |
| 77 | + FROM microblocks |
| 78 | + WHERE canonical = TRUE AND microblock_canonical = TRUE |
| 79 | + ), |
| 80 | + tx_count AS ( |
| 81 | + SELECT COUNT(*)::INTEGER AS tx_count |
| 82 | + FROM txs |
| 83 | + WHERE canonical = TRUE AND microblock_canonical = TRUE |
| 84 | + AND block_height <= (SELECT MAX(block_height) FROM blocks WHERE canonical = TRUE) |
| 85 | + ), |
| 86 | + tx_count_unanchored AS ( |
| 87 | + SELECT COUNT(*)::INTEGER AS tx_count_unanchored |
| 88 | + FROM txs |
| 89 | + WHERE canonical = TRUE AND microblock_canonical = TRUE |
| 90 | + ) |
| 91 | + SELECT *, block_tip.block_height AS block_count |
| 92 | + FROM block_tip |
| 93 | + LEFT JOIN microblock_tip ON TRUE |
| 94 | + LEFT JOIN microblock_count ON TRUE |
| 95 | + LEFT JOIN tx_count ON TRUE |
| 96 | + LEFT JOIN tx_count_unanchored ON TRUE |
| 97 | + `); |
| 98 | +} |
0 commit comments