Skip to content

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

chore: sort by advice level (error first) (#5913)

Danny Xub3e5a805818b
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.go
index 112cf449ef..8211ef6e61 100644
--- a/backend/plugin/advisor/advisor.go
+++ b/backend/plugin/advisor/advisor.go
@@ -41,6 +41,19 @@ func NewStatusBySQLReviewRuleLevel(level SQLReviewRuleLevel) (Status, error) {
return "", errors.Errorf("unexpected rule level type: %s", level)
}
+// GetPriority returns the priority of status.
+func (s Status) GetPriority() int {
+ switch s {
+ case Success:
+ return 0
+ case Warn:
+ return 1
+ case Error:
+ return 2
+ }
+ return 0
+}
+
// Type is the type of advisor.
// nolint
type Type string
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.go
index e9e573fe88..2458b9d668 100644
--- a/backend/plugin/advisor/sql_review.go
+++ b/backend/plugin/advisor/sql_review.go
@@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"regexp"
+ "sort"
"strings"
"github.com/pkg/errors"
@@ -491,6 +492,10 @@ func SQLReviewCheck(statements string, ruleList []*SQLReviewRule, checkContext S
if len(result) > 0 && result[0].Title == SyntaxErrorTitle {
return result[:1], nil
}
+ sort.SliceStable(result, func(i, j int) bool {
+ // Error is 2, warning is 1. So the error (value 2) should come first.
+ return result[i].Status.GetPriority() > result[j].Status.GetPriority()
+ })
if len(result) == 0 {
result = append(result, Advice{
Status: Success,
diff --git a/backend/tests/test-data/sql_review_mysql.yaml b/backend/tests/test-data/sql_review_mysql.yaml
index 35dd908a1e..60c5c5d62a 100644
--- a/backend/tests/test-data/sql_review_mysql.yaml
+++ b/backend/tests/test-data/sql_review_mysql.yaml
@@ -24,6 +24,24 @@
title: engine.mysql.use-innodb
content: '"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" doesn''t use InnoDB engine'
line: 1
+ - namespace: bb.advisor
+ code: 601
+ status: ERROR
+ title: table.require-pk
+ content: Table `userTable` requires PRIMARY KEY
+ line: 1
+ - namespace: bb.advisor
+ code: 602
+ status: ERROR
+ title: table.no-foreign-key
+ content: Foreign key is not allowed in the table `userTable`
+ line: 1
+ - namespace: bb.advisor
+ code: 411
+ status: ERROR
+ title: column.type-disallow-list
+ content: Disallow column type JSON but column `userTable`.`json_content` is
+ line: 1
- namespace: bb.advisor
code: 301
status: WARN
@@ -54,18 +72,6 @@
title: naming.index.fk
content: Foreign key in table `userTable` mismatches the naming convention, expect "^$|^fk_userTable_roomId_room_id$" but found `fk1`
line: 1
- - namespace: bb.advisor
- code: 601
- status: ERROR
- title: table.require-pk
- content: Table `userTable` requires PRIMARY KEY
- line: 1
- - namespace: bb.advisor
- code: 602
- status: ERROR
- title: table.no-foreign-key
- content: Foreign key is not allowed in the table `userTable`
- line: 1
- namespace: bb.advisor
code: 605
status: WARN
@@ -114,12 +120,6 @@
title: column.comment
content: Column `userTable`.`roomId` requires comments
line: 1
- - namespace: bb.advisor
- code: 411
- status: ERROR
- title: column.type-disallow-list
- content: Disallow column type JSON but column `userTable`.`json_content` is
- line: 1
- namespace: bb.advisor
code: 414
status: WARN
@@ -171,6 +171,12 @@
run: false
- statement: CREATE TABLE t_auto(auto_id varchar(20) AUTO_INCREMENT PRIMARY KEY COMMENT 'COMMENT') auto_increment = 2 COMMENT 'comment'
result:
+ - namespace: bb.advisor
+ code: 410
+ status: ERROR
+ title: column.auto-increment-must-integer
+ content: Auto-increment column `t_auto`.`auto_id` requires integer type
+ line: 1
- namespace: bb.advisor
code: 307
status: WARN
@@ -183,12 +189,6 @@
title: column.required
content: "Table `t_auto` requires columns: created_ts, creator_id, id, updated_ts, updater_id"
line: 1
- - namespace: bb.advisor
- code: 410
- status: ERROR
- title: column.auto-increment-must-integer
- content: Auto-increment column `t_auto`.`auto_id` requires integer type
- line: 1
- namespace: bb.advisor
code: 416
status: WARN
@@ -216,18 +216,18 @@
title: statement.where.require
content: '"DELETE FROM tech_book" requires WHERE clause'
line: 1
- - namespace: bb.advisor
- code: 209
- status: WARN
- title: statement.affected-row-limit
- content: '"DELETE FROM tech_book" dry runs failed: Error 1146 (42S02): Table ''testsqlreview.tech_book'' doesn''t exist'
- line: 1
- namespace: bb.advisor
code: 208
status: ERROR
title: statement.dml-dry-run
content: '"DELETE FROM tech_book" dry runs failed: Error 1146 (42S02): Table ''testsqlreview.tech_book'' doesn''t exist'
line: 1
+ - namespace: bb.advisor
+ code: 209
+ status: WARN
+ title: statement.affected-row-limit
+ content: '"DELETE FROM tech_book" dry runs failed: Error 1146 (42S02): Table ''testsqlreview.tech_book'' doesn''t exist'
+ line: 1
run: false
- statement: DELETE FROM tech_book WHERE name like `%abc`
result:
@@ -237,18 +237,18 @@
title: statement.where.no-leading-wildcard-like
content: '"DELETE FROM tech_book WHERE name like `%abc`" uses leading wildcard LIKE'
line: 1
- - namespace: bb.advisor
- code: 209
- status: WARN
- title: statement.affected-row-limit
- content: '"DELETE FROM tech_book WHERE name like `%abc`" dry runs failed: Error 1146 (42S02): Table ''testsqlreview.tech_book'' doesn''t exist'
- line: 1
- namespace: bb.advisor
code: 208
status: ERROR
title: statement.dml-dry-run
content: '"DELETE FROM tech_book WHERE name like `%abc`" dry runs failed: Error 1146 (42S02): Table ''testsqlreview.tech_book'' doesn''t exist'
line: 1
+ - namespace: bb.advisor
+ code: 209
+ status: WARN
+ title: statement.affected-row-limit
+ content: '"DELETE FROM tech_book WHERE name like `%abc`" dry runs failed: Error 1146 (42S02): Table ''testsqlreview.tech_book'' doesn''t exist'
+ line: 1
run: false
- statement: " INSERT INTO t_copy SELECT * FROM t"
result:
@@ -264,6 +264,12 @@
title: statement.where.require
content: '"INSERT INTO t_copy SELECT * FROM t" requires WHERE clause'
line: 1
+ - namespace: bb.advisor
+ code: 208
+ status: ERROR
+ title: statement.dml-dry-run
+ content: '"INSERT INTO t_copy SELECT * FROM t" dry runs failed: Error 1146 (42S02): Table ''testsqlreview.t_copy'' doesn''t exist'
+ line: 1
- namespace: bb.advisor
code: 1101
status: WARN
@@ -276,42 +282,36 @@
title: statement.insert.must-specify-column
content: The INSERT statement must specify columns but "INSERT INTO t_copy SELECT * FROM t" does not
line: 1
+ run: false
+- statement: " INSERT INTO t VALUES (1, 1, now(), 1, now())"
+ result:
- namespace: bb.advisor
code: 208
status: ERROR
title: statement.dml-dry-run
- content: '"INSERT INTO t_copy SELECT * FROM t" dry runs failed: Error 1146 (42S02): Table ''testsqlreview.t_copy'' doesn''t exist'
+ content: '"INSERT INTO t VALUES (1, 1, now(), 1, now())" dry runs failed: Error 1146 (42S02): Table ''testsqlreview.t'' doesn''t exist'
line: 1
- run: false
-- statement: " INSERT INTO t VALUES (1, 1, now(), 1, now())"
- result:
- namespace: bb.advisor
code: 1107
status: WARN
title: statement.insert.must-specify-column
content: The INSERT statement must specify columns but "INSERT INTO t VALUES (1, 1, now(), 1, now())" does not
line: 1
+ run: false
+- statement: DELETE FROM tech_book WHERE id = (SELECT max(id) FROM tech_book WHERE name = 'bytebase')
+ result:
- namespace: bb.advisor
code: 208
status: ERROR
title: statement.dml-dry-run
- content: '"INSERT INTO t VALUES (1, 1, now(), 1, now())" dry runs failed: Error 1146 (42S02): Table ''testsqlreview.t'' doesn''t exist'
+ content: '"DELETE FROM tech_book WHERE id = (SELECT max(id) FROM tech_book WHERE name = ''bytebase'')" dry runs failed: Error 1146 (42S02): Table ''testsqlreview.tech_book'' doesn''t exist'
line: 1
- run: false
-- statement: DELETE FROM tech_book WHERE id = (SELECT max(id) FROM tech_book WHERE name = 'bytebase')
- result:
- namespace: bb.advisor
code: 209
status: WARN
title: statement.affected-row-limit
content: '"DELETE FROM tech_book WHERE id = (SELECT max(id) FROM tech_book WHERE name = ''bytebase'')" dry runs failed: Error 1146 (42S02): Table ''testsqlreview.tech_book'' doesn''t exist'
line: 1
- - namespace: bb.advisor
- code: 208
- status: ERROR
- title: statement.dml-dry-run
- content: '"DELETE FROM tech_book WHERE id = (SELECT max(id) FROM tech_book WHERE name = ''bytebase'')" dry runs failed: Error 1146 (42S02): Table ''testsqlreview.tech_book'' doesn''t exist'
- line: 1
run: false
- statement: COMMIT;
result:
diff --git a/backend/tests/test-data/sql_review_pg.yaml b/backend/tests/test-data/sql_review_pg.yaml
index 1efde2fce4..76370a40e1 100644
--- a/backend/tests/test-data/sql_review_pg.yaml
+++ b/backend/tests/test-data/sql_review_pg.yaml
@@ -36,6 +36,25 @@
CONSTRAINT fk1 FOREIGN KEY ("roomId") REFERENCES room(id)
)
result:
+ - namespace: bb.advisor
+ code: 601
+ status: ERROR
+ title: table.require-pk
+ content: 'Table "public"."userTable" requires PRIMARY KEY, related statement: "CREATE TABLE \"userTable\"(\n id INT,\n name VARCHAR(255),\n \"roomId\" INT,\n CONSTRAINT uk1 UNIQUE (id, name),\n CONSTRAINT fk1 FOREIGN KEY (\"roomId\") REFERENCES room(id)\n )"'
+ line: 7
+ - namespace: bb.advisor
+ code: 602
+ status: ERROR
+ title: table.no-foreign-key
+ content: |-
+ Foreign key is not allowed in the table "public"."userTable", related statement: "CREATE TABLE "userTable"(
+ id INT,
+ name VARCHAR(255),
+ "roomId" INT,
+ CONSTRAINT uk1 UNIQUE (id, name),
+ CONSTRAINT fk1 FOREIGN KEY ("roomId") REFERENCES room(id)
+ )"
+ line: 7
- namespace: bb.advisor
code: 301
status: WARN
@@ -60,25 +79,6 @@
title: naming.index.fk
content: Foreign key in table "userTable" mismatches the naming convention, expect "^$|^fk_userTable_roomId_room_id$" but found "fk1"
line: 6
- - namespace: bb.advisor
- code: 601
- status: ERROR
- title: table.require-pk
- content: 'Table "public"."userTable" requires PRIMARY KEY, related statement: "CREATE TABLE \"userTable\"(\n id INT,\n name VARCHAR(255),\n \"roomId\" INT,\n CONSTRAINT uk1 UNIQUE (id, name),\n CONSTRAINT fk1 FOREIGN KEY (\"roomId\") REFERENCES room(id)\n )"'
- line: 7
- - namespace: bb.advisor
- code: 602
- status: ERROR
- title: table.no-foreign-key
- content: |-
- Foreign key is not allowed in the table "public"."userTable", related statement: "CREATE TABLE "userTable"(
- id INT,
- name VARCHAR(255),
- "roomId" INT,
- CONSTRAINT uk1 UNIQUE (id, name),
- CONSTRAINT fk1 FOREIGN KEY ("roomId") REFERENCES room(id)
- )"
- line: 7
- namespace: bb.advisor
code: 401
status: WARN