-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathupgrade_test.go
64 lines (52 loc) · 1.69 KB
/
upgrade_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
// SPDX-FileCopyrightText: 2014-2024 caixw
//
// SPDX-License-Identifier: MIT
package orm_test
import (
"testing"
"github.com/issue9/assert/v4"
"github.com/issue9/orm/v6/core"
"github.com/issue9/orm/v6/internal/test"
)
type u2 struct {
ID int64 `orm:"name(id);unique(u_id)"`
Name string `orm:"name(name);len(50);index(index_name)"`
UserName string `orm:"name(username);len(50);pk"`
Modified int64 `orm:"name(modified);default(0)"`
Created string `orm:"name(created);nullable"`
}
func (u *u2) TableName() string { return "upgrades" }
func (u *u2) ApplyModel(m *core.Model) error {
return m.NewCheck("chk_username", "{username} IS NOT NULL")
}
func TestUpgrader(t *testing.T) {
a := assert.New(t, false)
suite := test.NewSuite(a, "")
suite.Run(func(t *test.Driver) {
sql := t.DB.SQLBuilder().CreateTable().
Column("id", core.Int64, false, false, false, nil).
Column("name", core.String, false, false, false, nil, 50).
Column("username", core.String, false, false, false, nil, 50).
Column("created", core.Int64, false, false, false, nil).
Index(core.IndexDefault, "i_name", "name").
Unique("u_username", "username").
Check("chk_id", "id>0").
Table((&u2{}).TableName())
t.NotError(sql.Exec())
defer func(n string) {
t.NotError(t.DB.Drop(&u2{}))
}(t.DriverName)
u, err := t.DB.Upgrade(&u2{})
t.NotError(err, "%s@%s", err, t.DriverName).
NotNil(u)
err = u.DropConstraint("u_username", "chk_id").
DropIndex("i_name").
DropColumn("created").
AddColumn("modified").
AddConstraint("u_id", "chk_username", "u2_pk").
AddIndex("index_name").
AddColumn("created"). // 同名不同类型
Do()
t.NotError(err, "%s@%s", err, t.DriverName)
})
}