forked from go-gorm/clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests_test.go
More file actions
62 lines (49 loc) · 1.21 KB
/
tests_test.go
File metadata and controls
62 lines (49 loc) · 1.21 KB
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
55
56
57
58
59
60
61
62
package clickhouse_test
import (
"log"
"math/rand"
"os"
"time"
"gorm.io/driver/clickhouse"
"gorm.io/gorm"
)
var DB *gorm.DB
const defaultDBDSN = "http://gorm:gorm@127.0.0.1:9941/gorm?dial_timeout=10s&read_timeout=20s"
func testDSN() string {
if dsn := os.Getenv("CLICKHOUSE_TEST_DSN"); dsn != "" {
return dsn
}
return defaultDBDSN
}
func init() {
var (
err error
)
if DB, err = gorm.Open(clickhouse.Open(testDSN()), &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)
}
}
}