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 5 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
1 change: 1 addition & 0 deletions driver/configuration/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type Provider interface {
AdminListenOn() string
PublicListenOn() string
DSN() string
DSNAddress() string

SessionSecrets() [][]byte

Expand Down
9 changes: 9 additions & 0 deletions driver/configuration/provider_viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ func (p *ViperProvider) DSN() string {
return ""
}

func (p *ViperProvider) DSNAddress() string {
dsn := p.DSN()
if dsn == "memory" {
return "sqlite://:memory:?_fk=true"
zepatrik marked this conversation as resolved.
Show resolved Hide resolved
}

return dsn
}

func (p *ViperProvider) SelfServiceLoginBeforeHooks() []SelfServiceHook {
return p.selfServiceHooks(ViperKeySelfServiceLoginBeforeConfig)
}
Expand Down
16 changes: 13 additions & 3 deletions driver/registry.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package driver

import (
"github.com/go-errors/errors"
"context"

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

"github.com/ory/kratos/courier"
Expand Down Expand Up @@ -107,15 +109,23 @@ type selfServiceStrategy interface {
}

func NewRegistry(c configuration.Provider) (Registry, error) {
driver, err := dbal.GetDriverFor(c.DSN())
driver, err := dbal.GetDriverFor(c.DSNAddress())
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 c.DSN() == "memory" {
zepatrik marked this conversation as resolved.
Show resolved Hide resolved
registry.Logger().Print("Kratos is running migrations on every startup as DSN is memory.\n")
registry.Logger().Print("This means your data are lost when Kratos terminates.\n")
zepatrik marked this conversation as resolved.
Show resolved Hide resolved
if err := registry.Persister().MigrateUp(context.Background()); err != nil {
return nil, err
}
}
return registry, nil
}
2 changes: 1 addition & 1 deletion selfservice/errorx/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestPersister(p Persister) func(t *testing.T) {
actual, err := p.Read(context.Background(), actualID)
require.NoError(t, err)

assert.JSONEq(t, `{"code":404,"status":"Not Found","reason":"foobar","message":"The requested resource could not be found"}`, gjson.Get(toJSON(t, actual),"errors.0").String(), toJSON(t, actual))
assert.JSONEq(t, `{"code":404,"status":"Not Found","reason":"foobar","message":"The requested resource could not be found"}`, gjson.Get(toJSON(t, actual), "errors.0").String(), toJSON(t, actual))
})

t.Run("case=clear", func(t *testing.T) {
Expand Down