This repository has been archived by the owner on May 31, 2022. It is now read-only.
forked from skeema/tengo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_test.go
1225 lines (1147 loc) · 48.8 KB
/
table_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
package tengo
import (
"fmt"
"strings"
"testing"
)
func TestTableGeneratedCreateStatement(t *testing.T) {
for nextAutoInc := uint64(1); nextAutoInc < 3; nextAutoInc++ {
table := aTable(nextAutoInc)
if table.GeneratedCreateStatement(FlavorUnknown) != table.CreateStatement {
t.Errorf("Generated DDL does not match actual DDL\nExpected:\n%s\nFound:\n%s", table.CreateStatement, table.GeneratedCreateStatement(FlavorUnknown))
}
}
table := anotherTable()
if table.GeneratedCreateStatement(FlavorUnknown) != table.CreateStatement {
t.Errorf("Generated DDL does not match actual DDL\nExpected:\n%s\nFound:\n%s", table.CreateStatement, table.GeneratedCreateStatement(FlavorUnknown))
}
table = unsupportedTable()
if table.GeneratedCreateStatement(FlavorUnknown) == table.CreateStatement {
t.Error("Expected unsupported table's generated DDL to differ from actual DDL, but they match")
}
}
func TestTableClusteredIndexKey(t *testing.T) {
table := aTable(1)
if table.ClusteredIndexKey() == nil || table.ClusteredIndexKey() != table.PrimaryKey {
t.Error("ClusteredIndexKey() did not return primary key when it was supposed to")
}
table.Engine = "MyISAM"
if table.ClusteredIndexKey() != nil {
t.Errorf("Expected ClusteredIndexKey() to return nil for non-InnoDB table, instead found %+v", table.ClusteredIndexKey())
}
table.Engine = "InnoDB"
table.PrimaryKey = nil
if table.ClusteredIndexKey() != table.SecondaryIndexes[0] {
t.Errorf("Expected ClusteredIndexKey() to return %+v, instead found %+v", table.SecondaryIndexes[0], table.ClusteredIndexKey())
}
table.SecondaryIndexes[0], table.SecondaryIndexes[1] = table.SecondaryIndexes[1], table.SecondaryIndexes[0]
if table.ClusteredIndexKey() != table.SecondaryIndexes[1] {
t.Errorf("Expected ClusteredIndexKey() to return %+v, instead found %+v", table.SecondaryIndexes[1], table.ClusteredIndexKey())
}
table.Columns[4].Nullable = true
if table.ClusteredIndexKey() != nil {
t.Errorf("Expected ClusteredIndexKey() to return nil for table with unique-but-nullable index, instead found %+v", table.ClusteredIndexKey())
}
table.Columns[4].Nullable = false
table.SecondaryIndexes[0].Unique = true
if table.ClusteredIndexKey() != table.SecondaryIndexes[1] {
t.Errorf("Expected ClusteredIndexKey() to return %+v, instead found %+v", table.SecondaryIndexes[1], table.ClusteredIndexKey())
}
table.Columns[2].Nullable = false
if table.ClusteredIndexKey() != table.SecondaryIndexes[0] {
t.Errorf("Expected ClusteredIndexKey() to return %+v, instead found %+v", table.SecondaryIndexes[0], table.ClusteredIndexKey())
}
}
func TestTableRowFormatClause(t *testing.T) {
assertRowFormatClause := func(createOptions, expectRowFormat string) {
t.Helper()
table := aTable(1)
table.CreateOptions = createOptions
if actual := table.RowFormatClause(); actual != expectRowFormat {
t.Errorf("Unexpected result from RowFormatClause() with CreateOptions=%s: expected %s, found %s", createOptions, expectRowFormat, actual)
}
}
cases := map[string]string{
"": "",
"FOO=BAR": "",
"ROW_FORMAT=DYNAMIC": "DYNAMIC",
"ROW_FORMAT=COMPRESSED": "COMPRESSED",
"ROW_FORMAT=COMPACT FOO=BAR": "COMPACT",
"FOO=BAR ROW_FORMAT=REDUNDANT BIP=BAP": "REDUNDANT",
"KEY_BLOCK_SIZE=8": "COMPRESSED",
"ROW_FORMAT=DYNAMIC KEY_BLOCK_SIZE=8": "DYNAMIC",
}
for createOptions, expectRowFormat := range cases {
assertRowFormatClause(createOptions, expectRowFormat)
}
}
func TestTableAlterAddOrDropColumn(t *testing.T) {
from := aTable(1)
to := aTable(1)
// Add a column to an arbitrary position
newCol := &Column{
Name: "age",
TypeInDB: "int unsigned",
Nullable: true,
Default: "NULL",
}
to.Columns = append(to.Columns, newCol)
colCount := len(to.Columns)
to.Columns[colCount-2], to.Columns[colCount-1] = to.Columns[colCount-1], to.Columns[colCount-2]
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported := from.Diff(&to)
if len(tableAlters) != 1 || !supported {
t.Fatalf("Incorrect number of table alters: expected 1, found %d", len(tableAlters))
}
ta, ok := tableAlters[0].(AddColumn)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", ta, tableAlters[0])
}
if ta.Table != &to || ta.Column != newCol {
t.Error("Pointers in table alter do not point to expected values")
}
if ta.PositionFirst || ta.PositionAfter != to.Columns[colCount-3] || !strings.Contains(ta.Clause(StatementModifiers{}), " AFTER ") {
t.Errorf("Expected new column to be after `%s` / first=false, instead found after `%s` / first=%t", to.Columns[colCount-3].Name, ta.PositionAfter.Name, ta.PositionFirst)
}
// Reverse comparison should yield a drop-column
tableAlters, supported = to.Diff(&from)
if len(tableAlters) != 1 || !supported {
t.Fatalf("Incorrect number of table alters: expected 1, found %d", len(tableAlters))
}
ta2, ok := tableAlters[0].(DropColumn)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", ta2, tableAlters[0])
}
if ta2.Column != newCol {
t.Error("Pointer in table alter does not point to expected value")
}
// Add an addition column to first position
hadColumns := to.Columns
anotherCol := &Column{
Name: "net_worth",
TypeInDB: "decimal(9,2)",
Nullable: true,
Default: "NULL",
}
to.Columns = []*Column{anotherCol}
to.Columns = append(to.Columns, hadColumns...)
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 2 || !supported {
t.Fatalf("Incorrect number of table alters: expected 2, found %d", len(tableAlters))
}
ta, ok = tableAlters[0].(AddColumn)
if !ok {
t.Fatalf("Incorrect type of table alter[0] returned: expected %T, found %T", ta, tableAlters[0])
}
if ta.Table != &to || ta.Column != anotherCol {
t.Error("Pointers in table alter[0] do not point to expected values")
}
if !ta.PositionFirst || ta.PositionAfter != nil || !strings.Contains(ta.Clause(StatementModifiers{}), " FIRST") {
t.Errorf("Expected first new column to be after nil / first=true, instead found after %v / first=%t", ta.PositionAfter, ta.PositionFirst)
}
// Add an additional column to the last position
anotherCol = &Column{
Name: "awards_won",
TypeInDB: "int unsigned",
Nullable: false,
Default: "'0'",
}
to.Columns = append(to.Columns, anotherCol)
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 3 || !supported {
t.Fatalf("Incorrect number of table alters: expected 3, found %d", len(tableAlters))
}
ta, ok = tableAlters[2].(AddColumn)
if !ok {
t.Fatalf("Incorrect type of table alter[2] returned: expected %T, found %T", ta, tableAlters[2])
}
if ta.Table != &to || ta.Column != anotherCol {
t.Error("Pointers in table alter[2] do not point to expected values")
}
if ta.PositionFirst || ta.PositionAfter != nil {
t.Errorf("Expected new column to be after nil / first=false, instead found after %v / first=%t", ta.PositionAfter, ta.PositionFirst)
}
}
func TestTableAlterAddOrDropIndex(t *testing.T) {
from := aTable(1)
to := aTable(1)
// Add a secondary index
newSecondary := &Index{
Name: "idx_alive_lastname",
Parts: []IndexPart{
{ColumnName: to.Columns[5].Name},
{ColumnName: to.Columns[2].Name, PrefixLength: 10},
},
Type: "BTREE",
}
to.SecondaryIndexes = append(to.SecondaryIndexes, newSecondary)
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported := from.Diff(&to)
if len(tableAlters) != 1 || !supported {
t.Fatalf("Incorrect number of table alters: expected 1, found %d", len(tableAlters))
}
ta, ok := tableAlters[0].(AddIndex)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", ta, tableAlters[0])
}
if ta.Index != newSecondary {
t.Error("Pointer in table alter does not point to expected value")
}
// Reverse comparison should yield a drop index
tableAlters, supported = to.Diff(&from)
if len(tableAlters) != 1 || !supported {
t.Fatalf("Incorrect number of table alters: expected 1, found %d", len(tableAlters))
}
ta2, ok := tableAlters[0].(DropIndex)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", ta2, tableAlters[0])
}
if ta2.Index != newSecondary {
t.Error("Pointer in table alter does not point to expected value")
}
// Start over; change the last existing secondary index
to = aTable(1)
to.SecondaryIndexes[1].Unique = true
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 2 || !supported {
t.Fatalf("Incorrect number of table alters: expected 2, found %d", len(tableAlters))
}
ta2, ok = tableAlters[0].(DropIndex)
if !ok {
t.Fatalf("Incorrect type of table alter[0] returned: expected %T, found %T", ta2, tableAlters[0])
}
if ta2.Index != from.SecondaryIndexes[1] {
t.Error("Pointer in table alter[0] does not point to expected value")
}
ta, ok = tableAlters[1].(AddIndex)
if !ok {
t.Fatalf("Incorrect type of table alter[1] returned: expected %T, found %T", ta, tableAlters[1])
}
if ta.Index != to.SecondaryIndexes[1] {
t.Error("Pointer in table alter[1] does not point to expected value")
}
// Start over; change the primary key
to = aTable(1)
to.PrimaryKey.Parts = append(to.PrimaryKey.Parts, IndexPart{ColumnName: to.Columns[4].Name})
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 2 || !supported {
t.Fatalf("Incorrect number of table alters: expected 2, found %d", len(tableAlters))
}
ta2, ok = tableAlters[0].(DropIndex)
if !ok {
t.Fatalf("Incorrect type of table alter[0] returned: expected %T, found %T", ta2, tableAlters[0])
}
if ta2.Index != from.PrimaryKey {
t.Error("Pointer in table alter[0] does not point to expected value")
}
ta, ok = tableAlters[1].(AddIndex)
if !ok {
t.Fatalf("Incorrect type of table alter[1] returned: expected %T, found %T", ta, tableAlters[1])
}
if ta.Index != to.PrimaryKey {
t.Error("Pointer in table alter[1] does not point to expected value")
}
// Remove the primary key
to.PrimaryKey = nil
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 1 || !supported {
t.Fatalf("Incorrect number of table alters: expected 1, found %d", len(tableAlters))
}
ta2, ok = tableAlters[0].(DropIndex)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", ta2, tableAlters[0])
}
if ta2.Index != from.PrimaryKey {
t.Error("Pointer in table alter does not point to expected value")
}
// Reverse comparison should yield an add PK
tableAlters, supported = to.Diff(&from)
if len(tableAlters) != 1 || !supported {
t.Fatalf("Incorrect number of table alters: expected 1, found %d", len(tableAlters))
}
ta, ok = tableAlters[0].(AddIndex)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", ta, tableAlters[0])
}
if ta.Index != from.PrimaryKey {
t.Error("Pointer in table alter does not point to expected value")
}
// Start over; change a secondary index to FULLTEXT
to = aTable(1)
to.SecondaryIndexes[1].Type = "FULLTEXT"
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 2 || !supported {
t.Fatalf("Incorrect number of table alters: expected 2, found %d", len(tableAlters))
}
ta2, ok = tableAlters[0].(DropIndex)
if !ok {
t.Fatalf("Incorrect type of table alter[0] returned: expected %T, found %T", ta2, tableAlters[0])
}
if ta2.Index != from.SecondaryIndexes[1] {
t.Error("Pointer in table alter[0] does not point to expected value")
}
ta, ok = tableAlters[1].(AddIndex)
if !ok {
t.Fatalf("Incorrect type of table alter[1] returned: expected %T, found %T", ta, tableAlters[1])
}
if ta.Index != to.SecondaryIndexes[1] {
t.Error("Pointer in table alter[1] does not point to expected value")
}
}
func TestTableAlterAddOrDropForeignKey(t *testing.T) {
from := anotherTable()
to := anotherTable()
// Add the foreign key constraint
newFk := &ForeignKey{
Name: "actor_fk",
ColumnNames: []string{to.Columns[0].Name},
ReferencedSchemaName: "", // leave blank to signal its the same schema as the current table
ReferencedTableName: "actor",
ReferencedColumnNames: []string{"actor_id"},
DeleteRule: "RESTRICT",
UpdateRule: "CASCADE",
}
to.ForeignKeys = append(to.ForeignKeys, newFk)
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
// Normal Comparison should yield add ForeignKey
tableAlters, supported := from.Diff(&to)
if len(tableAlters) != 1 || !supported {
t.Fatalf("Incorrect number of table alters: expected 1, found %d", len(tableAlters))
}
taFk1, ok := tableAlters[0].(AddForeignKey)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", taFk1, tableAlters[0])
}
if taFk1.ForeignKey != newFk {
t.Error("Pointer in table alter does not point to expected value")
}
// Reverse comparison should yield a drop foreign key
tableAlters, supported = to.Diff(&from)
if len(tableAlters) != 1 || !supported {
t.Fatalf("Incorrect number of table alters: expected 1, found %d", len(tableAlters))
}
taFk2, ok := tableAlters[0].(DropForeignKey)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", taFk2, tableAlters[0])
}
if taFk2.ForeignKey != newFk {
t.Error("Pointer in table alter does not point to expected value")
}
// New situation: changing an existing foreign key
from = foreignKeyTable()
to = foreignKeyTable()
to.ForeignKeys[1].UpdateRule = "SET NULL"
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 2 || !supported {
t.Fatalf("Incorrect number of table alters: expected 2, found %d", len(tableAlters))
}
taFk2, ok = tableAlters[0].(DropForeignKey)
if !ok {
t.Fatalf("Incorrect type of table alter[0] returned: expected %T, found %T", taFk2, tableAlters[0])
}
if taFk2.ForeignKey != from.ForeignKeys[1] {
t.Error("Pointer in table alter[0] does not point to expected value")
}
taFk1, ok = tableAlters[1].(AddForeignKey)
if !ok {
t.Fatalf("Incorrect type of table alter[1] returned: expected %T, found %T", taFk1, tableAlters[1])
}
if taFk1.ForeignKey != to.ForeignKeys[1] {
t.Error("Pointer in table alter[1] does not point to expected value")
}
// Changing the first FK should not affect 2nd FK, since FKs are not ordered
to = foreignKeyTable()
to.ForeignKeys[0].ReferencedSchemaName = ""
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 2 || !supported {
t.Fatalf("Incorrect number of table alters: expected 2, found %d", len(tableAlters))
}
taFk2, ok = tableAlters[0].(DropForeignKey)
if !ok {
t.Fatalf("Incorrect type of table alter[0] returned: expected %T, found %T", taFk2, tableAlters[0])
}
if taFk2.ForeignKey != from.ForeignKeys[0] {
t.Error("Pointer in table alter[0] does not point to expected value")
}
taFk1, ok = tableAlters[1].(AddForeignKey)
if !ok {
t.Fatalf("Incorrect type of table alter[1] returned: expected %T, found %T", taFk1, tableAlters[1])
}
if taFk1.ForeignKey != to.ForeignKeys[0] {
t.Error("Pointer in table alter[1] does not point to expected value")
}
}
func TestTableAlterAddIndexOrder(t *testing.T) {
from := aTable(1)
to := aTable(1)
// Add 10 secondary indexes, and ensure their order is preserved
for n := 0; n < 10; n++ {
to.SecondaryIndexes = append(to.SecondaryIndexes, &Index{
Name: fmt.Sprintf("newidx_%d", n),
Parts: []IndexPart{{ColumnName: to.Columns[0].Name}},
Type: "BTREE",
})
}
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported := from.Diff(&to)
if len(tableAlters) != 10 || !supported {
t.Fatalf("Incorrect number of table alters: expected 10, found %d, supported=%t", len(tableAlters), supported)
}
for n := 0; n < 10; n++ {
ta, ok := tableAlters[n].(AddIndex)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", ta, tableAlters[0])
}
expectName := fmt.Sprintf("newidx_%d", n)
if ta.Index.Name != expectName {
t.Errorf("Incorrect index order: expected alters[%d] to be index name %s, instead found %s", n, expectName, ta.Index.Name)
}
}
// Also modify an existing index, and ensure its corresponding drop + re-add
// comes before the other 10 adds
to.SecondaryIndexes[1].Parts[1].PrefixLength = 6
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 12 || !supported {
t.Fatalf("Incorrect number of table alters: expected 12, found %d, supported=%t", len(tableAlters), supported)
}
if ta, ok := tableAlters[0].(DropIndex); !ok {
t.Errorf("Expected alters[0] to be a DropIndex, instead found %T", ta)
}
if ta, ok := tableAlters[1].(AddIndex); !ok {
t.Errorf("Expected alters[1] to be an AddIndex, instead found %T", ta)
} else if ta.Index.Name != to.SecondaryIndexes[1].Name {
t.Errorf("Expected alters[1] to be on index %s, instead found %s", to.SecondaryIndexes[1].Name, ta.Index.Name)
}
// Revert previous change, and instead change visibility on first index. This
// should be handled by an ALTER INDEX clause, w/o any need to drop anything.
to.SecondaryIndexes[1].Parts[1].PrefixLength = from.SecondaryIndexes[1].Parts[1].PrefixLength
to.SecondaryIndexes[0].Invisible = true
to.CreateStatement = to.GeneratedCreateStatement(FlavorMySQL80)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 11 || !supported {
t.Fatalf("Incorrect number of table alters: expected 11, found %d, supported=%t", len(tableAlters), supported)
}
expectClause := "ALTER INDEX `idx_ssn` INVISIBLE"
if ta, ok := tableAlters[0].(AlterIndex); !ok {
t.Errorf("Expected alters[0] to be an AlterIndex, instead found %T", ta)
} else if ta.Index.Name != to.SecondaryIndexes[0].Name || !ta.NewInvisible {
t.Errorf("Unexpected values in AlterIndex: %+v", ta)
} else if clauseWithoutFlavor := ta.Clause(StatementModifiers{}); clauseWithoutFlavor != "" {
t.Errorf("Unexpected result for AlterIndex.Clause() without a MySQLish 8.0+ flavor: %q", clauseWithoutFlavor)
} else if clauseWithFlavor := ta.Clause(StatementModifiers{Flavor: FlavorPercona80}); clauseWithFlavor != expectClause {
t.Errorf("Unexpected result for AlterIndex.Clause() with a MySQLish 8.0+ flavor: %q", clauseWithFlavor)
} else if clauseWithFlavor := ta.Clause(StatementModifiers{Flavor: FlavorMariaDB106}); clauseWithFlavor != strings.ReplaceAll(expectClause, "INVISIBLE", "IGNORED") {
t.Errorf("Unexpected result for AlterIndex.Clause() with a MariaDB 10.6 flavor: %q", clauseWithFlavor)
}
// Also change another aspect of the first index. Now this should be a DROP for
// index [0], re-ADD for [0], DROP for index [1], re-ADD for [1], followed by
// 10 ADDs for the 10 new indexes.
to.SecondaryIndexes[0].Parts = append(to.SecondaryIndexes[0].Parts, IndexPart{
ColumnName: "last_name",
Descending: true,
})
to.CreateStatement = to.GeneratedCreateStatement(FlavorMySQL80)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 14 || !supported {
t.Fatalf("Incorrect number of table alters: expected 14, found %d, supported=%t", len(tableAlters), supported)
}
for n, ta := range tableAlters {
var ok bool
if n == 0 || n == 2 {
_, ok = ta.(DropIndex)
} else {
_, ok = ta.(AddIndex)
}
if !ok {
t.Errorf("Unexpected type of alter clause at position %d: %T", n, ta)
}
}
}
func TestTableAlterIndexReorder(t *testing.T) {
// Table with three secondary indexes:
// [0] is UNIQUE KEY `idx_ssn` (`ssn`)
// [1] is KEY `idx_actor_name` (`last_name`(10),`first_name`(1))
// [2] is KEY `idx_alive_lastname` (`alive`, `last_name`(10))
getTable := func() Table {
table := aTable(1)
table.SecondaryIndexes = append(table.SecondaryIndexes, &Index{
Name: "idx_alive_lastname",
Parts: []IndexPart{
{ColumnName: table.Columns[5].Name},
{ColumnName: table.Columns[2].Name, PrefixLength: 10},
},
Type: "BTREE",
})
table.CreateStatement = table.GeneratedCreateStatement(FlavorUnknown)
return table
}
assertClauses := func(from, to *Table, strict bool, format string, a ...interface{}) {
t.Helper()
td := NewAlterTable(from, to)
var clauses string
if td != nil {
var err error
clauses, err = td.Clauses(StatementModifiers{
StrictIndexOrder: strict,
Flavor: FlavorMySQL80,
})
if err != nil {
t.Fatalf("Unexpected error result from Clauses(): %s", err)
}
}
expected := fmt.Sprintf(format, a...)
if clauses != expected {
t.Errorf("Unexpected result from Clauses()\nExpected:\n %s\nFound:\n %s", expected, clauses)
}
}
from, to := getTable(), getTable()
orig := from.SecondaryIndexes
// Reorder to's last couple indexes ([1] and [2]). Resulting diff should
// drop [1] and re-add [1], but should manifest as a no-op statement unless
// mods.StrictIndexOrder enabled.
to.SecondaryIndexes[1], to.SecondaryIndexes[2] = to.SecondaryIndexes[2], to.SecondaryIndexes[1]
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, _ := from.Diff(&to)
if len(tableAlters) != 2 {
t.Errorf("Expected 2 clauses, instead found %d", len(tableAlters))
} else {
if drop, ok := tableAlters[0].(DropIndex); !ok {
t.Errorf("Expected tableAlters[0] to be %T, instead found %T", drop, tableAlters[0])
} else if drop.Index.Name != orig[1].Name {
t.Errorf("Expected tableAlters[0] to drop %s, instead dropped %s", orig[1].Name, drop.Index.Name)
}
if add, ok := tableAlters[1].(AddIndex); !ok {
t.Errorf("Expected tableAlters[1] to be %T, instead found %T", add, tableAlters[1])
} else if add.Index.Name != orig[1].Name {
t.Errorf("Expected tableAlters[1] to add %s, instead added %s", orig[1].Name, add.Index.Name)
}
assertClauses(&from, &to, false, "")
assertClauses(&from, &to, true, "DROP KEY `%s`, ADD %s", orig[1].Name, orig[1].Definition(FlavorUnknown))
}
// Clustered index key changes: same effect as mods.StrictIndexOrder
to.PrimaryKey = nil
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
assertClauses(&from, &to, false, "DROP PRIMARY KEY, DROP KEY `%s`, ADD %s", orig[1].Name, orig[1].Definition(FlavorUnknown))
assertClauses(&from, &to, true, "DROP PRIMARY KEY, DROP KEY `%s`, ADD %s", orig[1].Name, orig[1].Definition(FlavorUnknown))
// Restore to previous state, and then modify definition of [1] and visibility
// of [2]. Resulting diff should:
// * drop [1]
// * re-add the modified [1]
// * modify visibility of [2] (suppressed if mods.StrictIndexOrder)
// * drop [2] (suppressed unless mods.StrictIndexOrder)
// * re-add [2] (suppressed unless mods.StrictIndexOrder)
to = getTable()
to.SecondaryIndexes[1].Parts[1].PrefixLength = 8
to.SecondaryIndexes[2].Invisible = true
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, _ = from.Diff(&to)
if len(tableAlters) != 5 {
t.Errorf("Expected 5 clauses, instead found %d", len(tableAlters))
} else {
drop0, ok0 := tableAlters[0].(DropIndex)
add1, ok1 := tableAlters[1].(AddIndex)
alter2, ok2 := tableAlters[2].(AlterIndex)
drop3, ok3 := tableAlters[3].(DropIndex)
add4, ok4 := tableAlters[4].(AddIndex)
if !ok0 || !ok1 || !ok2 || !ok3 || !ok4 {
t.Errorf("One or more type mismatches; ok: %t %t %t %t %t", ok0, ok1, ok2, ok3, ok4)
} else {
if !alter2.alsoReordering {
t.Error("Expected AlterIndex.alsoReordering to be true, but it was not")
}
if drop0.Index.Name == drop3.Index.Name {
t.Errorf("Both drops refer to same index %s", drop0.Index.Name)
}
if add1.Index.Name != orig[1].Name || add1.Index.Parts[1].PrefixLength != 8 {
t.Errorf("tableAlters[1] does not match expectations; found %+v", add1.Index)
}
if !add4.Index.EqualsIgnoringVisibility(orig[2]) {
t.Errorf("tableAlters[4] does not match expectations; found %+v", add4.Index)
}
}
assertClauses(&from, &to, false, "DROP KEY `%s`, ADD %s, ALTER INDEX `%s` INVISIBLE", orig[1].Name, to.SecondaryIndexes[1].Definition(FlavorUnknown), orig[2].Name)
assertClauses(&from, &to, true, "DROP KEY `%s`, ADD %s, DROP KEY `%s`, ADD %s", orig[1].Name, to.SecondaryIndexes[1].Definition(FlavorUnknown), orig[2].Name, to.SecondaryIndexes[2].Definition(FlavorMySQL80))
}
// Adding a new index before [1] should also result in dropping the old [1]
// and [2], and then re-adding them back in that order. But statement should
// only refer to adding the new index unless mods.StrictIndexOrder used.
to = getTable()
newIdx := &Index{
Name: "idx_firstname",
Parts: []IndexPart{
{ColumnName: to.Columns[1].Name},
},
Type: "BTREE",
}
to.SecondaryIndexes = []*Index{to.SecondaryIndexes[0], newIdx, to.SecondaryIndexes[1], to.SecondaryIndexes[2]}
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, _ = from.Diff(&to)
if len(tableAlters) != 5 {
t.Errorf("Expected 5 clauses, instead found %d", len(tableAlters))
} else {
assertClauses(&from, &to, false, "ADD %s", newIdx.Definition(FlavorUnknown))
assertClauses(&from, &to, true, "ADD %s, DROP KEY `%s`, ADD %s, DROP KEY `%s`, ADD %s", newIdx.Definition(FlavorUnknown), orig[1].Name, orig[1].Definition(FlavorUnknown), orig[2].Name, orig[2].Definition(FlavorUnknown))
}
// The opposite operation -- dropping the new index that we put before [1] --
// should just result in a drop, no need to reorder anything
tableAlters, _ = to.Diff(&from)
if len(tableAlters) != 1 {
t.Errorf("Expected 1 clause, instead found %d", len(tableAlters))
} else {
if drop, ok := tableAlters[0].(DropIndex); !ok {
t.Errorf("Expected tableAlters[0] to be %T, instead found %T", drop, tableAlters[0])
} else if drop.Index.Name != newIdx.Name {
t.Errorf("Expected tableAlters[0] to drop %s, instead dropped %s", newIdx.Name, drop.Index.Name)
}
assertClauses(&to, &from, false, "DROP KEY `%s`", newIdx.Name)
assertClauses(&to, &from, true, "DROP KEY `%s`", newIdx.Name)
}
}
func TestTableAlterModifyColumn(t *testing.T) {
from := aTable(1)
to := aTable(1)
// Reposition a col to first position
movedColPos := 3
movedCol := to.Columns[movedColPos]
to.Columns = append(to.Columns[:movedColPos], to.Columns[movedColPos+1:]...)
to.Columns = append([]*Column{movedCol}, to.Columns...)
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported := from.Diff(&to)
if len(tableAlters) != 1 || !supported {
t.Fatalf("Incorrect number of table alters: expected 1, found %d", len(tableAlters))
}
ta, ok := tableAlters[0].(ModifyColumn)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", ta, tableAlters[0])
}
if ta.Table != &to || ta.OldColumn != from.Columns[movedColPos] || ta.NewColumn != movedCol {
t.Error("Pointers in table alter do not point to expected values")
}
if !ta.PositionFirst || ta.PositionAfter != nil || !strings.Contains(ta.Clause(StatementModifiers{}), " FIRST") {
t.Errorf("Expected modified column to be after nil / first=true, instead found after %v / first=%t", ta.PositionAfter, ta.PositionFirst)
}
// Reposition same col to last position
to = aTable(1)
movedCol = to.Columns[movedColPos]
shouldBeAfter := to.Columns[len(to.Columns)-1]
to.Columns = append(to.Columns[:movedColPos], to.Columns[movedColPos+1:]...)
to.Columns = append(to.Columns, movedCol)
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 1 || !supported {
t.Fatalf("Incorrect number of table alters: expected 1, found %d", len(tableAlters))
}
ta, ok = tableAlters[0].(ModifyColumn)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", ta, tableAlters[0])
}
if ta.PositionFirst || ta.PositionAfter != shouldBeAfter {
t.Errorf("Expected modified column to be after %s / first=false, instead found after %v / first=%t", shouldBeAfter.Name, ta.PositionAfter, ta.PositionFirst)
}
if !ta.NewColumn.Equals(ta.OldColumn) {
t.Errorf("Column definition unexpectedly changed: was %s, now %s", ta.OldColumn.Definition(FlavorUnknown, nil), ta.NewColumn.Definition(FlavorUnknown, nil))
}
// Repos to last position AND change column definition
movedCol.Nullable = !movedCol.Nullable
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 1 || !supported {
t.Fatalf("Incorrect number of table alters: expected 1, found %d", len(tableAlters))
}
ta, ok = tableAlters[0].(ModifyColumn)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", ta, tableAlters[0])
}
if ta.PositionFirst || ta.PositionAfter != shouldBeAfter {
t.Errorf("Expected modified column to be after %s / first=false, instead found after %v / first=%t", shouldBeAfter.Name, ta.PositionAfter, ta.PositionFirst)
}
if ta.NewColumn.Equals(ta.OldColumn) {
t.Errorf("Column definition unexpectedly NOT changed: still %s", ta.NewColumn.Definition(FlavorUnknown, nil))
}
// Start over; delete a col, move last col to its former position, and add a new col after that
// FROM: actor_id, first_name, last_name, last_updated, ssn, alive, alive_bit
// TO: actor_id, first_name, last_name, alive, alive_bit, age, ssn
// current move algo treats this as a move of ssn to be after alive, rather than alive to be after last_name
to = aTable(1)
newCol := &Column{
Name: "age",
TypeInDB: "int unsigned",
Nullable: true,
Default: "NULL",
}
to.Columns = append(to.Columns[0:3], to.Columns[5], to.Columns[6], newCol, to.Columns[4])
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 3 || !supported {
t.Fatalf("Incorrect number of table alters: expected 3, found %d", len(tableAlters))
}
// The alters should always be in this order: drops, modifications, adds
if drop, ok := tableAlters[0].(DropColumn); ok {
if drop.Column != from.Columns[3] {
t.Error("Pointer in table alter[0] does not point to expected value")
}
} else {
t.Errorf("Incorrect type of table alter[0] returned: expected %T, found %T", drop, tableAlters[0])
}
if modify, ok := tableAlters[1].(ModifyColumn); ok {
if modify.NewColumn.Name != "ssn" {
t.Error("Pointers in table alter[1] do not point to expected values")
}
if modify.PositionFirst || modify.PositionAfter.Name != "alive_bit" {
t.Errorf("Expected moved column to be after alive_bit / first=false, instead found after %v / first=%t", modify.PositionAfter, modify.PositionFirst)
}
} else {
t.Errorf("Incorrect type of table alter[1] returned: expected %T, found %T", modify, tableAlters[1])
}
if add, ok := tableAlters[2].(AddColumn); ok {
if add.PositionFirst || add.PositionAfter.Name != "alive_bit" {
t.Errorf("Expected new column to be after alive_bit / first=false, instead found after %v / first=%t", add.PositionAfter, add.PositionFirst)
}
} else {
t.Errorf("Incorrect type of table alter[2] returned: expected %T, found %T", add, tableAlters[2])
}
// Start over; just change a column definition without moving anything
to = aTable(1)
to.Columns[4].TypeInDB = "varchar(10)"
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 1 || !supported {
t.Fatalf("Incorrect number of table alters: expected 1, found %d", len(tableAlters))
}
ta, ok = tableAlters[0].(ModifyColumn)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", ta, tableAlters[0])
}
if ta.PositionFirst || ta.PositionAfter != nil {
t.Errorf("Expected modified column to not be moved, instead found after %v / first=%t", ta.PositionAfter, ta.PositionFirst)
}
if ta.NewColumn.Equals(from.Columns[4]) {
t.Errorf("Column definition unexpectedly NOT changed: still %s", ta.NewColumn.Definition(FlavorUnknown, nil))
}
// Start over; change one column and move another column
to = aTable(1)
to.Columns[4].TypeInDB = "char(12)"
to.Columns = []*Column{to.Columns[0], to.Columns[6], to.Columns[1], to.Columns[2], to.Columns[3], to.Columns[4], to.Columns[5]}
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported = from.Diff(&to)
if len(tableAlters) != 2 || !supported {
stmt, _ := NewAlterTable(&from, &to).Statement(StatementModifiers{})
t.Fatalf("Incorrect number of table alters: expected 2, found %d: %s", len(tableAlters), stmt)
}
for _, ta := range tableAlters {
mc, ok := ta.(ModifyColumn)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", mc, ta)
}
if mc.PositionAfter != nil {
if mc.PositionAfter.Name != to.Columns[0].Name {
t.Errorf("Re-ordered column expected to be AFTER %s, instead AFTER %s", to.Columns[0].Name, mc.PositionAfter.Name)
}
if mc.OldColumn.Definition(FlavorUnknown, nil) != mc.NewColumn.Definition(FlavorUnknown, nil) {
t.Error("Expected re-ordered column definition to remain unchanged, but it was modified")
}
} else if mc.NewColumn.TypeInDB != "char(12)" || mc.PositionAfter != nil || mc.PositionFirst {
t.Errorf("Unexpected alter: %s", mc.Clause(StatementModifiers{}))
}
}
// Start over; move 2 columns and verify that each column is only mentioned
// once in the generated ALTER
to = aTable(1)
to.Columns = []*Column{to.Columns[0], to.Columns[1], to.Columns[2], to.Columns[4], to.Columns[6], to.Columns[3], to.Columns[5]}
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
tableAlters, supported = from.Diff(&to)
if !supported {
t.Error("Expected diff to be supported, but it was not")
} else {
stmt, _ := NewAlterTable(&from, &to).Statement(StatementModifiers{})
if len(tableAlters) != 2 {
t.Errorf("Incorrect number of table alters: expected 2, found %d: %s", len(tableAlters), stmt)
}
seen := make(map[string]bool, len(to.Columns))
for _, ta := range tableAlters {
mc, ok := ta.(ModifyColumn)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", mc, ta)
}
if seen[mc.NewColumn.Name] {
t.Fatalf("Column %s illegally referenced in generated ALTER multiple times:\n%s", EscapeIdentifier(mc.NewColumn.Name), stmt)
}
seen[mc.NewColumn.Name] = true
}
}
}
func TestTableAlterNoModify(t *testing.T) {
// Compare to a table with no common columns, and confirm no MODIFY clauses
// present
from := aTable(1)
to := aTable(1)
for n := range to.Columns {
to.Columns[n].Name = fmt.Sprintf("xzy%s", to.Columns[n].Name)
}
to.CreateStatement = to.GeneratedCreateStatement(FlavorUnknown)
if tableAlters, supported := from.Diff(&to); !supported {
t.Error("Expected diff to be supported, but it was not")
} else {
for _, ta := range tableAlters {
if _, ok := ta.(ModifyColumn); ok {
t.Errorf("Unexpected ModifyColumn: %+v", ta)
}
}
}
}
// TestTableAlterModifyColumnRandomly performs 20 iterations of semi-random
// modifications to a table's columns ordering and types, and then confirms
// that running the ALTER actually has the expected effect. It is commented out
// because this is overkill for routine testing, but the code can be useful
// when changing deep parts of the diff logic, especially the column reordering
// algorithm.
/*
func (s TengoIntegrationSuite) TestTableAlterModifyColumnRandomly(t *testing.T) {
exec := func(query string) {
t.Helper()
db, err := s.d.Connect("testing", "")
if err != nil {
t.Fatalf("Unable to connect to DockerizedInstance: %s", err)
}
_, err = db.Exec(query)
if err != nil {
t.Fatalf("Error running query on DockerizedInstance.\nQuery: %s\nError: %s", query, err)
}
}
assertCreate := func(table *Table) {
t.Helper()
createStatement, err := s.d.ShowCreateTable("testing", table.Name)
if err != nil {
t.Fatalf("Unexpected query error: %s", err)
}
if createStatement != table.CreateStatement {
t.Errorf("Mismatch between actual and expected CREATE TABLE.\nActual:\n%s\nExpected:\n%s", createStatement, table.CreateStatement)
}
}
from := aTableForFlavor(s.d.Flavor(), 1)
for n := 0; n < 20; n++ {
to := aTableForFlavor(s.d.Flavor(), 1)
swaps := rand.Intn(len(to.Columns) + 1)
for swap := 0; swap < swaps; swap++ {
a := rand.Intn(len(to.Columns))
b := rand.Intn(len(to.Columns))
to.Columns[a], to.Columns[b] = to.Columns[b], to.Columns[a]
}
mods := rand.Intn(3)
for mod := 0; mod < mods; mod++ {
n := rand.Intn(len(to.Columns))
col := to.Columns[n]
switch col.TypeInDB {
case "varchar(45)":
col.TypeInDB = "varchar(55)"
case "char(10)":
col.TypeInDB = "char(12)"
case "tinyint(1)", "bit(1)":
col.Nullable = true
case "smallint(5) unsigned":
col.TypeInDB = "int(10) unsigned"
}
}
to.CreateStatement = to.GeneratedCreateStatement(s.d.Flavor())
exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", from.Name))
exec(from.CreateStatement)
alter := NewAlterTable(&from, &to)
if alter == nil {
assertCreate(&to) // assert correct without any change
} else if !alter.supported {
t.Fatal("Expected diff to be supported, but it was not")
} else {
stmt, _ := alter.Statement(StatementModifiers{})
seen := make(map[string]bool, len(to.Columns))
for _, ta := range alter.AlterClauses {
mc, ok := ta.(ModifyColumn)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", mc, ta)
}
if seen[mc.NewColumn.Name] {
t.Fatalf("Column %s illegally referenced in generated ALTER multiple times:\n%s", EscapeIdentifier(mc.NewColumn.Name), stmt)
}
seen[mc.NewColumn.Name] = true
}
exec(stmt)
assertCreate(&to)
}
}
}
*/
func TestTableAlterChangeStorageEngine(t *testing.T) {
getTableWithEngine := func(engine string) Table {
t := aTable(1)
t.Engine = engine
t.CreateStatement = t.GeneratedCreateStatement(FlavorUnknown)
return t
}
assertChangeEngine := func(a, b *Table, expected string) {
tableAlters, supported := a.Diff(b)
if expected == "" {
if len(tableAlters) != 0 || !supported {
t.Fatalf("Incorrect result from Table.Diff(): expected len=0, true; found len=%d, %t", len(tableAlters), supported)
}
return
}
if len(tableAlters) != 1 || !supported {
t.Fatalf("Incorrect result from Table.Diff(): expected len=1, supported=true; found len=%d, supported=%t", len(tableAlters), supported)
}
ta, ok := tableAlters[0].(ChangeStorageEngine)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", ta, tableAlters[0])
}
if actual := ta.Clause(StatementModifiers{}); actual != expected {
t.Errorf("Incorrect ALTER TABLE clause returned; expected: %s; found: %s", expected, actual)
}
}
from := getTableWithEngine("InnoDB")
to := getTableWithEngine("InnoDB")
assertChangeEngine(&from, &to, "")
to = getTableWithEngine("MyISAM")
assertChangeEngine(&from, &to, "ENGINE=MyISAM")
assertChangeEngine(&to, &from, "ENGINE=InnoDB")
}
func TestTableAlterChangeAutoIncrement(t *testing.T) {
// Initial test: change next auto inc from 1 to 2
from := aTable(1)
to := aTable(2)
tableAlters, supported := from.Diff(&to)
if len(tableAlters) != 1 || !supported {
t.Fatalf("Incorrect number of table alters: expected 1, found %d", len(tableAlters))
}
ta, ok := tableAlters[0].(ChangeAutoIncrement)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", ta, tableAlters[0])
}
if ta.OldNextAutoIncrement != from.NextAutoIncrement || ta.NewNextAutoIncrement != to.NextAutoIncrement {
t.Error("Incorrect next-auto-increment values in alter clause")
}
// Reverse test: should emit an alter clause, even though higher-level caller
// may decide to ignore it
tableAlters, supported = to.Diff(&from)
if len(tableAlters) != 1 || !supported {
t.Fatalf("Incorrect number of table alters: expected 1, found %d", len(tableAlters))
}
ta, ok = tableAlters[0].(ChangeAutoIncrement)
if !ok {
t.Fatalf("Incorrect type of table alter returned: expected %T, found %T", ta, tableAlters[0])
}
if ta.OldNextAutoIncrement != to.NextAutoIncrement || ta.NewNextAutoIncrement != from.NextAutoIncrement {
t.Error("Incorrect next-auto-increment values in alter clause")
}