Files
threshold/internal/db/schema/edition.go
T
Timothy Messier 8caaf3e076 refactor(schema): Add support for migration editions
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
2021-10-08 06:50:26 -04:00

69 lines
1.4 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: struct{}{},
}
// 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) {
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))
}
}
e = append(e, edition.New(name, dialect, fs, priority))
e.Sort()
editions.m[dialect] = e
}