This adds the ability to define hooks that will run prior to a schema migration. The hooks should only be used when a migration would fail due to data being in an invalid state, likely due to a bug in the previous schema definition. For example if a column was missing a constraint, and a new migration is attempting to add the constraint, there might already be values that violate the new constraint. In such cases, we would want the user to make a decision to address the data, and need to provide them with details about what data is problematic in case they want to manually address the problem. To help the user, if there is a sane way to address the data, we should provide an option to automatically "repair" the data, allowing the migration to continue. However, this still should be an explicit opt-in from the user, and we need to clearly describe what the repair option would do to the user so they understand the consequences of opting in. To accomplish this each migration can be provided a hook that contains a CheckFunc, RepairFunc, and RepairDescription. These functions must operate at the point-in-time of the migration immediately preceding the current migration, as as such they should not use and domain packages or repositories, instead they should use direct sql with the sql library. The description is used to explain what the RepairFunc will do. Hooks should be defined in `internal/db/schema/migrations/oss/oss.go` and registered with the schema edition. See the example in `internal/db/schema/manager_example_test.go` for more details.
73 lines
1.5 KiB
Go
73 lines
1.5 KiB
Go
package schema
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/hashicorp/boundary/internal/db/schema/internal/edition"
|
|
)
|
|
|
|
// Dialect is same as edition.Dialect
|
|
type Dialect = edition.Dialect
|
|
|
|
// Supported dialects.
|
|
const (
|
|
Postgres Dialect = "postgres"
|
|
)
|
|
|
|
var supportedDialects = map[Dialect]struct{}{
|
|
Postgres: {},
|
|
}
|
|
|
|
// type dialects map[Dialect]edition.Editions
|
|
|
|
type dialects struct {
|
|
sync.Mutex
|
|
|
|
m map[Dialect]edition.Editions
|
|
}
|
|
|
|
var editions = dialects{
|
|
m: make(map[Dialect]edition.Editions),
|
|
}
|
|
|
|
// RegisterEdition registers an edition for use by the Manager. It will panic if:
|
|
// - An unsupported dialect is provided.
|
|
// - The same (dialect, name) is registered.
|
|
// - The same (dialect, priority) is registered.
|
|
func RegisterEdition(name string, dialect Dialect, fs embed.FS, priority int, opt ...edition.Option) {
|
|
editions.Lock()
|
|
defer editions.Unlock()
|
|
|
|
if _, ok := supportedDialects[dialect]; !ok {
|
|
panic(fmt.Sprintf("unsupported dialect: %s", dialect))
|
|
}
|
|
var e edition.Editions
|
|
var ok bool
|
|
|
|
e, ok = editions.m[dialect]
|
|
if !ok {
|
|
e = make(edition.Editions, 0)
|
|
}
|
|
|
|
for _, ee := range e {
|
|
if ee.Name == name {
|
|
panic(fmt.Sprintf("edition %s with dialect %s already registered", name, dialect))
|
|
}
|
|
|
|
if ee.Priority == priority {
|
|
panic(fmt.Sprintf("edition %s with dialect %s and priority %d has same priority as edition %s", name, dialect, priority, ee.Name))
|
|
}
|
|
}
|
|
|
|
ee, err := edition.New(name, dialect, fs, priority, opt...)
|
|
if err != nil {
|
|
panic(err.Error)
|
|
}
|
|
e = append(e, ee)
|
|
e.Sort()
|
|
|
|
editions.m[dialect] = e
|
|
}
|