-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
infoschema: load auto id related changes for multi-schema-change diff (…
- Loading branch information
1 parent
722da87
commit 8d4ad49
Showing
7 changed files
with
167 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2024 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 infoschema | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/pingcap/tidb/meta/autoid" | ||
"github.com/pingcap/tidb/parser/model" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type mockAlloc struct { | ||
autoid.Allocator | ||
tp autoid.AllocatorType | ||
} | ||
|
||
func (m *mockAlloc) GetType() autoid.AllocatorType { | ||
return m.tp | ||
} | ||
|
||
func TestGetKeptAllocators(t *testing.T) { | ||
checkAllocators := func(allocators autoid.Allocators, expected []autoid.AllocatorType) { | ||
require.Len(t, allocators.Allocs, len(expected)) | ||
for i, tp := range expected { | ||
require.Equal(t, tp, allocators.Allocs[i].GetType()) | ||
} | ||
} | ||
allocators := autoid.Allocators{Allocs: []autoid.Allocator{ | ||
&mockAlloc{tp: autoid.RowIDAllocType}, | ||
&mockAlloc{tp: autoid.AutoIncrementType}, | ||
&mockAlloc{tp: autoid.AutoRandomType}, | ||
}} | ||
cases := []struct { | ||
diff *model.SchemaDiff | ||
expected []autoid.AllocatorType | ||
}{ | ||
{ | ||
diff: &model.SchemaDiff{Type: model.ActionTruncateTable}, | ||
expected: []autoid.AllocatorType{autoid.RowIDAllocType, autoid.AutoIncrementType, autoid.AutoRandomType}, | ||
}, | ||
{ | ||
diff: &model.SchemaDiff{Type: model.ActionRebaseAutoID}, | ||
expected: []autoid.AllocatorType{autoid.AutoRandomType}, | ||
}, | ||
{ | ||
diff: &model.SchemaDiff{Type: model.ActionModifyTableAutoIdCache}, | ||
expected: []autoid.AllocatorType{autoid.AutoRandomType}, | ||
}, | ||
{ | ||
diff: &model.SchemaDiff{Type: model.ActionRebaseAutoRandomBase}, | ||
expected: []autoid.AllocatorType{autoid.RowIDAllocType, autoid.AutoIncrementType}, | ||
}, | ||
{ | ||
diff: &model.SchemaDiff{Type: model.ActionMultiSchemaChange, | ||
SubActionTypes: []model.ActionType{model.ActionAddColumn, model.ActionRebaseAutoID}}, | ||
expected: []autoid.AllocatorType{autoid.AutoRandomType}, | ||
}, | ||
{ | ||
diff: &model.SchemaDiff{Type: model.ActionMultiSchemaChange, | ||
SubActionTypes: []model.ActionType{model.ActionModifyTableAutoIdCache}}, | ||
expected: []autoid.AllocatorType{autoid.AutoRandomType}, | ||
}, | ||
{ | ||
diff: &model.SchemaDiff{Type: model.ActionMultiSchemaChange, | ||
SubActionTypes: []model.ActionType{model.ActionRebaseAutoRandomBase}}, | ||
expected: []autoid.AllocatorType{autoid.RowIDAllocType, autoid.AutoIncrementType}, | ||
}, | ||
{ | ||
diff: &model.SchemaDiff{Type: model.ActionMultiSchemaChange, | ||
SubActionTypes: []model.ActionType{model.ActionAddColumn}}, | ||
expected: []autoid.AllocatorType{autoid.RowIDAllocType, autoid.AutoIncrementType, autoid.AutoRandomType}, | ||
}, | ||
} | ||
for i, c := range cases { | ||
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) { | ||
res := getKeptAllocators(c.diff, allocators) | ||
checkAllocators(res, c.expected) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
drop table if exists t; | ||
create table t (a int auto_increment primary key, b int) auto_id_cache = 100; | ||
insert into t(b) values(1); | ||
alter table t modify column b tinyint, auto_increment = 200; | ||
insert into t (b) values (2); | ||
select * from t; | ||
a b | ||
1 1 | ||
200 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# TestMultiSchemaChangeTableOption | ||
drop table if exists t; | ||
create table t (a int auto_increment primary key, b int) auto_id_cache = 100; | ||
insert into t(b) values(1); | ||
alter table t modify column b tinyint, auto_increment = 200; | ||
insert into t (b) values (2); | ||
select * from t; |