Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: improve create table (#9432)
zll600faae3c0f409a
diff --git a/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml b/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yamlindex 1a4e6825b3..f5538ce776 100644--- a/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml+++ b/backend/plugin/advisor/catalog/test/mysqlv2_walk_through.yaml@@ -1,3 +1,72 @@+- statement: |-+ CREATE TABLE t1(+ a INT AUTO_INCREMENT,+ b INT AUTO_INCREMENT+ );+ ignore_case_sensitive: false+ want: ""+ err:+ type: 404+ content: There can be only one auto column for table `t1`+ line: 0+ payload: null+- statement: CREATE TABLE tvx SELECT (x,z) FROM (VALUES ROW(1,3,5), ROW(2,4,6)) AS v(x,y,z);+ ignore_case_sensitive: false+ want: ""+ err:+ type: 303+ content: Disallow the CREATE TABLE AS statement but "CREATE TABLE tvx SELECT (x,z) FROM (VALUES ROW(1,3,5), ROW(2,4,6)) AS v(x,y,z);" uses+ line: 0+ payload: null+- statement: |-+ CREATE TABLE t1(a int);+ CREATE TABLE t2 (b INT) SELECT a FROM t1;+ ignore_case_sensitive: false+ want: ""+ err:+ type: 303+ content: Disallow the CREATE TABLE AS statement but "CREATE TABLE t2 (b INT) SELECT a FROM t1;" uses+ line: 0+ payload: null+- statement: |-+ CREATE TABLE t1(+ a int+ );+ CREATE TABLE t2 LIKE t1;+ ignore_case_sensitive: false+ want: |-+ {+ "name": "test",+ "schemas": [+ {+ "tables": [+ {+ "name": "t1",+ "columns": [+ {+ "name": "a",+ "position": 1,+ "nullable": true,+ "type": "int"+ }+ ]+ },+ {+ "name": "t2",+ "columns": [+ {+ "name": "a",+ "position": 1,+ "nullable": true,+ "type": "int"+ }+ ]+ }+ ]+ }+ ]+ }+ err: null- statement: |-CREATE TABLE t1(a INT PRIMARY KEY,@@ -12,78 +81,78 @@ignore_case_sensitive: falsewant: |-{- "name": "test",- "schemas": [+ "name": "test",+ "schemas": [{- "tables": [+ "tables": [{- "name": "t1",- "columns": [+ "name": "t1",+ "columns": [{- "name": "a",- "position": 1,- "type": "int"+ "name": "a",+ "position": 1,+ "type": "int"},{- "name": "b",- "position": 2,- "default": "1",- "type": "int"+ "name": "b",+ "position": 2,+ "default": "1",+ "type": "int"},{- "name": "c",- "position": 3,- "nullable": true,- "type": "int"+ "name": "c",+ "position": 3,+ "nullable": true,+ "type": "int"},{- "name": "d",- "position": 4,- "type": "varchar"+ "name": "d",+ "position": 4,+ "type": "varchar"},{- "name": "e",- "position": 5,- "nullable": true,- "type": "varchar"+ "name": "e",+ "position": 5,+ "nullable": true,+ "type": "varchar"}],- "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": "d",- "expressions": [+ "name": "d",+ "expressions": ["d","e"],- "type": "FULLTEXT"+ "type": "FULLTEXT"},{- "name": "idx1",- "expressions": [+ "name": "idx1",+ "expressions": ["c"],- "type": "BTREE",- "visible": true+ "type": "BTREE",+ "visible": true},{- "name": "uk1",- "expressions": [+ "name": "uk1",+ "expressions": ["b","c"],- "type": "BTREE",- "unique": true,- "visible": true+ "type": "BTREE",+ "unique": true,+ "visible": true}]}@@ -137,70 +206,70 @@ignore_case_sensitive: falsewant: |-{- "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.goindex 1261e5b382..76b2d6d75f 100644--- a/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go+++ b/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go@@ -35,10 +35,15 @@ type mysqlV2Listener struct {*mysql.BaseMySQLParserListenerbaseLine int+ text stringdatabaseState *DatabaseStateerr *WalkThroughError}+func (l *mysqlV2Listener) EnterQuery(ctx *mysql.QueryContext) {+ l.text = ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)+}+func (d *DatabaseState) mysqlV2ChangeState(in *mysqlparser.ParseResult) (err *WalkThroughError) {defer func() {if err == nil {@@ -96,6 +101,20 @@ func (l *mysqlV2Listener) EnterCreateTable(ctx *mysql.CreateTableContext) {return}+ if ctx.DuplicateAsQueryExpression() != nil {+ l.err = &WalkThroughError{+ Type: ErrorTypeUseCreateTableAs,+ Content: fmt.Sprintf("Disallow the CREATE TABLE AS statement but \"%s\" uses", l.text),+ }+ return+ }++ if ctx.LIKE_SYMBOL() != nil {+ _, referTable := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())+ l.err = l.databaseState.mysqlV2CopyTable(databaseName, tableName, referTable)+ return+ }+table := &TableState{name: tableName,engine: newEmptyStringPointer(),@@ -110,6 +129,7 @@ func (l *mysqlV2Listener) EnterCreateTable(ctx *mysql.CreateTableContext) {return}+ hasAutoIncrement := falsefor _, tableElement := range ctx.TableElementList().AllTableElement() {switch {// handle column@@ -117,6 +137,16 @@ func (l *mysqlV2Listener) EnterCreateTable(ctx *mysql.CreateTableContext) {if tableElement.ColumnDefinition().FieldDefinition() == nil {continue}+ if mysqlparser.IsAutoIncrement(tableElement.ColumnDefinition().FieldDefinition()) {+ if hasAutoIncrement {+ l.err = &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.mysqlV2CreateColumn(l.databaseState.ctx, tableElement.ColumnDefinition()); err != nil {err.Line = l.baseLine + tableElement.GetStart().GetLine()l.err = err@@ -132,6 +162,44 @@ func (l *mysqlV2Listener) EnterCreateTable(ctx *mysql.CreateTableContext) {}}+func (d *DatabaseState) mysqlV2CopyTable(databaseName, tableName, referTable string) *WalkThroughError {+ targetTable, err := d.mysqlV2FindTableState(databaseName, referTable, true /* createIncompleteTable */)+ if err != nil {+ return err+ }++ schema := d.schemaSet[""]+ table := targetTable.copy()+ table.name = tableName+ schema.tableSet[table.name] = table+ return nil+}++func (d *DatabaseState) mysqlV2FindTableState(databaseName, tableName string, createIncompleteTable bool) (*TableState, *WalkThroughError) {+ if databaseName != "" && !d.isCurrentDatabase(databaseName) {+ return nil, NewAccessOtherDatabaseError(d.name, databaseName)+ }++ schema, exists := d.schemaSet[""]+ if !exists {+ schema = d.createSchema("")+ }++ table, exists := schema.getTable(tableName)+ if !exists {+ if schema.ctx.CheckIntegrity {+ return nil, NewTableNotExistsError(tableName)+ }+ if createIncompleteTable {+ table = schema.createIncompleteTable(tableName)+ } else {+ return nil, nil+ }+ }++ return table, nil+}+func (t *TableState) mysqlV2CreateConstraint(ctx *FinderContext, constraintDef mysql.ITableConstraintDefContext) *WalkThroughError {if constraintDef.GetType_() != nil {switch constraintDef.GetType_().GetTokenType() {diff --git a/backend/plugin/parser/mysql/normalize.go b/backend/plugin/parser/mysql/normalize.goindex 5d1f763156..b26de2d31b 100644--- a/backend/plugin/parser/mysql/normalize.go+++ b/backend/plugin/parser/mysql/normalize.go@@ -342,3 +342,13 @@ func IsTimeType(ctx parser.IDataTypeContext) bool {return false}}++// IsAutoIncrement check if this column is auto_increment.+func IsAutoIncrement(ctx parser.IFieldDefinitionContext) bool {+ for _, attr := range ctx.AllColumnAttribute() {+ if attr.AUTO_INCREMENT_SYMBOL() != nil {+ return true+ }+ }+ return false+}