Skip to content

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

chore: set line mapping in snowflake require primary key rule (#6602)

h3n4l068a2d26b7dc
diff --git a/backend/plugin/advisor/snowflake/advisor_table_require_pk.go b/backend/plugin/advisor/snowflake/advisor_table_require_pk.go
index fe72c77863..74bb0e7898 100644
--- a/backend/plugin/advisor/snowflake/advisor_table_require_pk.go
+++ b/backend/plugin/advisor/snowflake/advisor_table_require_pk.go
@@ -42,6 +42,7 @@ func (*TableRequirePkAdvisor) Check(ctx advisor.Context, statement string) ([]ad
currentNormalizedTableName: "",
tableHasPrimaryKey: make(map[string]bool),
tableOriginalName: make(map[string]string),
+ tableLine: make(map[string]int),
}
antlr.ParseTreeWalkerDefault.Walk(listener, tree)
@@ -76,8 +77,11 @@ type tableRequirePkChecker struct {
// tableHasPrimaryKey is a map of normalized table name to whether the table has primary key.
tableHasPrimaryKey map[string]bool
// tableOriginalName is a map of normalized table name to original table name.
- // The key of the tableHasPrimaryKey and tableOriginalName is the same.
+ // The key of the tableOriginalName is the superset of the key of the tableHasPrimaryKey.
tableOriginalName map[string]string
+ // tableLine is a map of normalized table name to the line number of the table.
+ // The key of the tableLine is the superset of the key of the tableHasPrimaryKey.
+ tableLine map[string]int
}
// generateAdvice returns the advices generated by the listener, the advices must not be empty.
@@ -89,6 +93,7 @@ func (l *tableRequirePkChecker) generateAdvice() ([]advisor.Advice, error) {
Code: advisor.TableNoPK,
Title: l.title,
Content: fmt.Sprintf("Table %s requires PRIMARY KEY.", l.tableOriginalName[tableName]),
+ Line: l.tableLine[tableName],
})
}
}
@@ -112,6 +117,7 @@ func (l *tableRequirePkChecker) EnterCreate_table(ctx *parser.Create_tableContex
l.tableHasPrimaryKey[normalizedTableName] = false
l.tableOriginalName[normalizedTableName] = originalTableName
+ l.tableLine[normalizedTableName] = ctx.GetStart().GetLine()
l.currentNormalizedTableName = normalizedTableName
l.currentConstraintAction = currentConstraintActionAdd
}
@@ -139,6 +145,7 @@ func (l *tableRequirePkChecker) EnterOut_of_line_constraint(ctx *parser.Out_of_l
l.tableHasPrimaryKey[l.currentNormalizedTableName] = true
} else if l.currentConstraintAction == currentConstraintActionDrop {
l.tableHasPrimaryKey[l.currentNormalizedTableName] = false
+ l.tableLine[l.currentNormalizedTableName] = ctx.GetStart().GetLine()
}
}
@@ -150,6 +157,7 @@ func (l *tableRequirePkChecker) EnterConstraint_action(ctx *parser.Constraint_ac
if ctx.DROP() != nil && ctx.PRIMARY() != nil {
if _, ok := l.tableHasPrimaryKey[l.currentNormalizedTableName]; ok {
l.tableHasPrimaryKey[l.currentNormalizedTableName] = false
+ l.tableLine[l.currentNormalizedTableName] = ctx.GetStart().GetLine()
}
return
}
diff --git a/backend/plugin/advisor/snowflake/test/table_require_pk.yaml b/backend/plugin/advisor/snowflake/test/table_require_pk.yaml
index e99e832542..8e105631be 100644
--- a/backend/plugin/advisor/snowflake/test/table_require_pk.yaml
+++ b/backend/plugin/advisor/snowflake/test/table_require_pk.yaml
@@ -39,7 +39,7 @@
code: 601
title: table.require-pk
content: Table CUSTOMER requires PRIMARY KEY.
- line: 0
+ line: 1
details: ""
- statement: |-
CREATE TABLE CUSTOMER
@@ -54,7 +54,7 @@
code: 601
title: table.require-pk
content: Table CUSTOMER requires PRIMARY KEY.
- line: 0
+ line: 7
details: ""
- statement: |-
CREATE TABLE CUSTOMER
@@ -102,11 +102,11 @@
code: 601
title: table.require-pk
content: Table CUSTOMER requires PRIMARY KEY.
- line: 0
+ line: 24
details: ""
- status: WARN
code: 601
title: table.require-pk
content: Table ORDERING requires PRIMARY KEY.
- line: 0
+ line: 25
details: ""