forked from go-gorm/clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrator_test.go
80 lines (70 loc) · 2.44 KB
/
migrator_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package clickhouse_test
import (
"testing"
"time"
)
type User struct {
ID uint `gorm:"primaryKey"`
Name string
FirstName string
LastName string
Age int64
Active bool
Salary float32
CreatedAt time.Time
UpdatedAt time.Time
}
func TestAutoMigrate(t *testing.T) {
type UserMigrateColumn struct {
ID uint
Name string
IsAdmin bool
Birthday time.Time `gorm:"precision:4"`
Debit float64 `gorm:"precision:4"`
Note string `gorm:"size:10;comment:my note"`
DefaultValue string `gorm:"default:hello world"`
}
if DB.Migrator().HasColumn("users", "is_admin") {
t.Fatalf("users's is_admin column should not exists")
}
if err := DB.Table("users").AutoMigrate(&UserMigrateColumn{}); err != nil {
t.Fatalf("no error should happen when auto migrate")
}
if !DB.Migrator().HasTable("users") {
t.Fatalf("users should exists")
}
if !DB.Migrator().HasColumn("users", "is_admin") {
t.Fatalf("users's is_admin column should exists after auto migrate")
}
columnTypes, err := DB.Migrator().ColumnTypes("users")
if err != nil {
t.Fatalf("failed to get column types, got error %v", err)
}
for _, columnType := range columnTypes {
switch columnType.Name() {
case "id":
if columnType.DatabaseTypeName() != "UInt64" {
t.Fatalf("column id primary key should be correct, name: %v, column: %#v", columnType.Name(), columnType)
}
case "note":
if length, ok := columnType.Length(); !ok || length != 10 {
t.Fatalf("column name length should be correct, name: %v, column: %#v", columnType.Name(), columnType)
}
if comment, ok := columnType.Comment(); !ok || comment != "my note" {
t.Fatalf("column name length should be correct, name: %v, column: %#v", columnType.Name(), columnType)
}
case "default_value":
if defaultValue, ok := columnType.DefaultValue(); !ok || defaultValue != "hello world" {
t.Fatalf("column name default_value should be correct, name: %v, column: %#v", columnType.Name(), columnType)
}
case "debit":
if decimal, scale, ok := columnType.DecimalSize(); !ok || scale != 0 || decimal != 4 {
t.Fatalf("column name debit should be correct, name: %v, column: %#v", columnType.Name(), columnType)
}
case "birthday":
if decimal, scale, ok := columnType.DecimalSize(); !ok || scale != 0 || decimal != 4 {
t.Fatalf("column name birthday should be correct, name: %v, column: %#v", columnType.Name(), columnType)
}
}
}
}