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
@@ -7,9 +7,7 @@ import (
|
||||
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/internal/cmd/config"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/db/migrations"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/db/schema"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
"github.com/hashicorp/boundary/sdk/wrapper"
|
||||
wrapping "github.com/hashicorp/go-kms-wrapping"
|
||||
@@ -186,8 +184,10 @@ func (c *InitCommand) Run(args []string) (retCode int) {
|
||||
}()
|
||||
}
|
||||
|
||||
if migrations.DevMigration != c.flagAllowDevMigrations {
|
||||
if migrations.DevMigration {
|
||||
dialect := "postgres"
|
||||
|
||||
if schema.DevMigration(dialect) != c.flagAllowDevMigrations {
|
||||
if schema.DevMigration(dialect) {
|
||||
c.UI.Error(base.WrapAtLength("This version of the binary has " +
|
||||
"dev database schema updates which may not be supported in the " +
|
||||
"next official release. To proceed anyways please use the " +
|
||||
@@ -263,6 +263,53 @@ func (c *InitCommand) Run(args []string) (retCode int) {
|
||||
return 1
|
||||
}
|
||||
|
||||
// This database is used to keep an exclusive lock on the database for the
|
||||
// remainder of the command
|
||||
dBase, err := sql.Open(dialect, dbaseUrl)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Errorf("Error establishing db connection for locking: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
man, err := schema.NewManager(c.Context, dialect, dBase)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Errorf("Error setting up schema manager for locking: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
{
|
||||
st, err := man.CurrentState(c.Context)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Errorf("Error getting database state: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
if st.Dirty {
|
||||
c.UI.Error(base.WrapAtLength("Database is in a bad initialization " +
|
||||
"state. Please revert back to the last known good state."))
|
||||
return 1
|
||||
}
|
||||
if st.InitializationStarted {
|
||||
// TODO: Separate from the "dirty" bit maintained by the schema
|
||||
// manager maintain a bit which indicates that this full command
|
||||
// was completed successfully (with all default resources being created).
|
||||
// Use that bit to determine if a previous init was completed
|
||||
// successfully or not.
|
||||
c.UI.Error(base.WrapAtLength("Database has already been " +
|
||||
"initialized. If the initialization did not complete successfully " +
|
||||
"please revert the database to its fresh state."))
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
// This is an advisory locks on the DB which is released when the db session ends.
|
||||
if err := man.ExclusiveLock(c.Context); err != nil {
|
||||
c.UI.Error(fmt.Errorf("Error capturing an exclusive lock: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
defer func() {
|
||||
if err := man.ExclusiveUnlock(c.Context); err != nil {
|
||||
c.UI.Error(fmt.Errorf("Unable to release exclusive lock to the database: %w", err).Error())
|
||||
}
|
||||
}()
|
||||
|
||||
migrationUrl, err := config.ParseAddress(migrationUrlToParse)
|
||||
if err != nil && err != config.ErrNotAUrl {
|
||||
c.UI.Error(fmt.Errorf("Error parsing migration url: %w", err).Error())
|
||||
@@ -271,26 +318,8 @@ func (c *InitCommand) Run(args []string) (retCode int) {
|
||||
|
||||
// Core migrations using the migration URL
|
||||
{
|
||||
c.srv.DatabaseUrl = strings.TrimSpace(migrationUrl)
|
||||
ldb, err := sql.Open("postgres", c.srv.DatabaseUrl)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Errorf("Error opening database to check init status: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
_, err = ldb.QueryContext(c.Context, "select version from schema_migrations")
|
||||
switch {
|
||||
case err == nil:
|
||||
if base.Format(c.UI) == "table" {
|
||||
c.UI.Info("Database already initialized.")
|
||||
return 0
|
||||
}
|
||||
case errors.IsMissingTableError(err):
|
||||
// Doesn't exist so we continue on
|
||||
default:
|
||||
c.UI.Error(fmt.Errorf("Error querying database for init status: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
ran, err := db.InitStore("postgres", nil, c.srv.DatabaseUrl)
|
||||
migrationUrl = strings.TrimSpace(migrationUrl)
|
||||
ran, err := schema.InitStore(c.Context, dialect, migrationUrl)
|
||||
if err != nil {
|
||||
c.UI.Error(fmt.Errorf("Error running database migrations: %w", err).Error())
|
||||
return 1
|
||||
@@ -308,7 +337,7 @@ func (c *InitCommand) Run(args []string) (retCode int) {
|
||||
|
||||
// Everything after is done with normal database URL and is affecting actual data
|
||||
c.srv.DatabaseUrl = strings.TrimSpace(dbaseUrl)
|
||||
if err := c.srv.ConnectToDatabase("postgres"); err != nil {
|
||||
if err := c.srv.ConnectToDatabase(dialect); err != nil {
|
||||
c.UI.Error(fmt.Errorf("Error connecting to database after migrations: %w", err).Error())
|
||||
return 1
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user