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

     1  -- +goose Up
     2  
     3  -- first drop the existing primary key
     4  alter table referral_programs drop constraint referral_programs_pkey;
     5  
     6  -- add the sequence number column to the table
     7  -- we can just set the default to 0 for any existing rows as previously it would only support
     8  -- one row per vega_time, but there is an edge case where it is possible for the program to start
     9  -- and end in the same block if it has been set up incorrectly, so we need to support this edge case
    10  -- and not fail
    11  alter table referral_programs add column if not exists seq_num bigint not null default 0;
    12  
    13  -- now add the new primary key to the table
    14  alter table referral_programs add constraint referral_programs_pkey primary key (vega_time, seq_num);
    15  
    16  -- update the current_referral_program view to correctly display the most recent referral program update
    17  
    18  -- make sure that it doesn't exist in case migrations have failed previously
    19  drop view if exists current_referral_program;
    20  
    21  -- simplify referral retrieval using a view that provides the latest referral information.
    22  create view current_referral_program as (
    23      select *
    24      from referral_programs
    25      order by vega_time desc, seq_num desc
    26      limit 1 -- there should only be 1 referral program running at any time, so just get the last record.
    27  );
    28  
    29  -- +goose Down
    30  
    31  -- make sure that it doesn't exist in case migrations have failed previously
    32  drop view if exists current_referral_program;
    33  
    34  alter table referral_programs drop constraint referral_programs_pkey;
    35  
    36  alter table referral_programs drop column seq_num;
    37  
    38  -- simplify referral retrieval using a view that provides the latest referral information.
    39  create view current_referral_program as (
    40      select *
    41      from referral_programs
    42      order by vega_time desc limit 1 -- there should only be 1 referral program running at any time, so just get the last record.
    43  );
    44  
    45  alter table referral_programs add constraint referral_programs_pkey primary key (vega_time);