2020-02-02 09:40:44 +03:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
2020-05-23 13:38:27 +03:00
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-02-02 09:40:44 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2020-05-23 13:38:27 +03:00
|
|
|
"github.com/jinzhu/gorm/dialects/mssql"
|
|
|
|
"github.com/jinzhu/gorm/dialects/mysql"
|
|
|
|
"github.com/jinzhu/gorm/dialects/postgres"
|
|
|
|
"github.com/jinzhu/gorm/dialects/sqlite"
|
|
|
|
"github.com/jinzhu/gorm/logger"
|
2020-02-02 09:40:44 +03:00
|
|
|
)
|
|
|
|
|
2020-05-23 13:38:27 +03:00
|
|
|
var DB *gorm.DB
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
|
|
|
if DB, err = OpenTestConnection(); err == nil {
|
|
|
|
RunMigrations()
|
|
|
|
} else {
|
|
|
|
log.Printf("failed to connect database, got error %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func OpenTestConnection() (db *gorm.DB, err error) {
|
|
|
|
dbDSN := os.Getenv("GORM_DSN")
|
|
|
|
switch os.Getenv("GORM_DIALECT") {
|
|
|
|
case "mysql":
|
|
|
|
log.Println("testing mysql...")
|
|
|
|
if dbDSN == "" {
|
|
|
|
dbDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local"
|
|
|
|
}
|
|
|
|
db, err = gorm.Open(mysql.Open(dbDSN), &gorm.Config{})
|
|
|
|
case "postgres":
|
|
|
|
log.Println("testing postgres...")
|
|
|
|
if dbDSN == "" {
|
|
|
|
dbDSN = "user=gorm password=gorm DB.name=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"
|
|
|
|
}
|
|
|
|
db, err = gorm.Open(postgres.Open(dbDSN), &gorm.Config{})
|
|
|
|
case "mssql":
|
|
|
|
// CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';
|
|
|
|
// CREATE DATABASE gorm;
|
|
|
|
// USE gorm;
|
|
|
|
// CREATE USER gorm FROM LOGIN gorm;
|
|
|
|
// sp_changedbowner 'gorm';
|
|
|
|
log.Println("testing mssql...")
|
|
|
|
if dbDSN == "" {
|
|
|
|
dbDSN = "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm"
|
|
|
|
}
|
|
|
|
db, err = gorm.Open(mssql.Open(dbDSN), &gorm.Config{})
|
|
|
|
default:
|
|
|
|
log.Println("testing sqlite3...")
|
|
|
|
db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db")), &gorm.Config{})
|
|
|
|
}
|
|
|
|
|
|
|
|
if debug := os.Getenv("DEBUG"); debug == "true" {
|
2020-05-31 18:55:56 +03:00
|
|
|
db.Logger = db.Logger.LogMode(logger.Info)
|
2020-05-23 13:38:27 +03:00
|
|
|
} else if debug == "false" {
|
2020-05-31 18:55:56 +03:00
|
|
|
db.Logger = db.Logger.LogMode(logger.Silent)
|
2020-05-23 13:38:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2020-02-02 09:40:44 +03:00
|
|
|
}
|
|
|
|
|
2020-05-23 13:38:27 +03:00
|
|
|
func RunMigrations() {
|
|
|
|
var err error
|
|
|
|
allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}}
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })
|
2020-03-08 13:05:22 +03:00
|
|
|
|
2020-05-23 16:03:28 +03:00
|
|
|
DB.Migrator().DropTable("user_friends", "user_speaks")
|
2020-05-23 13:38:27 +03:00
|
|
|
|
|
|
|
if err = DB.Migrator().DropTable(allModels...); err != nil {
|
|
|
|
log.Printf("Failed to drop table, got error %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = DB.AutoMigrate(allModels...); err != nil {
|
|
|
|
log.Printf("Failed to auto migrate, but got error %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range allModels {
|
|
|
|
if !DB.Migrator().HasTable(m) {
|
|
|
|
log.Printf("Failed to create table for %#v\n", m)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Now() *time.Time {
|
|
|
|
now := time.Now()
|
|
|
|
return &now
|
2020-02-02 09:40:44 +03:00
|
|
|
}
|