vspd/database/upgrade_v2.go
Jamie Holdstock 20cb546e74
Store each ticket in its own DB bucket.
**NOTE: This contains a backwards incompatible database migration, so if you plan to test it, please make a copy of your database first.**

Moves tickets from a single database bucket containing JSON encoded strings, to a bucket for each ticket.

This change is to preemptively deal with scaling issues seen with databases containing tens of thousands of tickets.
2021-05-24 17:45:47 +08:00

66 lines
1.4 KiB
Go

package database
import (
"encoding/json"
"fmt"
bolt "go.etcd.io/bbolt"
)
func removeOldFeeTxUpgrade(db *bolt.DB) error {
log.Infof("Upgrading database to version %d", removeOldFeeTxVersion)
// Run the upgrade in a single database transaction so it can be safely
// rolled back if an error is encountered.
err := db.Update(func(tx *bolt.Tx) error {
vspBkt := tx.Bucket(vspBktK)
ticketBkt := vspBkt.Bucket(ticketBktK)
count := 0
err := ticketBkt.ForEach(func(k, v []byte) error {
// Deserialize the old ticket.
var ticket v1Ticket
err := json.Unmarshal(v, &ticket)
if err != nil {
return fmt.Errorf("could not unmarshal ticket: %w", err)
}
// Remove the fee tx hex if the tx is already confirmed.
if ticket.FeeTxStatus == FeeConfirmed {
count++
ticket.FeeTxHex = ""
ticketBytes, err := json.Marshal(ticket)
if err != nil {
return fmt.Errorf("could not marshal ticket: %w", err)
}
err = ticketBkt.Put(k, ticketBytes)
if err != nil {
return fmt.Errorf("could not put updated ticket: %w", err)
}
}
return nil
})
if err != nil {
return err
}
log.Infof("Dropped %d unnecessary raw transactions", count)
// Update database version.
err = vspBkt.Put(versionK, uint32ToBytes(removeOldFeeTxVersion))
if err != nil {
return fmt.Errorf("failed to update db version: %w", err)
}
return nil
})
if err != nil {
return err
}
log.Info("Upgrade completed")
return nil
}