storage/sql: initial MySQL storage implementation
It will be shared by both Postgres and MySQL configs. Signed-off-by: Pavel Borzenkov <pavel.borzenkov@gmail.com>
This commit is contained in:
committed by
Nandor Kracser
parent
92920c86ea
commit
e53bdfabb9
@@ -32,15 +32,16 @@ func withTimeout(t time.Duration, f func()) {
|
||||
}
|
||||
|
||||
func cleanDB(c *conn) error {
|
||||
_, err := c.Exec(`
|
||||
delete from client;
|
||||
delete from auth_request;
|
||||
delete from auth_code;
|
||||
delete from refresh_token;
|
||||
delete from keys;
|
||||
delete from password;
|
||||
`)
|
||||
return err
|
||||
tables := []string{"client", "auth_request", "auth_code",
|
||||
"refresh_token", "keys", "password"}
|
||||
|
||||
for _, tbl := range tables {
|
||||
_, err := c.Exec("delete from " + tbl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var logger = &logrus.Logger{
|
||||
@@ -49,23 +50,39 @@ var logger = &logrus.Logger{
|
||||
Level: logrus.DebugLevel,
|
||||
}
|
||||
|
||||
func TestSQLite3(t *testing.T) {
|
||||
type opener interface {
|
||||
open(logrus.FieldLogger) (*conn, error)
|
||||
}
|
||||
|
||||
func testDB(t *testing.T, o opener, withTransactions bool) {
|
||||
// t.Fatal has a bad habbit of not actually printing the error
|
||||
fatal := func(i interface{}) {
|
||||
fmt.Fprintln(os.Stdout, i)
|
||||
t.Fatal(i)
|
||||
}
|
||||
|
||||
newStorage := func() storage.Storage {
|
||||
// NOTE(ericchiang): In memory means we only get one connection at a time. If we
|
||||
// ever write tests that require using multiple connections, for instance to test
|
||||
// transactions, we need to move to a file based system.
|
||||
s := &SQLite3{":memory:"}
|
||||
conn, err := s.open(logger)
|
||||
conn, err := o.open(logger)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stdout, err)
|
||||
t.Fatal(err)
|
||||
fatal(err)
|
||||
}
|
||||
if err := cleanDB(conn); err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
return conn
|
||||
}
|
||||
|
||||
withTimeout(time.Second*10, func() {
|
||||
withTimeout(time.Minute*1, func() {
|
||||
conformance.RunTests(t, newStorage)
|
||||
})
|
||||
if withTransactions {
|
||||
withTimeout(time.Minute*1, func() {
|
||||
conformance.RunTransactionTests(t, newStorage)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLite3(t *testing.T) {
|
||||
testDB(t, &SQLite3{":memory:"}, false)
|
||||
}
|
||||
|
||||
func getenv(key, defaultVal string) string {
|
||||
@@ -186,37 +203,42 @@ func TestPostgres(t *testing.T) {
|
||||
if host == "" {
|
||||
t.Skipf("test environment variable %q not set, skipping", testPostgresEnv)
|
||||
}
|
||||
p := Postgres{
|
||||
Database: getenv("DEX_POSTGRES_DATABASE", "postgres"),
|
||||
User: getenv("DEX_POSTGRES_USER", "postgres"),
|
||||
Password: getenv("DEX_POSTGRES_PASSWORD", "postgres"),
|
||||
Host: host,
|
||||
SSL: PostgresSSL{
|
||||
Mode: sslDisable, // Postgres container doesn't support SSL.
|
||||
p := &Postgres{
|
||||
NetworkDB: NetworkDB{
|
||||
Database: getenv("DEX_POSTGRES_DATABASE", "postgres"),
|
||||
User: getenv("DEX_POSTGRES_USER", "postgres"),
|
||||
Password: getenv("DEX_POSTGRES_PASSWORD", "postgres"),
|
||||
Host: host,
|
||||
ConnectionTimeout: 5,
|
||||
},
|
||||
SSL: SSL{
|
||||
Mode: pgSSLDisable, // Postgres container doesn't support SSL.
|
||||
},
|
||||
ConnectionTimeout: 5,
|
||||
}
|
||||
|
||||
// t.Fatal has a bad habbit of not actually printing the error
|
||||
fatal := func(i interface{}) {
|
||||
fmt.Fprintln(os.Stdout, i)
|
||||
t.Fatal(i)
|
||||
}
|
||||
|
||||
newStorage := func() storage.Storage {
|
||||
conn, err := p.open(logger, p.createDataSourceName())
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
if err := cleanDB(conn); err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
return conn
|
||||
}
|
||||
withTimeout(time.Minute*1, func() {
|
||||
conformance.RunTests(t, newStorage)
|
||||
})
|
||||
withTimeout(time.Minute*1, func() {
|
||||
conformance.RunTransactionTests(t, newStorage)
|
||||
})
|
||||
testDB(t, p, true)
|
||||
}
|
||||
|
||||
const testMySQLEnv = "DEX_MYSQL_HOST"
|
||||
|
||||
func TestMySQL(t *testing.T) {
|
||||
host := os.Getenv(testMySQLEnv)
|
||||
if host == "" {
|
||||
t.Skipf("test environment variable %q not set, skipping", testMySQLEnv)
|
||||
}
|
||||
s := &MySQL{
|
||||
NetworkDB: NetworkDB{
|
||||
Database: getenv("DEX_MYSQL_DATABASE", "mysql"),
|
||||
User: getenv("DEX_MYSQL_USER", "mysql"),
|
||||
Password: getenv("DEX_MYSQL_PASSWORD", ""),
|
||||
Host: host,
|
||||
ConnectionTimeout: 5,
|
||||
},
|
||||
SSL: SSL{
|
||||
Mode: mysqlSSLFalse,
|
||||
},
|
||||
params: map[string]string{
|
||||
"innodb_lock_wait_timeout": "3",
|
||||
},
|
||||
}
|
||||
testDB(t, s, true)
|
||||
}
|
||||
|
Reference in New Issue
Block a user