-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathddl_error_test.go
210 lines (179 loc) · 8.41 KB
/
ddl_error_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
// Copyright 2022 PingCAP, Inc.
//
// 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 ddl_test
import (
"testing"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
)
// This test file contains tests that test the expected or unexpected DDL error.
// For expected error, we use SQL to check it.
// For unexpected error, we mock a SQL job to check it.
func TestTableError(t *testing.T) {
store := testkit.CreateMockStoreWithSchemaLease(t, testLease)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table testDrop(a int)")
// Schema ID is wrong, so dropping table is failed.
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId", `return(-1)`))
err := tk.ExecToErr("drop table testDrop")
require.Error(t, err)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId"))
// Table ID is wrong, so dropping table is failed.
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/MockModifyJobTableId", `return(-1)`))
err = tk.ExecToErr("drop table testDrop")
require.Error(t, err)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/MockModifyJobTableId"))
// Args is wrong, so creating table is failed.
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/MockModifyJobArg", `return(true)`))
err = tk.ExecToErr("create table test.t1(a int)")
require.Error(t, err)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/MockModifyJobArg"))
// Table exists, so creating table is failed.
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId", `return(-1)`))
err = tk.ExecToErr("create table test.t1(a int)")
require.Error(t, err)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId"))
// Table exists, so creating table is failed.
tk.MustExec("create table test.t2(a int)")
tk.MustGetErrCode("create table test.t2(a int)", errno.ErrTableExists)
}
func TestViewError(t *testing.T) {
store := testkit.CreateMockStoreWithSchemaLease(t, testLease)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t (a int)")
// Args is wrong, so creating view is failed.
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/MockModifyJobArg", `return(true)`))
err := tk.ExecToErr("create view v as select * from t")
require.Error(t, err)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/MockModifyJobArg"))
}
func TestForeignKeyError(t *testing.T) {
store := testkit.CreateMockStoreWithSchemaLease(t, testLease)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t (a int, index(a))")
tk.MustExec("create table t1 (a int, FOREIGN KEY fk(a) REFERENCES t(a))")
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId", `return(-1)`))
err := tk.ExecToErr("alter table t1 add foreign key idx(a) REFERENCES t(a)")
require.Error(t, err)
err = tk.ExecToErr("alter table t1 drop index fk")
require.Error(t, err)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId"))
}
func TestIndexError(t *testing.T) {
store := testkit.CreateMockStoreWithSchemaLease(t, testLease)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t (a int)")
tk.MustExec("alter table t add index a(a)")
// Schema ID is wrong.
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId", `return(-1)`))
err := tk.ExecToErr("alter table t add index idx(a)")
require.Error(t, err)
err = tk.ExecToErr("alter table t1 drop a")
require.Error(t, err)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId"))
// for adding index
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/MockModifyJobArg", `return(true)`))
err = tk.ExecToErr("alter table t add index idx(a)")
require.Error(t, err)
err = tk.ExecToErr("alter table t drop index a")
require.Error(t, err)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/MockModifyJobArg"))
}
func TestColumnError(t *testing.T) {
store := testkit.CreateMockStoreWithSchemaLease(t, testLease)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t (a int, aa int, ab int)")
tk.MustExec("alter table t add index a(a)")
// Invalid schema ID.
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId", `return(-1)`))
err := tk.ExecToErr("alter table t add column ta int")
require.Error(t, err)
err = tk.ExecToErr("alter table t drop column aa")
require.Error(t, err)
err = tk.ExecToErr("alter table t drop column aa")
require.Error(t, err)
err = tk.ExecToErr("alter table t add column ta int, add column tb int")
require.Error(t, err)
err = tk.ExecToErr("alter table t drop column aa, drop column ab")
require.Error(t, err)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId"))
// Invalid table ID.
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/MockModifyJobTableId", `return(-1)`))
err = tk.ExecToErr("alter table t add column ta int")
require.Error(t, err)
err = tk.ExecToErr("alter table t drop column aa")
require.Error(t, err)
err = tk.ExecToErr("alter table t drop column aa")
require.Error(t, err)
err = tk.ExecToErr("alter table t add column ta int, add column tb int")
require.Error(t, err)
err = tk.ExecToErr("alter table t drop column aa, drop column ab")
require.Error(t, err)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/MockModifyJobTableId"))
// Invalid argument.
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/MockModifyJobArg", `return(true)`))
err = tk.ExecToErr("alter table t add column ta int")
require.Error(t, err)
err = tk.ExecToErr("alter table t drop column aa")
require.Error(t, err)
err = tk.ExecToErr("alter table t drop column aa")
require.Error(t, err)
err = tk.ExecToErr("alter table t add column ta int, add column tb int")
require.Error(t, err)
err = tk.ExecToErr("alter table t drop column aa, drop column ab")
require.Error(t, err)
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/MockModifyJobArg"))
tk.MustGetErrCode("alter table t add column c int after c5", errno.ErrBadField)
tk.MustGetErrCode("alter table t drop column c5", errno.ErrCantDropFieldOrKey)
tk.MustGetErrCode("alter table t add column c int after c5, add column d int", errno.ErrBadField)
tk.MustGetErrCode("alter table t drop column ab, drop column c5", errno.ErrCantDropFieldOrKey)
}
func TestCreateDatabaseError(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId", `return(-1)`))
tk.MustExec("create database db1;")
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/ddl/mockModifyJobSchemaId"))
}
func TestRenameViewOverDifferentSchemaError(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
//init
tk.MustExec("use test")
tk.MustExec("drop database if exists test_2;")
tk.MustExec("drop table if exists table_1;")
tk.MustExec("drop view if exists view_1;")
tk.MustExec("create database test_2;")
tk.MustExec("create table table_1 (a int);")
tk.MustExec("create view view_1 as select a from table_1;")
//different schema
tk.MustGetErrCode("rename table test.view_1 to test_2.view_1;", errno.ErrForbidSchemaChange)
tk.MustGetErrMsg("rename table test.view_1 to test_2.view_1;",
infoschema.ErrForbidSchemaChange.GenWithStackByArgs("test", "test_2").Error(),
)
tk.MustGetErrMsg("rename table test.view_1 to test_2.view_1;",
"[schema:1450]Changing schema from 'test' to 'test_2' is not allowed.",
)
//same schema
tk.MustExec("rename table test.view_1 to test.view_1000;")
}