-
Notifications
You must be signed in to change notification settings - Fork 2k
/
sysvar_test.go
231 lines (192 loc) · 7.37 KB
/
sysvar_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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
Copyright 2020 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package setstatement
import (
"context"
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/test/endtoend/cluster"
)
func TestCharsetIntro(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
vtParams := mysql.ConnParams{
Host: "localhost",
Port: clusterInstance.VtgateMySQLPort,
}
conn, err := mysql.Connect(ctx, &vtParams)
require.NoError(t, err)
defer conn.Close()
checkedExec(t, conn, "delete from test")
checkedExec(t, conn, "insert into test (id,val1) values (666, _binary'abc')")
checkedExec(t, conn, "update test set val1 = _latin1'xyz' where id = 666")
checkedExec(t, conn, "delete from test where val1 = _utf8'xyz'")
qr := checkedExec(t, conn, "select id from test where val1 = _utf8mb4'xyz'")
require.EqualValues(t, 0, qr.RowsAffected)
}
func TestSetSysVarSingle(t *testing.T) {
defer cluster.PanicHandler(t)
ctx := context.Background()
vtParams := mysql.ConnParams{
Host: "localhost",
Port: clusterInstance.VtgateMySQLPort,
}
type queriesWithExpectations struct {
name, expr, expected string
}
queries := []queriesWithExpectations{{
name: "default_storage_engine",
expr: "INNODB",
expected: `[[VARCHAR("InnoDB")]]`,
}, {
name: "character_set_client",
expr: "utf8",
expected: `[[VARCHAR("utf8")]]`,
}, {
name: "character_set_client", // ignored so will keep the actual value
expr: "@charvar",
expected: `[[VARCHAR("utf8")]]`,
}, {
name: "sql_mode",
expr: "''",
expected: `[[VARCHAR("")]]`,
}, {
name: "sql_mode",
expr: `concat(@@sql_mode,"NO_ZERO_DATE")`,
expected: `[[VARCHAR("NO_ZERO_DATE")]]`,
}, {
name: "sql_mode",
expr: "@@sql_mode",
expected: `[[VARCHAR("NO_ZERO_DATE")]]`,
}, {
name: "SQL_SAFE_UPDATES",
expr: "1",
expected: "[[INT64(1)]]",
}}
conn, err := mysql.Connect(ctx, &vtParams)
require.NoError(t, err)
defer conn.Close()
for i, q := range queries {
query := fmt.Sprintf("set %s = %s", q.name, q.expr)
t.Run(fmt.Sprintf("%d-%s", i, query), func(t *testing.T) {
_, err := exec(t, conn, query)
require.NoError(t, err)
assertMatches(t, conn, fmt.Sprintf("select @@%s", q.name), q.expected)
})
}
}
func TestSetSystemVariable(t *testing.T) {
vtParams := mysql.ConnParams{
Host: "localhost",
Port: clusterInstance.VtgateMySQLPort,
}
conn, err := mysql.Connect(context.Background(), &vtParams)
require.NoError(t, err)
defer conn.Close()
checkedExec(t, conn, "set session sql_mode = 'NO_ZERO_DATE', session default_week_format = 1")
q := `select str_to_date('00/00/0000', '%m/%d/%Y'), WEEK('2008-02-20')`
assertMatches(t, conn, q, `[[NULL INT64(8)]]`)
assertMatches(t, conn, "select @@sql_mode", `[[VARCHAR("NO_ZERO_DATE")]]`)
checkedExec(t, conn, "set @@sql_mode = '', session default_week_format = 0")
assertMatches(t, conn, q, `[[DATE("0000-00-00") INT64(7)]]`)
checkedExec(t, conn, "SET @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483")
assertMatches(t, conn, "select @@sql_mode", `[[VARCHAR("NO_AUTO_VALUE_ON_ZERO,STRICT_ALL_TABLES")]]`)
}
func TestSetSystemVarWithTxFailure(t *testing.T) {
vtParams := mysql.ConnParams{
Host: "localhost",
Port: clusterInstance.VtgateMySQLPort,
}
conn, err := mysql.Connect(context.Background(), &vtParams)
require.NoError(t, err)
defer conn.Close()
checkedExec(t, conn, "insert into test (id, val1) values (80, null)")
// before changing any settings, let's confirm sql_safe_updates value
assertMatches(t, conn, `select @@sql_safe_updates from test where id = 80`, `[[INT64(0)]]`)
checkedExec(t, conn, "set sql_safe_updates = 1")
checkedExec(t, conn, "begin")
qr := checkedExec(t, conn, "select connection_id() from test where id = 80")
// kill the mysql connection shard which has transaction open.
vttablet1 := clusterInstance.Keyspaces[0].Shards[0].MasterTablet() // 80-
vttablet1.VttabletProcess.QueryTablet(fmt.Sprintf("kill %s", qr.Rows[0][0].ToString()), keyspaceName, false)
// transaction fails on commit - we should no longer be in a transaction
_, err = conn.ExecuteFetch("commit", 1, true)
require.Error(t, err)
// we still want to have our system setting applied
assertMatches(t, conn, `select @@sql_safe_updates`, `[[INT64(1)]]`)
}
func TestSetSystemVarWithConnectionFailure(t *testing.T) {
t.Skip("failing at the moment")
vtParams := mysql.ConnParams{
Host: "localhost",
Port: clusterInstance.VtgateMySQLPort,
}
conn, err := mysql.Connect(context.Background(), &vtParams)
require.NoError(t, err)
defer conn.Close()
checkedExec(t, conn, "delete from test")
checkedExec(t, conn, "insert into test (id, val1) values (80, null)")
checkedExec(t, conn, "set sql_safe_updates = 1")
qr := checkedExec(t, conn, "select connection_id() from test where id = 80")
// kill the mysql connection shard which has transaction open.
vttablet1 := clusterInstance.Keyspaces[0].Shards[0].MasterTablet() // 80-
vttablet1.VttabletProcess.QueryTablet(fmt.Sprintf("kill %s", qr.Rows[0][0].ToString()), keyspaceName, false)
// we still want to have our system setting applied
_, err = exec(t, conn, `select @@sql_safe_updates from test where id = 80`)
require.NoError(t, err)
}
func TestSetSystemVariableAndThenSuccessfulTx(t *testing.T) {
vtParams := mysql.ConnParams{
Host: "localhost",
Port: clusterInstance.VtgateMySQLPort,
}
conn, err := mysql.Connect(context.Background(), &vtParams)
require.NoError(t, err)
defer conn.Close()
checkedExec(t, conn, "delete from test")
checkedExec(t, conn, "set sql_safe_updates = 1")
checkedExec(t, conn, "begin")
checkedExec(t, conn, "insert into test (id, val1) values (80, null)")
checkedExec(t, conn, "commit")
assertMatches(t, conn, "select id, val1 from test", "[[INT64(80) NULL]]")
assertMatches(t, conn, "select @@sql_safe_updates", "[[INT64(1)]]")
}
func TestStartTxAndSetSystemVariableAndThenSuccessfulCommit(t *testing.T) {
vtParams := mysql.ConnParams{
Host: "localhost",
Port: clusterInstance.VtgateMySQLPort,
}
conn, err := mysql.Connect(context.Background(), &vtParams)
require.NoError(t, err)
defer conn.Close()
checkedExec(t, conn, "delete from test")
checkedExec(t, conn, "begin")
checkedExec(t, conn, "set sql_safe_updates = 1")
checkedExec(t, conn, "insert into test (id, val1) values (54, null)")
checkedExec(t, conn, "commit")
assertMatches(t, conn, "select id, val1 from test", "[[INT64(54) NULL]]")
assertMatches(t, conn, "select @@sql_safe_updates", "[[INT64(1)]]")
}
func assertMatches(t *testing.T, conn *mysql.Conn, query, expected string) {
t.Helper()
qr, err := exec(t, conn, query)
require.NoError(t, err)
got := fmt.Sprintf("%v", qr.Rows)
diff := cmp.Diff(expected, got)
if diff != "" {
t.Errorf("Query: %s (-want +got):\n%s", query, diff)
}
}