fix: migrate prune policy options to oneof

This commit is contained in:
garethgeorge
2024-02-19 22:28:55 -08:00
parent bcad0e80e1
commit 65fe87475a
7 changed files with 680 additions and 207 deletions
@@ -0,0 +1,41 @@
package migrations
import v1 "github.com/garethgeorge/backrest/gen/go/v1"
func migration001PrunePolicy(config *v1.Config) {
// loop over plans and examine prune policy's
for _, plan := range config.Plans {
policy := plan.GetRetention()
if policy == nil {
continue
}
if policy.Policy != nil {
continue // already migrated
}
if policy.KeepLastN != 0 {
plan.Retention = &v1.RetentionPolicy{
Policy: &v1.RetentionPolicy_PolicyKeepLastN{
PolicyKeepLastN: policy.KeepLastN,
},
}
} else if policy.KeepDaily != 0 || policy.KeepHourly != 0 || policy.KeepMonthly != 0 || policy.KeepWeekly != 0 || policy.KeepYearly != 0 {
plan.Retention = &v1.RetentionPolicy{
Policy: &v1.RetentionPolicy_PolicyTimeBucketed{
PolicyTimeBucketed: &v1.RetentionPolicy_TimeBucketedCounts{
Hourly: policy.KeepHourly,
Daily: policy.KeepDaily,
Weekly: policy.KeepWeekly,
Monthly: policy.KeepMonthly,
Yearly: policy.KeepYearly,
},
},
}
} else {
policy.Policy = &v1.RetentionPolicy_PolicyKeepAll{
PolicyKeepAll: true,
}
}
}
}
@@ -0,0 +1,111 @@
package migrations
import (
"testing"
v1 "github.com/garethgeorge/backrest/gen/go/v1"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
func Test001Migration(t *testing.T) {
cases := []struct {
name string
config string
want *v1.Config
}{
{
name: "time bucketed policy",
config: `{
"plans": [
{
"retention": {
"keepHourly": 1,
"keepDaily": 2,
"keepWeekly": 3,
"keepMonthly": 4,
"keepYearly": 5
}
}
]
}`,
want: &v1.Config{
Plans: []*v1.Plan{
{
Retention: &v1.RetentionPolicy{
Policy: &v1.RetentionPolicy_PolicyTimeBucketed{
PolicyTimeBucketed: &v1.RetentionPolicy_TimeBucketedCounts{
Hourly: 1,
Daily: 2,
Weekly: 3,
Monthly: 4,
Yearly: 5,
},
},
},
},
},
},
},
{
name: "keep all policy",
config: `{
"plans": [
{
"retention": {}
}
]
}`,
want: &v1.Config{
Plans: []*v1.Plan{
{
Retention: &v1.RetentionPolicy{
Policy: &v1.RetentionPolicy_PolicyKeepAll{
PolicyKeepAll: true,
},
},
},
},
},
},
{
name: "keep by count",
config: `{
"plans": [
{
"retention": {
"keepLastN": 5
}
}
]
}`,
want: &v1.Config{
Plans: []*v1.Plan{
{
Retention: &v1.RetentionPolicy{
Policy: &v1.RetentionPolicy_PolicyKeepLastN{
PolicyKeepLastN: 5,
},
},
},
},
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
config := v1.Config{}
err := protojson.Unmarshal([]byte(tc.config), &config)
if err != nil {
t.Fatalf("failed to unmarshal config: %v", err)
}
migration001PrunePolicy(&config)
if !proto.Equal(&config, tc.want) {
t.Errorf("got: %+v, want: %+v", &config, tc.want)
}
})
}
}
+10
View File
@@ -0,0 +1,10 @@
package migrations
import v1 "github.com/garethgeorge/backrest/gen/go/v1"
func ApplyMigrations(config *v1.Config) {
if config.Version <= 1 {
migration001PrunePolicy(config)
}
config.Version = 1
}