Organize DB Schema Migration Code and DB Init Checks (#842)
Create Schema Manager and propagate contexts into the DB calls downstream. Add checks at controller startup and database init for correct schema version and schema migration dirty bit being set.
This commit is contained in:
1 parent
c141a050e5
commit
ec6151d174
83 files changed
+2425
-1176
No files matched your search
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/hashicorp/boundary/globals"
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/internal/cmd/config"
|
||||
"github.com/hashicorp/boundary/internal/db/schema"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller"
|
||||
"github.com/hashicorp/boundary/internal/servers/worker"
|
||||
"github.com/hashicorp/boundary/sdk/wrapper"
|
||||
@@ -341,6 +342,46 @@ func (c *Command) Run(args []string) int {
|
||||
c.UI.Error(fmt.Errorf("Error connecting to database: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
|
||||
sMan, err := schema.NewManager(c.Context, "postgres", c.Database.DB())
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Errorf("Can't get schema manager: %w.", err).Error())
|
||||
return 1
|
||||
}
|
||||
// This is an advisory locks on the DB which is released when the db session ends.
|
||||
if err := sMan.SharedLock(c.Context); err != nil {
|
||||
c.UI.Error(fmt.Errorf("Unable to gain shared access to the database: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
defer func() {
|
||||
if err := sMan.SharedUnlock(c.Context); err != nil {
|
||||
c.UI.Error(fmt.Errorf("Unable to release shared lock to the database: %w", err).Error())
|
||||
}
|
||||
}()
|
||||
ckState, err := sMan.CurrentState(c.Context)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Errorf("Error checking schema state: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
if !ckState.InitializationStarted {
|
||||
c.UI.Error("Database has not been initialized. Please run `boundary database init`.")
|
||||
return 1
|
||||
}
|
||||
if ckState.Dirty {
|
||||
c.UI.Error(base.WrapAtLength("Database is in a bad state. Please revert the database into the last known good state."))
|
||||
return 1
|
||||
}
|
||||
if ckState.BinarySchemaVersion > ckState.DatabaseSchemaVersion {
|
||||
// TODO: Add the command to migrate up the schema version once that command exists.
|
||||
c.UI.Error("Older schema version is than is expected from this binary.")
|
||||
return 1
|
||||
}
|
||||
if ckState.BinarySchemaVersion < ckState.DatabaseSchemaVersion {
|
||||
c.UI.Error(base.WrapAtLength(fmt.Sprintf("Newer schema version (%d) "+
|
||||
"than this binary expects. Please use a newer version of the boundary "+
|
||||
"binary.", ckState.DatabaseSchemaVersion)))
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
defer func() {
|
||||
|
||||
Reference in new issue
Block a user