code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/migrations/0044_volume_discount_programs.sql (about)

     1  -- +goose Up
     2  
     3  -- create a new volume discount record when a new volume discount program is created,
     4  -- updated, or ended so that we keep an audit trail, just in case.
     5  -- We create it as a hypertable and set a retention policy to make sure
     6  -- old and redundant data is removed in due course.
     7  create table if not exists volume_discount_programs
     8  (
     9      id                       bytea                    not null,
    10      version                  int                      not null,
    11      benefit_tiers            jsonb,
    12      end_of_program_timestamp timestamp with time zone not null,
    13      window_length            int                      not null,
    14      vega_time                timestamp with time zone not null,
    15      ended_at                 timestamp with time zone,
    16      seq_num                  bigint                   not null,
    17      primary key (vega_time, seq_num)
    18  );
    19  
    20  select create_hypertable('volume_discount_programs', 'vega_time', chunk_time_interval => INTERVAL '1 day');
    21  
    22  -- simplify volume discount retrieval using a view that provides the latest volume discount information.
    23  create view current_volume_discount_program as
    24  (
    25  select *
    26  from volume_discount_programs
    27  order by vega_time desc, seq_num desc
    28  limit 1 -- there should only be 1 volume discount program running at any time, so just get the last record.
    29      );
    30  
    31  create table volume_discount_stats
    32  (
    33      at_epoch                      bigint                   not null,
    34      parties_volume_discount_stats jsonb                    not null,
    35      vega_time                     timestamp with time zone not null,
    36      primary key (at_epoch, vega_time)
    37  );
    38  
    39  select create_hypertable('volume_discount_stats', 'vega_time', chunk_time_interval => INTERVAL '1 day');
    40  
    41  -- +goose Down
    42  
    43  drop table if exists volume_discount_stats;
    44  drop view if exists current_volume_discount_program;
    45  drop table if exists volume_discount_programs;