/
state.go
66 lines (53 loc) · 1.79 KB
/
state.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package schema
import "github.com/hashicorp/boundary/internal/db/schema/internal/provider"
const nilVersion = -1
// State contains information regarding the current state of a boundary database's schema.
type State struct {
// Initialized indicates if the current database has been previously initialized.
Initialized bool
Editions []EditionState
}
// MigrationsApplied checks to see that all Editions are in the Equal SchemaState.
func (s State) MigrationsApplied() bool {
for _, e := range s.Editions {
if e.DatabaseSchemaState != Equal {
return false
}
}
return true
}
func (s State) databaseState() provider.DatabaseState {
dbState := make(provider.DatabaseState)
for _, e := range s.Editions {
dbState[e.Name] = e.DatabaseSchemaVersion
}
return dbState
}
// DatabaseState defines the state of the Database schema as compared to the
// latest version for the binary.
type DatabaseState int
// Valid states.
const (
Behind DatabaseState = iota // Database schema version is older then latest for the binary, so it needs migrations applied.
Ahead // Database schema version is newer then latest for the binary, so binary needs to be updated.
Equal // Database schema version matches latest version for the binary.
)
// EditionState is the current state of a schema Edition.
type EditionState struct {
// Name is the identifier of the Edition.
Name string
// DatabaseSchemaVersion is the schema version that is currently running in the database.
DatabaseSchemaVersion int
// BinarySchemaVersion is the schema version which this boundary binary supports.
BinarySchemaVersion int
DatabaseSchemaState DatabaseState
}
func compareVersions(d int, b int) DatabaseState {
if d == b {
return Equal
}
if d < b {
return Behind
}
return Ahead
}