This refactors the schema package and subpackages to support editions.
An Edition is a collection of sql migrations. A given boundary binary
can have multiple migration editions. The `schema.Manager` is updated to
apply the necessary migrations in the correct order, leveraging an
Edition's priority to determine the order in which the separate Editions
are applied.
The sql migration files are now nested under a directory for the
edition.
This also switches from the generated code to using embed, which
eliminates the need for the `make migrations` target and the generated
code. As such this commit removes the target and generated code.
See:
https://pkg.go.dev/embed
41 lines
1015 B
Go
41 lines
1015 B
Go
package schema
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hashicorp/boundary/internal/db/common"
|
|
"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 WithEditions(...) option.
|
|
func MigrateStore(ctx context.Context, dialect Dialect, url string, opt ...Option) (bool, error) {
|
|
const op = "schema.MigrateStore"
|
|
|
|
d, err := common.SqlOpen(dialect.String(), 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.Initialized && st.MigrationsApplied() {
|
|
return false, nil
|
|
}
|
|
|
|
if err := sMan.ApplyMigrations(ctx); err != nil {
|
|
return false, errors.Wrap(ctx, err, op)
|
|
}
|
|
|
|
return true, nil
|
|
}
|