mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-27 04:11:03 +00:00
Add test for mysql
This commit is contained in:
parent
0c9a765956
commit
b9111a3f44
1 changed files with 45 additions and 1 deletions
|
@ -2,7 +2,11 @@ package database
|
|||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/drone/drone/pkg/database"
|
||||
"github.com/drone/drone/pkg/database/encrypt"
|
||||
|
@ -12,6 +16,11 @@ import (
|
|||
"github.com/russross/meddler"
|
||||
)
|
||||
|
||||
var (
|
||||
dbname, driver, dsn, login string
|
||||
db *sql.DB
|
||||
)
|
||||
|
||||
func init() {
|
||||
// create a cipher for ecnrypting and decrypting
|
||||
// database fields
|
||||
|
@ -24,11 +33,31 @@ func init() {
|
|||
// decrypt database fields.
|
||||
meddler.Register("gobencrypt", &encrypt.EncryptedField{cipher})
|
||||
|
||||
// Check for $DB_ENV
|
||||
dbenv := os.Getenv("DB_ENV")
|
||||
if dbenv == "mysql" {
|
||||
driver = dbenv
|
||||
dbname = "drone_test"
|
||||
login = os.Getenv("MYSQL_LOGIN")
|
||||
if len(login) == 0 {
|
||||
login = "root"
|
||||
}
|
||||
log.Println("Using mysql database ...")
|
||||
} else {
|
||||
driver = "sqlite3"
|
||||
dsn = ":memory:"
|
||||
log.Println("Using sqlite3 database ...")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Setup() {
|
||||
// create an in-memory database
|
||||
database.Init("sqlite3", ":memory:")
|
||||
if driver == "mysql" {
|
||||
idsn := fmt.Sprintf("%s@/?parseTime=true", login)
|
||||
db, dsn = createDB(dbname, idsn)
|
||||
}
|
||||
database.Init(driver, dsn)
|
||||
|
||||
// create dummy user data
|
||||
user1 := User{
|
||||
|
@ -197,4 +226,19 @@ func Setup() {
|
|||
|
||||
func Teardown() {
|
||||
database.Close()
|
||||
if driver == "mysql" {
|
||||
db.Exec(fmt.Sprintf("DROP DATABASE %s", dbname))
|
||||
}
|
||||
}
|
||||
|
||||
func createDB(name, datasource string) (*sql.DB, string) {
|
||||
db, err := sql.Open(driver, datasource)
|
||||
if err != nil {
|
||||
panic("Can't connect to database")
|
||||
}
|
||||
if _, err := db.Exec(fmt.Sprintf("CREATE DATABASE %s", name)); err != nil {
|
||||
panic("Can't create database")
|
||||
}
|
||||
dsn := strings.Replace(datasource, "/", fmt.Sprintf("/%s", name), 1)
|
||||
return db, dsn
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue