-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: migrate prune policy options to oneof
- Loading branch information
1 parent
bf6fb7e
commit ef41d34
Showing
7 changed files
with
681 additions
and
208 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.