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 primary key check logic in create table like for tidb (#16193)
h3n4lf59e8f203354
diff --git a/backend/plugin/advisor/tidb/advisor_table_require_pk.go b/backend/plugin/advisor/tidb/advisor_table_require_pk.goindex c45b86bfd1..c8941a664b 100644--- a/backend/plugin/advisor/tidb/advisor_table_require_pk.go+++ b/backend/plugin/advisor/tidb/advisor_table_require_pk.go@@ -71,7 +71,11 @@ func (v *tableRequirePKChecker) Enter(in ast.Node) (ast.Node, bool) {switch node := in.(type) {// CREATE TABLEcase *ast.CreateTableStmt:- v.createTable(node)+ if node.ReferTable != nil {+ v.createTableLike(node)+ } else {+ v.createTable(node)+ }v.line[node.Table.Name.O] = node.OriginTextPosition()// DROP TABLEcase *ast.DropTableStmt:@@ -161,6 +165,33 @@ func (v *tableRequirePKChecker) createTable(node *ast.CreateTableStmt) {}}+func (v *tableRequirePKChecker) createTableLike(node *ast.CreateTableStmt) {+ table := node.Table.Name.String()+ v.initEmptyTable(table)+ referTableName := node.ReferTable.Name.String()+ // Switch to check refer table's primary key.+ // Find refer table in context first, and then find it in catalog.+ if referTablePk, ok := v.tables[referTableName]; ok {+ var columns []string+ for column := range referTablePk {+ columns = append(columns, column)+ }+ v.tables[table] = newColumnSet(columns)+ } else {+ referTableState := v.catalog.Origin.FindTable(&catalog.TableFind{+ TableName: referTableName,+ })+ if referTableState != nil {+ indexSet := referTableState.Index(&catalog.TableIndexFind{})+ for _, index := range *indexSet {+ if index.Primary() {+ v.tables[table] = newColumnSet(index.ExpressionList())+ }+ }+ }+ }+}+func (v *tableRequirePKChecker) dropColumn(table string, column string) bool {if _, ok := v.tables[table]; !ok {_, pk := v.catalog.Origin.FindIndex(&catalog.IndexFind{diff --git a/backend/plugin/advisor/tidb/test/table_require_pk.yaml b/backend/plugin/advisor/tidb/test/table_require_pk.yamlindex 32dbee86f1..41fa74936e 100644--- a/backend/plugin/advisor/tidb/test/table_require_pk.yaml+++ b/backend/plugin/advisor/tidb/test/table_require_pk.yaml@@ -1,3 +1,26 @@+- statement: |-+ CREATE TABLE has_pk(id INT PRIMARY KEY);+ CREATE TABLE no_pk(id INT);+ CREATE TABLE has_pk_fork LIKE has_pk;+ CREATE TABLE no_pk_fork LIKE no_pk;+ changeType: 1+ want:+ - status: 2+ code: 601+ title: table.require-pk+ content: Table `no_pk` requires PRIMARY KEY+ startposition:+ line: 1+ column: 0+ endposition: null+ - status: 2+ code: 601+ title: table.require-pk+ content: Table `no_pk_fork` requires PRIMARY KEY+ startposition:+ line: 3+ column: 0+ endposition: null- statement: CREATE TABLE t(id INT PRIMARY KEY)changeType: 1- statement: CREATE TABLE t(id INT, PRIMARY KEY (id))