Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: handle nil pointer in table_require_pk (#9313)
zll600a7a7579a42af
diff --git a/backend/plugin/advisor/mysqlwip/advisor_table_require_pk.go b/backend/plugin/advisor/mysqlwip/advisor_table_require_pk.goindex b1e673bafa..7604a56b37 100644--- a/backend/plugin/advisor/mysqlwip/advisor_table_require_pk.go+++ b/backend/plugin/advisor/mysqlwip/advisor_table_require_pk.go@@ -83,6 +83,9 @@ func (checker *tableRequirePKChecker) EnterCreateTable(ctx *mysql.CreateTableCon}func (checker *tableRequirePKChecker) createTable(tableName string, ctx *mysql.CreateTableContext) {+ if ctx.TableElementList() == nil {+ return+ }checker.initEmptyTable(tableName)for _, tableElement := range ctx.TableElementList().AllTableElement() {@@ -103,25 +106,27 @@ func (checker *tableRequirePKChecker) createTable(tableName string, ctx *mysql.Cfunc (checker *tableRequirePKChecker) handleFieldDefinition(tableName string, columnName string, ctx mysql.IFieldDefinitionContext) {for _, attr := range ctx.AllColumnAttribute() {- if attr.GetValue().GetTokenType() == mysql.MySQLLexerKEY_SYMBOL {- checker.tables[tableName] = newColumnSet([]string{columnName})+ if attr == nil || attr.PRIMARY_SYMBOL() == nil {+ continue}+ checker.tables[tableName] = newColumnSet([]string{columnName})}}func (checker *tableRequirePKChecker) handleTableConstraintDef(tableName string, ctx mysql.ITableConstraintDefContext) {if ctx.GetType_() != nil {- switch strings.ToUpper(ctx.GetType_().GetText()) {- case "PRIMARY":+ if ctx.GetType_().GetTokenType() == mysql.MySQLParserPRIMARY_SYMBOL {list := mysqlparser.NormalizeKeyListVariants(ctx.KeyListVariants())checker.tables[tableName] = newColumnSet(list)- default:}}}// EnterDropTable is called when production dropTable is entered.func (checker *tableRequirePKChecker) EnterDropTable(ctx *mysql.DropTableContext) {+ if ctx.TableRefList() == nil {+ return+ }for _, tableRef := range ctx.TableRefList().AllTableRef() {_, tableName := mysqlparser.NormalizeMySQLTableRef(tableRef)delete(checker.tables, tableName)@@ -130,6 +135,12 @@ func (checker *tableRequirePKChecker) EnterDropTable(ctx *mysql.DropTableContext// EnterAlterTable is called when production alterTable is entered.func (checker *tableRequirePKChecker) EnterAlterTable(ctx *mysql.AlterTableContext) {+ if ctx.TableRef() == nil {+ return+ }+ if ctx.AlterTableActions() == nil || ctx.AlterTableActions().AlterCommandList() == nil || ctx.AlterTableActions().AlterCommandList() == nil {+ return+ }_, tableName := mysqlparser.NormalizeMySQLTableRef(ctx.TableRef())lineNumber := checker.baseLine + ctx.GetStart().GetLine()@@ -142,8 +153,8 @@ func (checker *tableRequirePKChecker) EnterAlterTable(ctx *mysql.AlterTableContecase option.DROP_SYMBOL() != nil && option.PRIMARY_SYMBOL() != nil:checker.initEmptyTable(tableName)checker.line[tableName] = lineNumber- // DROP INDEX/KEY- case option.DROP_SYMBOL() != nil && option.KeyOrIndex() != nil:+ // DROP INDEX/KEY+ case option.DROP_SYMBOL() != nil && option.KeyOrIndex() != nil && option.IndexRef() != nil:_, _, indexName := mysqlparser.NormalizeIndexRef(option.IndexRef())if strings.ToUpper(indexName) == primaryKeyName {checker.initEmptyTable(tableName)@@ -166,7 +177,7 @@ func (checker *tableRequirePKChecker) EnterAlterTable(ctx *mysql.AlterTableContecolumnName := mysqlparser.NormalizeMySQLColumnInternalRef(option.ColumnInternalRef())checker.handleFieldDefinition(tableName, columnName, option.FieldDefinition())// DROP COLUMN- case option.DROP_SYMBOL() != nil:+ case option.DROP_SYMBOL() != nil && option.ColumnInternalRef() != nil:columnName := mysqlparser.NormalizeMySQLColumnInternalRef(option.ColumnInternalRef())if checker.dropColumn(tableName, columnName) {checker.line[tableName] = lineNumberdiff --git a/backend/plugin/advisor/mysqlwip/test/table_require_pk.yaml b/backend/plugin/advisor/mysqlwip/test/table_require_pk.yamlindex 5d387953c9..ad02277d03 100644--- a/backend/plugin/advisor/mysqlwip/test/table_require_pk.yaml+++ b/backend/plugin/advisor/mysqlwip/test/table_require_pk.yaml@@ -1,3 +1,42 @@+- statement: |+ CREATE TABLE userTable(+ id INT NOT NULL,+ name VARCHAR(255) CHARSET ascii,+ roomId INT,+ time_created TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',+ time_updated TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',+ content BLOB NOT NULL COMMENT 'comment',+ json_content JSON NOT NULL COMMENT 'comment',+ INDEX idx1(name),+ UNIQUE KEY uk1(id, name),+ FOREIGN KEY fk1(roomId) REFERENCES room(id),+ INDEX idx_userTable_content(content)) ENGINE = CSV COLLATE latin1_bin;+ want:+ - status: WARN+ code: 601+ title: table.require-pk+ content: Table `userTable` requires PRIMARY KEY+ line: 1+ details: ""+- statement: |+ CREATE TABLE user(+ id INT PRIMARY KEY COMMENT 'comment',+ name VARCHAR(255) NOT NULL DEFAULT '' COMMENT 'comment',+ room_id INT NOT NULL DEFAULT 0 COMMENT 'comment',+ creator_id INT NOT NULL DEFAULT 0 COMMENT 'comment',+ created_ts TIMESTAMP NOT NULL DEFAULT NOW() COMMENT 'comment',+ updater_id INT NOT NULL DEFAULT 0 COMMENT 'comment',+ updated_ts TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW() COMMENT 'comment',+ INDEX idx_user_name(name),+ UNIQUE KEY uk_user_id_name(id, name)+ ) ENGINE = INNODB COMMENT 'comment';+ want:+ - status: SUCCESS+ code: 0+ title: OK+ content: ""+ line: 0+ details: ""- statement: |CREATE TABLE t1(id INT PRIMARY KEY);CREATE TABLE t2(id INT);