Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(catalog): check the auto_increment column count for CREATE TABLE (#3047)

rebeliceed4954c2eb39
diff --git a/plugin/advisor/catalog/walk_through.go b/plugin/advisor/catalog/walk_through.go
index 3511c6915d..978ec684f1 100644
--- a/plugin/advisor/catalog/walk_through.go
+++ b/plugin/advisor/catalog/walk_through.go
@@ -59,6 +59,8 @@ const (
ErrorTypeColumnNotExists = 402
// ErrorTypeDropAllColumns is the error that dropping all columns in a table.
ErrorTypeDropAllColumns = 403
+ // ErrorTypeAutoIncrementExists is the error that auto_increment exists.
+ ErrorTypeAutoIncrementExists = 404
// 501 ~ 599 index error type.
@@ -1033,8 +1035,19 @@ func (d *DatabaseState) createTable(node *tidbast.CreateTableStmt) *WalkThroughE
indexSet: make(indexStateMap),
}
schema.tableSet[table.name] = table
+ hasAutoIncrement := false
for _, column := range node.Cols {
+ if isAutoIncrement(column) {
+ if hasAutoIncrement {
+ return &WalkThroughError{
+ Type: ErrorTypeAutoIncrementExists,
+ // The content comes from MySQL error content.
+ Content: fmt.Sprintf("There can be only one auto column for table `%s`", table.name),
+ }
+ }
+ hasAutoIncrement = true
+ }
if err := table.createColumn(d.ctx, column, nil /* position */); err != nil {
err.Line = column.OriginTextPosition()
return err
@@ -1129,6 +1142,15 @@ func (t *TableState) validateAndGetKeyStringList(ctx *FinderContext, keyList []*
return res, nil
}
+func isAutoIncrement(column *tidbast.ColumnDef) bool {
+ for _, option := range column.Options {
+ if option.Tp == tidbast.ColumnOptionAutoIncrement {
+ return true
+ }
+ }
+ return false
+}
+
func (t *TableState) createColumn(ctx *FinderContext, column *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {
if _, exists := t.columnSet[column.Name.Name.O]; exists {
return &WalkThroughError{
diff --git a/plugin/advisor/catalog/walk_through_test.go b/plugin/advisor/catalog/walk_through_test.go
index e5590d6928..168eca4379 100644
--- a/plugin/advisor/catalog/walk_through_test.go
+++ b/plugin/advisor/catalog/walk_through_test.go
@@ -23,6 +23,20 @@ var (
func TestWalkThrough(t *testing.T) {
tests := []testData{
+ {
+ origin: &Database{
+ Name: "test",
+ DbType: db.MySQL,
+ },
+ statement: `
+ CREATE TABLE t(a int auto_increment, b int auto_increment);
+ `,
+ err: &WalkThroughError{
+ Type: ErrorTypeAutoIncrementExists,
+ Content: "There can be only one auto column for table `t`",
+ Line: 2,
+ },
+ },
{
origin: &Database{
Name: "test",