Files
threshold/internal/db/schema/migration_log.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

50 lines
1.3 KiB
Go

package schema
import (
"context"
"time"
"github.com/hashicorp/boundary/internal/db/schema/internal/log"
"github.com/hashicorp/boundary/internal/errors"
)
// LogEntry represents a log entry generated during migrations.
type LogEntry struct {
Id int
MigrationVersion int
MigrationEdition string
CreateTime time.Time
Entry string
}
// GetMigrationLog will retrieve the migration logs from the db for the last
// migration. Once it's read the entries, it will delete them from the database.
// The WithDeleteLog option is supported and will remove all log entries when provided.
func (b *Manager) GetMigrationLog(ctx context.Context, opt ...Option) ([]LogEntry, error) {
const op = "schema.GetMigrationLog"
var logOpts []log.Option
opts := getOpts(opt...)
if opts.withDeleteLog {
logOpts = append(logOpts, log.WithDeleteLog(opts.withDeleteLog))
}
entries, err := b.driver.GetMigrationLog(ctx, logOpts...)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
logEntries := make([]LogEntry, 0, len(entries))
for _, e := range entries {
logEntries = append(logEntries, LogEntry{
Id: e.Id,
MigrationVersion: e.MigrationVersion,
MigrationEdition: e.MigrationEdition,
CreateTime: e.CreateTime,
Entry: e.Entry,
})
}
return logEntries, nil
}