Skip to content

Commit

Permalink
chore(kv): wrap error using func instead of defer in index
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Mar 16, 2020
1 parent cfd9f18 commit 6dc9343
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
14 changes: 8 additions & 6 deletions kv/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,20 @@ func (i *IndexMigration) Name() string {

// Up initializes the index bucket and populates the index.
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)
wrapErr := func(err error) error {
if err == nil {
return nil
}
}()

return fmt.Errorf("migration (up) %s: %w", i.Name(), err)
}

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

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

// Down deletes all entries from the index.
Expand Down
47 changes: 26 additions & 21 deletions kv/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,23 @@ func (m *Migrator) List(ctx context.Context, store Store) (migrations []Migratio
// 0003 add index "foo on baz" | (down)
//
// Up would apply migration 0002 and then 0003.
func (m *Migrator) Up(ctx context.Context, store Store) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("up: %w", err)
func (m *Migrator) Up(ctx context.Context, store Store) error {
wrapErr := func(err error) error {
if err == nil {
return nil
}
}()

return fmt.Errorf("up: %w", err)
}

var lastMigration int
if err = m.walk(ctx, store, func(id influxdb.ID, mig Migration) {
if err := m.walk(ctx, store, func(id influxdb.ID, mig Migration) {
// we're interested in the last up migration
if mig.State == UpMigrationState {
lastMigration = int(id)
}
}); err != nil {
return
return wrapErr(err)
}

for idx, mig := range m.Migrations[lastMigration:] {
Expand All @@ -176,19 +178,19 @@ func (m *Migrator) Up(ctx context.Context, store Store) (err error) {
m.logMigrationEvent(UpMigrationState, migration, "started")

if err := m.putMigration(ctx, store, migration); err != nil {
return err
return wrapErr(err)
}

if err := mig.Up(ctx, store); err != nil {
return err
return wrapErr(err)
}

finishedAt := m.now()
migration.FinishedAt = &finishedAt
migration.State = UpMigrationState

if err := m.putMigration(ctx, store, migration); err != nil {
return err
return wrapErr(err)
}

m.logMigrationEvent(UpMigrationState, migration, "completed")
Expand All @@ -207,18 +209,20 @@ func (m *Migrator) Up(ctx context.Context, store Store) (err error) {
//
// Down would call down() on 0002 and then on 0001.
func (m *Migrator) Down(ctx context.Context, store Store) (err error) {
defer func() {
if err != nil {
err = fmt.Errorf("down: %w", err)
wrapErr := func(err error) error {
if err == nil {
return nil
}
}()

return fmt.Errorf("down: %w", err)
}

var migrations []struct {
MigrationSpec
Migration
}

if err = m.walk(ctx, store, func(id influxdb.ID, mig Migration) {
if err := m.walk(ctx, store, func(id influxdb.ID, mig Migration) {
migrations = append(
migrations,
struct {
Expand All @@ -230,25 +234,26 @@ func (m *Migrator) Down(ctx context.Context, store Store) (err error) {
},
)
}); err != nil {
return
return wrapErr(err)
}

for i := len(migrations) - 1; i >= 0; i-- {
migration := migrations[i]

m.logMigrationEvent(DownMigrationState, migration.Migration, "started")

if err = migration.MigrationSpec.Down(ctx, store); err != nil {
return
if err := migration.MigrationSpec.Down(ctx, store); err != nil {
return wrapErr(err)
}

if err = m.deleteMigration(ctx, store, migration.Migration); err != nil {
return
if err := m.deleteMigration(ctx, store, migration.Migration); err != nil {
return wrapErr(err)
}

m.logMigrationEvent(DownMigrationState, migration.Migration, "completed")
}
return

return nil
}

func (m *Migrator) logMigrationEvent(state MigrationState, mig Migration, event string) {
Expand Down

0 comments on commit 6dc9343

Please sign in to comment.