|
| 1 | +/** @param { import("node-pg-migrate").MigrationBuilder } pgm */ |
| 2 | +exports.up = pgm => { |
| 3 | + // Adds pox4_events table which matches previous pox2_events table |
| 4 | + pgm.createTable('pox4_events', { |
| 5 | + id: { |
| 6 | + type: 'bigserial', |
| 7 | + primaryKey: true, |
| 8 | + }, |
| 9 | + event_index: { |
| 10 | + type: 'integer', |
| 11 | + notNull: true, |
| 12 | + }, |
| 13 | + tx_id: { |
| 14 | + notNull: true, |
| 15 | + type: 'bytea', |
| 16 | + }, |
| 17 | + tx_index: { |
| 18 | + type: 'smallint', |
| 19 | + notNull: true, |
| 20 | + }, |
| 21 | + block_height: { |
| 22 | + type: 'integer', |
| 23 | + notNull: true, |
| 24 | + }, |
| 25 | + index_block_hash: { |
| 26 | + type: 'bytea', |
| 27 | + notNull: true, |
| 28 | + }, |
| 29 | + parent_index_block_hash: { |
| 30 | + type: 'bytea', |
| 31 | + notNull: true, |
| 32 | + }, |
| 33 | + microblock_hash: { |
| 34 | + type: 'bytea', |
| 35 | + notNull: true, |
| 36 | + }, |
| 37 | + microblock_sequence: { |
| 38 | + type: 'integer', |
| 39 | + notNull: true, |
| 40 | + }, |
| 41 | + microblock_canonical: { |
| 42 | + type: 'boolean', |
| 43 | + notNull: true, |
| 44 | + }, |
| 45 | + canonical: { |
| 46 | + type: 'boolean', |
| 47 | + notNull: true, |
| 48 | + }, |
| 49 | + stacker: { |
| 50 | + type: 'string', |
| 51 | + notNull: true, |
| 52 | + }, |
| 53 | + locked: { |
| 54 | + type: 'numeric', |
| 55 | + notNull: true, |
| 56 | + }, |
| 57 | + balance: { |
| 58 | + type: 'numeric', |
| 59 | + notNull: true, |
| 60 | + }, |
| 61 | + burnchain_unlock_height: { |
| 62 | + type: 'bigint', |
| 63 | + notNull: true, |
| 64 | + }, |
| 65 | + name: { |
| 66 | + type: 'string', |
| 67 | + notNull: true, |
| 68 | + }, |
| 69 | + pox_addr: { |
| 70 | + type: 'string', |
| 71 | + }, |
| 72 | + pox_addr_raw: { |
| 73 | + type: 'bytea', |
| 74 | + }, |
| 75 | + first_cycle_locked: { |
| 76 | + // unique to handle-unlock |
| 77 | + type: 'numeric', |
| 78 | + }, |
| 79 | + first_unlocked_cycle: { |
| 80 | + // unique to handle-unlock |
| 81 | + type: 'numeric', |
| 82 | + }, |
| 83 | + delegate_to: { |
| 84 | + // unique to delegate-stx |
| 85 | + type: 'string', |
| 86 | + }, |
| 87 | + lock_period: { |
| 88 | + // unique to stack-stx, delegate-stack-stx |
| 89 | + type: 'numeric', |
| 90 | + }, |
| 91 | + lock_amount: { |
| 92 | + // unique to stack-stx, delegate-stack-stx |
| 93 | + type: 'numeric', |
| 94 | + }, |
| 95 | + start_burn_height: { |
| 96 | + // unique to stack-stx, delegate-stack-stx |
| 97 | + type: 'numeric', |
| 98 | + }, |
| 99 | + unlock_burn_height: { |
| 100 | + // unique to stack-stx, stack-extend, delegate-stack-stx, delegate-stack-extend, delegate-stx |
| 101 | + type: 'numeric', |
| 102 | + }, |
| 103 | + delegator: { |
| 104 | + // unique to delegate-stack-stx, delegate-stack-increase, delegate-stack-extend |
| 105 | + type: 'string', |
| 106 | + }, |
| 107 | + increase_by: { |
| 108 | + // unique to stack-increase, delegate-stack-increase |
| 109 | + type: 'numeric', |
| 110 | + }, |
| 111 | + total_locked: { |
| 112 | + // unique to stack-increase, delegate-stack-increase |
| 113 | + type: 'numeric', |
| 114 | + }, |
| 115 | + extend_count: { |
| 116 | + // unique to stack-extend, delegate-stack-extend |
| 117 | + type: 'numeric', |
| 118 | + }, |
| 119 | + reward_cycle: { |
| 120 | + // unique to stack-aggregation-* |
| 121 | + type: 'numeric', |
| 122 | + }, |
| 123 | + amount_ustx: { |
| 124 | + // unique to stack-aggregation-*, delegate-stx |
| 125 | + type: 'numeric', |
| 126 | + }, |
| 127 | + }); |
| 128 | + |
| 129 | + pgm.addConstraint( |
| 130 | + 'pox4_events', |
| 131 | + 'valid_event_specific_columns', |
| 132 | + `CHECK ( |
| 133 | + CASE name |
| 134 | + WHEN 'handle-unlock' THEN |
| 135 | + first_cycle_locked IS NOT NULL AND |
| 136 | + first_unlocked_cycle IS NOT NULL |
| 137 | + WHEN 'stack-stx' THEN |
| 138 | + lock_period IS NOT NULL AND |
| 139 | + lock_amount IS NOT NULL AND |
| 140 | + start_burn_height IS NOT NULL AND |
| 141 | + unlock_burn_height IS NOT NULL |
| 142 | + WHEN 'stack-increase' THEN |
| 143 | + increase_by IS NOT NULL AND |
| 144 | + total_locked IS NOT NULL |
| 145 | + WHEN 'stack-extend' THEN |
| 146 | + extend_count IS NOT NULL AND |
| 147 | + unlock_burn_height IS NOT NULL |
| 148 | + WHEN 'delegate-stx' THEN |
| 149 | + amount_ustx IS NOT NULL AND |
| 150 | + delegate_to IS NOT NULL |
| 151 | + WHEN 'delegate-stack-stx' THEN |
| 152 | + lock_period IS NOT NULL AND |
| 153 | + lock_amount IS NOT NULL AND |
| 154 | + start_burn_height IS NOT NULL AND |
| 155 | + unlock_burn_height IS NOT NULL AND |
| 156 | + delegator IS NOT NULL |
| 157 | + WHEN 'delegate-stack-increase' THEN |
| 158 | + increase_by IS NOT NULL AND |
| 159 | + total_locked IS NOT NULL AND |
| 160 | + delegator IS NOT NULL |
| 161 | + WHEN 'delegate-stack-extend' THEN |
| 162 | + extend_count IS NOT NULL AND |
| 163 | + unlock_burn_height IS NOT NULL AND |
| 164 | + delegator IS NOT NULL |
| 165 | + WHEN 'stack-aggregation-commit' THEN |
| 166 | + reward_cycle IS NOT NULL AND |
| 167 | + amount_ustx IS NOT NULL |
| 168 | + WHEN 'stack-aggregation-commit-indexed' THEN |
| 169 | + reward_cycle IS NOT NULL AND |
| 170 | + amount_ustx IS NOT NULL |
| 171 | + WHEN 'stack-aggregation-increase' THEN |
| 172 | + reward_cycle IS NOT NULL AND |
| 173 | + amount_ustx IS NOT NULL |
| 174 | + ELSE false |
| 175 | + END |
| 176 | + )` |
| 177 | + ); |
| 178 | + |
| 179 | + pgm.createIndex('pox4_events', [ |
| 180 | + { name: 'block_height', sort: 'DESC' }, |
| 181 | + { name: 'microblock_sequence', sort: 'DESC' }, |
| 182 | + { name: 'tx_index', sort: 'DESC' }, |
| 183 | + { name: 'event_index', sort: 'DESC' }, |
| 184 | + ]); |
| 185 | + |
| 186 | + pgm.createIndex('pox4_events', 'tx_id'); |
| 187 | + pgm.createIndex('pox4_events', 'index_block_hash'); |
| 188 | + pgm.createIndex('pox4_events', 'microblock_hash'); |
| 189 | + |
| 190 | + pgm.createIndex('pox4_events', 'stacker'); |
| 191 | + pgm.createIndex('pox4_events', 'burnchain_unlock_height'); |
| 192 | + pgm.createIndex('pox4_events', 'pox_addr'); |
| 193 | + pgm.createIndex('pox4_events', 'delegator'); |
| 194 | + pgm.createIndex('pox4_events', 'name'); |
| 195 | + |
| 196 | + pgm.createIndex('pox4_events', 'delegate_to'); |
| 197 | + pgm.createIndex('pox4_events', 'unlock_burn_height'); |
| 198 | +}; |
| 199 | + |
| 200 | +/** @param { import("node-pg-migrate").MigrationBuilder } pgm */ |
| 201 | +exports.down = pgm => { |
| 202 | + pgm.dropTable('pox4_events'); |
| 203 | +}; |
0 commit comments