diff options
Diffstat (limited to 'swarm/storage/localstore.go')
-rw-r--r-- | swarm/storage/localstore.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/swarm/storage/localstore.go b/swarm/storage/localstore.go index 04701ee69..b28f62524 100644 --- a/swarm/storage/localstore.go +++ b/swarm/storage/localstore.go @@ -184,3 +184,42 @@ func (ls *LocalStore) Iterator(from uint64, to uint64, po uint8, f func(Address, func (ls *LocalStore) Close() { ls.DbStore.Close() } + +// Migrate checks the datastore schema vs the runtime schema, and runs migrations if they don't match +func (ls *LocalStore) Migrate() error { + schema, err := ls.DbStore.GetSchema() + if err != nil { + log.Error(err.Error()) + return err + } + + log.Debug("found schema", "schema", schema, "runtime-schema", CurrentDbSchema) + if schema != CurrentDbSchema { + // run migrations + + if schema == "" { + log.Debug("running migrations for", "schema", schema, "runtime-schema", CurrentDbSchema) + + cleanupFunc := func(c *chunk) bool { + // if one of the ls.Validators passes, it means a chunk is of particular type and it is valid + valid := false + for _, v := range ls.Validators { + if valid = v.Validate(c.Address(), c.Data()); valid { + break + } + } + return valid + } + + ls.DbStore.Cleanup(cleanupFunc) + + err := ls.DbStore.PutSchema(DbSchemaPurity) + if err != nil { + log.Error(err.Error()) + return err + } + } + } + + return nil +} |