|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/** @type {import('sequelize-cli').Migration} */ |
| 4 | +module.exports = { |
| 5 | + async up (queryInterface, Sequelize) { |
| 6 | + const transaction = await queryInterface.sequelize.transaction(); |
| 7 | + try { |
| 8 | + await queryInterface.createTable('transaction_events', { |
| 9 | + workspaceId: { |
| 10 | + type: Sequelize.INTEGER, |
| 11 | + allowNull: false, |
| 12 | + references: { |
| 13 | + key: 'id', |
| 14 | + model: { |
| 15 | + tableName: 'workspaces' |
| 16 | + } |
| 17 | + } |
| 18 | + }, |
| 19 | + transactionId: { |
| 20 | + primaryKey: true, |
| 21 | + type: Sequelize.INTEGER, |
| 22 | + allowNull: false, |
| 23 | + references: { |
| 24 | + key: 'id', |
| 25 | + model: { |
| 26 | + tableName: 'transactions' |
| 27 | + } |
| 28 | + } |
| 29 | + }, |
| 30 | + blockNumber : { |
| 31 | + type: Sequelize.INTEGER, |
| 32 | + allowNull: false, |
| 33 | + }, |
| 34 | + timestamp: { |
| 35 | + primaryKey: true, |
| 36 | + type: 'TIMESTAMPTZ', |
| 37 | + allowNull: false, |
| 38 | + }, |
| 39 | + transactionFee: { |
| 40 | + type: 'NUMERIC', |
| 41 | + allowNull: false |
| 42 | + }, |
| 43 | + gasPrice: { |
| 44 | + type: 'NUMERIC', |
| 45 | + allowNull: false |
| 46 | + }, |
| 47 | + gasUsed: { |
| 48 | + type: 'NUMERIC', |
| 49 | + allowNull: false |
| 50 | + }, |
| 51 | + from: { |
| 52 | + type: 'VARCHAR(42)', |
| 53 | + allowNull: false |
| 54 | + }, |
| 55 | + to: { |
| 56 | + type: 'VARCHAR(42)', |
| 57 | + allowNull: true |
| 58 | + } |
| 59 | + }, { transaction }); |
| 60 | + |
| 61 | + await queryInterface.sequelize.query(`SELECT create_hypertable('transaction_events', 'timestamp');`, { transaction }); |
| 62 | + await queryInterface.sequelize.query(` |
| 63 | + CREATE INDEX "transaction_events_workspaceId_timestamp" ON transaction_events("workspaceId", timestamp DESC); |
| 64 | + `, { transaction }); |
| 65 | + |
| 66 | + await queryInterface.createTable('token_transfer_events', { |
| 67 | + workspaceId: { |
| 68 | + type: Sequelize.INTEGER, |
| 69 | + allowNull: false, |
| 70 | + references: { |
| 71 | + key: 'id', |
| 72 | + model: { |
| 73 | + tableName: 'workspaces' |
| 74 | + } |
| 75 | + } |
| 76 | + }, |
| 77 | + tokenTransferId: { |
| 78 | + primaryKey: true, |
| 79 | + type: Sequelize.INTEGER, |
| 80 | + allowNull: false, |
| 81 | + references: { |
| 82 | + key: 'id', |
| 83 | + model: { |
| 84 | + tableName: 'token_transfers' |
| 85 | + } |
| 86 | + } |
| 87 | + }, |
| 88 | + blockNumber : { |
| 89 | + type: Sequelize.INTEGER, |
| 90 | + allowNull: false, |
| 91 | + }, |
| 92 | + timestamp: { |
| 93 | + primaryKey: true, |
| 94 | + type: 'TIMESTAMPTZ', |
| 95 | + allowNull: false, |
| 96 | + }, |
| 97 | + amount: { |
| 98 | + type: 'NUMERIC', |
| 99 | + allowNull: false |
| 100 | + }, |
| 101 | + token: { |
| 102 | + type: 'VARCHAR(42)', |
| 103 | + allowNull: false |
| 104 | + }, |
| 105 | + tokenType: { |
| 106 | + type: Sequelize.STRING, |
| 107 | + allowNull: true |
| 108 | + }, |
| 109 | + src: { |
| 110 | + type: 'VARCHAR(42)', |
| 111 | + allowNull: false |
| 112 | + }, |
| 113 | + dst: { |
| 114 | + type: 'VARCHAR(42)', |
| 115 | + allowNull: false |
| 116 | + } |
| 117 | + }, { transaction }); |
| 118 | + await queryInterface.sequelize.query(`SELECT create_hypertable('token_transfer_events', 'timestamp');`, { transaction }); |
| 119 | + await queryInterface.sequelize.query(` |
| 120 | + CREATE INDEX "token_transfer_events_workspaceId_timestamp" ON transaction_events("workspaceId", timestamp DESC); |
| 121 | + `, { transaction }); |
| 122 | + |
| 123 | + await queryInterface.createTable('token_balance_change_events', { |
| 124 | + workspaceId: { |
| 125 | + type: Sequelize.INTEGER, |
| 126 | + allowNull: false, |
| 127 | + references: { |
| 128 | + key: 'id', |
| 129 | + model: { |
| 130 | + tableName: 'workspaces' |
| 131 | + } |
| 132 | + } |
| 133 | + }, |
| 134 | + tokenBalanceChangeId: { |
| 135 | + primaryKey: true, |
| 136 | + type: Sequelize.INTEGER, |
| 137 | + allowNull: false, |
| 138 | + references: { |
| 139 | + key: 'id', |
| 140 | + model: { |
| 141 | + tableName: 'token_balance_changes' |
| 142 | + } |
| 143 | + } |
| 144 | + }, |
| 145 | + blockNumber : { |
| 146 | + type: Sequelize.INTEGER, |
| 147 | + allowNull: false, |
| 148 | + }, |
| 149 | + timestamp: { |
| 150 | + primaryKey: true, |
| 151 | + type: 'TIMESTAMPTZ', |
| 152 | + allowNull: false, |
| 153 | + }, |
| 154 | + token: { |
| 155 | + type: 'VARCHAR(42)', |
| 156 | + allowNull: false |
| 157 | + }, |
| 158 | + address: { |
| 159 | + type: 'VARCHAR(42)', |
| 160 | + allowNull: false |
| 161 | + }, |
| 162 | + currentBalance: { |
| 163 | + type: 'NUMERIC', |
| 164 | + allowNull: false |
| 165 | + }, |
| 166 | + tokenType: { |
| 167 | + type: Sequelize.STRING, |
| 168 | + allowNull: true |
| 169 | + }, |
| 170 | + }, { transaction }); |
| 171 | + await queryInterface.sequelize.query(`SELECT create_hypertable('token_balance_change_events', 'timestamp');`, { transaction }); |
| 172 | + await queryInterface.sequelize.query(` |
| 173 | + CREATE INDEX "token_balance_change_events_workspaceId_timestamp" ON token_balance_change_events("workspaceId", timestamp DESC); |
| 174 | + `, { transaction }); |
| 175 | + |
| 176 | + await transaction.commit(); |
| 177 | + } catch(error) { |
| 178 | + console.log(error); |
| 179 | + await transaction.rollback(); |
| 180 | + throw error; |
| 181 | + } |
| 182 | + }, |
| 183 | + |
| 184 | + async down (queryInterface, Sequelize) { |
| 185 | + const transaction = await queryInterface.sequelize.transaction(); |
| 186 | + try { |
| 187 | + await queryInterface.dropTable('transaction_events', { transaction }); |
| 188 | + await queryInterface.dropTable('token_transfer_events', { transaction }); |
| 189 | + await queryInterface.dropTable('token_balance_change_events', { transaction }); |
| 190 | + |
| 191 | + await transaction.commit(); |
| 192 | + } catch(error) { |
| 193 | + console.log(error); |
| 194 | + await transaction.rollback(); |
| 195 | + throw error; |
| 196 | + } |
| 197 | + } |
| 198 | +}; |
0 commit comments