Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dsn: memory shorthand #284

Merged
merged 11 commits into from
Mar 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion driver/configuration/provider_viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,13 @@ func (p *ViperProvider) PublicListenOn() string {
}

func (p *ViperProvider) DSN() string {
if dsn := viperx.GetString(p.l, ViperKeyDSN, ""); len(dsn) > 0 {
dsn := viperx.GetString(p.l, ViperKeyDSN, "")

if dsn == "memory" {
zepatrik marked this conversation as resolved.
Show resolved Hide resolved
return "sqlite://mem.db?mode=memory&_fk=true&cache=shared"
}

if len(dsn) > 0 {
return dsn
}

Expand Down
54 changes: 54 additions & 0 deletions driver/configuration/provider_viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,57 @@ func TestViperProvider(t *testing.T) {
})
})
}

type InterceptHook struct {
lastEntry *logrus.Entry
}

func (l InterceptHook) Levels() []logrus.Level {
return []logrus.Level{logrus.FatalLevel}
}

func (l InterceptHook) Fire(e *logrus.Entry) error {
l.lastEntry = e
return nil
}

func TestViperProvider_DSN(t *testing.T) {
t.Run("case=dsn: memory", func(t *testing.T) {
viper.Reset()
viper.Set(configuration.ViperKeyDSN, "memory")

l := logrus.New()
p := configuration.NewViperProvider(l, false)

assert.Equal(t, "sqlite://mem.db?mode=memory&_fk=true&cache=shared", p.DSN())
})

t.Run("case=dsn: not memory", func(t *testing.T) {
dsn := "sqlite://foo.db?_fk=true"
viper.Reset()
viper.Set(configuration.ViperKeyDSN, dsn)

l := logrus.New()
p := configuration.NewViperProvider(l, false)

assert.Equal(t, dsn, p.DSN())
})

t.Run("case=dsn: not set", func(t *testing.T) {
dsn := ""
viper.Reset()
viper.Set(configuration.ViperKeyDSN, dsn)

l := logrus.New()
p := configuration.NewViperProvider(l, false)

var exitCode int
l.ExitFunc = func(i int) {
exitCode = i
}
h := InterceptHook{}
l.AddHook(h)
assert.Equal(t, dsn, p.DSN())
assert.NotEqual(t, 0, exitCode)
})
}
25 changes: 22 additions & 3 deletions driver/registry.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package driver

import (
"github.com/go-errors/errors"
"context"
"net/url"
"strings"

"github.com/gorilla/sessions"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/ory/kratos/courier"
Expand Down Expand Up @@ -108,15 +112,30 @@ type selfServiceStrategy interface {
}

func NewRegistry(c configuration.Provider) (Registry, error) {
driver, err := dbal.GetDriverFor(c.DSN())
dsn := c.DSN()
driver, err := dbal.GetDriverFor(dsn)
if err != nil {
return nil, err
return nil, errors.WithStack(err)
}

registry, ok := driver.(Registry)
if !ok {
return nil, errors.Errorf("driver of type %T does not implement interface Registry", driver)
}

// if dsn is memory we have to run the migrations on every start
if urlParts := strings.SplitN(dsn, "?", 1); len(urlParts) == 2 && strings.HasPrefix(dsn, "sqlite://") {
queryVals, err := url.ParseQuery(urlParts[1])
if err != nil {
return nil, errors.WithMessage(errors.WithStack(err), "unable to parse the DSN url")
}
if queryVals.Get("mode") == "memory" {
registry.Logger().Print("Kratos is running migrations on every startup as DSN is memory.\n")
registry.Logger().Print("This means your data is lost when Kratos terminates.\n")
if err := registry.Persister().MigrateUp(context.Background()); err != nil {
return nil, err
}
}
}
return registry, nil
}