Skip to content

Commit

Permalink
fix(kv): initialize migrator bucket on kv service initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Mar 9, 2020
1 parent 39fbd01 commit a529539
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 14 additions & 4 deletions kv/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,28 @@ func (i *IndexMigration) Name() string {
}

// Up initializes the index bucket and populates the index.
func (i *IndexMigration) Up(ctx context.Context, store Store) error {
if err := i.initialize(ctx, store); err != nil {
func (i *IndexMigration) Up(ctx context.Context, store Store) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("migration (up) %s: %w", i.Name(), err)
}
}()

if err = i.initialize(ctx, store); err != nil {
return err
}

_, err := i.Populate(ctx, store, i.opts...)
_, err = i.Populate(ctx, store, i.opts...)
return err
}

// Down deletes all entries from the index.
func (i *IndexMigration) Down(ctx context.Context, store Store) error {
return i.DeleteAll(ctx, store)
if err := i.DeleteAll(ctx, store); err != nil {
return fmt.Errorf("migration (down) %s: %w", i.Name(), err)
}

return nil
}

// Migration creates an IndexMigration for the underlying Index.
Expand Down
4 changes: 4 additions & 0 deletions kv/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ type AutoMigrationStore interface {

// Initialize creates Buckets needed.
func (s *Service) Initialize(ctx context.Context) error {
if err := s.Migrator.Initialize(ctx, s.kv); err != nil {
return err
}

if store, ok := s.kv.(AutoMigrationStore); ok && store.AutoMigrate() {
return s.Migrator.Up(ctx, s.kv)
}
Expand Down

0 comments on commit a529539

Please sign in to comment.