Skip to content

Commit 3369ebd

Browse files
zepatrikory-bot
authored andcommitted
test(hydra): add plaintext backups for all DB types
GitOrigin-RevId: ee2c8ff9ffacff66d09241827780350979957dba
1 parent 07284c7 commit 3369ebd

18 files changed

Lines changed: 181 additions & 385 deletions

File tree

driver/registry.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/ory/kratos/x/nosurfx"
3838
"github.com/ory/nosurf"
3939
"github.com/ory/x/contextx"
40-
"github.com/ory/x/dbal"
4140
"github.com/ory/x/healthx"
4241
"github.com/ory/x/jsonnetsecure"
4342
"github.com/ory/x/logrusx"
@@ -47,8 +46,6 @@ import (
4746
)
4847

4948
type Registry interface {
50-
dbal.Driver
51-
5249
Init(ctx context.Context, ctxer contextx.Contextualizer, opts ...RegistryOption) error
5350

5451
SetLogger(l *logrusx.Logger)

oryx/dbal/canonicalize.go

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,7 @@
33

44
package dbal
55

6-
import "github.com/ory/x/cmdx"
7-
86
const (
9-
// DriverMySQL is the mysql driver name.
10-
DriverMySQL = "mysql"
11-
12-
// DriverPostgreSQL is the postgres driver name.
13-
DriverPostgreSQL = "postgres"
14-
15-
// DriverCockroachDB is the cockroach driver name.
7+
DriverMySQL = "mysql"
168
DriverCockroachDB = "cockroach"
17-
18-
// UnknownDriver is the driver name if the driver is unknown.
19-
UnknownDriver = "unknown"
209
)
21-
22-
// Canonicalize returns constants DriverMySQL, DriverPostgreSQL, DriverCockroachDB, UnknownDriver, depending on `database`.
23-
func Canonicalize(database string) string {
24-
switch database {
25-
case "mysql":
26-
return DriverMySQL
27-
case "pgx", "pq", "postgres", "postgresql":
28-
return DriverPostgreSQL
29-
case "cockroach":
30-
return DriverCockroachDB
31-
default:
32-
return UnknownDriver
33-
}
34-
}
35-
36-
// MustCanonicalize returns constants DriverMySQL, DriverPostgreSQL, DriverCockroachDB or fatals.
37-
func MustCanonicalize(database string) string {
38-
d := Canonicalize(database)
39-
if d == UnknownDriver {
40-
cmdx.Fatalf("Unknown database driver: %s", database)
41-
}
42-
return d
43-
}

oryx/dbal/driver.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,9 @@
44
package dbal
55

66
import (
7-
"context"
8-
"errors"
97
"strings"
108
)
119

12-
var (
13-
// ErrNoResponsibleDriverFound is returned when no driver was found for the provided DSN.
14-
ErrNoResponsibleDriverFound = errors.New("dsn value requested an unknown driver")
15-
ErrSQLiteSupportMissing = errors.New(`the DSN connection string looks like a SQLite connection, but SQLite support was not built into the binary. Please check if you have downloaded the correct binary or are using the correct Docker Image. Binary archives and Docker Images indicate SQLite support by appending the -sqlite suffix`)
16-
)
17-
18-
// Driver represents a driver
19-
type Driver interface {
20-
// Ping returns nil if the driver has connectivity and is healthy or an error otherwise.
21-
Ping() error
22-
PingContext(context.Context) error
23-
}
24-
2510
// IsSQLite returns true if the connection is a SQLite string.
2611
func IsSQLite(dsn string) bool {
2712
scheme := strings.Split(dsn, "://")[0]

oryx/dbal/dsn.go

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,12 @@ import (
77
"fmt"
88
"os"
99
"regexp"
10-
)
10+
"testing"
1111

12-
const (
13-
// SQLiteInMemory is a DNS string for SQLite in-memory database.
14-
//
15-
// DEPRECATED: Do not use this DSN string as it can cause flaky tests
16-
// due to the way SQL connection pooling works. Please use NewSQLiteTestDatabase instead.
17-
SQLiteInMemory = "sqlite://file::memory:?_fk=true"
18-
// SQLiteSharedInMemory is a DNS string for SQLite in-memory database in shared mode.
19-
//
20-
// DEPRECATED: Do not use this DSN string as it can cause flaky tests
21-
// due to the way SQL connection pooling works. Please use NewSQLiteTestDatabase instead.
22-
SQLiteSharedInMemory = "sqlite://file::memory:?_fk=true&cache=shared"
12+
"github.com/stretchr/testify/require"
2313
)
2414

25-
var dsnRegex = regexp.MustCompile(`^(sqlite://file:(?:.+)\?((\w+=\w+)(&\w+=\w+)*)?(&?mode=memory)(&\w+=\w+)*)$|(?:sqlite://(file:)?:memory:(?:\?\w+=\w+)?(?:&\w+=\w+)*)|^(?:(?::memory:)|(?:memory))$`)
15+
var sqliteMemoryRegexp = regexp.MustCompile(`^sqlite://file:.+\?.*&?mode=memory($|&.*)|sqlite://(file:)?:memory:\?.*$|^(:memory:|memory)$`)
2616

2717
// IsMemorySQLite returns true if a given DSN string is pointing to a SQLite database.
2818
//
@@ -31,28 +21,16 @@ var dsnRegex = regexp.MustCompile(`^(sqlite://file:(?:.+)\?((\w+=\w+)(&\w+=\w+)*
3121
// - shared connection
3222
// - shared but unique in the same process
3323
// see: https://sqlite.org/inmemorydb.html
34-
func IsMemorySQLite(dsn string) bool {
35-
return dsnRegex.MatchString(dsn)
36-
}
24+
func IsMemorySQLite(dsn string) bool { return sqliteMemoryRegexp.MatchString(dsn) }
3725

38-
// NewSharedUniqueInMemorySQLiteDatabase creates a new unique SQLite database
26+
// NewSQLiteTestDatabase creates a new, unique SQLite database
3927
// which is shared amongst all callers and identified by an individual file name.
40-
//
41-
// DEPRECATED: Please use NewSQLiteTestDatabase instead.
42-
func NewSharedUniqueInMemorySQLiteDatabase() (string, error) {
43-
dir, err := os.MkdirTemp(os.TempDir(), "unique-sqlite-db-*")
44-
if err != nil {
45-
return "", err
46-
}
47-
return fmt.Sprintf("sqlite://file:%s/db.sqlite?_fk=true&mode=memory&cache=shared", dir), nil
48-
}
49-
50-
// NewSQLiteTestDatabase creates a new in-memory, unique SQLite database
51-
// which is shared amongst all callers and identified by an individual file name.
52-
func NewSQLiteTestDatabase(t interface {
53-
TempDir() string
54-
}) string {
55-
return NewSQLiteInMemoryDatabase(t.TempDir())
28+
// The database file is created in the system's temporary directory, and not actively
29+
// removed to allow debugging in case of test failures.
30+
func NewSQLiteTestDatabase(t testing.TB) string {
31+
fn, err := os.MkdirTemp("", "sqlite-test-db-*")
32+
require.NoError(t, err)
33+
return NewSQLiteDatabase(fn)
5634
}
5735

5836
// NewSQLiteInMemoryDatabase creates a new in-memory, unique SQLite database
@@ -71,11 +49,3 @@ func NewSQLiteInMemoryDatabase(name string) string {
7149
func NewSQLiteDatabase(name string) string {
7250
return fmt.Sprintf("sqlite://file:%s/test.db?_fk=true&_journal=WAL", name)
7351
}
74-
75-
// NewSQLiteTestDatabaseOnDisk creates a new on-disk, unique SQLite database
76-
// which is shared amongst all callers and identified by an individual file name.
77-
func NewSQLiteTestDatabaseOnDisk(t interface {
78-
TempDir() string
79-
}) string {
80-
return NewSQLiteDatabase(t.TempDir())
81-
}

oryx/dbal/stub/a/1.sql

Lines changed: 0 additions & 7 deletions
This file was deleted.

oryx/dbal/stub/a/3.sql

Lines changed: 0 additions & 7 deletions
This file was deleted.

oryx/dbal/stub/b/2.sql

Lines changed: 0 additions & 7 deletions
This file was deleted.

oryx/dbal/stub/c/2.sql

Lines changed: 0 additions & 7 deletions
This file was deleted.

oryx/dbal/stub/c/4.sql

Lines changed: 0 additions & 7 deletions
This file was deleted.

oryx/dbal/stub/d/1_test.sql

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)