Skip to content

Commit

Permalink
postgres migrations (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukanus authored Mar 24, 2023
1 parent ac93531 commit d6c4bd3
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cmd/migration/postgres/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var cf = flags{}

func init() {
flag.StringVar(&cf.databaseURL, "db", "", "Database URL")
flag.StringVar(&cf.migrationType, "migrations", "", `Type of migration (available: "validators")`)
flag.StringVar(&cf.migrationType, "migrations", "", `Type of migration (available: "validators","evidence")`)
flag.BoolVar(&cf.verbose, "verbose", true, "Verbosity of logs during run")
flag.UintVar(&cf.version, "version", 0, "Version parameter sets db changes to specified revision (up or down)")
flag.Parse()
Expand All @@ -53,7 +53,7 @@ func migrateDB(dburl string) error {
}
defer db.Close()

if cf.migrationType != "validators" { // it will have more
if cf.migrationType != "validators" && cf.migrationType != "evidence" { // it will have more
log.Fatal("migration type (`migrations`) is not properly set")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
DROP INDEX IF EXISTS payload_delivered_slot_idx;
DROP INDEX IF EXISTS payload_delivered_blockhash_idx;
DROP INDEX IF EXISTS payload_delivered_blocknumber_idx;
DROP INDEX IF EXISTS payload_delivered_proposerpubkey_idx;
DROP INDEX IF EXISTS payload_delivered_builderpubkey_idx;
DROP INDEX IF EXISTS payload_delivered_executionpayloadid_idx;
DROP INDEX IF EXISTS payload_delivered_value_idx;
DROP TABLE IF EXISTS payload_delivered;

DROP INDEX IF EXISTS builder_block_submission_slot_idx;
DROP INDEX IF EXISTS builder_block_submission_blockhash_idx;
DROP INDEX IF EXISTS builder_block_submission_blocknumber_idx;
DROP INDEX IF EXISTS builder_block_submission_builderpubkey_idx;
DROP INDEX IF EXISTS builder_block_submission_simsuccess_idx;
DROP INDEX IF EXISTS builder_block_submission_mostprofit_idx;
DROP INDEX IF EXISTS builder_block_submission_executionpayloadid_idx;
DROP TABLE IF EXISTS builder_block_submission;

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
CREATE TABLE IF NOT EXISTS builder_block_submission (
slot bigint NOT NULL,
parent_hash varchar(66) NOT NULL,
block_hash varchar(66) NOT NULL,

builder_pubkey varchar(98) NOT NULL,
proposer_pubkey varchar(98) NOT NULL,
proposer_fee_recipient varchar(42) NOT NULL,

gas_used bigint NOT NULL,
gas_limit bigint NOT NULL,

value NUMERIC(48, 0),

epoch bigint NOT NULL,
block_number bigint NOT NULL,
was_most_profitable boolean NOT NULL,
block_time timestamp ,

inserted_at timestamp NOT NULL default current_timestamp,

UNIQUE (slot, proposer_pubkey, block_hash)
);

CREATE INDEX IF NOT EXISTS builder_block_submission_slot_idx ON builder_block_submission("slot");
CREATE INDEX IF NOT EXISTS builder_block_submission_slts_idx ON builder_block_submission("slot", "block_time");
CREATE INDEX IF NOT EXISTS builder_block_submission_ts_idx ON builder_block_submission("block_time");
CREATE INDEX IF NOT EXISTS builder_block_submission_blockhash_idx ON builder_block_submission("block_hash");
CREATE INDEX IF NOT EXISTS builder_block_submission_blocknumber_idx ON builder_block_submission("block_number");
CREATE INDEX IF NOT EXISTS builder_block_submission_builderpubkey_idx ON builder_block_submission("builder_pubkey");


CREATE TABLE IF NOT EXISTS payload_delivered (
builder_pubkey varchar(98) NOT NULL,
proposer_pubkey varchar(98) NOT NULL,
proposer_fee_recipient varchar(42) NOT NULL,

epoch bigint NOT NULL,
slot bigint NOT NULL,


parent_hash varchar(66) NOT NULL,
block_hash varchar(66) NOT NULL,
block_number bigint NOT NULL,

gas_used bigint NOT NULL,
gas_limit bigint NOT NULL,

value NUMERIC(48, 0),

inserted_at timestamp NOT NULL default current_timestamp,

UNIQUE (slot, proposer_pubkey, block_hash)
);

CREATE INDEX IF NOT EXISTS payload_delivered_slot_idx ON payload_delivered("slot");
CREATE INDEX IF NOT EXISTS payload_delivered_slbh_idx ON payload_delivered("slot","inserted_at");
CREATE INDEX IF NOT EXISTS payload_delivered_blockhash_idx ON payload_delivered("block_hash");
CREATE INDEX IF NOT EXISTS payload_delivered_blocknumber_idx ON payload_delivered("block_number");
CREATE INDEX IF NOT EXISTS payload_delivered_proposerpubkey_idx ON payload_delivered("proposer_pubkey");
CREATE INDEX IF NOT EXISTS payload_delivered_builderpubkey_idx ON payload_delivered("builder_pubkey");
CREATE INDEX IF NOT EXISTS payload_delivered_value_idx ON payload_delivered("value");

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE payload_delivered DROP COLUMN IF EXISTS num_tx;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE payload_delivered ADD COLUMN IF NOT EXISTS num_tx integer NOT NULL DEFAULT 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE builder_block_submission DROP COLUMN IF EXISTS num_tx;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE builder_block_submission ADD COLUMN IF NOT EXISTS num_tx integer NOT NULL DEFAULT 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE payload_delivered DROP COLUMN IF EXISTS relay_id;
ALTER TABLE payload_delivered DROP CONSTRAINT IF EXISTS payload_delivered_relay_id_slot_proposer_pubkey_block_hash_key;
ALTER TABLE payload_delivered ADD UNIQUE(slot, proposer_pubkey, block_hash);

ALTER TABLE builder_block_submission DROP COLUMN IF EXISTS relay_id;
ALTER TABLE builder_block_submission DROP CONSTRAINT IF EXISTS builder_block_submission_relay_id_slot_proposer_pubkey_block_hash_key;
ALTER TABLE builder_block_submission ADD UNIQUE(slot, proposer_pubkey, block_hash);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ALTER TABLE payload_delivered ADD COLUMN IF NOT EXISTS relay_id smallint NOT NULL DEFAULT 0;
ALTER TABLE payload_delivered DROP CONSTRAINT IF EXISTS payload_delivered_slot_proposer_pubkey_block_hash_key;
ALTER TABLE payload_delivered ADD UNIQUE(relay_id, slot, proposer_pubkey, block_hash);

ALTER TABLE builder_block_submission ADD COLUMN IF NOT EXISTS relay_id smallint NOT NULL DEFAULT 0;
ALTER TABLE builder_block_submission DROP CONSTRAINT IF EXISTS builder_block_submission_slot_proposer_pubkey_block_hash_key;
ALTER TABLE builder_block_submission ADD UNIQUE(relay_id, slot, proposer_pubkey, block_hash);

0 comments on commit d6c4bd3

Please sign in to comment.