2021-09-10 09:31:27 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2021-01-25 14:43:50 -07:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2021-01-19 10:40:41 -07:00
|
|
|
package schema
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2021-09-30 09:17:27 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/db/common"
|
2021-01-19 10:40:41 -07:00
|
|
|
"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-09-10 09:31:27 -04:00
|
|
|
// or if there was an error. Supports the WithEditions(...) option.
|
|
|
|
|
func MigrateStore(ctx context.Context, dialect Dialect, url string, opt ...Option) (bool, error) {
|
2021-01-25 14:43:50 -07:00
|
|
|
const op = "schema.MigrateStore"
|
|
|
|
|
|
2021-09-10 09:31:27 -04:00
|
|
|
d, err := common.SqlOpen(dialect.String(), url)
|
2021-01-19 10:40:41 -07:00
|
|
|
if err != nil {
|
2021-07-29 18:52:56 -06:00
|
|
|
return false, errors.Wrap(ctx, err, op)
|
2021-01-19 10:40:41 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-08 20:54:32 -04:00
|
|
|
sMan, err := NewManager(ctx, dialect, d, opt...)
|
2021-01-19 10:40:41 -07:00
|
|
|
if err != nil {
|
2021-07-29 18:52:56 -06:00
|
|
|
return false, errors.Wrap(ctx, err, op)
|
2021-01-19 10:40:41 -07:00
|
|
|
}
|
2025-02-06 13:49:49 -08:00
|
|
|
defer sMan.Close(ctx)
|
2021-01-19 10:40:41 -07:00
|
|
|
|
|
|
|
|
st, err := sMan.CurrentState(ctx)
|
|
|
|
|
if err != nil {
|
2021-07-29 18:52:56 -06:00
|
|
|
return false, errors.Wrap(ctx, err, op)
|
2021-01-19 10:40:41 -07:00
|
|
|
}
|
|
|
|
|
|
2021-09-10 09:31:27 -04:00
|
|
|
if st.Initialized && st.MigrationsApplied() {
|
2021-01-19 10:40:41 -07:00
|
|
|
return false, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-26 20:19:09 +00:00
|
|
|
if _, err := sMan.ApplyMigrations(ctx); err != nil {
|
2021-07-29 18:52:56 -06:00
|
|
|
return false, errors.Wrap(ctx, err, op)
|
2021-01-19 10:40:41 -07:00
|
|
|
}
|
2021-01-25 14:43:50 -07:00
|
|
|
|
2021-01-19 10:40:41 -07:00
|
|
|
return true, nil
|
|
|
|
|
}
|