Skip to content

Commit 7544d95

Browse files
committed
feat: allow additional SQL migrations
1 parent 0072ddf commit 7544d95

File tree

12 files changed

+59
-32
lines changed

12 files changed

+59
-32
lines changed

cmd/cli/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type Handler struct {
1616

1717
func NewHandler(slOpts []servicelocatorx.Option, dOpts []driver.OptionsModifier, cOpts []configx.OptionModifier) *Handler {
1818
return &Handler{
19-
Migration: newMigrateHandler(),
19+
Migration: newMigrateHandler(slOpts, dOpts, cOpts),
2020
Janitor: NewJanitorHandler(slOpts, dOpts, cOpts),
2121
}
2222
}

cmd/cli/handler_migrate.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,18 @@ import (
3434
"github.com/ory/x/flagx"
3535
)
3636

37-
type MigrateHandler struct{}
37+
type MigrateHandler struct {
38+
slOpts []servicelocatorx.Option
39+
dOpts []driver.OptionsModifier
40+
cOpts []configx.OptionModifier
41+
}
3842

39-
func newMigrateHandler() *MigrateHandler {
40-
return &MigrateHandler{}
43+
func newMigrateHandler(slOpts []servicelocatorx.Option, dOpts []driver.OptionsModifier, cOpts []configx.OptionModifier) *MigrateHandler {
44+
return &MigrateHandler{
45+
slOpts: slOpts,
46+
dOpts: dOpts,
47+
cOpts: cOpts,
48+
}
4149
}
4250

4351
const (
@@ -262,21 +270,21 @@ func (h *MigrateHandler) MigrateGen(cmd *cobra.Command, args []string) {
262270
os.Exit(0)
263271
}
264272

265-
func makePersister(cmd *cobra.Command, args []string) (p persistence.Persister, err error) {
273+
func (h *MigrateHandler) makePersister(cmd *cobra.Command, args []string) (p persistence.Persister, err error) {
266274
var d driver.Registry
267275

268276
if flagx.MustGetBool(cmd, "read-from-env") {
269277
d, err = driver.New(
270278
cmd.Context(),
271279
servicelocatorx.NewOptions(),
272-
[]driver.OptionsModifier{
280+
append([]driver.OptionsModifier{
273281
driver.WithOptions(
274282
configx.SkipValidation(),
275283
configx.WithFlags(cmd.Flags())),
276284
driver.DisableValidation(),
277285
driver.DisablePreloading(),
278286
driver.SkipNetworkInit(),
279-
})
287+
}, h.dOpts...))
280288
if err != nil {
281289
return nil, err
282290
}
@@ -292,7 +300,7 @@ func makePersister(cmd *cobra.Command, args []string) (p persistence.Persister,
292300
d, err = driver.New(
293301
cmd.Context(),
294302
servicelocatorx.NewOptions(),
295-
[]driver.OptionsModifier{
303+
append([]driver.OptionsModifier{
296304
driver.WithOptions(
297305
configx.WithFlags(cmd.Flags()),
298306
configx.SkipValidation(),
@@ -301,7 +309,7 @@ func makePersister(cmd *cobra.Command, args []string) (p persistence.Persister,
301309
driver.DisableValidation(),
302310
driver.DisablePreloading(),
303311
driver.SkipNetworkInit(),
304-
})
312+
}, h.dOpts...))
305313
if err != nil {
306314
return nil, err
307315
}
@@ -310,7 +318,7 @@ func makePersister(cmd *cobra.Command, args []string) (p persistence.Persister,
310318
}
311319

312320
func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) (err error) {
313-
p, err := makePersister(cmd, args)
321+
p, err := h.makePersister(cmd, args)
314322
if err != nil {
315323
return err
316324
}
@@ -360,7 +368,7 @@ func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) (err erro
360368
}
361369

362370
func (h *MigrateHandler) MigrateStatus(cmd *cobra.Command, args []string) error {
363-
p, err := makePersister(cmd, args)
371+
p, err := h.makePersister(cmd, args)
364372
if err != nil {
365373
return err
366374
}

cmd/migrate_status.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package cmd
55

66
import (
7+
"github.com/ory/x/cmdx"
78
"github.com/ory/x/configx"
89
"github.com/ory/x/servicelocatorx"
910

@@ -20,6 +21,7 @@ func NewMigrateStatusCmd(slOpts []servicelocatorx.Option, dOpts []driver.Options
2021
RunE: cli.NewHandler(slOpts, dOpts, cOpts).Migration.MigrateStatus,
2122
}
2223

24+
cmdx.RegisterFormatFlags(cmd.PersistentFlags())
2325
cmd.Flags().BoolP("read-from-env", "e", false, "If set, reads the database connection string from the environment variable DSN or config file key dsn.")
2426
cmd.Flags().Bool("block", false, "Block until all migrations have been applied")
2527

driver/factory.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package driver
55

66
import (
77
"context"
8+
"io/fs"
89

910
"github.com/ory/hydra/v2/driver/config"
1011
"github.com/ory/x/configx"
@@ -22,7 +23,10 @@ type (
2223
// The first default refers to determining the NID at startup; the second default referes to the fact that the Contextualizer may dynamically change the NID.
2324
skipNetworkInit bool
2425
tracerWrapper TracerWrapper
26+
extraMigrations []fs.FS
2527
}
28+
OptionsModifier func(*options)
29+
2630
TracerWrapper func(*otelx.Tracer) *otelx.Tracer
2731
)
2832

@@ -34,14 +38,12 @@ func newOptions() *options {
3438
}
3539
}
3640

37-
func WithConfig(config *config.DefaultProvider) func(o *options) {
41+
func WithConfig(config *config.DefaultProvider) OptionsModifier {
3842
return func(o *options) {
3943
o.config = config
4044
}
4145
}
4246

43-
type OptionsModifier func(*options)
44-
4547
func WithOptions(opts ...configx.OptionModifier) OptionsModifier {
4648
return func(o *options) {
4749
o.opts = append(o.opts, opts...)
@@ -77,6 +79,13 @@ func WithTracerWrapper(wrapper TracerWrapper) OptionsModifier {
7779
}
7880
}
7981

82+
// WithExtraMigrations specifies additional database migration.
83+
func WithExtraMigrations(m ...fs.FS) OptionsModifier {
84+
return func(o *options) {
85+
o.extraMigrations = append(o.extraMigrations, m...)
86+
}
87+
}
88+
8089
func New(ctx context.Context, sl *servicelocatorx.Options, opts []OptionsModifier) (Registry, error) {
8190
o := newOptions()
8291
for _, f := range opts {
@@ -115,7 +124,7 @@ func New(ctx context.Context, sl *servicelocatorx.Options, opts []OptionsModifie
115124
r.WithTracerWrapper(o.tracerWrapper)
116125
}
117126

118-
if err = r.Init(ctx, o.skipNetworkInit, false, ctxter); err != nil {
127+
if err = r.Init(ctx, o.skipNetworkInit, false, ctxter, o.extraMigrations); err != nil {
119128
l.WithError(err).Error("Unable to initialize service registry.")
120129
return nil, err
121130
}

driver/registry.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package driver
55

66
import (
77
"context"
8+
"io/fs"
89
"net/http"
910

1011
"go.opentelemetry.io/otel/trace"
@@ -44,7 +45,7 @@ import (
4445
type Registry interface {
4546
dbal.Driver
4647

47-
Init(ctx context.Context, skipNetworkInit bool, migrate bool, ctxer contextx.Contextualizer) error
48+
Init(ctx context.Context, skipNetworkInit bool, migrate bool, ctxer contextx.Contextualizer, extraMigrations []fs.FS) error
4849

4950
WithBuildInfo(v, h, d string) Registry
5051
WithConfig(c *config.DefaultProvider) Registry
@@ -89,7 +90,7 @@ func NewRegistryFromDSN(ctx context.Context, c *config.DefaultProvider, l *logru
8990
if err != nil {
9091
return nil, err
9192
}
92-
if err := registry.Init(ctx, skipNetworkInit, migrate, ctxer); err != nil {
93+
if err := registry.Init(ctx, skipNetworkInit, migrate, ctxer, nil); err != nil {
9394
return nil, err
9495
}
9596
return registry, nil

driver/registry_base_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestRegistryBase_newKeyStrategy_handlesNetworkError(t *testing.T) {
6767
r := registry.(*RegistrySQL)
6868
r.initialPing = failedPing(errors.New("snizzles"))
6969

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

7272
registryBase := RegistryBase{r: r, l: l}
7373
registryBase.WithConfig(c)

driver/registry_sql.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package driver
55

66
import (
77
"context"
8+
"io/fs"
89
"strings"
910
"time"
1011

@@ -64,7 +65,11 @@ func NewRegistrySQL() *RegistrySQL {
6465
}
6566

6667
func (m *RegistrySQL) Init(
67-
ctx context.Context, skipNetworkInit bool, migrate bool, ctxer contextx.Contextualizer,
68+
ctx context.Context,
69+
skipNetworkInit bool,
70+
migrate bool,
71+
ctxer contextx.Contextualizer,
72+
extraMigrations []fs.FS,
6873
) error {
6974
if m.persister == nil {
7075
m.WithContextualizer(ctxer)
@@ -100,7 +105,7 @@ func (m *RegistrySQL) Init(
100105
return errorsx.WithStack(err)
101106
}
102107

103-
p, err := sql.NewPersister(ctx, c, m, m.Config(), m.l)
108+
p, err := sql.NewPersister(ctx, c, m, m.Config(), extraMigrations)
104109
if err != nil {
105110
return err
106111
}

driver/registry_sql_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestDefaultKeyManager_HsmDisabled(t *testing.T) {
3131
reg, err := NewRegistryWithoutInit(c, l)
3232
r := reg.(*RegistrySQL)
3333
r.initialPing = sussessfulPing()
34-
if err := r.Init(context.Background(), true, false, &contextx.Default{}); err != nil {
34+
if err := r.Init(context.Background(), true, false, &contextx.Default{}, nil); err != nil {
3535
t.Fatalf("unable to init registry: %s", err)
3636
}
3737
assert.NoError(t, err)

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ require (
4040
github.com/ory/fosite v0.44.1-0.20230704083823-8098e48b2e09
4141
github.com/ory/go-acc v0.2.9-0.20230103102148-6b1c9a70dbbe
4242
github.com/ory/graceful v0.1.3
43-
github.com/ory/herodot v0.10.2
43+
github.com/ory/herodot v0.10.3-0.20230626083119-d7e5192f0d88
4444
github.com/ory/hydra-client-go/v2 v2.1.1
4545
github.com/ory/jsonschema/v3 v3.0.8
46-
github.com/ory/x v0.0.567
46+
github.com/ory/x v0.0.574
4747
github.com/pborman/uuid v1.2.1
4848
github.com/pkg/errors v0.9.1
4949
github.com/prometheus/client_golang v1.13.0

go.sum

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
549549
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
550550
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
551551
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
552+
github.com/laher/mergefs v0.1.1 h1:nV2bTS57vrmbMxeR6uvJpI8LyGl3QHj4bLBZO3aUV58=
552553
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
553554
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
554555
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
@@ -653,12 +654,12 @@ github.com/ory/go-convenience v0.1.0 h1:zouLKfF2GoSGnJwGq+PE/nJAE6dj2Zj5QlTgmMTs
653654
github.com/ory/go-convenience v0.1.0/go.mod h1:uEY/a60PL5c12nYz4V5cHY03IBmwIAEm8TWB0yn9KNs=
654655
github.com/ory/graceful v0.1.3 h1:FaeXcHZh168WzS+bqruqWEw/HgXWLdNv2nJ+fbhxbhc=
655656
github.com/ory/graceful v0.1.3/go.mod h1:4zFz687IAF7oNHHiB586U4iL+/4aV09o/PYLE34t2bA=
656-
github.com/ory/herodot v0.10.2 h1:gGvNMHgAwWzdP/eo+roSiT5CGssygHSjDU7MSQNlJ4E=
657-
github.com/ory/herodot v0.10.2/go.mod h1:MMNmY6MG1uB6fnXYFaHoqdV23DTWctlPsmRCeq/2+wc=
657+
github.com/ory/herodot v0.10.3-0.20230626083119-d7e5192f0d88 h1:J0CIFKdpUeqKbVMw7pQ1qLtUnflRM1JWAcOEq7Hp4yg=
658+
github.com/ory/herodot v0.10.3-0.20230626083119-d7e5192f0d88/go.mod h1:MMNmY6MG1uB6fnXYFaHoqdV23DTWctlPsmRCeq/2+wc=
658659
github.com/ory/jsonschema/v3 v3.0.8 h1:Ssdb3eJ4lDZ/+XnGkvQS/te0p+EkolqwTsDOCxr/FmU=
659660
github.com/ory/jsonschema/v3 v3.0.8/go.mod h1:ZPzqjDkwd3QTnb2Z6PAS+OTvBE2x5i6m25wCGx54W/0=
660-
github.com/ory/x v0.0.567 h1:oUj75hIqBv3ESsmIwc/4u8jaD2zSx/HTGzRnfMKUykg=
661-
github.com/ory/x v0.0.567/go.mod h1:g0QdN0Z47vdCYtfrTQkgWJdIOPuez9VGiuQivLxa4lo=
661+
github.com/ory/x v0.0.574 h1:JjdOP6iIh4ngoR1zDxaZL9bsBzIAyvw0aZdqSfJOEVI=
662+
github.com/ory/x v0.0.574/go.mod h1:aeJFTlvDLGYSABzPS3z5SeLcYC52Ek7uGZiuYGcTMSU=
662663
github.com/pborman/uuid v1.2.1 h1:+ZZIw58t/ozdjRaXh/3awHfmWRbzYxJoAdNJxe/3pvw=
663664
github.com/pborman/uuid v1.2.1/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
664665
github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE=

0 commit comments

Comments
 (0)