diff --git a/app/db/block.go b/app/db/block.go index 5cfd570f..683b88a3 100644 --- a/app/db/block.go +++ b/app/db/block.go @@ -45,21 +45,16 @@ func StoreBlock(dbWOTx *gorm.DB, block *PackedBlock, status *d.StatusHolder, que log.Printf("[!] Block %d already present in DB, similar ❌\n", block.Block.Number) - // -- If block is going to be updated, it's better - // we also remove associated entries for that block - // i.e. transactions, events - if err := RemoveEventsByBlockHash(dbWTx, persistedBlock.Hash); err != nil { + // cascaded deletion ! + if err := DeleteBlock(dbWTx, block.Block.Number); err != nil { return err } - if err := RemoveTransactionsByBlockHash(dbWTx, persistedBlock.Hash); err != nil { + if err := PutBlock(dbWTx, block.Block); err != nil { return err } - // -- block data clean up ends here - if err := UpdateBlock(dbWTx, block.Block); err != nil { - return err - } + blockInserted = true } else { @@ -128,6 +123,12 @@ func PutBlock(dbWTx *gorm.DB, block *Blocks) error { } +// DeleteBlock - Delete block entry, identified by block number, while +// cascading all dependent entries ( i.e. in transactions/ events table ) +func DeleteBlock(dbWTx *gorm.DB, number uint64) error { + return dbWTx.Where("number = ?", number).Delete(&Blocks{}).Error +} + // UpdateBlock - Updating already existing block func UpdateBlock(dbWTx *gorm.DB, block *Blocks) error { diff --git a/app/db/model.go b/app/db/model.go index 22d2e1c8..b28aba62 100644 --- a/app/db/model.go +++ b/app/db/model.go @@ -31,8 +31,8 @@ type Blocks struct { TransactionRootHash string `gorm:"column:txroothash;type:char(66);not null"` ReceiptRootHash string `gorm:"column:receiptroothash;type:char(66);not null"` ExtraData []byte `gorm:"column:extradata;type:bytea"` - Transactions Transactions `gorm:"foreignKey:blockhash"` - Events Events `gorm:"foreignKey:blockhash"` + Transactions Transactions `gorm:"foreignKey:blockhash;constraint:OnDelete:CASCADE;"` + Events Events `gorm:"foreignKey:blockhash;constraint:OnDelete:CASCADE;"` } // TableName - Overriding default table name @@ -73,7 +73,7 @@ type Transactions struct { Nonce uint64 `gorm:"column:nonce;type:bigint;not null;index"` State uint64 `gorm:"column:state;type:smallint;not null"` BlockHash string `gorm:"column:blockhash;type:char(66);not null;index"` - Events Events `gorm:"foreignKey:txhash"` + Events Events `gorm:"foreignKey:txhash;constraint:OnDelete:CASCADE;"` } // TableName - Overriding default table name diff --git a/db/schema.sql b/db/schema.sql index 05586e83..3b9012e0 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -36,7 +36,7 @@ create table transactions ( nonce bigint not null, state smallint not null, blockhash char(66) not null, - foreign key (blockhash) references blocks(hash) + foreign key (blockhash) references blocks(hash) on delete cascade ); create index on transactions(from); @@ -53,8 +53,8 @@ create table events ( txhash char(66) not null, blockhash char(66) not null, primary key (blockhash, index), - foreign key (txhash) references transactions(hash), - foreign key (blockhash) references blocks(hash) + foreign key (txhash) references transactions(hash) on delete cascade, + foreign key (blockhash) references blocks(hash) on delete cascade ); create index on events(origin); @@ -75,7 +75,7 @@ create table delivery_history ( client char(42) not null, ts timestamp not null, endpoint varchar(100) not null, - datalength bigint not null, + datalength bigint not null ); create index on delivery_history(client);