-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docdb): allow (major) version upgrades
Signed-off-by: Charel Baum (external expert on behalf of DB InfraGO AG) <[email protected]>
- Loading branch information
Charel Baum (external expert on behalf of DB InfraGO AG)
committed
Jul 4, 2024
1 parent
d17c9da
commit 1a2c379
Showing
8 changed files
with
259 additions
and
1 deletion.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,85 @@ | ||
package utils | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// EngineVersion represents an AWS DocDB engine version. | ||
type EngineVersion []any | ||
|
||
// ParseEngineVersion from a raw string. | ||
func ParseEngineVersion(raw string) EngineVersion { | ||
split := strings.Split(raw, ".") | ||
|
||
v := make(EngineVersion, len(split)) | ||
for i, s := range split { | ||
d, err := strconv.Atoi(s) | ||
if err != nil { | ||
v[i] = s | ||
} else { | ||
v[i] = d | ||
} | ||
} | ||
return v | ||
} | ||
|
||
const ( | ||
compareIsHigher = 1 | ||
compareIsEqual = 0 | ||
compareIsLower = -1 | ||
) | ||
|
||
// Compare returns a positive value if v is represents a higher version number | ||
// than other. A negative value is returned if other is higher than v. | ||
// It returns 0 if both are considered equal. | ||
func (v EngineVersion) Compare(other EngineVersion) int { | ||
if other == nil { | ||
return compareIsHigher | ||
} | ||
|
||
for i := 0; i < len(v); i++ { | ||
a := v.get(i) | ||
b := other.get(i) | ||
c := compareVersionComponents(a, b) | ||
if c != 0 { | ||
return c | ||
} | ||
} | ||
return compareIsEqual | ||
} | ||
|
||
func compareVersionComponents(a, b any) int { | ||
if a == b { | ||
return compareIsEqual | ||
} | ||
if b == nil { | ||
return compareIsHigher | ||
} | ||
aI, aIsInt := a.(int) | ||
bI, bIsInt := b.(int) | ||
if aIsInt { | ||
if bIsInt { | ||
return aI - bI | ||
} | ||
return compareIsHigher | ||
} | ||
if bIsInt { | ||
return compareIsLower | ||
} | ||
return compareIsEqual // We cannot decide if both are strings. | ||
} | ||
|
||
func (v EngineVersion) get(i int) any { | ||
if i >= 0 && i < len(v) { | ||
return v[i] | ||
} | ||
return nil | ||
} | ||
|
||
// CompareEngineVersions is a shortcut to compare two engine versions. | ||
func CompareEngineVersions(a, b string) int { | ||
av := ParseEngineVersion(a) | ||
bv := ParseEngineVersion(b) | ||
return av.Compare(bv) | ||
} |
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,83 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestEngineVersionIsHigherOrEqual(t *testing.T) { | ||
type args struct { | ||
spec string | ||
current string | ||
} | ||
|
||
type want struct { | ||
result int | ||
} | ||
|
||
cases := map[string]struct { | ||
args | ||
want | ||
}{ | ||
"DocDBIsEqual": { | ||
args: args{ | ||
spec: "5.0.0", | ||
current: "5.0.0", | ||
}, | ||
want: want{ | ||
result: 0, | ||
}, | ||
}, | ||
"DocDBIsEqual2": { | ||
args: args{ | ||
spec: "5.0", | ||
current: "5.0.0", | ||
}, | ||
want: want{ | ||
result: 0, | ||
}, | ||
}, | ||
"DocDBIsHigher": { | ||
args: args{ | ||
spec: "5.0", | ||
current: "4.0.0", | ||
}, | ||
want: want{ | ||
result: 1, | ||
}, | ||
}, | ||
"DocDBIsLower": { | ||
args: args{ | ||
spec: "4.0.0", | ||
current: "5.0.0", | ||
}, | ||
want: want{ | ||
result: -1, | ||
}, | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
specV := ParseEngineVersion(tc.args.spec) | ||
curV := ParseEngineVersion(tc.args.current) | ||
|
||
res := specV.Compare(curV) | ||
resSign := sign(res) | ||
if diff := cmp.Diff(tc.want.result, resSign); diff != "" { | ||
t.Errorf("r: -want, +got:\n%q\n%q\n%s", tc.args.spec, tc.args.current, diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func sign(x int) int { | ||
if x < 0 { | ||
return -1 | ||
} | ||
if x > 0 { | ||
return 1 | ||
} | ||
return 0 | ||
} |