Files
threshold/internal/db/schema/state.go
T
Timothy Messier 8caaf3e076 refactor(schema): Add support for migration editions
This refactors the schema package and subpackages to support editions.
An Edition is a collection of sql migrations. A given boundary binary
can have multiple migration editions. The `schema.Manager` is updated to
apply the necessary migrations in the correct order, leveraging an
Edition's priority to determine the order in which the separate Editions
are applied.

The sql migration files are now nested under a directory for the
edition.

This also switches from the generated code to using embed, which
eliminates the need for the `make migrations` target and the generated
code. As such this commit removes the target and generated code.

See:
    https://pkg.go.dev/embed
2021-10-08 06:50:26 -04:00

67 lines
1.8 KiB
Go

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
}