| 
 | 1 | +/** @param { import("node-pg-migrate").MigrationBuilder } pgm */  | 
 | 2 | +exports.up = pgm => {  | 
 | 3 | +  pgm.dropTable('principal_stx_txs');  | 
 | 4 | + | 
 | 5 | +  pgm.addColumn('txs', {  | 
 | 6 | +    stx_transfer_event_count: {  | 
 | 7 | +      type: 'integer',  | 
 | 8 | +      notNull: true,  | 
 | 9 | +      default: 0,  | 
 | 10 | +    },  | 
 | 11 | +    stx_mint_event_count: {  | 
 | 12 | +      type: 'integer',  | 
 | 13 | +      notNull: true,  | 
 | 14 | +      default: 0,  | 
 | 15 | +    },  | 
 | 16 | +    stx_burn_event_count: {  | 
 | 17 | +      type: 'integer',  | 
 | 18 | +      notNull: true,  | 
 | 19 | +      default: 0,  | 
 | 20 | +    },  | 
 | 21 | +    stx_lock_event_count: {  | 
 | 22 | +      type: 'integer',  | 
 | 23 | +      notNull: true,  | 
 | 24 | +      default: 0,  | 
 | 25 | +    },  | 
 | 26 | +    ft_transfer_event_count: {  | 
 | 27 | +      type: 'integer',  | 
 | 28 | +      notNull: true,  | 
 | 29 | +      default: 0,  | 
 | 30 | +    },  | 
 | 31 | +    ft_mint_event_count: {  | 
 | 32 | +      type: 'integer',  | 
 | 33 | +      notNull: true,  | 
 | 34 | +      default: 0,  | 
 | 35 | +    },  | 
 | 36 | +    ft_burn_event_count: {  | 
 | 37 | +      type: 'integer',  | 
 | 38 | +      notNull: true,  | 
 | 39 | +      default: 0,  | 
 | 40 | +    },  | 
 | 41 | +    nft_transfer_event_count: {  | 
 | 42 | +      type: 'integer',  | 
 | 43 | +      notNull: true,  | 
 | 44 | +      default: 0,  | 
 | 45 | +    },  | 
 | 46 | +    nft_mint_event_count: {  | 
 | 47 | +      type: 'integer',  | 
 | 48 | +      notNull: true,  | 
 | 49 | +      default: 0,  | 
 | 50 | +    },  | 
 | 51 | +    nft_burn_event_count: {  | 
 | 52 | +      type: 'integer',  | 
 | 53 | +      notNull: true,  | 
 | 54 | +      default: 0,  | 
 | 55 | +    },  | 
 | 56 | +    contract_log_event_count: {  | 
 | 57 | +      type: 'integer',  | 
 | 58 | +      notNull: true,  | 
 | 59 | +      default: 0,  | 
 | 60 | +    },  | 
 | 61 | +  });  | 
 | 62 | + | 
 | 63 | +  /**  | 
 | 64 | +   * Stores all `tx_id`s of transactions that affect a principal's STX balance since that cannot be  | 
 | 65 | +   * directly determined from the `txs` table (an expensive JOIN with `stx_events` is required).  | 
 | 66 | +   */  | 
 | 67 | +  pgm.createTable('principal_txs', {  | 
 | 68 | +    principal: {  | 
 | 69 | +      type: 'string',  | 
 | 70 | +      notNull: true,  | 
 | 71 | +    },  | 
 | 72 | +    tx_id: {  | 
 | 73 | +      type: 'bytea',  | 
 | 74 | +      notNull: true,  | 
 | 75 | +    },  | 
 | 76 | +    stx_balance_affected: {  | 
 | 77 | +      type: 'boolean',  | 
 | 78 | +      notNull: true,  | 
 | 79 | +    },  | 
 | 80 | +    ft_balance_affected: {  | 
 | 81 | +      type: 'boolean',  | 
 | 82 | +      notNull: true,  | 
 | 83 | +    },  | 
 | 84 | +    nft_balance_affected: {  | 
 | 85 | +      type: 'boolean',  | 
 | 86 | +      notNull: true,  | 
 | 87 | +    },  | 
 | 88 | +    stx_sent: {  | 
 | 89 | +      type: 'bigint',  | 
 | 90 | +      notNull: true,  | 
 | 91 | +    },  | 
 | 92 | +    stx_received: {  | 
 | 93 | +      type: 'bigint',  | 
 | 94 | +      notNull: true,  | 
 | 95 | +    },  | 
 | 96 | +    block_height: {  | 
 | 97 | +      type: 'integer',  | 
 | 98 | +      notNull: true,  | 
 | 99 | +    },  | 
 | 100 | +    index_block_hash: {  | 
 | 101 | +      type: 'bytea',  | 
 | 102 | +      notNull: true,  | 
 | 103 | +    },  | 
 | 104 | +    microblock_hash: {  | 
 | 105 | +      type: 'bytea',  | 
 | 106 | +      notNull: true,  | 
 | 107 | +    },  | 
 | 108 | +    microblock_sequence: {  | 
 | 109 | +      type: 'integer',  | 
 | 110 | +      notNull: true,  | 
 | 111 | +    },  | 
 | 112 | +    tx_index: {  | 
 | 113 | +      type: 'smallint',  | 
 | 114 | +      notNull: true,  | 
 | 115 | +    },  | 
 | 116 | +    canonical: {  | 
 | 117 | +      type: 'boolean',  | 
 | 118 | +      notNull: true,  | 
 | 119 | +    },  | 
 | 120 | +    microblock_canonical: {  | 
 | 121 | +      type: 'boolean',  | 
 | 122 | +      notNull: true,  | 
 | 123 | +    },  | 
 | 124 | +  });  | 
 | 125 | +  pgm.createIndex('principal_txs', 'tx_id');  | 
 | 126 | +  pgm.createIndex(  | 
 | 127 | +    'principal_txs',  | 
 | 128 | +    [  | 
 | 129 | +      { name: 'principal' },  | 
 | 130 | +      { name: 'block_height', sort: 'DESC' },  | 
 | 131 | +      { name: 'microblock_sequence', sort: 'DESC' },  | 
 | 132 | +      { name: 'tx_index', sort: 'DESC' },  | 
 | 133 | +    ],  | 
 | 134 | +    {  | 
 | 135 | +      where: 'canonical = TRUE AND microblock_canonical = TRUE',  | 
 | 136 | +    }  | 
 | 137 | +  );  | 
 | 138 | +  pgm.addConstraint(  | 
 | 139 | +    'principal_txs',  | 
 | 140 | +    'unique_principal_tx_id_index_block_hash_microblock_hash',  | 
 | 141 | +    `UNIQUE(principal, tx_id, index_block_hash, microblock_hash)`  | 
 | 142 | +  );  | 
 | 143 | +};  | 
 | 144 | + | 
 | 145 | +exports.down = pgm => {  | 
 | 146 | +  pgm.dropTable('principal_txs');  | 
 | 147 | +  pgm.createTable('principal_stx_txs', {  | 
 | 148 | +    id: {  | 
 | 149 | +      type: 'serial',  | 
 | 150 | +      primaryKey: true,  | 
 | 151 | +    },  | 
 | 152 | +    principal: {  | 
 | 153 | +      type: 'string',  | 
 | 154 | +      notNull: true,  | 
 | 155 | +    },  | 
 | 156 | +    tx_id: {  | 
 | 157 | +      type: 'bytea',  | 
 | 158 | +      notNull: true,  | 
 | 159 | +    },  | 
 | 160 | +    block_height: {  | 
 | 161 | +      type: 'integer',  | 
 | 162 | +      notNull: true,  | 
 | 163 | +    },  | 
 | 164 | +    index_block_hash: {  | 
 | 165 | +      type: 'bytea',  | 
 | 166 | +      notNull: true,  | 
 | 167 | +    },  | 
 | 168 | +    microblock_hash: {  | 
 | 169 | +      type: 'bytea',  | 
 | 170 | +      notNull: true,  | 
 | 171 | +    },  | 
 | 172 | +    microblock_sequence: {  | 
 | 173 | +      type: 'integer',  | 
 | 174 | +      notNull: true,  | 
 | 175 | +    },  | 
 | 176 | +    tx_index: {  | 
 | 177 | +      type: 'smallint',  | 
 | 178 | +      notNull: true,  | 
 | 179 | +    },  | 
 | 180 | +    canonical: {  | 
 | 181 | +      type: 'boolean',  | 
 | 182 | +      notNull: true,  | 
 | 183 | +    },  | 
 | 184 | +    microblock_canonical: {  | 
 | 185 | +      type: 'boolean',  | 
 | 186 | +      notNull: true,  | 
 | 187 | +    },  | 
 | 188 | +  });  | 
 | 189 | +  pgm.createIndex('principal_stx_txs', 'tx_id');  | 
 | 190 | +  pgm.createIndex(  | 
 | 191 | +    'principal_stx_txs',  | 
 | 192 | +    [  | 
 | 193 | +      { name: 'principal' },  | 
 | 194 | +      { name: 'block_height', sort: 'DESC' },  | 
 | 195 | +      { name: 'microblock_sequence', sort: 'DESC' },  | 
 | 196 | +      { name: 'tx_index', sort: 'DESC' },  | 
 | 197 | +    ],  | 
 | 198 | +    {  | 
 | 199 | +      where: 'canonical = TRUE AND microblock_canonical = TRUE',  | 
 | 200 | +    }  | 
 | 201 | +  );  | 
 | 202 | +  pgm.addConstraint(  | 
 | 203 | +    'principal_stx_txs',  | 
 | 204 | +    'unique_principal_tx_id_index_block_hash_microblock_hash',  | 
 | 205 | +    `UNIQUE(principal, tx_id, index_block_hash, microblock_hash)`  | 
 | 206 | +  );  | 
 | 207 | +  pgm.dropColumn('txs', 'stx_transfer_event_count');  | 
 | 208 | +  pgm.dropColumn('txs', 'stx_mint_event_count');  | 
 | 209 | +  pgm.dropColumn('txs', 'stx_burn_event_count');  | 
 | 210 | +  pgm.dropColumn('txs', 'stx_lock_event_count');  | 
 | 211 | +  pgm.dropColumn('txs', 'ft_transfer_event_count');  | 
 | 212 | +  pgm.dropColumn('txs', 'ft_mint_event_count');  | 
 | 213 | +  pgm.dropColumn('txs', 'ft_burn_event_count');  | 
 | 214 | +  pgm.dropColumn('txs', 'nft_transfer_event_count');  | 
 | 215 | +  pgm.dropColumn('txs', 'nft_mint_event_count');  | 
 | 216 | +  pgm.dropColumn('txs', 'nft_burn_event_count');  | 
 | 217 | +  pgm.dropColumn('txs', 'contract_log_event_count');  | 
 | 218 | +};  | 
0 commit comments