Skip to content

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

feat: located line for PG SQL review (#2320)

rebelicefc4b8eedd449
diff --git a/plugin/advisor/pg/advisor_column_required.go b/plugin/advisor/pg/advisor_column_required.go
index a63ecebcf0..70542f52a5 100644
--- a/plugin/advisor/pg/advisor_column_required.go
+++ b/plugin/advisor/pg/advisor_column_required.go
@@ -111,6 +111,7 @@ func (checker *columnRequirementChecker) Visit(node ast.Node) ast.Visitor {
Code: advisor.NoRequiredColumn,
Title: checker.title,
Content: fmt.Sprintf("Table %q requires columns: %s", table.Name, strings.Join(missingColumns, ", ")),
+ Line: node.Line(),
})
}
diff --git a/plugin/advisor/pg/advisor_column_required_test.go b/plugin/advisor/pg/advisor_column_required_test.go
index 7ae060ace2..19161d005f 100644
--- a/plugin/advisor/pg/advisor_column_required_test.go
+++ b/plugin/advisor/pg/advisor_column_required_test.go
@@ -18,6 +18,7 @@ func TestColumnRequirement(t *testing.T) {
Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table \"book\" requires columns: created_ts, creator_id, updated_ts, updater_id",
+ Line: 1,
},
},
},
@@ -45,6 +46,7 @@ func TestColumnRequirement(t *testing.T) {
Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table \"book\" requires columns: creator_id",
+ Line: 1,
},
},
},
@@ -56,6 +58,7 @@ func TestColumnRequirement(t *testing.T) {
Code: advisor.NoRequiredColumn,
Title: "column.required",
Content: "Table \"book\" requires columns: creator_id",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/pg/advisor_migration_compatibility.go b/plugin/advisor/pg/advisor_migration_compatibility.go
index f667fd372d..4d992be8de 100644
--- a/plugin/advisor/pg/advisor_migration_compatibility.go
+++ b/plugin/advisor/pg/advisor_migration_compatibility.go
@@ -112,6 +112,7 @@ func (checker *compatibilityChecker) Visit(node ast.Node) ast.Visitor {
Code: code,
Title: checker.title,
Content: fmt.Sprintf("\"%s\" may cause incompatibility with the existing data and code", checker.text),
+ Line: node.Line(),
})
}
return checker
diff --git a/plugin/advisor/pg/advisor_migration_compatibility_test.go b/plugin/advisor/pg/advisor_migration_compatibility_test.go
index 08f0f8df1a..17c8933179 100644
--- a/plugin/advisor/pg/advisor_migration_compatibility_test.go
+++ b/plugin/advisor/pg/advisor_migration_compatibility_test.go
@@ -16,6 +16,7 @@ func TestBasic(t *testing.T) {
Code: advisor.CompatibilityDropDatabase,
Title: "schema.backward-compatibility",
Content: "\"DROP DATABASE d1\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -27,6 +28,7 @@ func TestBasic(t *testing.T) {
Code: advisor.CompatibilityDropTable,
Title: "schema.backward-compatibility",
Content: "\"DROP TABLE t1\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -38,6 +40,7 @@ func TestBasic(t *testing.T) {
Code: advisor.CompatibilityRenameTable,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 RENAME to t2\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -49,6 +52,7 @@ func TestBasic(t *testing.T) {
Code: advisor.CompatibilityDropTable,
Title: "schema.backward-compatibility",
Content: "\"DROP VIEW v1\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -60,6 +64,7 @@ func TestBasic(t *testing.T) {
Code: advisor.CompatibilityAddUniqueKey,
Title: "schema.backward-compatibility",
Content: "\"CREATE UNIQUE INDEX idx1 ON t1 (f1)\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -71,12 +76,14 @@ func TestBasic(t *testing.T) {
Code: advisor.CompatibilityDropTable,
Title: "schema.backward-compatibility",
Content: "\"DROP TABLE t1;\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
{
Status: advisor.Warn,
Code: advisor.CompatibilityDropTable,
Title: "schema.backward-compatibility",
Content: "\"DROP TABLE t2;\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -110,6 +117,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityRenameColumn,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 RENAME COLUMN f1 to f2\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -121,6 +129,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityDropColumn,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 DROP COLUMN f1\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -132,6 +141,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityAddPrimaryKey,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ADD PRIMARY KEY (f1)\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -143,6 +153,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityAddUniqueKey,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ADD UNIQUE (f1)\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -154,6 +165,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityAddForeignKey,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ADD FOREIGN KEY (f1) REFERENCES t2(f2)\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -165,6 +177,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityAddCheck,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ADD CHECK (f1 > 0)\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -186,6 +199,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityAddCheck,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ADD CONSTRAINT check_f1_bigger_0 CHECK (f1 > 0)\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -208,6 +222,7 @@ func TestAlterTable(t *testing.T) {
Code: advisor.CompatibilityRenameTable,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 RENAME TO t2\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
@@ -230,6 +245,7 @@ func TestAlterTableChangeColumnType(t *testing.T) {
Code: advisor.CompatibilityAlterColumn,
Title: "schema.backward-compatibility",
Content: "\"ALTER TABLE t1 ALTER COLUMN f1 TYPE TEXT\" may cause incompatibility with the existing data and code",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/pg/advisor_naming_column.go b/plugin/advisor/pg/advisor_naming_column.go
index 1c53b24504..2bcbc17991 100644
--- a/plugin/advisor/pg/advisor_naming_column.go
+++ b/plugin/advisor/pg/advisor_naming_column.go
@@ -70,7 +70,11 @@ type namingColumnConventionChecker struct {
// Visit implements the ast.Visitor interface.
func (checker *namingColumnConventionChecker) Visit(node ast.Node) ast.Visitor {
- var columnList []string
+ type columnData struct {
+ name string
+ line int
+ }
+ var columnList []columnData
var tableName string
switch n := node.(type) {
@@ -78,36 +82,47 @@ func (checker *namingColumnConventionChecker) Visit(node ast.Node) ast.Visitor {
case *ast.CreateTableStmt:
tableName = n.Name.Name
for _, col := range n.ColumnList {
- columnList = append(columnList, col.ColumnName)
+ columnList = append(columnList, columnData{
+ name: col.ColumnName,
+ line: col.Line(),
+ })
}
// ALTER TABLE ADD COLUMN
case *ast.AddColumnListStmt:
tableName = n.Table.Name
for _, col := range n.ColumnList {
- columnList = append(columnList, col.ColumnName)
+ columnList = append(columnList, columnData{
+ name: col.ColumnName,
+ line: n.Line(),
+ })
}
// ALTER TABLE RENAME COLUMN
case *ast.RenameColumnStmt:
tableName = n.Table.Name
- columnList = append(columnList, n.NewName)
+ columnList = append(columnList, columnData{
+ name: n.NewName,
+ line: n.Line(),
+ })
}
for _, column := range columnList {
- if !checker.format.MatchString(column) {
+ if !checker.format.MatchString(column.name) {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: checker.level,
Code: advisor.NamingColumnConventionMismatch,
Title: checker.title,
- Content: fmt.Sprintf("\"%s\".\"%s\" mismatches column naming convention, naming format should be %q", tableName, column, checker.format),
+ Content: fmt.Sprintf("\"%s\".\"%s\" mismatches column naming convention, naming format should be %q", tableName, column.name, checker.format),
+ Line: column.line,
})
}
- if checker.maxLength > 0 && len(column) > checker.maxLength {
+ if checker.maxLength > 0 && len(column.name) > checker.maxLength {
checker.adviceList = append(checker.adviceList, advisor.Advice{
Status: checker.level,
Code: advisor.NamingColumnConventionMismatch,
Title: checker.title,
- Content: fmt.Sprintf("\"%s\".\"%s\" mismatches column naming convention, its length should be within %d characters", tableName, column, checker.maxLength),
+ Content: fmt.Sprintf("\"%s\".\"%s\" mismatches column naming convention, its length should be within %d characters", tableName, column.name, checker.maxLength),
+ Line: column.line,
})
}
}
diff --git a/plugin/advisor/pg/advisor_naming_column_test.go b/plugin/advisor/pg/advisor_naming_column_test.go
index 28794cb509..1ce944bba2 100644
--- a/plugin/advisor/pg/advisor_naming_column_test.go
+++ b/plugin/advisor/pg/advisor_naming_column_test.go
@@ -21,6 +21,7 @@ func TestNamingColumnConvention(t *testing.T) {
Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "\"book\".\"creatorId\" mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 1,
},
},
},
@@ -32,6 +33,7 @@ func TestNamingColumnConvention(t *testing.T) {
Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: fmt.Sprintf("\"book\".\"%s\" mismatches column naming convention, its length should be within 32 characters", invalidColumnName),
+ Line: 1,
},
},
},
@@ -67,6 +69,7 @@ func TestNamingColumnConvention(t *testing.T) {
Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "\"book\".\"creatorId\" mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 2,
},
},
},
@@ -91,6 +94,7 @@ func TestNamingColumnConvention(t *testing.T) {
Code: advisor.NamingColumnConventionMismatch,
Title: "naming.column",
Content: "\"book\".\"creatorId\" mismatches column naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 2,
},
},
},
diff --git a/plugin/advisor/pg/advisor_naming_foreign_key_convention.go b/plugin/advisor/pg/advisor_naming_foreign_key_convention.go
index 12869095df..2cd75c81bb 100644
--- a/plugin/advisor/pg/advisor_naming_foreign_key_convention.go
+++ b/plugin/advisor/pg/advisor_naming_foreign_key_convention.go
@@ -76,6 +76,7 @@ type namingFKConventionChecker struct {
type indexMetaData struct {
indexName string
tableName string
+ line int
metaData map[string]string
}
@@ -100,6 +101,7 @@ func (checker *namingFKConventionChecker) Visit(in ast.Node) ast.Visitor {
Code: advisor.NamingFKConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf(`Foreign key in table "%s" mismatches the naming convention, expect %q but found "%s"`, indexData.tableName, regex, indexData.indexName),
+ Line: indexData.line,
})
}
if checker.maxLength > 0 && len(indexData.indexName) > checker.maxLength {
@@ -108,6 +110,7 @@ func (checker *namingFKConventionChecker) Visit(in ast.Node) ast.Visitor {
Code: advisor.NamingFKConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf(`Foreign key "%s" in table "%s" mismatches the naming convention, its length should be within %d characters`, indexData.indexName, indexData.tableName, checker.maxLength),
+ Line: indexData.line,
})
}
}
@@ -121,26 +124,26 @@ func (*namingFKConventionChecker) getMetaDataList(in ast.Node) []*indexMetaData
switch node := in.(type) {
case *ast.CreateTableStmt:
for _, constraint := range node.ConstraintList {
- if metadata := getForeignKeyMetadata(constraint, node.Name.Name); metadata != nil {
+ if metadata := getForeignKeyMetadata(constraint, node.Name.Name, constraint.Line()); metadata != nil {
res = append(res, metadata)
}
}
for _, column := range node.ColumnList {
for _, constraint := range column.ConstraintList {
- if metadata := getForeignKeyMetadata(constraint, node.Name.Name); metadata != nil {
+ if metadata := getForeignKeyMetadata(constraint, node.Name.Name, column.Line()); metadata != nil {
res = append(res, metadata)
}
}
}
case *ast.AddConstraintStmt:
constraint := node.Constraint
- if metadata := getForeignKeyMetadata(constraint, node.Table.Name); metadata != nil {
+ if metadata := getForeignKeyMetadata(constraint, node.Table.Name, node.Line()); metadata != nil {
res = append(res, metadata)
}
case *ast.AddColumnListStmt:
for _, column := range node.ColumnList {
for _, constraint := range column.ConstraintList {
- if metadata := getForeignKeyMetadata(constraint, node.Table.Name); metadata != nil {
+ if metadata := getForeignKeyMetadata(constraint, node.Table.Name, node.Line()); metadata != nil {
res = append(res, metadata)
}
}
@@ -150,7 +153,7 @@ func (*namingFKConventionChecker) getMetaDataList(in ast.Node) []*indexMetaData
}
// getForeignKeyMetadata returns index metadata of a foreign key constraint, nil if other constraints.
-func getForeignKeyMetadata(constraint *ast.ConstraintDef, tableName string) *indexMetaData {
+func getForeignKeyMetadata(constraint *ast.ConstraintDef, tableName string, line int) *indexMetaData {
if constraint.Type == ast.ConstraintTypeForeign {
referencingColumnList := constraint.KeyList
referencedColumnList := constraint.Foreign.ColumnList
@@ -165,6 +168,7 @@ func getForeignKeyMetadata(constraint *ast.ConstraintDef, tableName string) *ind
return &indexMetaData{
indexName: constraint.Name,
tableName: tableName,
+ line: line,
metaData: metaData,
}
}
diff --git a/plugin/advisor/pg/advisor_naming_foreign_key_convention_test.go b/plugin/advisor/pg/advisor_naming_foreign_key_convention_test.go
index 2ae10abeb8..9f949bddc5 100644
--- a/plugin/advisor/pg/advisor_naming_foreign_key_convention_test.go
+++ b/plugin/advisor/pg/advisor_naming_foreign_key_convention_test.go
@@ -33,6 +33,7 @@ func TestNamingFKConvention(t *testing.T) {
Code: advisor.NamingFKConventionMismatch,
Title: "naming.index.fk",
Content: "Foreign key in table \"tech_book\" mismatches the naming convention, expect \"^fk_tech_book_author_id_author_id$\" but found \"fk_author_id\"",
+ Line: 1,
},
},
},
@@ -44,12 +45,14 @@ func TestNamingFKConvention(t *testing.T) {
Code: advisor.NamingFKConventionMismatch,
Title: "naming.index.fk",
Content: fmt.Sprintf("Foreign key in table \"tech_book\" mismatches the naming convention, expect \"^fk_tech_book_author_id_author_id$\" but found \"%s\"", invalidFKName),
+ Line: 1,
},
{
Status: advisor.Error,
Code: advisor.NamingFKConventionMismatch,
Title: "naming.index.fk",
Content: fmt.Sprintf("Foreign key \"%s\" in table \"tech_book\" mismatches the naming convention, its length should be within %d characters", invalidFKName, maxLength),
+ Line: 1,
},
},
},
@@ -72,6 +75,7 @@ func TestNamingFKConvention(t *testing.T) {
Code: advisor.NamingFKConventionMismatch,
Title: "naming.index.fk",
Content: "Foreign key in table \"tech_book\" mismatches the naming convention, expect \"^fk_tech_book_author_id_author_id$\" but found \"fk_author_id\"",
+ Line: 1,
},
},
},
@@ -94,6 +98,7 @@ func TestNamingFKConvention(t *testing.T) {
Code: advisor.NamingFKConventionMismatch,
Title: "naming.index.fk",
Content: "Foreign key in table \"book\" mismatches the naming convention, expect \"^fk_book_author_id_author_id$\" but found \"fk_book_author_id\"",
+ Line: 1,
},
},
},
@@ -109,13 +114,18 @@ func TestNamingFKConvention(t *testing.T) {
},
},
{
- Statement: "CREATE TABLE book(id INT, author_id INT CONSTRAINT fk_book_author_id REFERENCES author (id))",
+ Statement: `
+ CREATE TABLE book(
+ id INT,
+ author_id INT CONSTRAINT fk_book_author_id REFERENCES author (id)
+ )`,
Want: []advisor.Advice{
{
Status: advisor.Error,
Code: advisor.NamingFKConventionMismatch,
Title: "naming.index.fk",
Content: "Foreign key in table \"book\" mismatches the naming convention, expect \"^fk_book_author_id_author_id$\" but found \"fk_book_author_id\"",
+ Line: 4,
},
},
},
diff --git a/plugin/advisor/pg/advisor_naming_index_convention.go b/plugin/advisor/pg/advisor_naming_index_convention.go
index 63c959aad6..7f144929f4 100644
--- a/plugin/advisor/pg/advisor_naming_index_convention.go
+++ b/plugin/advisor/pg/advisor_naming_index_convention.go
@@ -95,6 +95,7 @@ func (checker *namingIndexConventionChecker) Visit(node ast.Node) ast.Visitor {
Code: advisor.NamingIndexConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf("Index in table %q mismatches the naming convention, expect %q but found %q", indexData.tableName, regex, indexData.indexName),
+ Line: node.Line(),
})
}
if checker.maxLength > 0 && len(indexData.indexName) > checker.maxLength {
@@ -103,6 +104,7 @@ func (checker *namingIndexConventionChecker) Visit(node ast.Node) ast.Visitor {
Code: advisor.NamingIndexConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf("Index %q in table %q mismatches the naming convention, its length should be within %d characters", indexData.indexName, indexData.tableName, checker.maxLength),
+ Line: node.Line(),
})
}
}
diff --git a/plugin/advisor/pg/advisor_naming_index_convention_test.go b/plugin/advisor/pg/advisor_naming_index_convention_test.go
index 7130102a85..3fbc1b6568 100644
--- a/plugin/advisor/pg/advisor_naming_index_convention_test.go
+++ b/plugin/advisor/pg/advisor_naming_index_convention_test.go
@@ -33,6 +33,7 @@ func TestNamingIndexConvention(t *testing.T) {
Code: advisor.NamingIndexConventionMismatch,
Title: "naming.index.idx",
Content: "Index in table \"tech_book\" mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found \"tech_book_id_name\"",
+ Line: 1,
},
},
},
@@ -44,12 +45,14 @@ func TestNamingIndexConvention(t *testing.T) {
Code: advisor.NamingIndexConventionMismatch,
Title: "naming.index.idx",
Content: fmt.Sprintf("Index in table \"tech_book\" mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found \"%s\"", invalidIndexName),
+ Line: 1,
},
{
Status: advisor.Error,
Code: advisor.NamingIndexConventionMismatch,
Title: "naming.index.idx",
Content: fmt.Sprintf("Index \"%s\" in table \"tech_book\" mismatches the naming convention, its length should be within 32 characters", invalidIndexName),
+ Line: 1,
},
},
},
@@ -79,6 +82,7 @@ func TestNamingIndexConvention(t *testing.T) {
Code: advisor.NamingIndexConventionMismatch,
Title: "naming.index.idx",
Content: "Index in table \"tech_book\" mismatches the naming convention, expect \"^$|^idx_tech_book_id_name$\" but found \"idx_tech_book\"",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/pg/advisor_naming_primary_key_convention.go b/plugin/advisor/pg/advisor_naming_primary_key_convention.go
index e3d56c7c5a..2863378439 100644
--- a/plugin/advisor/pg/advisor_naming_primary_key_convention.go
+++ b/plugin/advisor/pg/advisor_naming_primary_key_convention.go
@@ -96,6 +96,7 @@ func (checker *namingPKConventionChecker) Visit(in ast.Node) ast.Visitor {
Code: advisor.NamingPKConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf(`Primary key in table "%s" mismatches the naming convention, expect %q but found "%s"`, indexData.tableName, regex, indexData.indexName),
+ Line: indexData.line,
})
}
if checker.maxLength > 0 && len(indexData.indexName) > checker.maxLength {
@@ -104,6 +105,7 @@ func (checker *namingPKConventionChecker) Visit(in ast.Node) ast.Visitor {
Code: advisor.NamingPKConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf(`Primary key "%s" in table "%s" mismatches the naming convention, its length should be within %d characters`, indexData.indexName, indexData.tableName, checker.maxLength),
+ Line: indexData.line,
})
}
}
@@ -117,26 +119,26 @@ func (checker *namingPKConventionChecker) getMetaDataList(in ast.Node) []*indexM
switch node := in.(type) {
case *ast.CreateTableStmt:
for _, constraint := range node.ConstraintList {
- if metadata := checker.getPrimaryKeyMetadata(node.Name.Schema, node.Name.Name, constraint); metadata != nil {
+ if metadata := checker.getPrimaryKeyMetadata(node.Name.Schema, node.Name.Name, constraint, constraint.Line()); metadata != nil {
res = append(res, metadata)
}
}
for _, column := range node.ColumnList {
for _, constraint := range column.ConstraintList {
- if metadata := checker.getPrimaryKeyMetadata(node.Name.Schema, node.Name.Name, constraint); metadata != nil {
+ if metadata := checker.getPrimaryKeyMetadata(node.Name.Schema, node.Name.Name, constraint, column.Line()); metadata != nil {
res = append(res, metadata)
}
}
}
case *ast.AddConstraintStmt:
constraint := node.Constraint
- if metadata := checker.getPrimaryKeyMetadata(node.Table.Schema, node.Table.Name, constraint); metadata != nil {
+ if metadata := checker.getPrimaryKeyMetadata(node.Table.Schema, node.Table.Name, constraint, in.Line()); metadata != nil {
res = append(res, metadata)
}
case *ast.AddColumnListStmt:
for _, column := range node.ColumnList {
for _, constraint := range column.ConstraintList {
- if metadata := checker.getPrimaryKeyMetadata(node.Table.Schema, node.Table.Name, constraint); metadata != nil {
+ if metadata := checker.getPrimaryKeyMetadata(node.Table.Schema, node.Table.Name, constraint, in.Line()); metadata != nil {
res = append(res, metadata)
}
}
@@ -151,6 +153,7 @@ func (checker *namingPKConventionChecker) getMetaDataList(in ast.Node) []*indexM
res = append(res, &indexMetaData{
indexName: node.NewName,
tableName: node.Table.Name,
+ line: in.Line(),
metaData: metaData,
})
}
@@ -165,6 +168,7 @@ func (checker *namingPKConventionChecker) getMetaDataList(in ast.Node) []*indexM
res = append(res, &indexMetaData{
indexName: node.NewName,
tableName: tableName,
+ line: in.Line(),
metaData: metaData,
})
}
@@ -173,7 +177,7 @@ func (checker *namingPKConventionChecker) getMetaDataList(in ast.Node) []*indexM
}
// getPrimaryKeyMetadata returns index metadata of a primary key constraint, nil if other constraints.
-func (checker *namingPKConventionChecker) getPrimaryKeyMetadata(schemaName string, tableName string, constraint *ast.ConstraintDef) *indexMetaData {
+func (checker *namingPKConventionChecker) getPrimaryKeyMetadata(schemaName string, tableName string, constraint *ast.ConstraintDef, line int) *indexMetaData {
switch constraint.Type {
case ast.ConstraintTypePrimary:
metaData := map[string]string{
@@ -183,6 +187,7 @@ func (checker *namingPKConventionChecker) getPrimaryKeyMetadata(schemaName strin
return &indexMetaData{
indexName: constraint.Name,
tableName: tableName,
+ line: line,
metaData: metaData,
}
case ast.ConstraintTypePrimaryUsingIndex:
@@ -195,6 +200,7 @@ func (checker *namingPKConventionChecker) getPrimaryKeyMetadata(schemaName strin
return &indexMetaData{
indexName: constraint.Name,
tableName: tableName,
+ line: line,
metaData: metaData,
}
}
diff --git a/plugin/advisor/pg/advisor_naming_primary_key_convention_test.go b/plugin/advisor/pg/advisor_naming_primary_key_convention_test.go
index 4c9918b496..99435deb13 100644
--- a/plugin/advisor/pg/advisor_naming_primary_key_convention_test.go
+++ b/plugin/advisor/pg/advisor_naming_primary_key_convention_test.go
@@ -34,6 +34,7 @@ func TestNamingPKConvention(t *testing.T) {
Code: advisor.NamingPKConventionMismatch,
Title: "naming.index.pk",
Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_id_name$\" but found \"tech_book_id_name\"",
+ Line: 1,
},
},
},
@@ -45,12 +46,14 @@ func TestNamingPKConvention(t *testing.T) {
Code: advisor.NamingPKConventionMismatch,
Title: "naming.index.pk",
Content: fmt.Sprintf("Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_id_name$\" but found \"%s\"", invalidPKName),
+ Line: 1,
},
{
Status: advisor.Error,
Code: advisor.NamingPKConventionMismatch,
Title: "naming.index.pk",
Content: fmt.Sprintf(`Primary key "%s" in table "tech_book" mismatches the naming convention, its length should be within %d characters`, invalidPKName, maxLength),
+ Line: 1,
},
},
},
@@ -66,13 +69,19 @@ func TestNamingPKConvention(t *testing.T) {
},
},
{
- Statement: "CREATE TABLE tech_book(id INT, name VARCHAR(20), CONSTRAINT tech_book_name PRIMARY KEY (name))",
+ Statement: `
+ CREATE TABLE tech_book(
+ id INT,
+ name VARCHAR(20),
+ CONSTRAINT tech_book_name PRIMARY KEY (name)
+ )`,
Want: []advisor.Advice{
{
Status: advisor.Error,
Code: advisor.NamingPKConventionMismatch,
Title: "naming.index.pk",
Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_name$\" but found \"tech_book_name\"",
+ Line: 5,
},
},
},
@@ -124,6 +133,7 @@ func TestNamingPKConvention(t *testing.T) {
Code: advisor.NamingPKConventionMismatch,
Title: "naming.index.pk",
Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_id_name$\" but found \"pk_tech_book\"",
+ Line: 1,
},
},
},
@@ -153,6 +163,7 @@ func TestNamingPKConvention(t *testing.T) {
Code: advisor.NamingPKConventionMismatch,
Title: "naming.index.pk",
Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_id_name$\" but found \"pk_tech_book\"",
+ Line: 1,
},
},
},
@@ -182,6 +193,7 @@ func TestNamingPKConvention(t *testing.T) {
Code: advisor.NamingPKConventionMismatch,
Title: "naming.index.pk",
Content: "Primary key in table \"tech_book\" mismatches the naming convention, expect \"^$|^pk_tech_book_id_name$\" but found \"pk_tech_book\"",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/pg/advisor_naming_table.go b/plugin/advisor/pg/advisor_naming_table.go
index 318df22d2c..0c253b6c51 100644
--- a/plugin/advisor/pg/advisor_naming_table.go
+++ b/plugin/advisor/pg/advisor_naming_table.go
@@ -88,6 +88,7 @@ func (checker *namingTableConventionChecker) Visit(node ast.Node) ast.Visitor {
Code: advisor.NamingTableConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf(`"%s" mismatches table naming convention, naming format should be %q`, tableName, checker.format),
+ Line: node.Line(),
})
}
if checker.maxLength > 0 && len(tableName) > checker.maxLength {
@@ -96,6 +97,7 @@ func (checker *namingTableConventionChecker) Visit(node ast.Node) ast.Visitor {
Code: advisor.NamingTableConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf("\"%s\" mismatches table naming convention, its length should be within %d characters", tableName, checker.maxLength),
+ Line: node.Line(),
})
}
}
diff --git a/plugin/advisor/pg/advisor_naming_table_test.go b/plugin/advisor/pg/advisor_naming_table_test.go
index ccd5f20d79..d624ce57ca 100644
--- a/plugin/advisor/pg/advisor_naming_table_test.go
+++ b/plugin/advisor/pg/advisor_naming_table_test.go
@@ -21,6 +21,7 @@ func TestPostgreSQLNamingTableConvention(t *testing.T) {
Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "\"techBook\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 1,
},
},
},
@@ -32,6 +33,7 @@ func TestPostgreSQLNamingTableConvention(t *testing.T) {
Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: fmt.Sprintf("\"%s\" mismatches table naming convention, its length should be within 32 characters", invalidTableName),
+ Line: 1,
},
},
},
@@ -43,6 +45,7 @@ func TestPostgreSQLNamingTableConvention(t *testing.T) {
Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "\"_techbook\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 1,
},
},
},
@@ -77,6 +80,7 @@ func TestPostgreSQLNamingTableConvention(t *testing.T) {
Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "\"TechBook\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 1,
},
},
},
@@ -100,12 +104,14 @@ func TestPostgreSQLNamingTableConvention(t *testing.T) {
Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "\"_techbook\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 1,
},
{
Status: advisor.Error,
Code: advisor.NamingTableConventionMismatch,
Title: "naming.table",
Content: "\"TechBook\" mismatches table naming convention, naming format should be \"^[a-z]+(_[a-z]+)*$\"",
+ Line: 2,
},
},
},
diff --git a/plugin/advisor/pg/advisor_naming_unique_key_convention.go b/plugin/advisor/pg/advisor_naming_unique_key_convention.go
index a75eb696af..a479ec7f0e 100644
--- a/plugin/advisor/pg/advisor_naming_unique_key_convention.go
+++ b/plugin/advisor/pg/advisor_naming_unique_key_convention.go
@@ -96,6 +96,7 @@ func (checker *namingUKConventionChecker) Visit(in ast.Node) ast.Visitor {
Code: advisor.NamingUKConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf(`Unique key in table "%s" mismatches the naming convention, expect %q but found "%s"`, indexData.tableName, regex, indexData.indexName),
+ Line: indexData.line,
})
}
if checker.maxLength > 0 && len(indexData.indexName) > checker.maxLength {
@@ -104,6 +105,7 @@ func (checker *namingUKConventionChecker) Visit(in ast.Node) ast.Visitor {
Code: advisor.NamingUKConventionMismatch,
Title: checker.title,
Content: fmt.Sprintf(`Unique key "%s" in table "%s" mismatches the naming convention, its length should be within %d characters`, indexData.indexName, indexData.tableName, checker.maxLength),
+ Line: indexData.line,
})
}
}
@@ -117,26 +119,26 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
switch node := in.(type) {
case *ast.CreateTableStmt:
for _, constraint := range node.ConstraintList {
- if metadata := checker.getUniqueKeyMetadata(node.Name.Schema, node.Name.Name, constraint); metadata != nil {
+ if metadata := checker.getUniqueKeyMetadata(node.Name.Schema, node.Name.Name, constraint, constraint.Line()); metadata != nil {
res = append(res, metadata)
}
}
for _, column := range node.ColumnList {
for _, constraint := range column.ConstraintList {
- if metadata := checker.getUniqueKeyMetadata(node.Name.Schema, node.Name.Name, constraint); metadata != nil {
+ if metadata := checker.getUniqueKeyMetadata(node.Name.Schema, node.Name.Name, constraint, column.Line()); metadata != nil {
res = append(res, metadata)
}
}
}
case *ast.AddConstraintStmt:
constraint := node.Constraint
- if metadata := checker.getUniqueKeyMetadata(node.Table.Schema, node.Table.Name, constraint); metadata != nil {
+ if metadata := checker.getUniqueKeyMetadata(node.Table.Schema, node.Table.Name, constraint, node.Line()); metadata != nil {
res = append(res, metadata)
}
case *ast.AddColumnListStmt:
for _, column := range node.ColumnList {
for _, constraint := range column.ConstraintList {
- if metadata := checker.getUniqueKeyMetadata(node.Table.Schema, node.Table.Name, constraint); metadata != nil {
+ if metadata := checker.getUniqueKeyMetadata(node.Table.Schema, node.Table.Name, constraint, node.Line()); metadata != nil {
res = append(res, metadata)
}
}
@@ -154,6 +156,7 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
res = append(res, &indexMetaData{
indexName: node.Index.Name,
tableName: node.Index.Table.Name,
+ line: node.Line(),
metaData: metaData,
})
}
@@ -167,6 +170,7 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
res = append(res, &indexMetaData{
indexName: node.NewName,
tableName: node.Table.Name,
+ line: node.Line(),
metaData: metaData,
})
}
@@ -181,6 +185,7 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
res = append(res, &indexMetaData{
indexName: node.NewName,
tableName: tableName,
+ line: node.Line(),
metaData: metaData,
})
}
@@ -189,7 +194,7 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
}
// getUniqueKeyMetadata returns index metadata of a unique key constraint, nil if other constraints.
-func (checker *namingUKConventionChecker) getUniqueKeyMetadata(schemaName string, tableName string, constraint *ast.ConstraintDef) *indexMetaData {
+func (checker *namingUKConventionChecker) getUniqueKeyMetadata(schemaName string, tableName string, constraint *ast.ConstraintDef, line int) *indexMetaData {
switch constraint.Type {
case ast.ConstraintTypeUnique:
metaData := map[string]string{
@@ -199,6 +204,7 @@ func (checker *namingUKConventionChecker) getUniqueKeyMetadata(schemaName string
return &indexMetaData{
indexName: constraint.Name,
tableName: tableName,
+ line: line,
metaData: metaData,
}
case ast.ConstraintTypeUniqueUsingIndex:
@@ -211,6 +217,7 @@ func (checker *namingUKConventionChecker) getUniqueKeyMetadata(schemaName string
return &indexMetaData{
indexName: constraint.Name,
tableName: tableName,
+ line: line,
metaData: metaData,
}
}
diff --git a/plugin/advisor/pg/advisor_naming_unique_key_convention_test.go b/plugin/advisor/pg/advisor_naming_unique_key_convention_test.go
index ff505a4e17..d1b82c08d8 100644
--- a/plugin/advisor/pg/advisor_naming_unique_key_convention_test.go
+++ b/plugin/advisor/pg/advisor_naming_unique_key_convention_test.go
@@ -34,6 +34,7 @@ func TestNamingUKConvention(t *testing.T) {
Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"tech_book_id_name\"",
+ Line: 1,
},
},
},
@@ -45,12 +46,14 @@ func TestNamingUKConvention(t *testing.T) {
Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: fmt.Sprintf("Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"%s\"", invalidUKName),
+ Line: 1,
},
{
Status: advisor.Error,
Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: fmt.Sprintf("Unique key \"%s\" in table \"tech_book\" mismatches the naming convention, its length should be within %d characters", invalidUKName, maxLength),
+ Line: 1,
},
},
},
@@ -73,6 +76,7 @@ func TestNamingUKConvention(t *testing.T) {
Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"tech_book_id_name\"",
+ Line: 1,
},
},
},
@@ -88,13 +92,19 @@ func TestNamingUKConvention(t *testing.T) {
},
},
{
- Statement: "CREATE TABLE tech_book(id INT PRIMARY KEY, name VARCHAR(20), CONSTRAINT tech_book_name UNIQUE (name))",
+ Statement: `
+ CREATE TABLE tech_book(
+ id INT PRIMARY KEY,
+ name VARCHAR(20),
+ CONSTRAINT tech_book_name UNIQUE (name)
+ )`,
Want: []advisor.Advice{
{
Status: advisor.Error,
Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_name$\" but found \"tech_book_name\"",
+ Line: 5,
},
},
},
@@ -146,6 +156,7 @@ func TestNamingUKConvention(t *testing.T) {
Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"uk_tech_book\"",
+ Line: 1,
},
},
},
@@ -175,6 +186,7 @@ func TestNamingUKConvention(t *testing.T) {
Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"uk_tech_book\"",
+ Line: 1,
},
},
},
@@ -204,6 +216,7 @@ func TestNamingUKConvention(t *testing.T) {
Code: advisor.NamingUKConventionMismatch,
Title: "naming.index.uk",
Content: "Unique key in table \"tech_book\" mismatches the naming convention, expect \"^$|^uk_tech_book_id_name$\" but found \"uk_tech_book\"",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like.go b/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like.go
index 2bdbb965e7..480cb2d658 100644
--- a/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like.go
+++ b/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like.go
@@ -53,6 +53,7 @@ func (*NoLeadingWildcardLikeAdvisor) Check(ctx advisor.Context, statement string
Code: advisor.StatementLeadingWildcardLike,
Title: checker.title,
Content: fmt.Sprintf("\"%s\" uses leading wildcard LIKE", checker.text),
+ Line: stmt.Line(),
})
}
}
diff --git a/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like_test.go b/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like_test.go
index 2cb2844d15..1d84bcc06c 100644
--- a/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like_test.go
+++ b/plugin/advisor/pg/advisor_stmt_no_leading_wildcard_like_test.go
@@ -27,6 +27,7 @@ func TestNoLeadingWildcardLike(t *testing.T) {
Code: advisor.StatementLeadingWildcardLike,
Title: "statement.where.no-leading-wildcard-like",
Content: "\"SELECT * FROM t WHERE a LIKE '%abc'\" uses leading wildcard LIKE",
+ Line: 1,
},
},
},
@@ -38,6 +39,7 @@ func TestNoLeadingWildcardLike(t *testing.T) {
Code: advisor.StatementLeadingWildcardLike,
Title: "statement.where.no-leading-wildcard-like",
Content: "\"SELECT * FROM t WHERE a LIKE 'abc' OR a LIKE '%abc'\" uses leading wildcard LIKE",
+ Line: 1,
},
},
},
@@ -49,6 +51,7 @@ func TestNoLeadingWildcardLike(t *testing.T) {
Code: advisor.StatementLeadingWildcardLike,
Title: "statement.where.no-leading-wildcard-like",
Content: "\"SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc'\" uses leading wildcard LIKE",
+ Line: 1,
},
},
},
@@ -60,6 +63,7 @@ func TestNoLeadingWildcardLike(t *testing.T) {
Code: advisor.StatementLeadingWildcardLike,
Title: "statement.where.no-leading-wildcard-like",
Content: "\"SELECT * FROM (SELECT * FROM t WHERE a LIKE '%acc' OR a LIKE '%abc') t1\" uses leading wildcard LIKE",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/pg/advisor_stmt_no_select_all.go b/plugin/advisor/pg/advisor_stmt_no_select_all.go
index f5046ab431..b6d5180ff8 100644
--- a/plugin/advisor/pg/advisor_stmt_no_select_all.go
+++ b/plugin/advisor/pg/advisor_stmt_no_select_all.go
@@ -39,6 +39,7 @@ func (*NoSelectAllAdvisor) Check(ctx advisor.Context, statement string) ([]advis
}
for _, stmt := range stmts {
checker.text = stmt.Text()
+ checker.line = stmt.Line()
ast.Walk(checker, stmt)
}
@@ -58,6 +59,7 @@ type noSelectAllChecker struct {
level advisor.Status
title string
text string
+ line int
}
// Visit implements the ast.Visitor interface.
@@ -70,6 +72,7 @@ func (checker *noSelectAllChecker) Visit(node ast.Node) ast.Visitor {
Code: advisor.StatementSelectAll,
Title: checker.title,
Content: fmt.Sprintf("\"%s\" uses SELECT all", checker.text),
+ Line: checker.line,
})
break
}
diff --git a/plugin/advisor/pg/advisor_stmt_no_select_all_test.go b/plugin/advisor/pg/advisor_stmt_no_select_all_test.go
index a56d29629d..4f5b3c4d14 100644
--- a/plugin/advisor/pg/advisor_stmt_no_select_all_test.go
+++ b/plugin/advisor/pg/advisor_stmt_no_select_all_test.go
@@ -16,6 +16,7 @@ func TestNoSelectAll(t *testing.T) {
Code: advisor.StatementSelectAll,
Title: "statement.select.no-select-all",
Content: "\"SELECT * FROM t\" uses SELECT all",
+ Line: 1,
},
},
},
@@ -38,6 +39,19 @@ func TestNoSelectAll(t *testing.T) {
Code: advisor.StatementSelectAll,
Title: "statement.select.no-select-all",
Content: "\"SELECT a, b FROM (SELECT * from t1, t2) t\" uses SELECT all",
+ Line: 1,
+ },
+ },
+ },
+ {
+ Statement: "INSERT INTO t SELECT * FROM t1",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: advisor.StatementSelectAll,
+ Title: "statement.select.no-select-all",
+ Content: "\"INSERT INTO t SELECT * FROM t1\" uses SELECT all",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/pg/advisor_stmt_where_required.go b/plugin/advisor/pg/advisor_stmt_where_required.go
index 8f5e51a01b..d76eb4356b 100644
--- a/plugin/advisor/pg/advisor_stmt_where_required.go
+++ b/plugin/advisor/pg/advisor_stmt_where_required.go
@@ -39,6 +39,7 @@ func (*WhereRequirementAdvisor) Check(ctx advisor.Context, statement string) ([]
for _, stmt := range stmts {
checker.text = stmt.Text()
+ checker.line = stmt.Line()
ast.Walk(checker, stmt)
}
@@ -58,6 +59,7 @@ type whereRequirementChecker struct {
level advisor.Status
title string
text string
+ line int
}
// Visit implements the ast.Visitor interface.
@@ -87,6 +89,7 @@ func (checker *whereRequirementChecker) Visit(node ast.Node) ast.Visitor {
Code: code,
Title: checker.title,
Content: fmt.Sprintf("\"%s\" requires WHERE clause", checker.text),
+ Line: checker.line,
})
}
return checker
diff --git a/plugin/advisor/pg/advisor_stmt_where_required_test.go b/plugin/advisor/pg/advisor_stmt_where_required_test.go
index 621ded8743..b6ab8607d1 100644
--- a/plugin/advisor/pg/advisor_stmt_where_required_test.go
+++ b/plugin/advisor/pg/advisor_stmt_where_required_test.go
@@ -27,6 +27,7 @@ func TestWhereRequirement(t *testing.T) {
Code: advisor.StatementNoWhere,
Title: "statement.where.require",
Content: "\"DELETE FROM t1\" requires WHERE clause",
+ Line: 1,
},
},
},
@@ -38,6 +39,7 @@ func TestWhereRequirement(t *testing.T) {
Code: advisor.StatementNoWhere,
Title: "statement.where.require",
Content: "\"UPDATE t1 SET a = 1\" requires WHERE clause",
+ Line: 1,
},
},
},
@@ -71,6 +73,7 @@ func TestWhereRequirement(t *testing.T) {
Code: advisor.StatementNoWhere,
Title: "statement.where.require",
Content: "\"SELECT a FROM t\" requires WHERE clause",
+ Line: 1,
},
},
},
@@ -93,6 +96,7 @@ func TestWhereRequirement(t *testing.T) {
Code: advisor.StatementNoWhere,
Title: "statement.where.require",
Content: "\"SELECT a FROM t WHERE a > (SELECT max(id) FROM user)\" requires WHERE clause",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/pg/advisor_table_no_fk.go b/plugin/advisor/pg/advisor_table_no_fk.go
index 558955633e..24622b8b9e 100644
--- a/plugin/advisor/pg/advisor_table_no_fk.go
+++ b/plugin/advisor/pg/advisor_table_no_fk.go
@@ -93,6 +93,7 @@ func (checker *tableNoFKChecker) Visit(node ast.Node) ast.Visitor {
tableHasFK.Name,
checker.text,
),
+ Line: node.Line(),
})
}
diff --git a/plugin/advisor/pg/advisor_table_no_fk_test.go b/plugin/advisor/pg/advisor_table_no_fk_test.go
index 2c5c5aee72..d870662f66 100644
--- a/plugin/advisor/pg/advisor_table_no_fk_test.go
+++ b/plugin/advisor/pg/advisor_table_no_fk_test.go
@@ -16,6 +16,7 @@ func TestTableNoFK(t *testing.T) {
Code: advisor.TableHasFK,
Title: "table.no-foreign-key",
Content: "Foreign key is not allowed in the table \"public\".\"tech_book\", related statement: \"ALTER TABLE tech_book ADD CONSTRAINT fk_tech_book_author_id_author_id FOREIGN KEY (author_id) REFERENCES author (id)\"",
+ Line: 1,
},
},
},
@@ -27,6 +28,7 @@ func TestTableNoFK(t *testing.T) {
Code: advisor.TableHasFK,
Title: "table.no-foreign-key",
Content: "Foreign key is not allowed in the table \"public\".\"book\", related statement: \"CREATE TABLE book(id INT, author_id INT, CONSTRAINT fk_book_author_id_author_id FOREIGN KEY (author_id) REFERENCES author (id))\"",
+ Line: 1,
},
},
},
diff --git a/plugin/advisor/pg/advisor_table_require_pk.go b/plugin/advisor/pg/advisor_table_require_pk.go
index 5e665f3395..9a9f7a7e7f 100644
--- a/plugin/advisor/pg/advisor_table_require_pk.go
+++ b/plugin/advisor/pg/advisor_table_require_pk.go
@@ -117,6 +117,7 @@ func (checker *tableRequirePKChecker) Visit(node ast.Node) ast.Visitor {
missingPK.Name,
checker.text,
),
+ Line: node.Line(),
})
}
diff --git a/plugin/advisor/pg/advisor_table_require_pk_test.go b/plugin/advisor/pg/advisor_table_require_pk_test.go
index eb28b2d7bd..4be90b434c 100644
--- a/plugin/advisor/pg/advisor_table_require_pk_test.go
+++ b/plugin/advisor/pg/advisor_table_require_pk_test.go
@@ -39,6 +39,7 @@ func TestRequirePK(t *testing.T) {
Code: advisor.TableNoPK,
Title: "table.require-pk",
Content: "Table \"public\".\"t\" requires PRIMARY KEY, related statement: \"CREATE TABLE t(id INT)\"",
+ Line: 1,
},
},
},
@@ -50,6 +51,7 @@ func TestRequirePK(t *testing.T) {
Code: advisor.TableNoPK,
Title: "table.require-pk",
Content: "Table \"public\".\"tech_book\" requires PRIMARY KEY, related statement: \"ALTER TABLE \\\"tech_book\\\" DROP CONSTRAINT \\\"old_pk\\\"\"",
+ Line: 1,
},
},
},
@@ -83,6 +85,7 @@ func TestRequirePK(t *testing.T) {
Code: advisor.TableNoPK,
Title: "table.require-pk",
Content: "Table \"public\".\"tech_book\" requires PRIMARY KEY, related statement: \"ALTER TABLE \\\"tech_book\\\" DROP COLUMN id\"",
+ Line: 1,
},
},
},