forked from go-gorm/sqlserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrator_test.go
117 lines (92 loc) · 2.98 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
package sqlserver_test
import (
"os"
"testing"
"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)
}
}