/
versiong.go
45 lines (37 loc) · 1.24 KB
/
versiong.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package spirali
import (
"strconv"
"time"
)
// VersionG represents the version generator of migration.
type VersionG interface {
GenerateNextVersion() (uint64, error) // generates next version.
IsSmall(targetVersion uint64, comparisonVersion uint64) bool // returns whether targetVersion is smaller than comparisonVersion.
}
// TimestampBasedVersionG is the timestamp based version generator.
type TimestampBasedVersionG struct{}
// GenerateNextVersion ...
func (vg *TimestampBasedVersionG) GenerateNextVersion() (uint64, error) {
v, err := strconv.ParseUint(time.Now().Format("20060102150405"), 10, 64)
if err != nil {
return 0, err
}
return v, nil
}
// IsSmall ...
func (vg *TimestampBasedVersionG) IsSmall(targetVersion uint64, comparisonVersion uint64) bool {
return targetVersion < comparisonVersion
}
// IncrementalVersionG is the incremental version generator.
type IncrementalVersionG struct {
CurrentVersion uint64
}
// GenerateNextVersion ...
func (vg *IncrementalVersionG) GenerateNextVersion() (uint64, error) {
vg.CurrentVersion++
return vg.CurrentVersion, nil
}
// IsSmall ...
func (vg *IncrementalVersionG) IsSmall(targetVersion uint64, comparisonVersion uint64) bool {
return targetVersion < comparisonVersion
}