code.vegaprotocol.io/vega@v0.79.0/blockexplorer/store/migrations/0005_add_block_height_to_tx_result.sql (about)

     1  -- +goose Up
     2  
     3  -- make the block height nullable so tendermint can still insert and the
     4  -- trigger takes over to set the value.
     5  ALTER TABLE tx_results
     6    ADD COLUMN IF NOT EXISTS block_height BIGINT DEFAULT 0;
     7  
     8  -- First drop any foreign key constraints that depend on the tx_results table
     9  -- This will be restored after all the data has been migrated to the new tx_results table
    10  ALTER TABLE events DROP constraint events_tx_id_fkey;
    11  
    12  -- Rename the tx_results table to tx_results_old
    13  ALTER TABLE IF EXISTS tx_results RENAME TO tx_results_old;
    14  ALTER INDEX IF EXISTS tx_results_tx_hash_index RENAME TO tx_results_old_tx_hash_index;
    15  ALTER INDEX IF EXISTS tx_results_submitter_block_id_index_idx RENAME TO tx_results_old_submitter_block_id_index_idx;
    16  ALTER INDEX IF EXISTS tx_results_cmd_type_block_id_index RENAME TO tx_results_old_cmd_type_block_id_index;
    17  ALTER INDEX IF EXISTS tx_results_cmd_type_index RENAME TO tx_results_old_cmd_type_index;
    18  
    19  -- We need to make sure the next value in the rowid serial for the new tx_results table
    20  -- continues where the old one leaves off otherwise we will break foreign key constraints
    21  -- in the events table which we have had to drop temporarily and will restore once all the
    22  -- data has been migrated.
    23  -- +goose StatementBegin
    24  do $$
    25      declare
    26          tx_results_seq_name text;
    27          tx_results_seq_next bigint;
    28      begin
    29          -- get the next value of the sequence for tx_results_old
    30          -- we will use this to reset the sequence value for the new tx_results table
    31          select nextval(pg_get_serial_sequence('tx_results_old', 'rowid'))
    32          into tx_results_seq_next;
    33  
    34          -- Create a new tx_results table with all the necessary fields
    35          CREATE TABLE tx_results (
    36              rowid BIGSERIAL PRIMARY KEY,
    37              -- The block to which this transaction belongs.
    38              block_id BIGINT NOT NULL REFERENCES blocks(rowid),
    39              -- The sequential index of the transaction within the block.
    40              index INTEGER NOT NULL,
    41              -- When this result record was logged into the sink, in UTC.
    42              created_at TIMESTAMPTZ NOT NULL,
    43              -- The hex-encoded hash of the transaction.
    44              tx_hash VARCHAR NOT NULL,
    45              -- The protobuf wire encoding of the TxResult message.
    46              tx_result BYTEA NOT NULL,
    47              submitter TEXT,
    48              cmd_type TEXT,
    49              block_height BIGINT DEFAULT 0,
    50              UNIQUE (block_id, index)
    51          );
    52  
    53          CREATE INDEX tx_results_tx_hash_index ON tx_results(tx_hash);
    54          CREATE INDEX tx_results_submitter_block_id_index_idx ON tx_results(submitter, block_id, index);
    55          CREATE INDEX tx_results_cmd_type_block_id_index ON tx_results
    56              USING btree (cmd_type, block_id, index);
    57          CREATE INDEX tx_results_submitter_block_height_index_idx ON tx_results(submitter, block_height, index);
    58          CREATE INDEX tx_results_cmd_type_block_height_index ON tx_results
    59              USING btree (cmd_type, block_height, index);
    60          CREATE INDEX tx_results_cmd_type_index ON tx_results(cmd_type, submitter);
    61          CREATE INDEX tx_results_block_height_index_idx ON tx_results(block_height, index);
    62  
    63          -- get the sequence name for the new tx_results serial
    64          select pg_get_serial_sequence('tx_results', 'rowid')
    65          into tx_results_seq_name;
    66  
    67          -- restart the sequence with the current value of the sequence for tx_results_old
    68          -- when nextval is called, we should get the restart value, which is the next value
    69          -- in the sequence for tx_results_old
    70          execute format('alter sequence %s restart with %s', tx_results_seq_name, tx_results_seq_next);
    71      end;
    72  $$;
    73  -- +goose StatementEnd
    74  
    75  -- Recreate views, functions and triggers associated with the original tx_results table
    76  CREATE OR REPLACE VIEW tx_events AS
    77  SELECT height, index, chain_id, type, key, composite_key, value, tx_results.created_at
    78  FROM blocks JOIN tx_results ON (blocks.rowid = tx_results.block_id)
    79      JOIN event_attributes ON (tx_results.rowid = event_attributes.tx_id)
    80  WHERE event_attributes.tx_id IS NOT NULL;
    81  
    82  -- +goose StatementBegin
    83  CREATE OR REPLACE FUNCTION update_txresult_submitter()
    84      RETURNS TRIGGER
    85      LANGUAGE PLPGSQL AS
    86  $$
    87  BEGIN
    88  UPDATE tx_results SET submitter=NEW.value
    89      FROM events e
    90  WHERE e.rowid = NEW.event_id
    91    AND tx_results.rowid = e.tx_id;
    92  RETURN NEW;
    93  END;
    94  $$;
    95  -- +goose StatementEnd
    96  
    97  DROP TRIGGER IF EXISTS update_txresult_submitter ON attributes;
    98  
    99  CREATE TRIGGER update_txresult_submitter AFTER INSERT ON attributes
   100      FOR EACH ROW
   101      WHEN (NEW.composite_key='tx.submitter')
   102      EXECUTE function update_txresult_submitter();
   103  
   104  -- +goose StatementBegin
   105  CREATE OR REPLACE FUNCTION update_txresult_cmd_type()
   106      RETURNS TRIGGER
   107      LANGUAGE PLPGSQL AS
   108  $$
   109  BEGIN
   110      UPDATE tx_results SET cmd_type=NEW.value
   111      FROM events e
   112      WHERE e.rowid = NEW.event_id
   113      AND tx_results.rowid = e.tx_id;
   114  
   115      RETURN NEW;
   116  END;
   117  $$;
   118  -- +goose StatementEnd
   119  
   120  DROP TRIGGER IF EXISTS update_txresult_cmd_type ON attributes;
   121  
   122  CREATE TRIGGER update_txresult_cmd_type AFTER INSERT ON attributes
   123      FOR EACH ROW
   124      WHEN (NEW.composite_key='command.type')
   125      EXECUTE function update_txresult_cmd_type();
   126  
   127  -- +goose StatementBegin
   128  CREATE OR REPLACE FUNCTION add_block_height_to_tx_results()
   129      RETURNS TRIGGER
   130      LANGUAGE plpgsql AS
   131  $$
   132  BEGIN
   133      UPDATE tx_results
   134      SET block_height=b.height
   135      FROM blocks b
   136      WHERE b.rowid = NEW.block_id
   137      AND tx_results.rowid = NEW.rowid;
   138  
   139      RETURN NEW;
   140  END;
   141  $$;
   142  -- +goose StatementEnd
   143  
   144  CREATE TRIGGER add_block_height_to_tx_results
   145      AFTER INSERT
   146      ON tx_results
   147      FOR EACH ROW
   148      EXECUTE PROCEDURE add_block_height_to_tx_results();
   149  
   150  -- +goose Down
   151  
   152  -- we don't want to do anything to and leave things as they are for this migration.