forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_integration_test.go
1284 lines (1131 loc) · 52.8 KB
/
db_integration_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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package ddl_test
import (
"context"
"fmt"
"math"
"strings"
"sync/atomic"
"time"
. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
tmysql "github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/mockstore/mocktikv"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/testkit"
)
var _ = Suite(&testIntegrationSuite{})
type testIntegrationSuite struct {
lease time.Duration
cluster *mocktikv.Cluster
mvccStore mocktikv.MVCCStore
store kv.Storage
dom *domain.Domain
ctx sessionctx.Context
tk *testkit.TestKit
}
func (s *testIntegrationSuite) TearDownTest(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
r := tk.MustQuery("show tables")
for _, tb := range r.Rows() {
tableName := tb[0]
tk.MustExec(fmt.Sprintf("drop table %v", tableName))
}
}
func (s *testIntegrationSuite) SetUpSuite(c *C) {
var err error
s.lease = 50 * time.Millisecond
s.cluster = mocktikv.NewCluster()
mocktikv.BootstrapWithSingleStore(s.cluster)
s.mvccStore = mocktikv.MustNewMVCCStore()
s.store, err = mockstore.NewMockTikvStore(
mockstore.WithCluster(s.cluster),
mockstore.WithMVCCStore(s.mvccStore),
)
c.Assert(err, IsNil)
session.SetSchemaLease(s.lease)
session.SetStatsLease(0)
s.dom, err = session.BootstrapSession(s.store)
c.Assert(err, IsNil)
se, err := session.CreateSession4Test(s.store)
c.Assert(err, IsNil)
s.ctx = se.(sessionctx.Context)
_, err = se.Execute(context.Background(), "create database test_db")
c.Assert(err, IsNil)
s.tk = testkit.NewTestKit(c, s.store)
}
func (s *testIntegrationSuite) TearDownSuite(c *C) {
s.dom.Close()
s.store.Close()
}
func (s *testIntegrationSuite) TestNoZeroDateMode(c *C) {
tk := testkit.NewTestKit(c, s.store)
defer tk.MustExec("set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';")
tk.MustExec("use test;")
tk.MustExec("set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ENGINE_SUBSTITUTION';")
assertErrorCode(c, tk, "create table test_zero_date(agent_start_time date NOT NULL DEFAULT '0000-00-00')", mysql.ErrInvalidDefault)
assertErrorCode(c, tk, "create table test_zero_date(agent_start_time datetime NOT NULL DEFAULT '0000-00-00 00:00:00')", mysql.ErrInvalidDefault)
assertErrorCode(c, tk, "create table test_zero_date(agent_start_time timestamp NOT NULL DEFAULT '0000-00-00 00:00:00')", mysql.ErrInvalidDefault)
assertErrorCode(c, tk, "create table test_zero_date(a timestamp default '0000-00-00 00');", mysql.ErrInvalidDefault)
assertErrorCode(c, tk, "create table test_zero_date(a timestamp default 0);", mysql.ErrInvalidDefault)
}
func (s *testIntegrationSuite) TestInvalidDefault(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("USE test;")
_, err := tk.Exec("create table t(c1 decimal default 1.7976931348623157E308)")
c.Assert(err, NotNil)
c.Assert(terror.ErrorEqual(err, types.ErrInvalidDefault), IsTrue, Commentf("err %v", err))
_, err = tk.Exec("create table t( c1 varchar(2) default 'TiDB');")
c.Assert(err, NotNil)
c.Assert(terror.ErrorEqual(err, types.ErrInvalidDefault), IsTrue, Commentf("err %v", err))
}
// for issue #3848
func (s *testIntegrationSuite) TestInvalidNameWhenCreateTable(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("USE test;")
_, err := tk.Exec("create table t(xxx.t.a bigint)")
c.Assert(err, NotNil)
c.Assert(terror.ErrorEqual(err, ddl.ErrWrongDBName), IsTrue, Commentf("err %v", err))
_, err = tk.Exec("create table t(test.tttt.a bigint)")
c.Assert(err, NotNil)
c.Assert(terror.ErrorEqual(err, ddl.ErrWrongTableName), IsTrue, Commentf("err %v", err))
_, err = tk.Exec("create table t(t.tttt.a bigint)")
c.Assert(err, NotNil)
c.Assert(terror.ErrorEqual(err, ddl.ErrWrongDBName), IsTrue, Commentf("err %v", err))
}
// for issue #6879
func (s *testIntegrationSuite) TestCreateTableIfNotExists(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("USE test;")
tk.MustExec("create table t1(a bigint)")
tk.MustExec("create table t(a bigint)")
// Test duplicate create-table with `LIKE` clause
tk.MustExec("create table if not exists t like t1;")
warnings := tk.Se.GetSessionVars().StmtCtx.GetWarnings()
c.Assert(len(warnings), GreaterEqual, 1)
lastWarn := warnings[len(warnings)-1]
c.Assert(terror.ErrorEqual(infoschema.ErrTableExists, lastWarn.Err), IsTrue, Commentf("err %v", lastWarn.Err))
c.Assert(lastWarn.Level, Equals, stmtctx.WarnLevelNote)
// Test duplicate create-table without `LIKE` clause
tk.MustExec("create table if not exists t(b bigint, c varchar(60));")
warnings = tk.Se.GetSessionVars().StmtCtx.GetWarnings()
c.Assert(len(warnings), GreaterEqual, 1)
lastWarn = warnings[len(warnings)-1]
c.Assert(terror.ErrorEqual(infoschema.ErrTableExists, lastWarn.Err), IsTrue)
}
func (s *testIntegrationSuite) TestUniqueKeyNullValue(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("USE test")
tk.MustExec("create table t(a int primary key, b varchar(255))")
tk.MustExec("insert into t values(1, NULL)")
tk.MustExec("insert into t values(2, NULL)")
tk.MustExec("alter table t add unique index b(b);")
res := tk.MustQuery("select count(*) from t use index(b);")
res.Check(testkit.Rows("2"))
tk.MustExec("admin check table t")
tk.MustExec("admin check index t b")
}
func (s *testIntegrationSuite) TestEndIncluded(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("USE test")
tk.MustExec("create table t(a int, b int)")
for i := 0; i < ddl.DefaultTaskHandleCnt+1; i++ {
tk.MustExec("insert into t values(1, 1)")
}
tk.MustExec("alter table t add index b(b);")
tk.MustExec("admin check index t b")
tk.MustExec("admin check table t")
}
// TestModifyColumnAfterAddIndex Issue 5134
func (s *testIntegrationSuite) TestModifyColumnAfterAddIndex(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table city (city VARCHAR(2) KEY);")
tk.MustExec("alter table city change column city city varchar(50);")
tk.MustExec(`insert into city values ("abc"), ("abd");`)
}
func (s *testIntegrationSuite) TestIssue2293(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t_issue_2293 (a int)")
sql := "alter table t_issue_2293 add b int not null default 'a'"
assertErrorCode(c, tk, sql, tmysql.ErrInvalidDefault)
tk.MustExec("insert into t_issue_2293 value(1)")
tk.MustQuery("select * from t_issue_2293").Check(testkit.Rows("1"))
}
func (s *testIntegrationSuite) TestIssue6101(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t1 (quantity decimal(2) unsigned);")
_, err := tk.Exec("insert into t1 values (500), (-500), (~0), (-1);")
terr := errors.Cause(err).(*terror.Error)
c.Assert(terr.Code(), Equals, terror.ErrCode(tmysql.ErrWarnDataOutOfRange))
tk.MustExec("drop table t1")
tk.MustExec("set sql_mode=''")
tk.MustExec("create table t1 (quantity decimal(2) unsigned);")
tk.MustExec("insert into t1 values (500), (-500), (~0), (-1);")
tk.MustQuery("select * from t1").Check(testkit.Rows("99", "0", "99", "0"))
tk.MustExec("drop table t1")
}
func (s *testIntegrationSuite) TestIssue3833(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table issue3833 (b char(0))")
assertErrorCode(c, tk, "create index idx on issue3833 (b)", tmysql.ErrWrongKeyColumn)
assertErrorCode(c, tk, "alter table issue3833 add index idx (b)", tmysql.ErrWrongKeyColumn)
assertErrorCode(c, tk, "create table issue3833_2 (b char(0), index (b))", tmysql.ErrWrongKeyColumn)
}
func (s *testIntegrationSuite) TestIssue2858And2717(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t_issue_2858_bit (a bit(64) default b'0')")
tk.MustExec("insert into t_issue_2858_bit value ()")
tk.MustExec(`insert into t_issue_2858_bit values (100), ('10'), ('\0')`)
tk.MustQuery("select a+0 from t_issue_2858_bit").Check(testkit.Rows("0", "100", "12592", "0"))
tk.MustExec(`alter table t_issue_2858_bit alter column a set default '\0'`)
tk.MustExec("create table t_issue_2858_hex (a int default 0x123)")
tk.MustExec("insert into t_issue_2858_hex value ()")
tk.MustExec("insert into t_issue_2858_hex values (123), (0x321)")
tk.MustQuery("select a from t_issue_2858_hex").Check(testkit.Rows("291", "123", "801"))
tk.MustExec(`alter table t_issue_2858_hex alter column a set default 0x321`)
}
func (s *testIntegrationSuite) TestIssue4432(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table tx (col bit(10) default 'a')")
tk.MustExec("insert into tx value ()")
tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
tk.MustExec("drop table tx")
tk.MustExec("create table tx (col bit(10) default 0x61)")
tk.MustExec("insert into tx value ()")
tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
tk.MustExec("drop table tx")
tk.MustExec("create table tx (col bit(10) default 97)")
tk.MustExec("insert into tx value ()")
tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
tk.MustExec("drop table tx")
tk.MustExec("create table tx (col bit(10) default 0b1100001)")
tk.MustExec("insert into tx value ()")
tk.MustQuery("select * from tx").Check(testkit.Rows("\x00a"))
tk.MustExec("drop table tx")
}
func (s *testIntegrationSuite) TestMySQLErrorCode(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test_db")
// create database
sql := "create database aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
assertErrorCode(c, tk, sql, tmysql.ErrTooLongIdent)
sql = "create database test"
assertErrorCode(c, tk, sql, tmysql.ErrDBCreateExists)
sql = "create database test1 character set uft8;"
assertErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet)
sql = "create database test2 character set gkb;"
assertErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet)
sql = "create database test3 character set laitn1;"
assertErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet)
// drop database
sql = "drop database db_not_exist"
assertErrorCode(c, tk, sql, tmysql.ErrDBDropExists)
// create table
tk.MustExec("create table test_error_code_succ (c1 int, c2 int, c3 int, primary key(c3))")
sql = "create table test_error_code_succ (c1 int, c2 int, c3 int)"
assertErrorCode(c, tk, sql, tmysql.ErrTableExists)
sql = "create table test_error_code1 (c1 int, c2 int, c2 int)"
assertErrorCode(c, tk, sql, tmysql.ErrDupFieldName)
sql = "create table test_error_code1 (c1 int, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa int)"
assertErrorCode(c, tk, sql, tmysql.ErrTooLongIdent)
sql = "create table aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(a int)"
assertErrorCode(c, tk, sql, tmysql.ErrTooLongIdent)
sql = "create table test_error_code1 (c1 int, c2 int, key aa (c1, c2), key aa (c1))"
assertErrorCode(c, tk, sql, tmysql.ErrDupKeyName)
sql = "create table test_error_code1 (c1 int, c2 int, c3 int, key(c_not_exist))"
assertErrorCode(c, tk, sql, tmysql.ErrKeyColumnDoesNotExits)
sql = "create table test_error_code1 (c1 int, c2 int, c3 int, primary key(c_not_exist))"
assertErrorCode(c, tk, sql, tmysql.ErrKeyColumnDoesNotExits)
sql = "create table test_error_code1 (c1 int not null default '')"
assertErrorCode(c, tk, sql, tmysql.ErrInvalidDefault)
sql = "CREATE TABLE `t` (`a` double DEFAULT 1.0 DEFAULT 2.0 DEFAULT now());"
assertErrorCode(c, tk, sql, tmysql.ErrInvalidDefault)
sql = "CREATE TABLE `t` (`a` double DEFAULT now());"
assertErrorCode(c, tk, sql, tmysql.ErrInvalidDefault)
sql = "create table t1(a int) character set uft8;"
assertErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet)
sql = "create table t1(a int) character set gkb;"
assertErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet)
sql = "create table t1(a int) character set laitn1;"
assertErrorCode(c, tk, sql, tmysql.ErrUnknownCharacterSet)
sql = "create table test_error_code (a int not null ,b int not null,c int not null, d int not null, foreign key (b, c) references product(id));"
assertErrorCode(c, tk, sql, tmysql.ErrWrongFkDef)
sql = "create table test_error_code_2;"
assertErrorCode(c, tk, sql, tmysql.ErrTableMustHaveColumns)
sql = "create table test_error_code_2 (unique(c1));"
assertErrorCode(c, tk, sql, tmysql.ErrTableMustHaveColumns)
sql = "create table test_error_code_2(c1 int, c2 int, c3 int, primary key(c1), primary key(c2));"
assertErrorCode(c, tk, sql, tmysql.ErrMultiplePriKey)
sql = "create table test_error_code_3(pt blob ,primary key (pt));"
assertErrorCode(c, tk, sql, tmysql.ErrBlobKeyWithoutLength)
sql = "create table test_error_code_3(a text, unique (a(3073)));"
assertErrorCode(c, tk, sql, tmysql.ErrTooLongKey)
sql = "create table test_error_code_3(`id` int, key `primary`(`id`));"
assertErrorCode(c, tk, sql, tmysql.ErrWrongNameForIndex)
sql = "create table t2(c1.c2 blob default null);"
assertErrorCode(c, tk, sql, tmysql.ErrWrongTableName)
sql = "create table t2 (id int default null primary key , age int);"
assertErrorCode(c, tk, sql, tmysql.ErrInvalidDefault)
sql = "create table t2 (id int null primary key , age int);"
assertErrorCode(c, tk, sql, tmysql.ErrPrimaryCantHaveNull)
sql = "create table t2 (id int default null, age int, primary key(id));"
assertErrorCode(c, tk, sql, tmysql.ErrPrimaryCantHaveNull)
sql = "create table t2 (id int null, age int, primary key(id));"
assertErrorCode(c, tk, sql, tmysql.ErrPrimaryCantHaveNull)
sql = "create table t2 (id int primary key , age int);"
tk.MustExec(sql)
// add column
sql = "alter table test_error_code_succ add column c1 int"
assertErrorCode(c, tk, sql, tmysql.ErrDupFieldName)
sql = "alter table test_error_code_succ add column aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa int"
assertErrorCode(c, tk, sql, tmysql.ErrTooLongIdent)
sql = "alter table test_comment comment 'test comment'"
assertErrorCode(c, tk, sql, tmysql.ErrNoSuchTable)
sql = "alter table test_error_code_succ add column `a ` int ;"
assertErrorCode(c, tk, sql, tmysql.ErrWrongColumnName)
tk.MustExec("create table test_on_update (c1 int, c2 int);")
sql = "alter table test_on_update add column c3 int on update current_timestamp;"
assertErrorCode(c, tk, sql, tmysql.ErrInvalidOnUpdate)
sql = "create table test_on_update_2(c int on update current_timestamp);"
assertErrorCode(c, tk, sql, tmysql.ErrInvalidOnUpdate)
// drop column
sql = "alter table test_error_code_succ drop c_not_exist"
assertErrorCode(c, tk, sql, tmysql.ErrCantDropFieldOrKey)
tk.MustExec("create table test_drop_column (c1 int );")
sql = "alter table test_drop_column drop column c1;"
assertErrorCode(c, tk, sql, tmysql.ErrCantRemoveAllFields)
// add index
sql = "alter table test_error_code_succ add index idx (c_not_exist)"
assertErrorCode(c, tk, sql, tmysql.ErrKeyColumnDoesNotExits)
tk.MustExec("alter table test_error_code_succ add index idx (c1)")
sql = "alter table test_error_code_succ add index idx (c1)"
assertErrorCode(c, tk, sql, tmysql.ErrDupKeyName)
// drop index
sql = "alter table test_error_code_succ drop index idx_not_exist"
assertErrorCode(c, tk, sql, tmysql.ErrCantDropFieldOrKey)
sql = "alter table test_error_code_succ drop column c3"
assertErrorCode(c, tk, sql, int(tmysql.ErrUnknown))
// modify column
sql = "alter table test_error_code_succ modify testx.test_error_code_succ.c1 bigint"
assertErrorCode(c, tk, sql, tmysql.ErrWrongDBName)
sql = "alter table test_error_code_succ modify t.c1 bigint"
assertErrorCode(c, tk, sql, tmysql.ErrWrongTableName)
// insert value
tk.MustExec("create table test_error_code_null(c1 char(100) not null);")
sql = "insert into test_error_code_null (c1) values(null);"
assertErrorCode(c, tk, sql, tmysql.ErrBadNull)
}
func (s *testIntegrationSuite) TestTableDDLWithFloatType(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
s.tk.MustExec("drop table if exists t")
assertErrorCode(c, s.tk, "create table t (a decimal(1, 2))", tmysql.ErrMBiggerThanD)
assertErrorCode(c, s.tk, "create table t (a float(1, 2))", tmysql.ErrMBiggerThanD)
assertErrorCode(c, s.tk, "create table t (a double(1, 2))", tmysql.ErrMBiggerThanD)
s.tk.MustExec("create table t (a double(1, 1))")
assertErrorCode(c, s.tk, "alter table t add column b decimal(1, 2)", tmysql.ErrMBiggerThanD)
// add multi columns now not support, so no case.
assertErrorCode(c, s.tk, "alter table t modify column a float(1, 4)", tmysql.ErrMBiggerThanD)
assertErrorCode(c, s.tk, "alter table t change column a aa float(1, 4)", tmysql.ErrMBiggerThanD)
s.tk.MustExec("drop table t")
}
func (s *testIntegrationSuite) TestTableDDLWithTimeType(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
s.tk.MustExec("drop table if exists t")
assertErrorCode(c, s.tk, "create table t (a time(7))", tmysql.ErrTooBigPrecision)
assertErrorCode(c, s.tk, "create table t (a datetime(7))", tmysql.ErrTooBigPrecision)
assertErrorCode(c, s.tk, "create table t (a timestamp(7))", tmysql.ErrTooBigPrecision)
_, err := s.tk.Exec("create table t (a time(-1))")
c.Assert(err, NotNil)
s.tk.MustExec("create table t (a datetime)")
assertErrorCode(c, s.tk, "alter table t add column b time(7)", tmysql.ErrTooBigPrecision)
assertErrorCode(c, s.tk, "alter table t add column b datetime(7)", tmysql.ErrTooBigPrecision)
assertErrorCode(c, s.tk, "alter table t add column b timestamp(7)", tmysql.ErrTooBigPrecision)
assertErrorCode(c, s.tk, "alter table t modify column a time(7)", tmysql.ErrTooBigPrecision)
assertErrorCode(c, s.tk, "alter table t modify column a datetime(7)", tmysql.ErrTooBigPrecision)
assertErrorCode(c, s.tk, "alter table t modify column a timestamp(7)", tmysql.ErrTooBigPrecision)
assertErrorCode(c, s.tk, "alter table t change column a aa time(7)", tmysql.ErrTooBigPrecision)
assertErrorCode(c, s.tk, "alter table t change column a aa datetime(7)", tmysql.ErrTooBigPrecision)
assertErrorCode(c, s.tk, "alter table t change column a aa timestamp(7)", tmysql.ErrTooBigPrecision)
s.tk.MustExec("alter table t change column a aa datetime(0)")
s.tk.MustExec("drop table t")
}
func (s *testIntegrationSuite) TestUpdateMultipleTable(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("create database umt_db")
tk.MustExec("use umt_db")
tk.MustExec("create table t1 (c1 int, c2 int)")
tk.MustExec("insert t1 values (1, 1), (2, 2)")
tk.MustExec("create table t2 (c1 int, c2 int)")
tk.MustExec("insert t2 values (1, 3), (2, 5)")
ctx := tk.Se.(sessionctx.Context)
dom := domain.GetDomain(ctx)
is := dom.InfoSchema()
db, ok := is.SchemaByName(model.NewCIStr("umt_db"))
c.Assert(ok, IsTrue)
t1Tbl, err := is.TableByName(model.NewCIStr("umt_db"), model.NewCIStr("t1"))
c.Assert(err, IsNil)
t1Info := t1Tbl.Meta()
// Add a new column in write only state.
newColumn := &model.ColumnInfo{
ID: 100,
Name: model.NewCIStr("c3"),
Offset: 2,
DefaultValue: 9,
OriginDefaultValue: 9,
FieldType: *types.NewFieldType(tmysql.TypeLonglong),
State: model.StateWriteOnly,
}
t1Info.Columns = append(t1Info.Columns, newColumn)
kv.RunInNewTxn(s.store, false, func(txn kv.Transaction) error {
m := meta.NewMeta(txn)
_, err = m.GenSchemaVersion()
c.Assert(err, IsNil)
c.Assert(m.UpdateTable(db.ID, t1Info), IsNil)
return nil
})
err = dom.Reload()
c.Assert(err, IsNil)
tk.MustExec("update t1, t2 set t1.c1 = 8, t2.c2 = 10 where t1.c2 = t2.c1")
tk.MustQuery("select * from t1").Check(testkit.Rows("8 1", "8 2"))
tk.MustQuery("select * from t2").Check(testkit.Rows("1 10", "2 10"))
newColumn.State = model.StatePublic
kv.RunInNewTxn(s.store, false, func(txn kv.Transaction) error {
m := meta.NewMeta(txn)
_, err = m.GenSchemaVersion()
c.Assert(err, IsNil)
c.Assert(m.UpdateTable(db.ID, t1Info), IsNil)
return nil
})
err = dom.Reload()
c.Assert(err, IsNil)
tk.MustQuery("select * from t1").Check(testkit.Rows("8 1 9", "8 2 9"))
tk.MustExec("drop database umt_db")
}
func (s *testIntegrationSuite) TestNullGeneratedColumn(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("CREATE TABLE `t` (" +
"`a` int(11) DEFAULT NULL," +
"`b` int(11) DEFAULT NULL," +
"`c` int(11) GENERATED ALWAYS AS (`a` + `b`) VIRTUAL DEFAULT NULL," +
"`h` varchar(10) DEFAULT NULL," +
"`m` int(11) DEFAULT NULL" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin")
tk.MustExec("insert into t values()")
tk.MustExec("alter table t add index idx_c(c)")
tk.MustExec("drop table t")
}
func (s *testIntegrationSuite) TestChangingCharsetToUtf8(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t1(a varchar(20) charset utf8)")
tk.MustExec("insert into t1 values (?)", "t1_value")
tk.MustExec("alter table t1 modify column a varchar(20) charset utf8mb4")
tk.MustQuery("select * from t1;").Check(testkit.Rows("t1_value"))
tk.MustExec("create table t(a varchar(20) charset latin1)")
tk.MustExec("insert into t values (?)", "t_value")
tk.MustExec("alter table t modify column a varchar(20) charset latin1")
tk.MustQuery("select * from t;").Check(testkit.Rows("t_value"))
rs, err := tk.Exec("alter table t modify column a varchar(20) charset utf8")
if rs != nil {
rs.Close()
}
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[ddl:210]unsupported modify charset from latin1 to utf8")
rs, err = tk.Exec("alter table t modify column a varchar(20) charset utf8mb4")
if rs != nil {
rs.Close()
}
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[ddl:210]unsupported modify charset from latin1 to utf8mb4")
rs, err = tk.Exec("alter table t modify column a varchar(20) charset utf8mb4 collate utf8bin")
if rs != nil {
rs.Close()
}
c.Assert(err, NotNil)
rs, err = tk.Exec("alter table t modify column a varchar(20) charset utf8 collate utf8_bin")
if rs != nil {
rs.Close()
}
c.Assert(err, NotNil)
rs, err = tk.Exec("alter table t modify column a varchar(20) charset utf8mb4 collate utf8mb4_general_ci")
if rs != nil {
rs.Close()
}
c.Assert(err, NotNil)
}
func (s *testIntegrationSuite) TestChangingTableCharset(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("USE test")
tk.MustExec("create table t(a char(10)) charset latin1 collate latin1_bin")
rs, err := tk.Exec("alter table t charset gbk")
if rs != nil {
rs.Close()
}
c.Assert(err.Error(), Equals, "Unknown charset gbk")
rs, err = tk.Exec("alter table t charset utf8")
if rs != nil {
rs.Close()
}
c.Assert(err.Error(), Equals, "[ddl:210]unsupported modify charset from latin1 to utf8")
rs, err = tk.Exec("alter table t charset utf8 collate latin1_bin")
if rs != nil {
rs.Close()
}
c.Assert(err, NotNil)
rs, err = tk.Exec("alter table t charset utf8mb4")
if rs != nil {
rs.Close()
}
c.Assert(err.Error(), Equals, "[ddl:210]unsupported modify charset from latin1 to utf8mb4")
rs, err = tk.Exec("alter table t charset utf8mb4 collate utf8mb4_bin")
c.Assert(err, NotNil)
}
func (s *testIntegrationSuite) TestCaseInsensitiveCharsetAndCollate(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("create database if not exists test_charset_collate")
defer tk.MustExec("drop database test_charset_collate")
tk.MustExec("use test_charset_collate")
tk.MustExec("create table t(id int) ENGINE=InnoDB DEFAULT CHARSET=UTF8 COLLATE=UTF8_BIN;")
tk.MustExec("create table t1(id int) ENGINE=InnoDB DEFAULT CHARSET=UTF8 COLLATE=uTF8_BIN;")
tk.MustExec("create table t2(id int) ENGINE=InnoDB DEFAULT CHARSET=Utf8 COLLATE=utf8_BIN;")
tk.MustExec("create table t3(id int) ENGINE=InnoDB DEFAULT CHARSET=Utf8mb4 COLLATE=utf8MB4_BIN;")
tk.MustExec("create table t4(id int) ENGINE=InnoDB DEFAULT CHARSET=Utf8mb4 COLLATE=utf8MB4_general_ci;")
}
func (s *testIntegrationSuite) TestZeroFillCreateTable(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
s.tk.MustExec("drop table if exists abc;")
s.tk.MustExec("create table abc(y year, z tinyint(10) zerofill, primary key(y));")
is := s.dom.InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("abc"))
c.Assert(err, IsNil)
var yearCol, zCol *model.ColumnInfo
for _, col := range tbl.Meta().Columns {
if col.Name.String() == "y" {
yearCol = col
}
if col.Name.String() == "z" {
zCol = col
}
}
c.Assert(yearCol, NotNil)
c.Assert(yearCol.Tp, Equals, mysql.TypeYear)
c.Assert(mysql.HasUnsignedFlag(yearCol.Flag), IsTrue)
c.Assert(zCol, NotNil)
c.Assert(mysql.HasUnsignedFlag(zCol.Flag), IsTrue)
}
func (s *testIntegrationSuite) TestBitDefaultValue(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t_bit (c1 bit(10) default 250, c2 int);")
tk.MustExec("insert into t_bit set c2=1;")
tk.MustQuery("select bin(c1),c2 from t_bit").Check(testkit.Rows("11111010 1"))
tk.MustExec("drop table t_bit")
tk.MustExec(`create table testalltypes1 (
field_1 bit default 1,
field_2 tinyint null default null
);`)
tk.MustExec(`create table testalltypes2 (
field_1 bit null default null,
field_2 tinyint null default null,
field_3 tinyint unsigned null default null,
field_4 bigint null default null,
field_5 bigint unsigned null default null,
field_6 mediumblob null default null,
field_7 longblob null default null,
field_8 blob null default null,
field_9 tinyblob null default null,
field_10 varbinary(255) null default null,
field_11 binary(255) null default null,
field_12 mediumtext null default null,
field_13 longtext null default null,
field_14 text null default null,
field_15 tinytext null default null,
field_16 char(255) null default null,
field_17 numeric null default null,
field_18 decimal null default null,
field_19 integer null default null,
field_20 integer unsigned null default null,
field_21 int null default null,
field_22 int unsigned null default null,
field_23 mediumint null default null,
field_24 mediumint unsigned null default null,
field_25 smallint null default null,
field_26 smallint unsigned null default null,
field_27 float null default null,
field_28 double null default null,
field_29 double precision null default null,
field_30 real null default null,
field_31 varchar(255) null default null,
field_32 date null default null,
field_33 time null default null,
field_34 datetime null default null,
field_35 timestamp null default null
);`)
}
func (s *testIntegrationSuite) TestBackwardCompatibility(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("create database if not exists test_backward_compatibility")
defer tk.MustExec("drop database test_backward_compatibility")
tk.MustExec("use test_backward_compatibility")
tk.MustExec("create table t(a int primary key, b int)")
for i := 0; i < 200; i++ {
tk.MustExec(fmt.Sprintf("insert into t values(%v, %v)", i, i))
}
// alter table t add index idx_b(b);
is := s.dom.InfoSchema()
schemaName := model.NewCIStr("test_backward_compatibility")
tableName := model.NewCIStr("t")
schema, ok := is.SchemaByName(schemaName)
c.Assert(ok, IsTrue)
tbl, err := is.TableByName(schemaName, tableName)
c.Assert(err, IsNil)
// Split the table.
s.cluster.SplitTable(s.mvccStore, tbl.Meta().ID, 100)
unique := false
indexName := model.NewCIStr("idx_b")
idxColName := &ast.IndexColName{
Column: &ast.ColumnName{
Schema: schemaName,
Table: tableName,
Name: model.NewCIStr("b"),
},
Length: types.UnspecifiedLength,
}
idxColNames := []*ast.IndexColName{idxColName}
var indexOption *ast.IndexOption
job := &model.Job{
SchemaID: schema.ID,
TableID: tbl.Meta().ID,
Type: model.ActionAddIndex,
BinlogInfo: &model.HistoryInfo{},
Args: []interface{}{unique, indexName, idxColNames, indexOption},
}
txn, err := s.store.Begin()
c.Assert(err, IsNil)
t := meta.NewMeta(txn)
job.ID, err = t.GenGlobalID()
c.Assert(err, IsNil)
job.Version = 1
job.StartTS = txn.StartTS()
// Simulate old TiDB init the add index job, old TiDB will not init the model.Job.ReorgMeta field,
// if we set job.SnapshotVer here, can simulate the behavior.
job.SnapshotVer = txn.StartTS()
err = t.EnQueueDDLJob(job)
c.Assert(err, IsNil)
err = txn.Commit(context.Background())
c.Assert(err, IsNil)
ticker := time.NewTicker(s.lease)
for range ticker.C {
historyJob, err := s.getHistoryDDLJob(job.ID)
c.Assert(err, IsNil)
if historyJob == nil {
continue
}
c.Assert(historyJob.Error, IsNil)
if historyJob.IsSynced() {
break
}
}
// finished add index
tk.MustExec("admin check index t idx_b")
}
func (s *testIntegrationSuite) TestMultiRegionGetTableEndHandle(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("drop database if exists test_get_endhandle")
tk.MustExec("create database test_get_endhandle")
tk.MustExec("use test_get_endhandle")
tk.MustExec("create table t(a bigint PRIMARY KEY, b int)")
for i := 0; i < 1000; i++ {
tk.MustExec(fmt.Sprintf("insert into t values(%v, %v)", i, i))
}
// Get table ID for split.
dom := domain.GetDomain(tk.Se)
is := dom.InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test_get_endhandle"), model.NewCIStr("t"))
c.Assert(err, IsNil)
tblID := tbl.Meta().ID
d := s.dom.DDL()
testCtx := newTestMaxTableRowIDContext(c, d, tbl)
// Split the table.
s.cluster.SplitTable(s.mvccStore, tblID, 100)
maxID, emptyTable := s.getMaxTableRowID(testCtx)
c.Assert(emptyTable, IsFalse)
c.Assert(maxID, Equals, int64(999))
tk.MustExec("insert into t values(10000, 1000)")
maxID, emptyTable = s.getMaxTableRowID(testCtx)
c.Assert(emptyTable, IsFalse)
c.Assert(maxID, Equals, int64(10000))
tk.MustExec("insert into t values(-1, 1000)")
maxID, emptyTable = s.getMaxTableRowID(testCtx)
c.Assert(emptyTable, IsFalse)
c.Assert(maxID, Equals, int64(10000))
}
type testMaxTableRowIDContext struct {
c *C
d ddl.DDL
tbl table.Table
}
func newTestMaxTableRowIDContext(c *C, d ddl.DDL, tbl table.Table) *testMaxTableRowIDContext {
return &testMaxTableRowIDContext{
c: c,
d: d,
tbl: tbl,
}
}
func (s *testIntegrationSuite) getMaxTableRowID(ctx *testMaxTableRowIDContext) (int64, bool) {
c := ctx.c
d := ctx.d
tbl := ctx.tbl
curVer, err := s.store.CurrentVersion()
c.Assert(err, IsNil)
maxID, emptyTable, err := d.GetTableMaxRowID(curVer.Ver, tbl.(table.PhysicalTable))
c.Assert(err, IsNil)
return maxID, emptyTable
}
func (s *testIntegrationSuite) checkGetMaxTableRowID(ctx *testMaxTableRowIDContext, expectEmpty bool, expectMaxID int64) {
c := ctx.c
maxID, emptyTable := s.getMaxTableRowID(ctx)
c.Assert(emptyTable, Equals, expectEmpty)
c.Assert(maxID, Equals, expectMaxID)
}
func (s *testIntegrationSuite) TestGetTableEndHandle(c *C) {
// TestGetTableEndHandle test ddl.GetTableMaxRowID method, which will return the max row id of the table.
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("drop database if exists test_get_endhandle")
tk.MustExec("create database test_get_endhandle")
tk.MustExec("use test_get_endhandle")
// Test PK is handle.
tk.MustExec("create table t(a bigint PRIMARY KEY, b int)")
is := s.dom.InfoSchema()
d := s.dom.DDL()
tbl, err := is.TableByName(model.NewCIStr("test_get_endhandle"), model.NewCIStr("t"))
c.Assert(err, IsNil)
testCtx := newTestMaxTableRowIDContext(c, d, tbl)
// test empty table
s.checkGetMaxTableRowID(testCtx, true, int64(math.MaxInt64))
tk.MustExec("insert into t values(-1, 1)")
s.checkGetMaxTableRowID(testCtx, false, int64(-1))
tk.MustExec("insert into t values(9223372036854775806, 1)")
s.checkGetMaxTableRowID(testCtx, false, int64(9223372036854775806))
tk.MustExec("insert into t values(9223372036854775807, 1)")
s.checkGetMaxTableRowID(testCtx, false, int64(9223372036854775807))
tk.MustExec("insert into t values(10, 1)")
tk.MustExec("insert into t values(102149142, 1)")
s.checkGetMaxTableRowID(testCtx, false, int64(9223372036854775807))
tk.MustExec("create table t1(a bigint PRIMARY KEY, b int)")
for i := 0; i < 1000; i++ {
tk.MustExec(fmt.Sprintf("insert into t1 values(%v, %v)", i, i))
}
is = s.dom.InfoSchema()
testCtx.tbl, err = is.TableByName(model.NewCIStr("test_get_endhandle"), model.NewCIStr("t1"))
c.Assert(err, IsNil)
s.checkGetMaxTableRowID(testCtx, false, int64(999))
// Test PK is not handle
tk.MustExec("create table t2(a varchar(255))")
is = s.dom.InfoSchema()
testCtx.tbl, err = is.TableByName(model.NewCIStr("test_get_endhandle"), model.NewCIStr("t2"))
c.Assert(err, IsNil)
s.checkGetMaxTableRowID(testCtx, true, int64(math.MaxInt64))
for i := 0; i < 1000; i++ {
tk.MustExec(fmt.Sprintf("insert into t2 values(%v)", i))
}
result := tk.MustQuery("select MAX(_tidb_rowid) from t2")
maxID, emptyTable := s.getMaxTableRowID(testCtx)
result.Check(testkit.Rows(fmt.Sprintf("%v", maxID)))
c.Assert(emptyTable, IsFalse)
tk.MustExec("insert into t2 values(100000)")
result = tk.MustQuery("select MAX(_tidb_rowid) from t2")
maxID, emptyTable = s.getMaxTableRowID(testCtx)
result.Check(testkit.Rows(fmt.Sprintf("%v", maxID)))
c.Assert(emptyTable, IsFalse)
tk.MustExec(fmt.Sprintf("insert into t2 values(%v)", math.MaxInt64-1))
result = tk.MustQuery("select MAX(_tidb_rowid) from t2")
maxID, emptyTable = s.getMaxTableRowID(testCtx)
result.Check(testkit.Rows(fmt.Sprintf("%v", maxID)))
c.Assert(emptyTable, IsFalse)
tk.MustExec(fmt.Sprintf("insert into t2 values(%v)", math.MaxInt64))
result = tk.MustQuery("select MAX(_tidb_rowid) from t2")
maxID, emptyTable = s.getMaxTableRowID(testCtx)
result.Check(testkit.Rows(fmt.Sprintf("%v", maxID)))
c.Assert(emptyTable, IsFalse)
tk.MustExec("insert into t2 values(100)")
result = tk.MustQuery("select MAX(_tidb_rowid) from t2")
maxID, emptyTable = s.getMaxTableRowID(testCtx)
result.Check(testkit.Rows(fmt.Sprintf("%v", maxID)))
c.Assert(emptyTable, IsFalse)
}
func (s *testIntegrationSuite) getHistoryDDLJob(id int64) (*model.Job, error) {
var job *model.Job
err := kv.RunInNewTxn(s.store, false, func(txn kv.Transaction) error {
t := meta.NewMeta(txn)
var err1 error
job, err1 = t.GetHistoryDDLJob(id)
return errors.Trace(err1)
})
return job, errors.Trace(err)
}
func (s *testIntegrationSuite) TestCreateTableTooLarge(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
sql := "create table t_too_large ("
cnt := 3000
for i := 1; i <= cnt; i++ {
sql += fmt.Sprintf("a%d double, b%d double, c%d double, d%d double", i, i, i, i)
if i != cnt {
sql += ","
}
}
sql += ");"
assertErrorCode(c, s.tk, sql, tmysql.ErrTooManyFields)
originLimit := atomic.LoadUint32(&ddl.TableColumnCountLimit)
atomic.StoreUint32(&ddl.TableColumnCountLimit, uint32(cnt*4))
_, err := s.tk.Exec(sql)
c.Assert(kv.ErrEntryTooLarge.Equal(err), IsTrue, Commentf("err:%v", err))
atomic.StoreUint32(&ddl.TableColumnCountLimit, originLimit)
}
func (s *testIntegrationSuite) TestChangeColumnPosition(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
s.tk.MustExec("create table position (a int default 1, b int default 2)")
s.tk.MustExec("insert into position value ()")
s.tk.MustExec("insert into position values (3,4)")
s.tk.MustQuery("select * from position").Check(testkit.Rows("1 2", "3 4"))
s.tk.MustExec("alter table position modify column b int first")
s.tk.MustQuery("select * from position").Check(testkit.Rows("2 1", "4 3"))
s.tk.MustExec("insert into position value ()")
s.tk.MustQuery("select * from position").Check(testkit.Rows("2 1", "4 3", "<nil> 1"))
s.tk.MustExec("create table position1 (a int, b int, c double, d varchar(5))")
s.tk.MustExec(`insert into position1 value (1, 2, 3.14, 'TiDB')`)
s.tk.MustExec("alter table position1 modify column d varchar(5) after a")
s.tk.MustQuery("select * from position1").Check(testkit.Rows("1 TiDB 2 3.14"))
s.tk.MustExec("alter table position1 modify column a int after c")
s.tk.MustQuery("select * from position1").Check(testkit.Rows("TiDB 2 3.14 1"))
s.tk.MustExec("alter table position1 modify column c double first")
s.tk.MustQuery("select * from position1").Check(testkit.Rows("3.14 TiDB 2 1"))
assertErrorCode(c, s.tk, "alter table position1 modify column b int after b", tmysql.ErrBadField)
s.tk.MustExec("create table position2 (a int, b int)")
s.tk.MustExec("alter table position2 add index t(a, b)")
s.tk.MustExec("alter table position2 modify column b int first")
s.tk.MustExec("insert into position2 value (3, 5)")
s.tk.MustQuery("select a from position2 where a = 3").Check(testkit.Rows())
s.tk.MustExec("alter table position2 change column b c int first")
s.tk.MustQuery("select * from position2 where c = 3").Check(testkit.Rows("3 5"))
assertErrorCode(c, s.tk, "alter table position2 change column c b int after c", tmysql.ErrBadField)
s.tk.MustExec("create table position3 (a int default 2)")
s.tk.MustExec("alter table position3 modify column a int default 5 first")
s.tk.MustExec("insert into position3 value ()")
s.tk.MustQuery("select * from position3").Check(testkit.Rows("5"))
s.tk.MustExec("create table position4 (a int, b int)")
s.tk.MustExec("alter table position4 add index t(b)")
s.tk.MustExec("alter table position4 change column b c int first")
createSQL := s.tk.MustQuery("show create table position4").Rows()[0][1]
exceptedSQL := []string{
"CREATE TABLE `position4` (",
" `c` int(11) DEFAULT NULL,",
" `a` int(11) DEFAULT NULL,",
" KEY `t` (`c`)",
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
}
c.Assert(createSQL, Equals, strings.Join(exceptedSQL, "\n"))
}
func (s *testIntegrationSuite) TestAddIndexAfterAddColumn(c *C) {
s.tk = testkit.NewTestKit(c, s.store)
s.tk.MustExec("use test")
s.tk.MustExec("create table test_add_index_after_add_col(a int, b int not null default '0')")
s.tk.MustExec("insert into test_add_index_after_add_col values(1, 2),(2,2)")