Files
threshold/internal/db/schema/schema.go
T
Jim 8d6dee09a9 refact: Add db.DB wrapper and refact all test fixtures to use it. (#1535)
* refact: Changes required for gorm v2

* refact: Add db.DB wrapper and refact all test fixtures to use it

Stop leaking the gorm abstraction within our unit tests.

* refact: Migrate direct calls to pq to pgx driver

The refactor will remove the remaining direct dependencies on
the pq driver which has been deprecated in favor of the pgx
driver.

It includes the addition of common.SqlOpen which is a wrapper around
sql.Open, which is needed to deal with the driver name changing from
"postgres" to "pgx" but in our code base the dialect and driver are
synonyms.
2021-09-30 09:17:27 -04:00

44 lines
1.1 KiB
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 WithMigrationStates(...) option.
func MigrateStore(ctx context.Context, dialect string, url string, opt ...Option) (bool, error) {
const op = "schema.MigrateStore"
d, err := common.SqlOpen(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
}