-
-
Notifications
You must be signed in to change notification settings - Fork 931
/
persister.go
98 lines (77 loc) · 2.45 KB
/
persister.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package sql
import (
"context"
"io"
"github.com/gobuffalo/pop/v5"
"github.com/markbates/pkger"
"github.com/pkg/errors"
"github.com/ory/kratos/driver/configuration"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/persistence"
"github.com/ory/kratos/schema"
"github.com/ory/kratos/x"
)
var _ persistence.Persister = new(Persister)
var migrations = pkger.Dir("/persistence/sql/migrations/sql") // do not remove this!
type (
persisterDependencies interface {
IdentityTraitsSchemas() schema.Schemas
identity.ValidationProvider
x.LoggingProvider
}
Persister struct {
c *pop.Connection
mb x.MigrationPkger
r persisterDependencies
cf configuration.Provider
isSQLite bool
}
)
func NewPersister(r persisterDependencies, conf configuration.Provider, c *pop.Connection) (*Persister, error) {
m, err := x.NewPkgerMigration(migrations, c)
if err != nil {
return nil, errors.WithStack(err)
}
return &Persister{c: c, mb: m, cf: conf, r: r, isSQLite: c.Dialect.Name() == "sqlite3"}, nil
}
func (p *Persister) Connection() *pop.Connection {
return p.c
}
func (p *Persister) MigrationStatus(ctx context.Context, w io.Writer) error {
return errors.WithStack(p.mb.Status(w))
}
func (p *Persister) MigrateDown(ctx context.Context, steps int) error {
// FIXME https://github.com/gobuffalo/pop/issues/574
if p.isSQLite {
if err := p.c.RawQuery(`PRAGMA legacy_alter_table=on; PRAGMA foreign_keys=off;`).Exec(); err != nil {
return errors.WithStack(err)
}
if err := p.mb.Down(steps); err != nil {
return errors.WithStack(err)
}
return p.c.RawQuery(`PRAGMA legacy_alter_table=off; PRAGMA foreign_keys=on;`).Exec()
}
return errors.WithStack(p.mb.Down(steps))
}
func (p *Persister) MigrateUp(ctx context.Context) error {
// FIXME https://github.com/gobuffalo/pop/issues/574
if p.isSQLite && false {
if err := p.c.RawQuery(`PRAGMA legacy_alter_table=on; PRAGMA foreign_keys=off;`).Exec(); err != nil {
return errors.WithStack(err)
}
if err := p.mb.Up(); err != nil {
return errors.WithStack(err)
}
return p.c.RawQuery(`PRAGMA legacy_alter_table=off; PRAGMA foreign_keys=on;`).Exec()
}
return errors.WithStack(p.mb.Up())
}
func (p *Persister) Close(ctx context.Context) error {
return errors.WithStack(p.GetConnection(ctx).Close())
}
func (p *Persister) Ping(ctx context.Context) error {
type pinger interface {
Ping() error
}
return errors.WithStack(p.GetConnection(ctx).Store.(pinger).Ping())
}