Skip to content

Commit

Permalink
fix: remove migrations for fields that have been since backrest 1.0.0 (
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge authored Sep 4, 2024
1 parent 9205da1 commit 546482f
Show file tree
Hide file tree
Showing 16 changed files with 366 additions and 723 deletions.
545 changes: 204 additions & 341 deletions gen/go/v1/config.pb.go

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions internal/api/backresthandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestBackup(t *testing.T) {
Schedule: &v1.Schedule_Disabled{Disabled: true},
},
Retention: &v1.RetentionPolicy{
KeepHourly: 1,
Policy: &v1.RetentionPolicy_PolicyKeepLastN{PolicyKeepLastN: 100},
},
},
},
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestMultipleBackup(t *testing.T) {
sut.orch.Run(ctx)
}()

for i := 0; i < 3; i++ {
for i := 0; i < 2; i++ {
_, err := sut.handler.Backup(context.Background(), connect.NewRequest(&types.StringValue{Value: "test"}))
if err != nil {
t.Fatalf("Backup() error = %v", err)
Expand All @@ -234,10 +234,12 @@ func TestMultipleBackup(t *testing.T) {
if index := slices.IndexFunc(operations, func(op *v1.Operation) bool {
forget, ok := op.GetOp().(*v1.Operation_OperationForget)
return op.Status == v1.OperationStatus_STATUS_SUCCESS && ok && len(forget.OperationForget.Forget) > 0
}); index != -1 {
return nil
}); index == -1 {
return errors.New("forget not indexed")
} else if len(operations[index].GetOp().(*v1.Operation_OperationForget).OperationForget.Forget) != 1 {
return fmt.Errorf("expected 1 item removed in the forget operation, got %d", len(operations[index].GetOp().(*v1.Operation_OperationForget).OperationForget.Forget))
}
return errors.New("forget not indexed")
return nil
}); err != nil {
t.Fatalf("Couldn't find forget with 1 item removed in the operation log")
}
Expand Down Expand Up @@ -669,7 +671,7 @@ func TestRestore(t *testing.T) {
Schedule: &v1.Schedule_Disabled{Disabled: true},
},
Retention: &v1.RetentionPolicy{
KeepHourly: 1,
Policy: &v1.RetentionPolicy_PolicyKeepAll{PolicyKeepAll: true},
},
},
},
Expand Down
5 changes: 1 addition & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ConfigStore interface {

func NewDefaultConfig() *v1.Config {
return &v1.Config{
Version: migrations.CurrentVersion,
Instance: "",
Repos: []*v1.Repo{},
Plans: []*v1.Plan{},
Expand Down Expand Up @@ -55,10 +56,6 @@ func (c *CachingValidatingStore) Get() (*v1.Config, error) {
return nil, err
}

if config.Version != migrations.CurrentVersion {
return nil, fmt.Errorf("migration failed to update config to version %d", migrations.CurrentVersion)
}

// Write back the migrated config.
if err := c.ConfigStore.Update(config); err != nil {
return nil, err
Expand Down
14 changes: 14 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@ import (
"testing"

v1 "github.com/garethgeorge/backrest/gen/go/v1"
"github.com/garethgeorge/backrest/internal/config/migrations"
"google.golang.org/protobuf/proto"
)

func TestMigrationsOnDefaultConfig(t *testing.T) {
config := NewDefaultConfig()
t.Logf("config: %v", config)
err := migrations.ApplyMigrations(config)
if err != nil {
t.Errorf("ApplyMigrations() error = %v, want nil", err)
}

if config.Version != migrations.CurrentVersion {
t.Errorf("ApplyMigrations() config.Version = %v, want %v", config.Version, migrations.CurrentVersion)
}
}

func TestConfig(t *testing.T) {
dir := t.TempDir()

Expand Down
45 changes: 0 additions & 45 deletions internal/config/migrations/001prunepolicy.go

This file was deleted.

112 changes: 0 additions & 112 deletions internal/config/migrations/001prunepolicy_test.go

This file was deleted.

42 changes: 0 additions & 42 deletions internal/config/migrations/002schedules.go

This file was deleted.

4 changes: 3 additions & 1 deletion internal/config/migrations/003relativescheduling.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package migrations

import (
v1 "github.com/garethgeorge/backrest/gen/go/v1"
"go.uber.org/zap"
)

func migration003RelativeScheduling(config *v1.Config) {
var migration003RelativeScheduling = func(config *v1.Config) {
zap.L().Info("applying config migration 003: relative scheduling")
// loop over plans and examine prune policy's
for _, repo := range config.Repos {
prunePolicy := repo.GetPrunePolicy()
Expand Down
34 changes: 26 additions & 8 deletions internal/config/migrations/migrations.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
package migrations

import (
"fmt"

v1 "github.com/garethgeorge/backrest/gen/go/v1"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)

var migrations = []func(*v1.Config){
migration001PrunePolicy,
migration002Schedules,
migration003RelativeScheduling,
var migrations = []*func(*v1.Config){
&noop, // migration001PrunePolicy
&noop, // migration002Schedules is deprecated
&migration003RelativeScheduling,
}

var CurrentVersion = int32(len(migrations))

func ApplyMigrations(config *v1.Config) error {
startMigration := int(config.Version - 1)
if config.Version == 0 {
if proto.Equal(config, &v1.Config{}) {
config.Version = CurrentVersion
return nil
}
return fmt.Errorf("config version 0 is invalid")
}

startMigration := int(config.Version)
if startMigration < 0 {
startMigration = 0
}

for idx := startMigration; idx < len(migrations); idx += 1 {
zap.S().Infof("applying config migration %d", idx+1)
migrations[idx](config)
m := migrations[idx]
if m == &noop {
return fmt.Errorf("config version %d is too old to migrate, please try first upgrading to backrest 1.4.0 which is the last version that may be compatible with your config", config.Version)
}
(*m)(config)
}
config.Version = CurrentVersion
return nil
}

var noop = func(config *v1.Config) {
// do nothing
}
Loading

0 comments on commit 546482f

Please sign in to comment.