erigon-pulse/migrations/migrations.go
2020-02-29 20:37:53 +00:00

60 lines
1.3 KiB
Go

package migrations
import (
"github.com/ledgerwatch/turbo-geth/common/dbutils"
"github.com/ledgerwatch/turbo-geth/ethdb"
"github.com/ledgerwatch/turbo-geth/log"
)
type Migration struct {
Name string
Up func(db ethdb.Database, history, receipts, txIndex, preImages, thinHistory bool) error
}
func NewMigrator() *Migrator {
return &Migrator{
Migrations: migrations,
}
}
type Migrator struct {
Migrations []Migration
}
func (m *Migrator) Apply(db ethdb.Database, history, receipts, txIndex, preImages, thinHistory bool) error {
if len(m.Migrations) == 0 {
return nil
}
lastApplied, err := db.Get(dbutils.DatabaseInfoBucket, dbutils.LastAppliedMigration)
if err != nil && err != ethdb.ErrKeyNotFound {
return err
}
i := len(m.Migrations) - 1
for ; i >= 0; i-- {
if m.Migrations[i].Name == string(lastApplied) {
break
}
}
m.Migrations = m.Migrations[i+1:]
for _, v := range m.Migrations {
log.Warn("Apply migration", "name", v.Name)
err := v.Up(db, history, receipts, txIndex, preImages, thinHistory)
if err != nil {
return err
}
err = db.Put(dbutils.DatabaseInfoBucket, dbutils.LastAppliedMigration, []byte(v.Name))
if err != nil {
return err
}
log.Warn("Applied migration", "name", v.Name)
}
return nil
}
var migrations = []Migration{
splitChangeSetMigration(splitChangesetBatchSize),
}