Skip to content

Commit

Permalink
fix(kv): update auto migration store to return migratable store
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Mar 13, 2020
1 parent 47c56a4 commit 3264284
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
9 changes: 6 additions & 3 deletions bolt/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
// check that *KVStore implement kv.Store interface.
var _ kv.Store = (*KVStore)(nil)

// ensure *KVStore implements kv.AutoMigrationStore.
var _ kv.AutoMigrationStore = (*KVStore)(nil)

// KVStore is a kv.Store backed by boltdb.
type KVStore struct {
path string
Expand All @@ -34,9 +37,9 @@ func NewKVStore(log *zap.Logger, path string) *KVStore {
}
}

// AutoMigrate returns true as the bolt KVStore is safe to migrate on initialize.
func (s *KVStore) AutoMigrate() bool {
return true
// AutoMigrate returns itself as it is safe to automatically apply migrations on initialization.
func (s *KVStore) AutoMigrate() kv.Store {
return s
}

// Open creates boltDB file it doesn't exists and opens it otherwise.
Expand Down
9 changes: 6 additions & 3 deletions inmem/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
// ensure *KVStore implement kv.Store interface
var _ kv.Store = (*KVStore)(nil)

// ensure *KVStore implements kv.AutoMigrationStore
var _ kv.AutoMigrationStore = (*KVStore)(nil)

// cursorBatchSize is the size of a batch sent by a forward cursors
// tree iterator
const cursorBatchSize = 1000
Expand Down Expand Up @@ -61,9 +64,9 @@ func (s *KVStore) Backup(ctx context.Context, w io.Writer) error {
panic("not implemented")
}

// AutoMigrate returns true as inmem KVStore is safe to migrate on initialize.
func (s *KVStore) AutoMigrate() bool {
return true
// AutoMigrate returns itlsef as *KVStore is safe to migrate automically on initialize.
func (s *KVStore) AutoMigrate() kv.Store {
return s
}

// Flush removes all data from the buckets. Used for testing.
Expand Down
15 changes: 10 additions & 5 deletions kv/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ type ServiceConfig struct {

// AutoMigrationStore is a Store which also describes whether or not
// migrations can be applied automatically.
// Given the AutoMigrate method is defined and it returns true then migrations
// will automatically be applied on Service.Initialize(...).
// Given the AutoMigrate method is defined and it returns a non-nil kv.Store
// implementation, then it will automatically invoke migrator.Up(store)
// on the returned kv.Store during Service.Initialize(...).
type AutoMigrationStore interface {
Store
AutoMigrate() bool
AutoMigrate() Store
}

// Initialize creates Buckets needed.
Expand All @@ -114,8 +115,12 @@ func (s *Service) Initialize(ctx context.Context) error {
return err
}

if store, ok := s.kv.(AutoMigrationStore); ok && store.AutoMigrate() {
return s.Migrator.Up(ctx, s.kv)
// if store implements auto migrate and the resulting Store from
// AutoMigrate() is non-nil, apply migrator.Up() to the resulting store.
if store, ok := s.kv.(AutoMigrationStore); ok {
if migrateStore := store.AutoMigrate(); migrateStore != nil {
return s.Migrator.Up(ctx, migrateStore)
}
}

return nil
Expand Down

0 comments on commit 3264284

Please sign in to comment.