Repository navigation
Expand file tree
/
Copy pathmigrator_test.go
More file actions
210 lines (180 loc) · 6.43 KB
/
Copy pathmigrator_test.go
File metadata and controls
210 lines (180 loc) · 6.43 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package clickhouse_test
import (
"regexp"
"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)
}
if err := DB.Table("mytable").AutoMigrate(&MyTable{}); err != nil {
t.Fatalf("no error should happen when auto migrate, but got %v", err)
}
columnTypes, err := DB.Migrator().ColumnTypes("mytable")
if err != nil {
t.Fatalf("failed to inspect columns, got %v", err)
}
foundString := false
for _, col := range columnTypes {
if col.Name() == "my_field" {
foundString = true
if col.DatabaseTypeName() != "String" {
t.Fatalf("my_field column should remain String, got %s", col.DatabaseTypeName())
}
}
}
if !foundString {
t.Fatalf("my_field column not found after auto migrate")
}
}
func TestMigrator_OnClusterSupport(t *testing.T) {
type ClusterTable struct {
ID uint64
Name string
CreatedAt time.Time
}
// Test ON CLUSTER extraction from gorm:table_options
sqlStrings := make([]string, 0)
// Create a new DB instance for this test
options, err := clickhousego.ParseDSN(dbDSN)
if err != nil {
t.Fatalf("Can not parse dsn, got error %v", err)
}
testDB, err := gorm.Open(clickhouse.New(clickhouse.Config{
Conn: clickhousego.OpenDB(options),
}))
if err != nil {
t.Fatalf("failed to connect database, got error %v", err)
}
// Replace raw callback to capture SQL
if err := testDB.Callback().Raw().Replace("gorm:raw", func(db *gorm.DB) {
sqlToExecute := db.Statement.SQL.String()
sqlStrings = append(sqlStrings, sqlToExecute)
// Don't actually execute for this test
}); err != nil {
t.Fatalf("no error should happen when registering a callback, but got %v", err)
}
// Test with ON CLUSTER in table_options
err = testDB.Set("gorm:table_options", "ON CLUSTER 'test_cluster' ENGINE ReplicatedMergeTree ORDER BY id").Table("cluster_test").AutoMigrate(&ClusterTable{})
if err != nil {
t.Fatalf("no error should happen when auto migrate with ON CLUSTER, but got %v", err)
}
// Check if ON CLUSTER was placed correctly in the SQL
if len(sqlStrings) == 0 {
t.Fatalf("expected SQL to be captured")
}
createSQL := sqlStrings[len(sqlStrings)-1] // Get the last (CREATE TABLE) statement
// Verify ON CLUSTER appears after table name but before column definitions
expectedPattern := `CREATE TABLE.*cluster_test.* ON CLUSTER 'test_cluster' \(.*id.*\).*ENGINE ReplicatedMergeTree`
matched, err := regexp.MatchString(expectedPattern, createSQL)
if err != nil {
t.Fatalf("regex error: %v", err)
}
if !matched {
t.Fatalf("ON CLUSTER not placed correctly. Got SQL: %s", createSQL)
}
}