|
| 1 | +const { PgLiteral } = require('node-pg-migrate'); |
| 2 | + |
| 3 | +exports.up = (pgm) => { |
| 4 | + // ----- ADD UUID EXTENSION |
| 5 | + pgm.createExtension('uuid-ossp', { |
| 6 | + ifNotExists: true, |
| 7 | + }); |
| 8 | + // ----- CREATE TABLES |
| 9 | + // --- config |
| 10 | + // table with static/semi-static information and consists of 1 row per unique pool. |
| 11 | + // operations on this table: insert for new pools, update for existing pools |
| 12 | + pgm.createTable('config', { |
| 13 | + config_id: { |
| 14 | + type: 'uuid', // uuid is created in the application |
| 15 | + primaryKey: true, |
| 16 | + }, |
| 17 | + updated_at: { |
| 18 | + type: 'timestamptz', |
| 19 | + notNull: true, |
| 20 | + default: pgm.func('current_timestamp'), |
| 21 | + }, |
| 22 | + pool: { type: 'text', notNull: true, unique: true }, |
| 23 | + project: { type: 'text', notNull: true }, |
| 24 | + chain: { type: 'text', notNull: true }, |
| 25 | + symbol: { type: 'text', notNull: true }, |
| 26 | + poolMeta: 'text', |
| 27 | + underlyingTokens: { type: 'text[]' }, |
| 28 | + rewardTokens: { type: 'text[]' }, |
| 29 | + url: { type: 'text', notNull: true }, |
| 30 | + }); |
| 31 | + |
| 32 | + // --- yield |
| 33 | + // our timeseries table. insert only on hourly granularity |
| 34 | + pgm.createTable('yield', { |
| 35 | + yield_id: { |
| 36 | + type: 'uuid', |
| 37 | + default: new PgLiteral('uuid_generate_v4()'), |
| 38 | + primaryKey: true, |
| 39 | + }, |
| 40 | + // configID is a FK in this table and references the PK (config_id) in config |
| 41 | + configID: { |
| 42 | + type: 'uuid', |
| 43 | + notNull: true, |
| 44 | + references: '"config"', |
| 45 | + onDelete: 'cascade', |
| 46 | + }, |
| 47 | + timestamp: { |
| 48 | + type: 'timestamptz', |
| 49 | + notNull: true, |
| 50 | + }, |
| 51 | + tvlUsd: { type: 'bigint', notNull: true }, |
| 52 | + apy: { type: 'numeric', notNull: true }, |
| 53 | + apyBase: 'numeric', |
| 54 | + apyReward: 'numeric', |
| 55 | + }); |
| 56 | + |
| 57 | + // --- stat |
| 58 | + // table which contains rolling statistics required to calculate ML features values |
| 59 | + // and other things we use for plotting on the /overview page |
| 60 | + pgm.createTable('stat', { |
| 61 | + stat_id: { |
| 62 | + type: 'uuid', |
| 63 | + default: new PgLiteral('uuid_generate_v4()'), |
| 64 | + primaryKey: true, |
| 65 | + }, |
| 66 | + // configID is a FK in this table and references the PK (config_id) in config |
| 67 | + configID: { |
| 68 | + type: 'uuid', |
| 69 | + notNull: true, |
| 70 | + references: '"config"', |
| 71 | + unique: true, |
| 72 | + onDelete: 'cascade', |
| 73 | + }, |
| 74 | + updated_at: { |
| 75 | + type: 'timestamptz', |
| 76 | + notNull: true, |
| 77 | + default: pgm.func('current_timestamp'), |
| 78 | + }, |
| 79 | + count: { type: 'smallint', notNull: true }, |
| 80 | + meanAPY: { type: 'numeric', notNull: true }, |
| 81 | + mean2APY: { type: 'numeric', notNull: true }, |
| 82 | + meanDR: { type: 'numeric', notNull: true }, |
| 83 | + mean2DR: { type: 'numeric', notNull: true }, |
| 84 | + productDR: { type: 'numeric', notNull: true }, |
| 85 | + }); |
| 86 | + |
| 87 | + // --- median |
| 88 | + // median table content is used for the median chart on /overview (append only) |
| 89 | + pgm.createTable('median', { |
| 90 | + median_id: { |
| 91 | + type: 'uuid', |
| 92 | + default: new PgLiteral('uuid_generate_v4()'), |
| 93 | + primaryKey: true, |
| 94 | + }, |
| 95 | + timestamp: { |
| 96 | + type: 'timestamptz', |
| 97 | + notNull: true, |
| 98 | + unique: true, |
| 99 | + }, |
| 100 | + uniquePools: { type: 'integer', notNull: true }, |
| 101 | + medianAPY: { type: 'numeric', notNull: true }, |
| 102 | + }); |
| 103 | + |
| 104 | + // ----- FUNCTION |
| 105 | + // for creating the updated_at timestamp field |
| 106 | + pgm.createFunction( |
| 107 | + 'update_updated_at', |
| 108 | + [], // no params |
| 109 | + // options |
| 110 | + { |
| 111 | + language: 'plpgsql', |
| 112 | + returns: 'TRIGGER', |
| 113 | + replace: true, |
| 114 | + }, |
| 115 | + // function body |
| 116 | + ` |
| 117 | + BEGIN |
| 118 | + NEW.updated_at = now(); |
| 119 | + RETURN NEW; |
| 120 | + END |
| 121 | + ` |
| 122 | + ); |
| 123 | + |
| 124 | + // ----- TRIGGERS; |
| 125 | + // to trigger the defined function |
| 126 | + pgm.createTrigger('config', 'update_updated_at', { |
| 127 | + when: 'BEFORE', |
| 128 | + operation: 'UPDATE', |
| 129 | + function: 'update_updated_at', |
| 130 | + level: 'ROW', |
| 131 | + }); |
| 132 | + pgm.createTrigger('stat', 'update_updated_at', { |
| 133 | + when: 'BEFORE', |
| 134 | + operation: 'UPDATE', |
| 135 | + function: 'update_updated_at', |
| 136 | + level: 'ROW', |
| 137 | + }); |
| 138 | +}; |
0 commit comments