From 8eed306800fa330a1cda752dbb11ddf09faf25ad Mon Sep 17 00:00:00 2001 From: Arne Luenser Date: Thu, 10 Aug 2023 14:29:05 +0200 Subject: [PATCH] feat: allow Go migrations (#3602) --- driver/factory.go | 10 +++++++++- driver/registry.go | 5 +++-- driver/registry_base_test.go | 2 +- driver/registry_sql.go | 4 +++- driver/registry_sql_test.go | 2 +- hsm/manager_hsm_test.go | 2 +- persistence/sql/persister.go | 7 +++++-- 7 files changed, 23 insertions(+), 9 deletions(-) diff --git a/driver/factory.go b/driver/factory.go index 4b206ac71c..5fccbe4126 100644 --- a/driver/factory.go +++ b/driver/factory.go @@ -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" ) @@ -24,6 +25,7 @@ type ( skipNetworkInit bool tracerWrapper TracerWrapper extraMigrations []fs.FS + goMigrations []popx.Migration } OptionsModifier func(*options) @@ -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 { @@ -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 } diff --git a/driver/registry.go b/driver/registry.go index 4c956c4cd4..d518d8a7f6 100644 --- a/driver/registry.go +++ b/driver/registry.go @@ -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" @@ -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 @@ -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 diff --git a/driver/registry_base_test.go b/driver/registry_base_test.go index 4dedab5dea..c15b484805 100644 --- a/driver/registry_base_test.go +++ b/driver/registry_base_test.go @@ -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) diff --git a/driver/registry_sql.go b/driver/registry_sql.go index 361d1aa154..1fa8b9bbb6 100644 --- a/driver/registry_sql.go +++ b/driver/registry_sql.go @@ -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" ) @@ -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) @@ -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 } diff --git a/driver/registry_sql_test.go b/driver/registry_sql_test.go index cd126a3f71..218ad46b10 100644 --- a/driver/registry_sql_test.go +++ b/driver/registry_sql_test.go @@ -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) diff --git a/hsm/manager_hsm_test.go b/hsm/manager_hsm_test.go index e7bc145180..186fcc0d29 100644 --- a/hsm/manager_hsm_test.go +++ b/hsm/manager_hsm_test.go @@ -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()) diff --git a/persistence/sql/persister.go b/persistence/sql/persister.go index a9000dd314..69ee151cd6 100644 --- a/persistence/sql/persister.go +++ b/persistence/sql/persister.go @@ -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) }