Skip to content

Commit

Permalink
feat: allow Go migrations (#3602)
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr authored Aug 10, 2023
1 parent 598c21d commit 8eed306
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 9 deletions.
10 changes: 9 additions & 1 deletion driver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ory/x/configx"
"github.com/ory/x/logrusx"
"github.com/ory/x/otelx"
"github.com/ory/x/popx"
"github.com/ory/x/servicelocatorx"
)

Expand All @@ -24,6 +25,7 @@ type (
skipNetworkInit bool
tracerWrapper TracerWrapper
extraMigrations []fs.FS
goMigrations []popx.Migration
}
OptionsModifier func(*options)

Expand Down Expand Up @@ -86,6 +88,12 @@ func WithExtraMigrations(m ...fs.FS) OptionsModifier {
}
}

func WithGoMigrations(m ...popx.Migration) OptionsModifier {
return func(o *options) {
o.goMigrations = append(o.goMigrations, m...)
}
}

func New(ctx context.Context, sl *servicelocatorx.Options, opts []OptionsModifier) (Registry, error) {
o := newOptions()
for _, f := range opts {
Expand Down Expand Up @@ -124,7 +132,7 @@ func New(ctx context.Context, sl *servicelocatorx.Options, opts []OptionsModifie
r.WithTracerWrapper(o.tracerWrapper)
}

if err = r.Init(ctx, o.skipNetworkInit, false, ctxter, o.extraMigrations); err != nil {
if err = r.Init(ctx, o.skipNetworkInit, false, ctxter, o.extraMigrations, o.goMigrations); err != nil {
l.WithError(err).Error("Unable to initialize service registry.")
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions driver/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.opentelemetry.io/otel/trace"

"github.com/ory/x/httprouterx"
"github.com/ory/x/popx"

"github.com/ory/hydra/v2/aead"
"github.com/ory/hydra/v2/hsm"
Expand Down Expand Up @@ -45,7 +46,7 @@ import (
type Registry interface {
dbal.Driver

Init(ctx context.Context, skipNetworkInit bool, migrate bool, ctxer contextx.Contextualizer, extraMigrations []fs.FS) error
Init(ctx context.Context, skipNetworkInit bool, migrate bool, ctxer contextx.Contextualizer, extraMigrations []fs.FS, goMigrations []popx.Migration) error

WithBuildInfo(v, h, d string) Registry
WithConfig(c *config.DefaultProvider) Registry
Expand Down Expand Up @@ -90,7 +91,7 @@ func NewRegistryFromDSN(ctx context.Context, c *config.DefaultProvider, l *logru
if err != nil {
return nil, err
}
if err := registry.Init(ctx, skipNetworkInit, migrate, ctxer, nil); err != nil {
if err := registry.Init(ctx, skipNetworkInit, migrate, ctxer, nil, nil); err != nil {
return nil, err
}
return registry, nil
Expand Down
2 changes: 1 addition & 1 deletion driver/registry_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestRegistryBase_newKeyStrategy_handlesNetworkError(t *testing.T) {
r := registry.(*RegistrySQL)
r.initialPing = failedPing(errors.New("snizzles"))

_ = r.Init(context.Background(), true, false, &contextx.TestContextualizer{}, nil)
_ = r.Init(context.Background(), true, false, &contextx.TestContextualizer{}, nil, nil)

registryBase := RegistryBase{r: r, l: l}
registryBase.WithConfig(c)
Expand Down
4 changes: 3 additions & 1 deletion driver/registry_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/ory/x/dbal"
"github.com/ory/x/errorsx"
otelsql "github.com/ory/x/otelx/sql"
"github.com/ory/x/popx"
"github.com/ory/x/resilience"
"github.com/ory/x/sqlcon"
)
Expand Down Expand Up @@ -70,6 +71,7 @@ func (m *RegistrySQL) Init(
migrate bool,
ctxer contextx.Contextualizer,
extraMigrations []fs.FS,
goMigrations []popx.Migration,
) error {
if m.persister == nil {
m.WithContextualizer(ctxer)
Expand Down Expand Up @@ -105,7 +107,7 @@ func (m *RegistrySQL) Init(
return errorsx.WithStack(err)
}

p, err := sql.NewPersister(ctx, c, m, m.Config(), extraMigrations)
p, err := sql.NewPersister(ctx, c, m, m.Config(), extraMigrations, goMigrations)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion driver/registry_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestDefaultKeyManager_HsmDisabled(t *testing.T) {
reg, err := NewRegistryWithoutInit(c, l)
r := reg.(*RegistrySQL)
r.initialPing = sussessfulPing()
if err := r.Init(context.Background(), true, false, &contextx.Default{}, nil); err != nil {
if err := r.Init(context.Background(), true, false, &contextx.Default{}, nil, nil); err != nil {
t.Fatalf("unable to init registry: %s", err)
}
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion hsm/manager_hsm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestDefaultKeyManager_HSMEnabled(t *testing.T) {
reg.WithLogger(l)
reg.WithConfig(c)
reg.WithHsmContext(mockHsmContext)
err := reg.Init(context.Background(), false, true, &contextx.TestContextualizer{}, nil)
err := reg.Init(context.Background(), false, true, &contextx.TestContextualizer{}, nil, nil)
assert.NoError(t, err)
assert.IsType(t, &jwk.ManagerStrategy{}, reg.KeyManager())
assert.IsType(t, &sql.Persister{}, reg.SoftwareKeyManager())
Expand Down
7 changes: 5 additions & 2 deletions persistence/sql/persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,11 @@ func (p *Persister) Rollback(ctx context.Context) (err error) {
return errorsx.WithStack(tx.TX.Rollback())
}

func NewPersister(ctx context.Context, c *pop.Connection, r Dependencies, config *config.DefaultProvider, extraMigrations []fs.FS) (*Persister, error) {
mb, err := popx.NewMigrationBox(fsx.Merge(append([]fs.FS{migrations}, extraMigrations...)...), popx.NewMigrator(c, r.Logger(), r.Tracer(ctx), 0))
func NewPersister(ctx context.Context, c *pop.Connection, r Dependencies, config *config.DefaultProvider, extraMigrations []fs.FS, goMigrations []popx.Migration) (*Persister, error) {
mb, err := popx.NewMigrationBox(
fsx.Merge(append([]fs.FS{migrations}, extraMigrations...)...),
popx.NewMigrator(c, r.Logger(), r.Tracer(ctx), 0),
popx.WithGoMigrations(goMigrations))
if err != nil {
return nil, errorsx.WithStack(err)
}
Expand Down

0 comments on commit 8eed306

Please sign in to comment.