-
Notifications
You must be signed in to change notification settings - Fork 17
/
schema_test.go
168 lines (133 loc) · 4.18 KB
/
schema_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
package buildsqlx
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
)
const TableToCreate = "big_tbl"
func TestDB_CreateEmptyTable(t *testing.T) {
_, err := db.DropIfExists(TableToCreate)
require.NoError(t, err)
_, err = db.Schema(TableToCreate, func(table *Table) error {
return nil
})
}
func TestDB_CreateTable(t *testing.T) {
_, err := db.DropIfExists(TableToCreate)
require.NoError(t, err)
_, err = db.Schema(TableToCreate, func(table *Table) error {
table.Increments("id")
table.String("title", 128).Default("The quick brown fox jumped over the lazy dog").Unique("idx_ttl")
table.Boolean("is_active")
table.SmallInt("cnt").Default(1)
table.Integer("points").NotNull()
table.BigInt("likes").Index("idx_likes")
table.Text("comment").Comment("user comment").Collation("de-LU-x-icu")
table.DblPrecision("likes_to_points").Default(0.0)
table.Decimal("tax", 2, 2)
table.TsVector("body")
table.TsQuery("body_query")
table.Point("pt")
table.Polygon("poly")
table.TableComment("big table for big data")
return nil
})
require.NoError(t, err)
is, err := db.HasTable("public", TableToCreate)
require.NoError(t, err)
require.True(t, is)
_, err = db.Schema("tbl_to_ref", func(table *Table) error {
table.Increments("id")
table.Integer("big_tbl_id").ForeignKey("fk_idx_big_tbl_id", TableToCreate, "id").Concurrently()
return nil
})
require.NoError(t, err)
// test some err returning from fn()
_, err = db.Schema(TableToCreate, func(table *Table) error {
return errors.New("some err")
})
require.Error(t, err)
// 1st drop the referencing tbl
_, err = db.Drop("tbl_to_ref")
require.NoError(t, err)
// then referenced
_, err = db.Drop(TableToCreate)
require.NoError(t, err)
}
func TestTable_BigIncrements(t *testing.T) {
_, err := db.DropIfExists(TableToCreate)
require.NoError(t, err)
res, err := db.SchemaIfNotExists(TableToCreate, func(table *Table) error {
table.BigIncrements("id")
table.Numeric("price", 4, 3).Index("idx_price").Concurrently()
table.Jsonb("taxes")
return nil
})
require.NoError(t, err)
_, err = res.RowsAffected()
require.NoError(t, err)
is, err := db.HasTable("public", TableToCreate)
require.NoError(t, err)
require.True(t, is)
// test add columns
_, err = db.Schema(TableToCreate, func(table *Table) error {
// if not exists will propagate onto both column and index
table.String("title", 64).IfNotExists().Index("ttl_idx_if_not_exists").IfNotExists()
table.DropIndex("idx_price")
table.DropIndex("foo").IfExists()
table.RenameIndex("ttl_idx_if_not_exists", "ttl_idx_renamed")
return nil
})
require.NoError(t, err)
isCol, err := db.HasColumns("public", TableToCreate, "title")
require.NoError(t, err)
require.True(t, isCol)
// test modify the column
_, err = db.Schema(TableToCreate, func(table *Table) error {
table.String("title", 128).Change()
return nil
})
require.NoError(t, err)
// test drop the column
_, err = db.Schema(TableToCreate, func(table *Table) error {
table.DropColumn("title")
table.DropColumn("foo").IfExists()
return nil
})
require.NoError(t, err)
isCol, err = db.HasColumns("public", TableToCreate, "title")
require.NoError(t, err)
require.False(t, isCol)
_, err = db.Drop(TableToCreate)
require.NoError(t, err)
}
func TestTable_DateTime(t *testing.T) {
_, err := db.DropIfExists(TableToCreate)
require.NoError(t, err)
_, err = db.Schema(TableToCreate, func(table *Table) error {
table.Increments("id")
table.Json("settings")
table.Char("tag", 10)
table.Date("birthday", false)
table.Time("celebration_time", false)
table.DateTime("created_at", true)
table.DateTimeTz("updated_at", true)
return nil
})
require.NoError(t, err)
is, err := db.HasTable("public", TableToCreate)
require.NoError(t, err)
require.True(t, is)
// test modify the column
_, err = db.Schema(TableToCreate, func(table *Table) error {
table.String("tag", 12).Index("idx_tag").Include("birthday", "created_at")
table.Rename("settings", "options")
return nil
})
require.NoError(t, err)
isCol, err := db.HasColumns("public", TableToCreate, "options")
require.NoError(t, err)
require.True(t, isCol)
_, err = db.Drop(TableToCreate)
require.NoError(t, err)
}