forked from danielkurniadi/clickhouse
-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
migrator_test.go
148 lines (127 loc) · 4.65 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package clickhouse_test
import (
"testing"
"time"
clickhousego "github.com/ClickHouse/clickhouse-go/v2"
"gorm.io/driver/clickhouse"
"gorm.io/gorm"
)
type User struct {
ID uint64 `gorm:"primaryKey"`
Name string
FirstName string
LastName string
Age int64 `gorm:"type:Nullable(Int64)"`
Active bool
Salary float32
Attrs map[string]string `gorm:"type:Map(String,String);"`
CreatedAt time.Time
UpdatedAt time.Time
}
func TestAutoMigrate(t *testing.T) {
type UserMigrateColumn struct {
ID uint64
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, but got %v", err)
}
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)
}
}
}
}
func TestMigrator_HasIndex(t *testing.T) {
type UserWithIndex struct {
FirstName string `gorm:"index:full_name"`
LastName string `gorm:"index:full_name"`
CreatedAt time.Time `gorm:"index"`
}
if DB.Migrator().HasIndex("users", "full_name") {
t.Fatalf("users's full_name index should not exists")
}
if err := DB.Table("users").AutoMigrate(&UserWithIndex{}); err != nil {
t.Fatalf("no error should happen when auto migrate, but got %v", err)
}
if !DB.Migrator().HasIndex("users", "full_name") {
t.Fatalf("users's full_name index should exists after auto migrate")
}
if err := DB.Table("users").AutoMigrate(&UserWithIndex{}); err != nil {
t.Fatalf("no error should happen when auto migrate again")
}
}
func TestMigrator_DontSupportEmptyDefaultValue(t *testing.T) {
options, err := clickhousego.ParseDSN(dbDSN)
if err != nil {
t.Fatalf("Can not parse dsn, got error %v", err)
}
DB, err := gorm.Open(clickhouse.New(clickhouse.Config{
Conn: clickhousego.OpenDB(options),
DontSupportEmptyDefaultValue: true,
}))
if err != nil {
t.Fatalf("failed to connect database, got error %v", err)
}
type MyTable struct {
MyField string
}
// Create the table with AutoMigrate
if err := DB.Table("mytable").AutoMigrate(&MyTable{}); err != nil {
t.Fatalf("no error should happen when auto migrate, but got %v", err)
}
// Replace every gorm raw SQL command with a function that appends the SQL string to a slice
sqlStrings := make([]string, 0)
if err := DB.Callback().Raw().Replace("gorm:raw", func(db *gorm.DB) {
sqlToExecute := db.Statement.SQL.String()
sqlStrings = append(sqlStrings, sqlToExecute)
}); err != nil {
t.Fatalf("no error should happen when registering a callback, but got %v", err)
}
if err := DB.Table("mytable").AutoMigrate(&MyTable{}); err != nil {
t.Fatalf("no error should happen when auto migrate, but got %v", err)
}
if len(sqlStrings) > 0 {
t.Fatalf("should not auto-migrate table if there have not been any changes to the schema")
}
}