forked from go-gorm/sqlserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrator_test.go
190 lines (157 loc) · 5.74 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
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
package sqlserver_test
import (
"os"
"reflect"
"testing"
"time"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
)
var sqlserverDSN = "sqlserver://gorm:LoremIpsum86@localhost:9930?database=gorm"
func init() {
if dbDSN := os.Getenv("GORM_DSN"); dbDSN != "" {
sqlserverDSN = dbDSN
}
}
type Testtable struct {
Test uint64 `gorm:"index"`
}
type Testtable2 struct {
Test uint64 `gorm:"index"`
Test2 uint64
}
func (*Testtable2) TableName() string { return "testtables" }
type Testtable3 struct {
Test3 uint64
}
func (*Testtable3) TableName() string { return "testschema1.Testtables" }
type Testtable4 struct {
Test4 uint64
}
func (*Testtable4) TableName() string { return "testschema2.Testtables" }
type Testtable5 struct {
Test4 uint64
Test5 uint64 `gorm:"index"`
}
func (*Testtable5) TableName() string { return "testschema2.Testtables" }
func TestAutomigrateTablesWithoutDefaultSchema(t *testing.T) {
db, err := gorm.Open(sqlserver.Open(sqlserverDSN))
if err != nil {
t.Error(err)
}
if tx := db.Exec("create schema testschema1"); tx.Error != nil {
t.Error("couldn't create schema testschema1", tx.Error)
}
if tx := db.Exec("create schema testschema2"); tx.Error != nil {
t.Error("couldn't create schema testschema2", tx.Error)
}
if err = db.AutoMigrate(&Testtable{}); err != nil {
t.Error("couldn't create a table at user default schema", err)
}
if err = db.AutoMigrate(&Testtable2{}); err != nil {
t.Error("couldn't update a table at user default schema", err)
}
if err = db.AutoMigrate(&Testtable3{}); err != nil {
t.Error("couldn't create a table at schema testschema1", err)
}
if err = db.AutoMigrate(&Testtable4{}); err != nil {
t.Error("couldn't create a table at schema testschema2", err)
}
if err = db.AutoMigrate(&Testtable5{}); err != nil {
t.Error("couldn't update a table at schema testschema2", err)
}
if tx := db.Exec("drop table testtables"); tx.Error != nil {
t.Error("couldn't drop table testtable at user default schema", tx.Error)
}
if tx := db.Exec("drop table testschema1.testtables"); tx.Error != nil {
t.Error("couldn't drop table testschema1.testtable", tx.Error)
}
if tx := db.Exec("drop table testschema2.testtables"); tx.Error != nil {
t.Error("couldn't drop table testschema2.testtable", tx.Error)
}
if tx := db.Exec("drop schema testschema1"); tx.Error != nil {
t.Error("couldn't drop schema testschema1", tx.Error)
}
if tx := db.Exec("drop schema testschema2"); tx.Error != nil {
t.Error("couldn't drop schema testschema2", tx.Error)
}
}
type Testtable6 struct {
ID string `gorm:"index:unique_id,class:UNIQUE,where:id IS NOT NULL"`
}
func (*Testtable6) TableName() string { return "testtable" }
func TestCreateIndex(t *testing.T) {
db, err := gorm.Open(sqlserver.Open(sqlserverDSN))
if err != nil {
t.Error(err)
}
if err = db.AutoMigrate(&Testtable6{}); err != nil {
t.Error("couldn't create table at user default schema", err)
}
if tx := db.Exec("drop table testtable"); tx.Error != nil {
t.Error("couldn't drop table testtable", tx.Error)
}
}
type TestTableDefaultValue struct {
ID string `gorm:"column:id;primaryKey"`
Name string `gorm:"column:name"`
Age uint `gorm:"column:age"`
Birthday *time.Time `gorm:"column:birthday"`
CompanyID *int `gorm:"column:company_id;default:0"`
ManagerID *uint `gorm:"column:manager_id;default:0"`
Active bool `gorm:"column:active;default:1"`
}
func (*TestTableDefaultValue) TableName() string { return "test_table_default_value" }
func TestReMigrateTableFieldsWithoutDefaultValue(t *testing.T) {
db, err := gorm.Open(sqlserver.Open(sqlserverDSN))
if err != nil {
t.Error(err)
}
var (
migrator = db.Migrator()
tableModel = new(TestTableDefaultValue)
fieldsWithDefault = []string{"company_id", "manager_id", "active"}
fieldsWithoutDefault = []string{"id", "name", "age", "birthday"}
columnsWithDefault []string
columnsWithoutDefault []string
)
defer func() {
if err = migrator.DropTable(tableModel); err != nil {
t.Errorf("couldn't drop table %q, got error: %v", tableModel.TableName(), err)
}
}()
if !migrator.HasTable(tableModel) {
if err = migrator.AutoMigrate(tableModel); err != nil {
t.Errorf("couldn't auto migrate table %q, got error: %v", tableModel.TableName(), err)
}
}
// If in the `Migrator.ColumnTypes` method `column.DefaultValueValue.Valid = true`,
// re-migrate the table will alter all fields without default value except for the primary key.
if err = db.Debug().Migrator().AutoMigrate(tableModel); err != nil {
t.Errorf("couldn't re-migrate table %q, got error: %v", tableModel.TableName(), err)
}
columnsWithDefault, columnsWithoutDefault, err = testGetMigrateColumns(db, tableModel)
if !reflect.DeepEqual(columnsWithDefault, fieldsWithDefault) {
// If in the `Migrator.ColumnTypes` method `column.DefaultValueValue.Valid = true`,
// fields with default value will include all fields: `[id name age birthday company_id manager_id active]`.
t.Errorf("expected columns with default value %v, got %v", fieldsWithDefault, columnsWithDefault)
}
if !reflect.DeepEqual(columnsWithoutDefault, fieldsWithoutDefault) {
t.Errorf("expected columns without default value %v, got %v", fieldsWithoutDefault, columnsWithoutDefault)
}
}
func testGetMigrateColumns(db *gorm.DB, dst interface{}) (columnsWithDefault, columnsWithoutDefault []string, err error) {
migrator := db.Migrator()
var columnTypes []gorm.ColumnType
if columnTypes, err = migrator.ColumnTypes(dst); err != nil {
return
}
for _, columnType := range columnTypes {
if _, ok := columnType.DefaultValue(); ok {
columnsWithDefault = append(columnsWithDefault, columnType.Name())
} else {
columnsWithoutDefault = append(columnsWithoutDefault, columnType.Name())
}
}
return
}