mirror of
https://github.com/garethgeorge/backrest.git
synced 2026-09-26 18:05:34 +00:00
fix: migrate prune policy options to oneof
This commit is contained in:
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user