Skip to content

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

chore: support table constraint in create-table statement (#9429)

zll60077f026b0fb74
diff --git a/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml b/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml
index dfd10d9c5f..1a4e6825b3 100644
--- a/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml
+++ b/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml
@@ -1,3 +1,97 @@
+- statement: |-
+ CREATE TABLE t1(
+ a INT PRIMARY KEY,
+ b INT NOT NULL DEFAULT 1,
+ c INT,
+ d VARCHAR(255) NOT NULL,
+ e VARCHAR(255),
+ UNIQUE KEY uk1 (b, c),
+ KEY idx1 (c),
+ FULLTEXT(d, e) WITH PARSER ngram INVISIBLE
+ );
+ ignore_case_sensitive: false
+ want: |-
+ {
+ "name": "test",
+ "schemas": [
+ {
+ "tables": [
+ {
+ "name": "t1",
+ "columns": [
+ {
+ "name": "a",
+ "position": 1,
+ "type": "int"
+ },
+ {
+ "name": "b",
+ "position": 2,
+ "default": "1",
+ "type": "int"
+ },
+ {
+ "name": "c",
+ "position": 3,
+ "nullable": true,
+ "type": "int"
+ },
+ {
+ "name": "d",
+ "position": 4,
+ "type": "varchar"
+ },
+ {
+ "name": "e",
+ "position": 5,
+ "nullable": true,
+ "type": "varchar"
+ }
+ ],
+ "indexes": [
+ {
+ "name": "PRIMARY",
+ "expressions": [
+ "a"
+ ],
+ "type": "BTREE",
+ "unique": true,
+ "primary": true,
+ "visible": true
+ },
+ {
+ "name": "d",
+ "expressions": [
+ "d",
+ "e"
+ ],
+ "type": "FULLTEXT"
+ },
+ {
+ "name": "idx1",
+ "expressions": [
+ "c"
+ ],
+ "type": "BTREE",
+ "visible": true
+ },
+ {
+ "name": "uk1",
+ "expressions": [
+ "b",
+ "c"
+ ],
+ "type": "BTREE",
+ "unique": true,
+ "visible": true
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ err: null
- statement: |-
CREATE TABLE t1(
a INT NOT NULL DEFAULT NULL
@@ -43,70 +137,70 @@
ignore_case_sensitive: false
want: |-
{
- "name": "test",
- "schemas": [
+ "name": "test",
+ "schemas": [
{
- "tables": [
+ "tables": [
{
- "name": "t1",
- "columns": [
+ "name": "t1",
+ "columns": [
{
- "name": "a",
- "position": 1,
- "default": "1",
- "type": "int"
+ "name": "a",
+ "position": 1,
+ "default": "1",
+ "type": "int"
},
{
- "name": "b",
- "position": 2,
- "nullable": true,
- "type": "int"
+ "name": "b",
+ "position": 2,
+ "nullable": true,
+ "type": "int"
},
{
- "name": "c",
- "position": 3,
- "default": "NULL",
- "type": "varchar",
- "comment": "column c"
+ "name": "c",
+ "position": 3,
+ "default": "NULL",
+ "type": "varchar",
+ "comment": "column c"
},
{
- "name": "e",
- "position": 4,
- "nullable": true,
- "type": "int"
+ "name": "e",
+ "position": 4,
+ "nullable": true,
+ "type": "int"
},
{
- "name": "f",
- "position": 5,
- "type": "varchar",
- "collation": "utf8mb4_polish_ci"
+ "name": "f",
+ "position": 5,
+ "type": "varchar",
+ "collation": "utf8mb4_polish_ci"
},
{
- "name": "g",
- "position": 6,
- "type": "varchar",
- "characterSet": "utf8mb4"
+ "name": "g",
+ "position": 6,
+ "type": "varchar",
+ "characterSet": "utf8mb4"
}
],
- "indexes": [
+ "indexes": [
{
- "name": "PRIMARY",
- "expressions": [
+ "name": "PRIMARY",
+ "expressions": [
"a"
],
- "type": "BTREE",
- "unique": true,
- "primary": true,
- "visible": true
+ "type": "BTREE",
+ "unique": true,
+ "primary": true,
+ "visible": true
},
{
- "name": "b",
- "expressions": [
+ "name": "b",
+ "expressions": [
"b"
],
- "type": "BTREE",
- "unique": true,
- "visible": true
+ "type": "BTREE",
+ "unique": true,
+ "visible": true
}
]
}
diff --git a/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go b/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go
index fdc69983a8..1261e5b382 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go
@@ -2,6 +2,7 @@ package catalog
import (
"fmt"
+ "strings"
"github.com/antlr4-go/antlr/v4"
mysql "github.com/bytebase/mysql-parser"
@@ -121,9 +122,154 @@ func (l *mysqlV2Listener) EnterCreateTable(ctx *mysql.CreateTableContext) {
l.err = err
return
}
- default:
+ case tableElement.TableConstraintDef() != nil:
+ if err := table.mysqlV2CreateConstraint(l.databaseState.ctx, tableElement.TableConstraintDef()); err != nil {
+ err.Line = tableElement.GetStart().GetLine()
+ l.err = err
+ return
+ }
+ }
+ }
+}
+
+func (t *TableState) mysqlV2CreateConstraint(ctx *FinderContext, constraintDef mysql.ITableConstraintDefContext) *WalkThroughError {
+ if constraintDef.GetType_() != nil {
+ switch constraintDef.GetType_().GetTokenType() {
+ // PRIMARY KEY.
+ case mysql.MySQLParserPRIMARY_SYMBOL:
+ if constraintDef.KeyListVariants() == nil {
+ // never reach here.
+ return nil
+ }
+ keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())
+ if err := t.mysqlV2ValidateKeyStringList(ctx, keyList, true /* primary */, false /* isSpatial*/); err != nil {
+ return err
+ }
+ if err := t.mysqlV2CreatePrimaryKey(keyList, mysqlV2GetIndexType(constraintDef)); err != nil {
+ return err
+ }
+ // normal KEY/INDEX.
+ case mysql.MySQLParserKEY_SYMBOL, mysql.MySQLParserINDEX_SYMBOL:
+ if constraintDef.KeyListVariants() == nil {
+ // never reach here.
+ return nil
+ }
+ keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())
+ if err := t.mysqlV2ValidateKeyStringList(ctx, keyList, false /* primary */, false /* isSpatial */); err != nil {
+ return err
+ }
+
+ indexName := ""
+ if constraintDef.IndexNameAndType() != nil && constraintDef.IndexNameAndType().IndexName() != nil {
+ indexName = mysqlparser.NormalizeIndexName(constraintDef.IndexNameAndType().IndexName())
+ }
+ if err := t.mysqlV2CreateIndex(indexName, keyList, false /* unique */, mysqlV2GetIndexType(constraintDef), constraintDef); err != nil {
+ return err
+ }
+ // UNIQUE KEY.
+ case mysql.MySQLParserUNIQUE_SYMBOL:
+ if constraintDef.KeyListVariants() == nil {
+ // never reach here.
+ return nil
+ }
+ keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())
+ if err := t.mysqlV2ValidateKeyStringList(ctx, keyList, false /* primary */, false /* isSpatial*/); err != nil {
+ return err
+ }
+
+ indexName := ""
+ if constraintDef.ConstraintName() != nil {
+ indexName = mysqlparser.NormalizeConstraintName(constraintDef.ConstraintName())
+ }
+ if constraintDef.IndexNameAndType() != nil && constraintDef.IndexNameAndType().IndexName() != nil {
+ indexName = mysqlparser.NormalizeIndexName(constraintDef.IndexNameAndType().IndexName())
+ }
+ if err := t.mysqlV2CreateIndex(indexName, keyList, true /* unique */, mysqlV2GetIndexType(constraintDef), constraintDef); err != nil {
+ return err
+ }
+ // FULLTEXT KEY.
+ case mysql.MySQLParserFULLTEXT_SYMBOL:
+ if constraintDef.KeyListVariants() == nil {
+ // never reach here.
+ return nil
+ }
+ keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())
+ if err := t.mysqlV2ValidateKeyStringList(ctx, keyList, false /* primary */, false /* isSpatial*/); err != nil {
+ return err
+ }
+ indexName := ""
+ if constraintDef.IndexName() != nil {
+ indexName = mysqlparser.NormalizeIndexName(constraintDef.IndexName())
+ }
+ if err := t.mysqlV2CreateIndex(indexName, keyList, false /* unique */, mysqlV2GetIndexType(constraintDef), constraintDef); err != nil {
+ return err
+ }
+ case mysql.MySQLParserFOREIGN_SYMBOL:
+ // we do not deal with FOREIGN KEY constraints.
+ }
+ }
+
+ // we do not deal with check constraints.
+ // if constraintDef.CheckConstraint() != nil {}
+ return nil
+}
+
+func (t *TableState) mysqlV2ValidateKeyStringList(ctx *FinderContext, keyList []string, primary bool, isSpatial bool) *WalkThroughError {
+ for _, columnName := range keyList {
+ column, exists := t.columnSet[columnName]
+ if !exists {
+ if ctx.CheckIntegrity {
+ return NewColumnNotExistsError(t.name, columnName)
+ }
+ } else {
+ if primary {
+ column.nullable = newFalsePointer()
+ }
+ if isSpatial && column.nullable != nil && *column.nullable {
+ return &WalkThroughError{
+ Type: ErrorTypeSpatialIndexKeyNullable,
+ // The error content comes from MySQL.
+ Content: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.name),
+ }
+ }
}
}
+ return nil
+}
+
+func mysqlV2GetIndexType(tableConstraint mysql.ITableConstraintDefContext) string {
+ if tableConstraint.GetType_() == nil {
+ return "BTREE"
+ }
+
+ // I still need to handle IndexNameAndType to get index type(algorithm).
+ switch tableConstraint.GetType_().GetTokenType() {
+ case mysql.MySQLParserPRIMARY_SYMBOL,
+ mysql.MySQLParserKEY_SYMBOL,
+ mysql.MySQLParserINDEX_SYMBOL,
+ mysql.MySQLParserUNIQUE_SYMBOL:
+
+ if tableConstraint.IndexNameAndType() != nil {
+ if tableConstraint.IndexNameAndType().IndexType() != nil {
+ indexType := tableConstraint.IndexNameAndType().IndexType().GetText()
+ return strings.ToUpper(indexType)
+ }
+ }
+
+ for _, option := range tableConstraint.AllIndexOption() {
+ if option == nil || option.IndexTypeClause() == nil {
+ continue
+ }
+
+ indexType := option.IndexTypeClause().IndexType().GetText()
+ return strings.ToUpper(indexType)
+ }
+ case mysql.MySQLParserFULLTEXT_SYMBOL:
+ return "FULLTEXT"
+ case mysql.MySQLParserFOREIGN_SYMBOL:
+ }
+ // for mysql, we use BTREE as default index type.
+ return "BTREE"
}
func (t *TableState) mysqlV2CreateColumn(_ *FinderContext, columnDef mysql.IColumnDefinitionContext) *WalkThroughError {
@@ -289,6 +435,10 @@ func (t *TableState) mysqlV2CreateIndex(name string, keyList []string, unique bo
// need to check the visibility of index.
// we need a for-loop to determined the visibility of index.
+
+ // NORMAL KEY/INDEX.
+ // PRIMARY KEY.
+ // UNIQUE KEY.
for _, attribute := range tableConstraint.AllIndexOption() {
if attribute == nil || attribute.CommonIndexOption() == nil {
continue
@@ -298,6 +448,26 @@ func (t *TableState) mysqlV2CreateIndex(name string, keyList []string, unique bo
}
}
+ // FULLTEXT INDEX.
+ for _, attribute := range tableConstraint.AllFulltextIndexOption() {
+ if attribute == nil || attribute.CommonIndexOption() == nil {
+ continue
+ }
+ if attribute.CommonIndexOption().Visibility() != nil && attribute.CommonIndexOption().Visibility().INVISIBLE_SYMBOL() != nil {
+ index.visible = newFalsePointer()
+ }
+ }
+
+ // SPATIAL INDEX.
+ for _, attribute := range tableConstraint.AllSpatialIndexOption() {
+ if attribute == nil || attribute.CommonIndexOption() == nil {
+ continue
+ }
+ if attribute.CommonIndexOption().Visibility() != nil && attribute.CommonIndexOption().Visibility().INVISIBLE_SYMBOL() != nil {
+ index.visible = newFalsePointer()
+ }
+ }
+
t.indexSet[name] = index
return nil
}