2020-06-02 05:34:50 +03:00
|
|
|
package tests_test
|
2020-02-02 09:40:44 +03:00
|
|
|
|
|
|
|
import (
|
2020-05-23 13:38:27 +03:00
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-02-02 09:40:44 +03:00
|
|
|
"time"
|
|
|
|
|
2020-06-02 05:34:50 +03:00
|
|
|
"gorm.io/driver/mysql"
|
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/driver/sqlserver"
|
2020-06-02 04:16:07 +03:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/logger"
|
2020-06-02 05:34:50 +03:00
|
|
|
. "gorm.io/gorm/utils/tests"
|
2020-02-02 09:40:44 +03:00
|
|
|
)
|
|
|
|
|
2020-05-23 13:38:27 +03:00
|
|
|
var DB *gorm.DB
|
2022-05-22 11:12:28 +03:00
|
|
|
var (
|
|
|
|
mysqlDSN = "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local"
|
|
|
|
postgresDSN = "user=gorm password=gorm dbname=gorm host=localhost port=9920 sslmode=disable TimeZone=Asia/Shanghai"
|
|
|
|
sqlserverDSN = "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm"
|
2023-02-08 11:29:09 +03:00
|
|
|
tidbDSN = "root:@tcp(localhost:9940)/test?charset=utf8&parseTime=True&loc=Local"
|
2022-05-22 11:12:28 +03:00
|
|
|
)
|
2020-05-23 13:38:27 +03:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
var err error
|
2023-06-10 16:05:19 +03:00
|
|
|
if DB, err = OpenTestConnection(&gorm.Config{}); err != nil {
|
2020-09-03 14:16:55 +03:00
|
|
|
log.Printf("failed to connect database, got error %v", err)
|
2020-05-23 13:38:27 +03:00
|
|
|
os.Exit(1)
|
2020-06-02 17:13:53 +03:00
|
|
|
} else {
|
2020-06-17 14:56:03 +03:00
|
|
|
sqlDB, err := DB.DB()
|
2021-12-28 13:22:17 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed to connect database, got error %v", err)
|
|
|
|
os.Exit(1)
|
2020-06-17 14:56:03 +03:00
|
|
|
}
|
|
|
|
|
2021-12-28 13:22:17 +03:00
|
|
|
err = sqlDB.Ping()
|
2020-06-17 14:56:03 +03:00
|
|
|
if err != nil {
|
2021-12-28 13:22:17 +03:00
|
|
|
log.Printf("failed to ping sqlDB, got error %v", err)
|
|
|
|
os.Exit(1)
|
2020-06-17 14:56:03 +03:00
|
|
|
}
|
|
|
|
|
2020-06-02 17:13:53 +03:00
|
|
|
RunMigrations()
|
2020-06-20 05:51:36 +03:00
|
|
|
if DB.Dialector.Name() == "sqlite" {
|
|
|
|
DB.Exec("PRAGMA foreign_keys = ON")
|
|
|
|
}
|
2020-05-23 13:38:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-10 16:05:19 +03:00
|
|
|
func OpenTestConnection(cfg *gorm.Config) (db *gorm.DB, err error) {
|
2020-05-23 13:38:27 +03:00
|
|
|
dbDSN := os.Getenv("GORM_DSN")
|
|
|
|
switch os.Getenv("GORM_DIALECT") {
|
|
|
|
case "mysql":
|
|
|
|
log.Println("testing mysql...")
|
|
|
|
if dbDSN == "" {
|
2022-05-22 11:12:28 +03:00
|
|
|
dbDSN = mysqlDSN
|
2020-05-23 13:38:27 +03:00
|
|
|
}
|
2023-06-10 16:05:19 +03:00
|
|
|
db, err = gorm.Open(mysql.Open(dbDSN), cfg)
|
2020-05-23 13:38:27 +03:00
|
|
|
case "postgres":
|
|
|
|
log.Println("testing postgres...")
|
|
|
|
if dbDSN == "" {
|
2022-05-22 11:12:28 +03:00
|
|
|
dbDSN = postgresDSN
|
2020-05-23 13:38:27 +03:00
|
|
|
}
|
2020-07-03 03:53:38 +03:00
|
|
|
db, err = gorm.Open(postgres.New(postgres.Config{
|
|
|
|
DSN: dbDSN,
|
|
|
|
PreferSimpleProtocol: true,
|
2023-06-10 16:05:19 +03:00
|
|
|
}), cfg)
|
2020-06-02 05:34:50 +03:00
|
|
|
case "sqlserver":
|
2022-01-30 17:00:56 +03:00
|
|
|
// go install github.com/microsoft/go-sqlcmd/cmd/sqlcmd@latest
|
|
|
|
// SQLCMDPASSWORD=LoremIpsum86 sqlcmd -U sa -S localhost:9930
|
2020-05-23 13:38:27 +03:00
|
|
|
// CREATE DATABASE gorm;
|
2022-01-30 17:00:56 +03:00
|
|
|
// GO
|
|
|
|
// CREATE LOGIN gorm WITH PASSWORD = 'LoremIpsum86';
|
2020-05-23 13:38:27 +03:00
|
|
|
// CREATE USER gorm FROM LOGIN gorm;
|
2022-01-30 17:00:56 +03:00
|
|
|
// ALTER SERVER ROLE sysadmin ADD MEMBER [gorm];
|
|
|
|
// GO
|
2020-06-02 05:34:50 +03:00
|
|
|
log.Println("testing sqlserver...")
|
2020-05-23 13:38:27 +03:00
|
|
|
if dbDSN == "" {
|
2022-05-22 11:12:28 +03:00
|
|
|
dbDSN = sqlserverDSN
|
2020-05-23 13:38:27 +03:00
|
|
|
}
|
2023-06-10 16:05:19 +03:00
|
|
|
db, err = gorm.Open(sqlserver.Open(dbDSN), cfg)
|
2023-02-08 11:29:09 +03:00
|
|
|
case "tidb":
|
|
|
|
log.Println("testing tidb...")
|
|
|
|
if dbDSN == "" {
|
|
|
|
dbDSN = tidbDSN
|
|
|
|
}
|
2023-06-10 16:05:19 +03:00
|
|
|
db, err = gorm.Open(mysql.Open(dbDSN), cfg)
|
2020-05-23 13:38:27 +03:00
|
|
|
default:
|
|
|
|
log.Println("testing sqlite3...")
|
2023-07-12 16:21:22 +03:00
|
|
|
db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db?_foreign_keys=on")), cfg)
|
2020-05-23 13:38:27 +03:00
|
|
|
}
|
|
|
|
|
2021-12-28 13:22:17 +03:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-23 13:38:27 +03:00
|
|
|
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
|
2022-03-17 18:53:31 +03:00
|
|
|
allModels := []interface{}{&User{}, &Account{}, &Pet{}, &Company{}, &Toy{}, &Language{}, &Coupon{}, &CouponProduct{}, &Order{}, &Parent{}, &Child{}}
|
2020-05-23 13:38:27 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|