Files
threshold/internal/db/schema/schema.go
T
Todd Knight ec6151d174 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.
2021-01-19 10:40:41 -07:00

41 lines
970 B
Go

package schema
import (
"context"
"database/sql"
"github.com/hashicorp/boundary/internal/errors"
)
// InitStore executes the migrations needed to initialize the store. It
// returns true if migrations actually ran; false if the database is already current.
func InitStore(ctx context.Context, dialect string, url string) (bool, error) {
const op = "schema.InitStore"
d, err := sql.Open(dialect, url)
if err != nil {
return false, errors.Wrap(err, op)
}
sMan, err := NewManager(ctx, dialect, d)
if err != nil {
return false, errors.Wrap(err, op)
}
st, err := sMan.CurrentState(ctx)
if err != nil {
return false, errors.Wrap(err, op)
}
if st.Dirty {
return false, errors.New(errors.MigrationIntegrity, op, "db marked dirty")
}
if st.InitializationStarted && st.DatabaseSchemaVersion == st.BinarySchemaVersion {
return false, nil
}
if err := sMan.RollForward(ctx); err != nil {
return false, errors.Wrap(err, op)
}
return true, nil
}