* add new error funcs which take a ctx and deprecate existing funcs * use context where available when writing error events * remove circular internal/errors pkg dep * suppress some chatty errors using errors.WithoutEvent() * convert auth oidc funcs to take context where needed to write events Co-authored-by: Jim <jlambert@hashicorp.com>
44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
package schema
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
)
|
|
|
|
// MigrateStore executes the migrations needed to initialize the store. It
|
|
// returns true if migrations actually ran; false if the database is already current
|
|
// or if there was an error. Supports the WithMigrationStates(...) option.
|
|
func MigrateStore(ctx context.Context, dialect string, url string, opt ...Option) (bool, error) {
|
|
const op = "schema.MigrateStore"
|
|
|
|
d, err := sql.Open(dialect, url)
|
|
if err != nil {
|
|
return false, errors.Wrap(ctx, err, op)
|
|
}
|
|
|
|
sMan, err := NewManager(ctx, dialect, d, opt...)
|
|
if err != nil {
|
|
return false, errors.Wrap(ctx, err, op)
|
|
}
|
|
|
|
st, err := sMan.CurrentState(ctx)
|
|
if err != nil {
|
|
return false, errors.Wrap(ctx, err, op)
|
|
}
|
|
if st.Dirty {
|
|
return false, errors.New(ctx, 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(ctx, err, op)
|
|
}
|
|
|
|
return true, nil
|
|
}
|