-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
197 lines (164 loc) · 4.43 KB
/
mysql.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
191
192
193
194
195
196
197
package sqlassert
import (
"database/sql"
"fmt"
"strings"
)
// MysqlAsserter contains assertion helpers for mysqlql databases
type MysqlAsserter struct {
db *sql.DB
dbName string
rowExistsQueryBuilder rowExistsQueryBuilder
}
func NewMysqlAsserter(db *sql.DB) *MysqlAsserter {
var dbName string
err := db.QueryRow("SELECT DATABASE()").Scan(&dbName)
if err != nil {
panic(err)
}
return &MysqlAsserter{
db: db,
dbName: dbName,
rowExistsQueryBuilder: func(
table string,
colVals map[string]interface{},
) (string, []interface{}) {
var wheres []string
var vals []interface{}
for col, val := range colVals {
wheres = append(wheres, fmt.Sprintf("`%s` = ?", col))
vals = append(vals, val)
}
sql := fmt.Sprintf(mysqlRowExistsQuery, table, strings.Join(wheres, " AND "))
return sql, vals
},
}
}
func (pa *MysqlAsserter) TableExists(t testingT, table string) bool {
exists := queryExists(pa.db, mysqlTableExistsQuery, pa.dbName, table)
if !exists {
t.Errorf(errTableNotExists, table)
}
return exists
}
func (pa *MysqlAsserter) TableNotExists(t testingT, table string) bool {
exists := queryExists(pa.db, mysqlTableExistsQuery, pa.dbName, table)
if exists {
t.Errorf(errTableExists, table)
}
return !exists
}
func (pa *MysqlAsserter) ColumnExists(t testingT, table, column string) bool {
exists := queryExists(pa.db, mysqlColumnExistsQuery, pa.dbName, table, column)
if !exists {
t.Errorf(errColumnNotExists, column, table)
}
return exists
}
func (pa *MysqlAsserter) ColumnNotExists(t testingT, table, column string) bool {
exists := queryExists(pa.db, mysqlColumnExistsQuery, pa.dbName, table, column)
if exists {
t.Errorf(errColumnExists, column, table)
}
return !exists
}
func (pa *MysqlAsserter) ColumnOfType(t testingT, table, column, dataType string) bool {
exists := queryExists(pa.db, mysqlColumnOfTypeQuery, pa.dbName, table, column, dataType)
if !exists {
t.Errorf(errColumnNotOfType, column, table, dataType)
}
return exists
}
func (pa *MysqlAsserter) ConstraintExists(t testingT, table, constraint string) bool {
exists := queryExists(pa.db, mysqlConstraintExistsQuery, pa.dbName, table, constraint)
if !exists {
t.Errorf(errConstraintNotExists, constraint, table)
}
return exists
}
func (pa *MysqlAsserter) ConstraintNotExists(t testingT, table, constraint string) bool {
exists := queryExists(pa.db, mysqlConstraintExistsQuery, pa.dbName, table, constraint)
if exists {
t.Errorf(errConstraintExists, constraint, table)
}
return !exists
}
func (pa *MysqlAsserter) RowExists(t testingT, table string, colVals map[string]interface{}) bool {
query, args := pa.rowExistsQueryBuilder(table, colVals)
exists := queryExists(pa.db, query, args...)
if !exists {
t.Errorf(errRowNotExists, colVals, table)
}
return exists
}
func (pa *MysqlAsserter) RowNotExists(t testingT, table string, colVals map[string]interface{}) bool {
query, args := pa.rowExistsQueryBuilder(table, colVals)
exists := queryExists(pa.db, query, args...)
if exists {
t.Errorf(errRowExists, colVals, table)
}
return !exists
}
func (pa *MysqlAsserter) IndexExists(t testingT, table, index string) bool {
exists := queryExists(pa.db, mysqlIndexExistsQuery, pa.dbName, table, index)
if !exists {
t.Errorf(errIndexNotExists, index, table)
}
return exists
}
func (pa *MysqlAsserter) IndexNotExists(t testingT, table, index string) bool {
exists := queryExists(pa.db, mysqlIndexExistsQuery, pa.dbName, table, index)
if exists {
t.Errorf(errIndexExists, index, table)
}
return !exists
}
const (
mysqlTableExistsQuery = `
SELECT EXISTS (
SELECT 1 FROM information_schema.tables WHERE table_schema = ? AND table_name = ?
)
`
mysqlColumnExistsQuery = `
SELECT EXISTS(
SELECT 1 FROM information_schema.columns
WHERE
table_schema = ?
AND table_name = ?
AND column_name = ?
);
`
mysqlColumnOfTypeQuery = `
SELECT EXISTS(
SELECT 1 FROM information_schema.columns
WHERE
table_schema = ?
AND table_name = ?
AND column_name = ?
AND data_type = ?
);
`
mysqlConstraintExistsQuery = `
SELECT EXISTS(
SELECT 1 FROM information_schema.table_constraints
WHERE
table_schema = ?
AND table_name = ?
AND constraint_name = ?
);
`
mysqlRowExistsQuery = `
SELECT EXISTS(
SELECT 1 FROM ` + "`%s`" + ` WHERE %s
);
`
mysqlIndexExistsQuery = `
SELECT EXISTS(
SELECT 1 FROM information_schema.statistics
WHERE
table_schema = ?
AND table_name = ?
AND index_name = ?
);
`
)