Skip to content

Commit

Permalink
Refactor SQL constraints and fix DELETE statement.
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
billettc committed Nov 14, 2024
1 parent 77ea4b3 commit 2b585eb
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 7 deletions.
3 changes: 1 addition & 2 deletions cmd/honey-tracker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions data/psql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
146 changes: 144 additions & 2 deletions data/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
`

0 comments on commit 2b585eb

Please sign in to comment.