Skip to content

Commit

Permalink
feat: allow extra migrations in NewPersister
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr committed Jul 19, 2023
1 parent ffda1a0 commit 96c1ff7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
14 changes: 11 additions & 3 deletions driver/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package driver

import (
"context"
"io/fs"

"github.com/ory/kratos/selfservice/sessiontokenexchange"
"github.com/ory/x/contextx"
Expand Down Expand Up @@ -182,6 +183,7 @@ type options struct {
config *config.Config
replaceTracer func(*otelx.Tracer) *otelx.Tracer
inspect func(Registry) error
extraMigrations []fs.FS
}

type RegistryOption func(*options)
Expand All @@ -190,24 +192,30 @@ func SkipNetworkInit(o *options) {
o.skipNetworkInit = true
}

func WithConfig(config *config.Config) func(o *options) {
func WithConfig(config *config.Config) RegistryOption {
return func(o *options) {
o.config = config
}
}

func ReplaceTracer(f func(*otelx.Tracer) *otelx.Tracer) func(o *options) {
func ReplaceTracer(f func(*otelx.Tracer) *otelx.Tracer) RegistryOption {
return func(o *options) {
o.replaceTracer = f
}
}

func Inspect(f func(reg Registry) error) func(o *options) {
func Inspect(f func(reg Registry) error) RegistryOption {
return func(o *options) {
o.inspect = f
}
}

func WithExtraMigrations(m ...fs.FS) RegistryOption {
return func(o *options) {
o.extraMigrations = append(o.extraMigrations, m...)
}
}

func newOptions(os []RegistryOption) *options {
o := new(options)
for _, f := range os {
Expand Down
2 changes: 1 addition & 1 deletion driver/registry_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ func (m *RegistryDefault) Init(ctx context.Context, ctxer contextx.Contextualize
m.Logger().WithError(err).Warnf("Unable to open database, retrying.")
return errors.WithStack(err)
}
p, err := sql.NewPersister(ctx, m, c)
p, err := sql.NewPersister(ctx, m, c, o.extraMigrations...)
if err != nil {
m.Logger().WithError(err).Warnf("Unable to initialize persister, retrying.")
return err
Expand Down
5 changes: 3 additions & 2 deletions persistence/sql/persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package sql
import (
"context"
"embed"
"io/fs"
"time"

"github.com/gobuffalo/pop/v6"
Expand Down Expand Up @@ -53,8 +54,8 @@ type (
}
)

func NewPersister(ctx context.Context, r persisterDependencies, c *pop.Connection) (*Persister, error) {
m, err := popx.NewMigrationBox(mergefs.Merge(migrations, networkx.Migrations), popx.NewMigrator(c, r.Logger(), r.Tracer(ctx), 0))
func NewPersister(ctx context.Context, r persisterDependencies, c *pop.Connection, extraMigrations ...fs.FS) (*Persister, error) {
m, err := popx.NewMigrationBox(mergefs.Merge(append([]fs.FS{migrations, networkx.Migrations}, extraMigrations...)...), popx.NewMigrator(c, r.Logger(), r.Tracer(ctx), 0))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 96c1ff7

Please sign in to comment.