forked from go-gorm/clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests_test.go
54 lines (43 loc) · 1.09 KB
/
tests_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package clickhouse_test
import (
"log"
"math/rand"
"os"
"time"
"gorm.io/driver/clickhouse"
"gorm.io/gorm"
)
var DB *gorm.DB
func init() {
var (
err error
dbDSN = "clickhouse://gorm:gorm@localhost:9942/gorm?read_timeout=10s&write_timeout=20s"
)
if DB, err = gorm.Open(clickhouse.Open(dbDSN), &gorm.Config{}); err != nil {
log.Printf("failed to connect database, got error %v", err)
os.Exit(1)
}
RunMigrations()
if os.Getenv("DEBUG") == "true" {
DB = DB.Debug()
}
}
func RunMigrations() {
allModels := []interface{}{&User{}}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(allModels), func(i, j int) { allModels[i], allModels[j] = allModels[j], allModels[i] })
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)
}
}
}