Files
threshold/internal/db/schema/schema.go
T

43 lines
1.1 KiB
Go
Raw Normal View History

package schema
import (
"context"
"database/sql"
"github.com/hashicorp/boundary/internal/errors"
)
2021-01-25 14:43:50 -07:00
// MigrateStore executes the migrations needed to initialize the store. It
// returns true if migrations actually ran; false if the database is already current
2021-04-08 20:54:32 -04:00
// or if there was an error. Supports the WithMigrationStates(...) option.
func MigrateStore(ctx context.Context, dialect string, url string, opt ...Option) (bool, error) {
2021-01-25 14:43:50 -07:00
const op = "schema.MigrateStore"
d, err := sql.Open(dialect, url)
if err != nil {
return false, errors.Wrap(err, op)
}
2021-04-08 20:54:32 -04:00
sMan, err := NewManager(ctx, dialect, d, opt...)
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)
}
2021-01-25 14:43:50 -07:00
return true, nil
}