Skip to content

Commit

Permalink
feat: allow extra database migrations (#1365)
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr authored Jul 4, 2023
1 parent f0839ee commit d3b62a9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
28 changes: 15 additions & 13 deletions internal/driver/registry_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package driver

import (
"context"
"io/fs"
"net/http"
"sync"

Expand Down Expand Up @@ -50,18 +51,19 @@ var (

type (
RegistryDefault struct {
p persistence.Persister
traverser relationtuple.Traverser
mb *popx.MigrationBox
l *logrusx.Logger
w herodot.Writer
ce *check.Engine
ee *expand.Engine
c *config.Config
conn *pop.Connection
ctxer ketoctx.Contextualizer
mapper *relationtuple.Mapper
readOnlyMapper *relationtuple.Mapper
p persistence.Persister
traverser relationtuple.Traverser
mb *popx.MigrationBox
extraMigrations []fs.FS
l *logrusx.Logger
w herodot.Writer
ce *check.Engine
ee *expand.Engine
c *config.Config
conn *pop.Connection
ctxer ketoctx.Contextualizer
mapper *relationtuple.Mapper
readOnlyMapper *relationtuple.Mapper

initialized sync.Once
healthH *healthx.Handler
Expand Down Expand Up @@ -251,7 +253,7 @@ func (r *RegistryDefault) MigrationBox(ctx context.Context) (*popx.MigrationBox,
}

mb, err := popx.NewMigrationBox(
fsx.Merge(sql.Migrations, networkx.Migrations),
fsx.Merge(append([]fs.FS{sql.Migrations, networkx.Migrations}, r.extraMigrations...)...),
popx.NewMigrator(c, r.Logger(), r.Tracer(ctx), 0),
append(
[]popx.MigrationBoxOption{popx.WithGoMigrations(uuidmapping.Migrations(namespaces))},
Expand Down
1 change: 1 addition & 0 deletions internal/driver/registry_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func NewDefaultRegistry(ctx context.Context, flags *pflag.FlagSet, withoutNetwor
defaultUnaryInterceptors: options.GRPCUnaryInterceptors(),
defaultStreamInterceptors: options.GRPCStreamInterceptors(),
defaultHttpMiddlewares: options.HTTPMiddlewares(),
extraMigrations: options.ExtraMigrations(),
defaultMigrationOptions: options.MigrationOptions(),
healthReadyCheckers: options.ReadyCheckers(),
}
Expand Down
13 changes: 13 additions & 0 deletions ketoctx/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package ketoctx

import (
"io/fs"
"net/http"

"github.com/ory/x/healthx"
Expand All @@ -23,6 +24,7 @@ type (
grpcStreamInterceptors []grpc.StreamServerInterceptor
migrationOpts []popx.MigrationBoxOption
readyCheckers healthx.ReadyCheckers
extraMigrations []fs.FS
}
Option func(o *opts)
TracerWrapper func(*otelx.Tracer) *otelx.Tracer
Expand Down Expand Up @@ -68,6 +70,13 @@ func WithGRPCStreamInterceptors(i ...grpc.StreamServerInterceptor) Option {
}
}

// WithExtraMigrations adds additional database migrations.
func WithExtraMigrations(o ...fs.FS) Option {
return func(opts *opts) {
opts.extraMigrations = append(opts.extraMigrations, o...)
}
}

// WithMigrationOptions adds migration options to the list of migration options.
func WithMigrationOptions(o ...popx.MigrationBoxOption) Option {
return func(opts *opts) {
Expand Down Expand Up @@ -107,6 +116,10 @@ func (o *opts) GRPCStreamInterceptors() []grpc.StreamServerInterceptor {
return o.grpcStreamInterceptors
}

func (o *opts) ExtraMigrations() []fs.FS {
return o.extraMigrations
}

func (o *opts) MigrationOptions() []popx.MigrationBoxOption {
return o.migrationOpts
}
Expand Down

0 comments on commit d3b62a9

Please sign in to comment.