forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_rename_test.go
295 lines (259 loc) · 11.7 KB
/
db_rename_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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// 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 (
"fmt"
"testing"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
)
func TestRenameIndex(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t (pk int primary key, c int default 1, c1 int default 1, unique key k1(c), key k2(c1))")
// Test rename success
tk.MustExec("alter table t rename index k1 to k3")
tk.MustExec("admin check index t k3")
// Test rename to the same name
tk.MustExec("alter table t rename index k3 to k3")
tk.MustExec("admin check index t k3")
// Test rename on non-exists keys
tk.MustGetErrCode("alter table t rename index x to x", errno.ErrKeyDoesNotExist)
// Test rename on already-exists keys
tk.MustGetErrCode("alter table t rename index k3 to k2", errno.ErrDupKeyName)
tk.MustExec("alter table t rename index k2 to K2")
tk.MustGetErrCode("alter table t rename key k3 to K2", errno.ErrDupKeyName)
}
// See issue: https://github.com/pingcap/tidb/issues/29752
// Ref https://dev.mysql.com/doc/refman/8.0/en/rename-table.html
func TestRenameTableWithLocked(t *testing.T) {
defer config.RestoreFunc()()
config.UpdateGlobal(func(conf *config.Config) {
conf.EnableTableLock = true
})
store := testkit.CreateMockStore(t, mockstore.WithDDLChecker())
tk := testkit.NewTestKit(t, store)
tk.MustExec("create database renamedb")
tk.MustExec("create database renamedb2")
tk.MustExec("use renamedb")
tk.MustExec("DROP TABLE IF EXISTS t1;")
tk.MustExec("CREATE TABLE t1 (a int);")
tk.MustExec("LOCK TABLES t1 WRITE;")
tk.MustGetErrCode("drop database renamedb2;", errno.ErrLockOrActiveTransaction)
tk.MustExec("RENAME TABLE t1 TO t2;")
tk.MustQuery("select * from renamedb.t2").Check(testkit.Rows())
tk.MustExec("UNLOCK TABLES")
tk.MustExec("RENAME TABLE t2 TO t1;")
tk.MustQuery("select * from renamedb.t1").Check(testkit.Rows())
tk.MustExec("LOCK TABLES t1 READ;")
tk.MustGetErrCode("RENAME TABLE t1 TO t2;", errno.ErrTableNotLockedForWrite)
tk.MustExec("UNLOCK TABLES")
tk.MustExec("drop database renamedb")
}
func TestRenameTable2(t *testing.T) {
isAlterTable := false
renameTableTest(t, "rename table %s to %s", isAlterTable)
}
func TestAlterTableRenameTable(t *testing.T) {
isAlterTable := true
renameTableTest(t, "alter table %s rename to %s", isAlterTable)
}
func renameTableTest(t *testing.T, sql string, isAlterTable bool) {
store := testkit.CreateMockStore(t, mockstore.WithDDLChecker())
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustGetErrCode("rename table tb1 to tb2;", errno.ErrNoSuchTable)
// for different databases
tk.MustExec("create table t (c1 int, c2 int)")
tk.MustExec("insert t values (1, 1), (2, 2)")
ctx := tk.Session()
is := domain.GetDomain(ctx).InfoSchema()
oldTblInfo, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
require.NoError(t, err)
oldTblID := oldTblInfo.Meta().ID
tk.MustExec("create database test1")
tk.MustExec("use test1")
tk.MustExec(fmt.Sprintf(sql, "test.t", "test1.t1"))
is = domain.GetDomain(ctx).InfoSchema()
newTblInfo, err := is.TableByName(model.NewCIStr("test1"), model.NewCIStr("t1"))
require.NoError(t, err)
require.Equal(t, oldTblID, newTblInfo.Meta().ID)
tk.MustQuery("select * from t1").Check(testkit.Rows("1 1", "2 2"))
tk.MustExec("use test")
// Make sure t doesn't exist.
tk.MustExec("create table t (c1 int, c2 int)")
tk.MustExec("drop table t")
// for the same database
tk.MustExec("use test1")
tk.MustExec(fmt.Sprintf(sql, "t1", "t2"))
is = domain.GetDomain(ctx).InfoSchema()
newTblInfo, err = is.TableByName(model.NewCIStr("test1"), model.NewCIStr("t2"))
require.NoError(t, err)
require.Equal(t, oldTblID, newTblInfo.Meta().ID)
tk.MustQuery("select * from t2").Check(testkit.Rows("1 1", "2 2"))
isExist := is.TableExists(model.NewCIStr("test1"), model.NewCIStr("t1"))
require.False(t, isExist)
tk.MustQuery("show tables").Check(testkit.Rows("t2"))
// for failure case
failSQL := fmt.Sprintf(sql, "test_not_exist.t", "test_not_exist.t")
tk.MustGetErrCode(failSQL, errno.ErrNoSuchTable)
failSQL = fmt.Sprintf(sql, "test.test_not_exist", "test.test_not_exist")
tk.MustGetErrCode(failSQL, errno.ErrNoSuchTable)
failSQL = fmt.Sprintf(sql, "test.t_not_exist", "test_not_exist.t")
tk.MustGetErrCode(failSQL, errno.ErrNoSuchTable)
failSQL = fmt.Sprintf(sql, "test1.t2", "test_not_exist.t")
tk.MustGetErrCode(failSQL, errno.ErrErrorOnRename)
tk.MustExec("use test1")
tk.MustExec("create table if not exists t_exist (c1 int, c2 int)")
failSQL = fmt.Sprintf(sql, "test1.t2", "test1.t_exist")
tk.MustGetErrCode(failSQL, errno.ErrTableExists)
failSQL = fmt.Sprintf(sql, "test.t_not_exist", "test1.t_exist")
if isAlterTable {
tk.MustGetErrCode(failSQL, errno.ErrNoSuchTable)
} else {
tk.MustGetErrCode(failSQL, errno.ErrTableExists)
}
failSQL = fmt.Sprintf(sql, "test_not_exist.t", "test1.t_exist")
if isAlterTable {
tk.MustGetErrCode(failSQL, errno.ErrNoSuchTable)
} else {
tk.MustGetErrCode(failSQL, errno.ErrTableExists)
}
failSQL = fmt.Sprintf(sql, "test_not_exist.t", "test1.t_not_exist")
if isAlterTable {
tk.MustGetErrCode(failSQL, errno.ErrNoSuchTable)
}
// for the same table name
tk.MustExec("use test1")
tk.MustExec("create table if not exists t (c1 int, c2 int)")
tk.MustExec("create table if not exists t1 (c1 int, c2 int)")
if isAlterTable {
tk.MustExec(fmt.Sprintf(sql, "test1.t", "t"))
tk.MustExec(fmt.Sprintf(sql, "test1.t1", "test1.T1"))
} else {
tk.MustGetErrCode(fmt.Sprintf(sql, "test1.t", "t"), errno.ErrTableExists)
tk.MustGetErrCode(fmt.Sprintf(sql, "test1.t1", "test1.T1"), errno.ErrTableExists)
}
// Test rename table name too long.
tk.MustGetErrCode("rename table test1.t1 to test1.txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", errno.ErrTooLongIdent)
tk.MustGetErrCode("alter table test1.t1 rename to test1.txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", errno.ErrTooLongIdent)
tk.MustExec("drop database test1")
}
func TestRenameMultiTables(t *testing.T) {
store := testkit.CreateMockStore(t, mockstore.WithDDLChecker())
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t1(id int)")
tk.MustExec("create table t2(id int)")
sql := "rename table t1 to t3, t2 to t4"
_, err := tk.Exec(sql)
require.NoError(t, err)
tk.MustExec("drop table t3, t4")
tk.MustExec("create table t1 (c1 int, c2 int)")
tk.MustExec("create table t2 (c1 int, c2 int)")
tk.MustExec("insert t1 values (1, 1), (2, 2)")
tk.MustExec("insert t2 values (1, 1), (2, 2)")
ctx := tk.Session()
is := domain.GetDomain(ctx).InfoSchema()
oldTblInfo1, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t1"))
require.NoError(t, err)
oldTblID1 := oldTblInfo1.Meta().ID
oldTblInfo2, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t2"))
require.NoError(t, err)
oldTblID2 := oldTblInfo2.Meta().ID
tk.MustExec("create database test1")
tk.MustExec("use test1")
tk.MustExec("rename table test.t1 to test1.t1, test.t2 to test1.t2")
is = domain.GetDomain(ctx).InfoSchema()
newTblInfo1, err := is.TableByName(model.NewCIStr("test1"), model.NewCIStr("t1"))
require.NoError(t, err)
require.Equal(t, oldTblID1, newTblInfo1.Meta().ID)
newTblInfo2, err := is.TableByName(model.NewCIStr("test1"), model.NewCIStr("t2"))
require.NoError(t, err)
require.Equal(t, oldTblID2, newTblInfo2.Meta().ID)
tk.MustQuery("select * from t1").Check(testkit.Rows("1 1", "2 2"))
tk.MustQuery("select * from t2").Check(testkit.Rows("1 1", "2 2"))
// Make sure t1,t2 doesn't exist.
isExist := is.TableExists(model.NewCIStr("test"), model.NewCIStr("t1"))
require.False(t, isExist)
isExist = is.TableExists(model.NewCIStr("test"), model.NewCIStr("t2"))
require.False(t, isExist)
// for the same database
tk.MustExec("use test1")
tk.MustExec("rename table test1.t1 to test1.t3, test1.t2 to test1.t4")
is = domain.GetDomain(ctx).InfoSchema()
newTblInfo1, err = is.TableByName(model.NewCIStr("test1"), model.NewCIStr("t3"))
require.NoError(t, err)
require.Equal(t, oldTblID1, newTblInfo1.Meta().ID)
newTblInfo2, err = is.TableByName(model.NewCIStr("test1"), model.NewCIStr("t4"))
require.NoError(t, err)
require.Equal(t, oldTblID2, newTblInfo2.Meta().ID)
tk.MustQuery("select * from t3").Check(testkit.Rows("1 1", "2 2"))
isExist = is.TableExists(model.NewCIStr("test1"), model.NewCIStr("t1"))
require.False(t, isExist)
tk.MustQuery("select * from t4").Check(testkit.Rows("1 1", "2 2"))
isExist = is.TableExists(model.NewCIStr("test1"), model.NewCIStr("t2"))
require.False(t, isExist)
tk.MustQuery("show tables").Check(testkit.Rows("t3", "t4"))
// for multi tables same database
tk.MustExec("create table t5 (c1 int, c2 int)")
tk.MustExec("insert t5 values (1, 1), (2, 2)")
is = domain.GetDomain(ctx).InfoSchema()
oldTblInfo3, err := is.TableByName(model.NewCIStr("test1"), model.NewCIStr("t5"))
require.NoError(t, err)
oldTblID3 := oldTblInfo3.Meta().ID
tk.MustExec("rename table test1.t3 to test1.t1, test1.t4 to test1.t2, test1.t5 to test1.t3")
is = domain.GetDomain(ctx).InfoSchema()
newTblInfo1, err = is.TableByName(model.NewCIStr("test1"), model.NewCIStr("t1"))
require.NoError(t, err)
require.Equal(t, oldTblID1, newTblInfo1.Meta().ID)
newTblInfo2, err = is.TableByName(model.NewCIStr("test1"), model.NewCIStr("t2"))
require.NoError(t, err)
require.Equal(t, oldTblID2, newTblInfo2.Meta().ID)
newTblInfo3, err := is.TableByName(model.NewCIStr("test1"), model.NewCIStr("t3"))
require.NoError(t, err)
require.Equal(t, oldTblID3, newTblInfo3.Meta().ID)
tk.MustQuery("show tables").Check(testkit.Rows("t1", "t2", "t3"))
// for multi tables different databases
tk.MustExec("use test")
tk.MustExec("rename table test1.t1 to test.t2, test1.t2 to test.t3, test1.t3 to test.t4")
is = domain.GetDomain(ctx).InfoSchema()
newTblInfo1, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t2"))
require.NoError(t, err)
require.Equal(t, oldTblID1, newTblInfo1.Meta().ID)
newTblInfo2, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t3"))
require.NoError(t, err)
require.Equal(t, oldTblID2, newTblInfo2.Meta().ID)
newTblInfo3, err = is.TableByName(model.NewCIStr("test"), model.NewCIStr("t4"))
require.NoError(t, err)
require.Equal(t, oldTblID3, newTblInfo3.Meta().ID)
tk.MustQuery("show tables").Check(testkit.Rows("t2", "t3", "t4"))
// for failure case
failSQL := "rename table test_not_exist.t to test_not_exist.t, test_not_exist.t to test_not_exist.t"
tk.MustGetErrCode(failSQL, errno.ErrNoSuchTable)
failSQL = "rename table test.test_not_exist to test.test_not_exist, test.test_not_exist to test.test_not_exist"
tk.MustGetErrCode(failSQL, errno.ErrNoSuchTable)
failSQL = "rename table test.t_not_exist to test_not_exist.t, test.t_not_exist to test_not_exist.t"
tk.MustGetErrCode(failSQL, errno.ErrNoSuchTable)
failSQL = "rename table test1.t2 to test_not_exist.t, test1.t2 to test_not_exist.t"
tk.MustGetErrCode(failSQL, errno.ErrNoSuchTable)
tk.MustExec("drop database test1")
tk.MustExec("drop database test")
}