Skip to content

Commit

Permalink
chore(kv): rename Name method to MigrationName for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeMac committed Mar 18, 2020
1 parent 6dc9343 commit 30b565e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
9 changes: 6 additions & 3 deletions kv/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ func indexKeyParts(indexKey []byte) (fk, pk []byte, err error) {
return
}

// ensure IndexMigration implements MigrationSpec
var _ MigrationSpec = (*IndexMigration)(nil)

// IndexMigration is a migration for adding and removing an index.
// These are constructed via the Index.Migration function.
type IndexMigration struct {
Expand All @@ -187,7 +190,7 @@ type IndexMigration struct {
}

// Name returns a readable name for the index migration.
func (i *IndexMigration) Name() string {
func (i *IndexMigration) MigrationName() string {
return fmt.Sprintf("add index %q", string(i.IndexBucket()))
}

Expand All @@ -198,7 +201,7 @@ func (i *IndexMigration) Up(ctx context.Context, store Store) (err error) {
return nil
}

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

if err = i.initialize(ctx, store); err != nil {
Expand All @@ -212,7 +215,7 @@ func (i *IndexMigration) Up(ctx context.Context, store Store) (err error) {
// Down deletes all entries from the index.
func (i *IndexMigration) Down(ctx context.Context, store Store) error {
if err := i.DeleteAll(ctx, store); err != nil {
return fmt.Errorf("migration (down) %s: %w", i.Name(), err)
return fmt.Errorf("migration (down) %s: %w", i.MigrationName(), err)
}

return nil
Expand Down
28 changes: 14 additions & 14 deletions kv/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Migration struct {
// It describes the name of the migration and up and down operations
// needed to fulfill the migration.
type MigrationSpec interface {
Name() string
MigrationName() string
Up(ctx context.Context, store Store) error
Down(ctx context.Context, store Store) error
}
Expand All @@ -75,7 +75,7 @@ func NewAnonymousMigration(name string, up, down MigrationFunc) AnonymousMigrati
}

// Name returns the name of the migration.
func (a AnonymousMigration) Name() string { return a.name }
func (a AnonymousMigration) MigrationName() string { return a.name }

// Up calls the underlying up migration func.
func (a AnonymousMigration) Up(ctx context.Context, store Store) error { return a.up(ctx, store) }
Expand All @@ -87,8 +87,8 @@ func (a AnonymousMigration) Down(ctx context.Context, store Store) error { retur
// It takes a list of migration specifications and undo (down) all or apply (up) outstanding migrations.
// It records the state of the world in store under the migrations bucket.
type Migrator struct {
logger *zap.Logger
Migrations []MigrationSpec
logger *zap.Logger
MigrationSpecs []MigrationSpec

now func() time.Time
}
Expand All @@ -107,7 +107,7 @@ func NewMigrator(logger *zap.Logger, ms ...MigrationSpec) *Migrator {

// AddMigrations appends the provided migration specs onto the Migrator.
func (m *Migrator) AddMigrations(ms ...MigrationSpec) {
m.Migrations = append(m.Migrations, ms...)
m.MigrationSpecs = append(m.MigrationSpecs, ms...)
}

// Initialize creates the migration bucket if it does not yet exist.
Expand All @@ -127,10 +127,10 @@ func (m *Migrator) List(ctx context.Context, store Store) (migrations []Migratio
}

migrationsLen := len(migrations)
for idx, migration := range m.Migrations[migrationsLen:] {
for idx, spec := range m.MigrationSpecs[migrationsLen:] {
migration := Migration{
ID: influxdb.ID(migrationsLen + idx + 1),
Name: migration.Name(),
Name: spec.MigrationName(),
}

migrations = append(migrations, migration)
Expand Down Expand Up @@ -167,11 +167,11 @@ func (m *Migrator) Up(ctx context.Context, store Store) error {
return wrapErr(err)
}

for idx, mig := range m.Migrations[lastMigration:] {
for idx, spec := range m.MigrationSpecs[lastMigration:] {
startedAt := m.now()
migration := Migration{
ID: influxdb.ID(lastMigration + idx + 1),
Name: mig.Name(),
Name: spec.MigrationName(),
StartedAt: &startedAt,
}

Expand All @@ -181,7 +181,7 @@ func (m *Migrator) Up(ctx context.Context, store Store) error {
return wrapErr(err)
}

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

Expand Down Expand Up @@ -229,7 +229,7 @@ func (m *Migrator) Down(ctx context.Context, store Store) (err error) {
MigrationSpec
Migration
}{
m.Migrations[int(id)-1],
m.MigrationSpecs[int(id)-1],
mig,
},
)
Expand Down Expand Up @@ -284,12 +284,12 @@ func (m *Migrator) walk(ctx context.Context, store Store, fn func(id influxdb.ID
}

idx := int(id) - 1
if idx >= len(m.Migrations) {
if idx >= len(m.MigrationSpecs) {
return fmt.Errorf("migration %q: %w", migration.Name, ErrMigrationSpecNotFound)
}

if mig := m.Migrations[idx]; mig.Name() != migration.Name {
return fmt.Errorf("expected migration %q, found %q", mig.Name(), migration.Name)
if spec := m.MigrationSpecs[idx]; spec.MigrationName() != migration.Name {
return fmt.Errorf("expected migration %q, found %q", spec.MigrationName(), migration.Name)
}

if migration.FinishedAt != nil {
Expand Down
4 changes: 4 additions & 0 deletions kv/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func NewService(log *zap.Logger, kv Store, configs ...ServiceConfig) *Service {
return nil
},
),
// and new migrations below here (and move this comment down):
)

if len(configs) > 0 {
Expand Down Expand Up @@ -127,6 +128,9 @@ func (s *Service) Initialize(ctx context.Context) error {
}

func (s *Service) initializeAll(ctx context.Context, store Store) error {
// please do not initialize anymore buckets here
// add them as a new migration to the list of migrations
// defined in NewService.
if err := store.Update(ctx, func(tx Tx) error {
if err := s.initializeAuths(ctx, tx); err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions testing/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func Migrator(t *testing.T, store kv.Store, newMigrator func(*zap.Logger, NowFun

t.Run("List() missing migration spec errors as expected", func(t *testing.T) {
// remove last specification from migration list
migrator.Migrations = migrator.Migrations[:len(migrator.Migrations)-1]
migrator.MigrationSpecs = migrator.MigrationSpecs[:len(migrator.MigrationSpecs)-1]
// list migration again
_, err := migrator.List(ctx, store)
if !errors.Is(err, kv.ErrMigrationSpecNotFound) {
Expand All @@ -335,7 +335,7 @@ type spyMigrationSpec struct {
downCalled int
}

func (s *spyMigrationSpec) Name() string {
func (s *spyMigrationSpec) MigrationName() string {
return s.name
}

Expand Down

0 comments on commit 30b565e

Please sign in to comment.