From 2b585eb1c3c3ecac5659f5f0a07066eadfcb511a Mon Sep 17 00:00:00 2001 From: Charles Billette Date: Thu, 14 Nov 2024 07:20:09 -0500 Subject: [PATCH] Refactor SQL constraints and fix DELETE statement. Rearrange import statements for consistency across files. Update foreign key constraints in SQL tables with `ON DELETE CASCADE` for better relational integrity. Remove redundant `CASCADE` from the DELETE statement in `HandleBlocksUndo`. --- cmd/honey-tracker/main.go | 3 +- data/psql.go | 5 +- data/sql.go | 146 +++++++++++++++++++++++++++++++++++++- 3 files changed, 147 insertions(+), 7 deletions(-) diff --git a/cmd/honey-tracker/main.go b/cmd/honey-tracker/main.go index 8efc7f0..c0b31ee 100644 --- a/cmd/honey-tracker/main.go +++ b/cmd/honey-tracker/main.go @@ -7,13 +7,12 @@ import ( "os/exec" "time" - "github.com/streamingfast/honey-tracker/web" - "github.com/spf13/cobra" "github.com/streamingfast/bstream" "github.com/streamingfast/cli/sflags" "github.com/streamingfast/honey-tracker/data" "github.com/streamingfast/honey-tracker/price" + "github.com/streamingfast/honey-tracker/web" "github.com/streamingfast/logging" sink "github.com/streamingfast/substreams-sink" "github.com/streamingfast/substreams/client" diff --git a/data/psql.go b/data/psql.go index 1904e3d..5a6a9b9 100644 --- a/data/psql.go +++ b/data/psql.go @@ -6,12 +6,11 @@ import ( "fmt" "time" - "go.uber.org/zap" - _ "github.com/lib/pq" pb "github.com/streamingfast/honey-tracker/data/pb/hivemapper/v1" sink "github.com/streamingfast/substreams-sink" pbsubstreams "github.com/streamingfast/substreams/pb/sf/substreams/v1" + "go.uber.org/zap" ) type PreparedStatement struct { @@ -243,7 +242,7 @@ func (p *Psql) HandleInitializedAccount(dbBlockID int64, initializedAccounts []* } func (p *Psql) HandleBlocksUndo(lastValidBlockNum uint64) error { - _, err := p.tx.Exec("DELETE CASCADE FROM solana_tokens.blocks WHERE num > $1", lastValidBlockNum) + _, err := p.tx.Exec("DELETE FROM solana_tokens.blocks WHERE num > $1", lastValidBlockNum) if err != nil { return fmt.Errorf("deleting block from %d: %w", lastValidBlockNum, err) } diff --git a/data/sql.go b/data/sql.go index 2010f90..5ef0e06 100644 --- a/data/sql.go +++ b/data/sql.go @@ -10,7 +10,6 @@ CREATE TABLE IF NOT EXISTS hivemapper.blocks ( id SERIAL PRIMARY KEY, number INTEGER NOT NULL, hash TEXT NOT NULL, - hash TEXT NOT NULL, timestamp TIMESTAMP NOT NULL ); CREATE TABLE IF NOT EXISTS hivemapper.prices ( @@ -33,7 +32,6 @@ CREATE TABLE IF NOT EXISTS hivemapper.derived_addresses ( CONSTRAINT fk_transaction FOREIGN KEY (transaction_id) REFERENCES hivemapper.transactions(id) ); - CREATE TABLE IF NOT EXISTS hivemapper.fleets ( id SERIAL PRIMARY KEY, transaction_id INTEGER NOT NULL, @@ -103,6 +101,7 @@ CREATE TABLE IF NOT EXISTS hivemapper.operational_payments ( mint_id INTEGER NOT NULL, CONSTRAINT fk_mint FOREIGN KEY (mint_id) REFERENCES hivemapper.mints(id) ); + CREATE TABLE IF NOT EXISTS hivemapper.reward_payments ( id SERIAL PRIMARY KEY, mint_id INTEGER NOT NULL, @@ -134,4 +133,147 @@ CREATE TABLE IF NOT EXISTS hivemapper.transfers ( CONSTRAINT fk_transaction FOREIGN KEY (transaction_id) REFERENCES hivemapper.transactions(id) ); +alter table hivemapper.transactions + drop constraint fk_block; + +alter table hivemapper.transactions + add foreign key (block_id) references hivemapper.blocks(id) + on delete cascade; + +alter table hivemapper.derived_addresses + drop constraint fk_transaction; + +alter table hivemapper.derived_addresses + add foreign key (transaction_id) references hivemapper.transactions(id) + on delete cascade; + +alter table hivemapper.fleets + drop constraint fk_transaction; + +alter table hivemapper.fleets + add foreign key (transaction_id) references hivemapper.transactions(id) + on delete cascade; + +alter table hivemapper.drivers + drop constraint fk_transaction; + +alter table hivemapper.drivers + add foreign key (transaction_id) references hivemapper.transactions(id) + on delete cascade; + +alter table hivemapper.fleet_drivers + drop constraint fk_fleet; + +alter table hivemapper.fleet_drivers + add foreign key (fleet_id) references hivemapper.fleets(id) + on delete cascade; + +alter table hivemapper.fleet_drivers + drop constraint fk_driver; + +alter table hivemapper.fleet_drivers + add foreign key (driver_id) references hivemapper.drivers(id) + on delete cascade; + +alter table hivemapper.fleet_drivers + drop constraint fk_transaction; + +alter table hivemapper.fleet_drivers + add foreign key (transaction_id) references hivemapper.transactions(id) + on delete cascade; + +alter table hivemapper.mints + drop constraint fk_transaction; + +alter table hivemapper.mints + add foreign key (transaction_id) references hivemapper.transactions(id) + on delete cascade; + + +alter table hivemapper.burns + drop constraint fk_transaction; + +alter table hivemapper.burns + add foreign key (transaction_id) references hivemapper.transactions(id) + on delete cascade; + + +alter table hivemapper.payments + drop constraint fk_mint; + +alter table hivemapper.payments + add foreign key (mint_id) references hivemapper.mints(id) + on delete cascade; + +alter table hivemapper.map_create + drop constraint fk_mint; + +alter table hivemapper.map_create + add foreign key (burn_id) references hivemapper.burns(id) + on delete cascade; + +alter table hivemapper.map_consumption_reward + drop constraint fk_mint; + +alter table hivemapper.map_consumption_reward + add foreign key (mint_id) references hivemapper.mints(id) + on delete cascade; + + +alter table hivemapper.ai_payments + drop constraint fk_mint; + +alter table hivemapper.ai_payments + add foreign key (mint_id) references hivemapper.mints(id) + on delete cascade; + +alter table hivemapper.operational_payments + drop constraint fk_mint; + +alter table hivemapper.operational_payments + add foreign key (mint_id) references hivemapper.mints(id) + on delete cascade; + +alter table hivemapper.reward_payments + drop constraint fk_mint; + +alter table hivemapper.reward_payments + add foreign key (mint_id) references hivemapper.mints(id) + on delete cascade; + +alter table hivemapper.split_payments + drop constraint fk_transaction; + +alter table hivemapper.split_payments + add foreign key (transaction_id) references hivemapper.transactions(id) + on delete cascade; + +alter table hivemapper.split_payments + drop constraint fk_fleet_mint; + +alter table hivemapper.split_payments + add foreign key (fleet_mint_id) references hivemapper.mints(id) + on delete cascade; + +alter table hivemapper.split_payments + drop constraint fk_driver_mint; + +alter table hivemapper.split_payments + add foreign key (driver_mint_id) references hivemapper.mints(id) + on delete cascade; + +alter table hivemapper.no_split_payments + drop constraint fk_mint; + +alter table hivemapper.no_split_payments + add foreign key (mint_id) references hivemapper.mints(id) + on delete cascade; + +alter table hivemapper.transfers + drop constraint fk_transaction; + +alter table hivemapper.transfers + add foreign key (transaction_id) references hivemapper.transactions(id) + on delete cascade; + `