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(advisor/pg): make TABLE_REQUIRE_PK ignore unrelated ALTER TABLE
rebelice231df0fe8f62
diff --git a/backend/plugin/advisor/pg/advisor_table_require_pk.go b/backend/plugin/advisor/pg/advisor_table_require_pk.goindex 8a77305393..f095a139b0 100644--- a/backend/plugin/advisor/pg/advisor_table_require_pk.go+++ b/backend/plugin/advisor/pg/advisor_table_require_pk.go@@ -22,6 +22,11 @@ func init() {}// TableRequirePKAdvisor is the advisor checking table requires PK.+//+// The rule only guards the net change of a batch: a table created in the batch+// must end with a PRIMARY KEY, and a table that had a PRIMARY KEY before the+// batch must still have one afterwards. ALTER TABLE on a pre-existing table+// without a PRIMARY KEY is out of scope so unrelated changes are not blocked.type TableRequirePKAdvisor struct {}@@ -37,8 +42,9 @@ func (*TableRequirePKAdvisor) Check(_ context.Context, checkCtx advisor.Context)Level: level,Title: checkCtx.Rule.Type.String(),},- finalMetadata: checkCtx.FinalMetadata,- tableMentions: make(map[string]*tableMention),+ originalMetadata: checkCtx.OriginalMetadata,+ finalMetadata: checkCtx.FinalMetadata,+ tableMentions: make(map[string]*tableMention),}// Manually iterate statements instead of using RunOmniRules because@@ -67,7 +73,8 @@ type tableMention struct {type tableRequirePKRule struct {OmniBaseRule- finalMetadata *model.DatabaseMetadata+ originalMetadata *model.DatabaseMetadata+ finalMetadata *model.DatabaseMetadata// Track last mention of each tabletableMentions map[string]*tableMention // key: "schema.table", value: last mention info@@ -106,7 +113,9 @@ func (r *tableRequirePKRule) handleCreateStmt(n *ast.CreateStmt) {}}-// handleAlterTableStmt records ALTER TABLE statements.+// handleAlterTableStmt records ALTER TABLE statements on tables that had a+// PRIMARY KEY before this batch. Tables created in this batch are already+// tracked by handleCreateStmt.func (r *tableRequirePKRule) handleAlterTableStmt(n *ast.AlterTableStmt) {tableName := omniTableName(n.Relation)if tableName == "" {@@ -115,6 +124,9 @@ func (r *tableRequirePKRule) handleAlterTableStmt(n *ast.AlterTableStmt) {schema := omniSchemaName(n.Relation)key := fmt.Sprintf("%s.%s", schema, tableName)+ if _, tracked := r.tableMentions[key]; !tracked && !r.originalTableHasPK(schema, tableName) {+ return+ }r.tableMentions[key] = &tableMention{startLine: int(r.ContentStartLine()) + r.BaseLine,text: r.TrimmedStmtText(),@@ -133,6 +145,15 @@ func (r *tableRequirePKRule) handleDropStmt(n *ast.DropStmt) {}}+// originalTableHasPK reports whether the table had a PRIMARY KEY before this batch.+func (r *tableRequirePKRule) originalTableHasPK(schemaName, tableName string) bool {+ if r.originalMetadata == nil {+ return false+ }+ table := r.originalMetadata.GetSchemaMetadata(schemaName).GetTable(tableName)+ return table != nil && table.GetPrimaryKey() != nil+}+// validateFinalState checks all mentioned tables against FinalMetadata for PRIMARY KEY.func (r *tableRequirePKRule) validateFinalState() {for tableKey, mention := range r.tableMentions {diff --git a/backend/plugin/advisor/pg/test/table_require_pk.yaml b/backend/plugin/advisor/pg/test/table_require_pk.yamlindex 11752a96d3..f3a23860f9 100644--- a/backend/plugin/advisor/pg/test/table_require_pk.yaml+++ b/backend/plugin/advisor/pg/test/table_require_pk.yaml@@ -74,3 +74,22 @@line: 3column: 0endposition: null+- statement: ALTER TABLE no_index_table ADD COLUMN note TEXT+ changeType: 1+- statement: ALTER TABLE no_index_table DROP COLUMN col_b+ changeType: 1+- statement: ALTER TABLE no_index_table ALTER COLUMN col_a TYPE bigint+ changeType: 1+- statement: |-+ CREATE TABLE audit_log(id INT, payload TEXT);+ ALTER TABLE audit_log ADD COLUMN note TEXT;+ changeType: 1+ want:+ - status: 2+ code: 601+ title: TABLE_REQUIRE_PK+ content: 'Table "public"."audit_log" requires PRIMARY KEY, related statement: "ALTER TABLE audit_log ADD COLUMN note TEXT"'+ startposition:+ line: 2+ column: 0+ endposition: null