Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: sort by advice level (error first) (#5913)
Danny Xub3e5a805818b
diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.goindex 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.// nolinttype Type stringdiff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex 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 Sif 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.yamlindex 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-innodbcontent: '"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.advisorcode: 301status: WARN@@ -54,18 +72,6 @@title: naming.index.fkcontent: 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.advisorcode: 605status: WARN@@ -114,12 +120,6 @@title: column.commentcontent: Column `userTable`.`roomId` requires commentsline: 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.advisorcode: 414status: 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.advisorcode: 307status: WARN@@ -183,12 +189,6 @@title: column.requiredcontent: "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.advisorcode: 416status: WARN@@ -216,18 +216,18 @@title: statement.where.requirecontent: '"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.advisorcode: 208status: ERRORtitle: statement.dml-dry-runcontent: '"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: 1run: false- statement: DELETE FROM tech_book WHERE name like `%abc`result:@@ -237,18 +237,18 @@title: statement.where.no-leading-wildcard-likecontent: '"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.advisorcode: 208status: ERRORtitle: statement.dml-dry-runcontent: '"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: 1run: false- statement: " INSERT INTO t_copy SELECT * FROM t"result:@@ -264,6 +264,12 @@title: statement.where.requirecontent: '"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.advisorcode: 1101status: WARN@@ -276,42 +282,36 @@title: statement.insert.must-specify-columncontent: The INSERT statement must specify columns but "INSERT INTO t_copy SELECT * FROM t" does notline: 1+ run: false+- statement: " INSERT INTO t VALUES (1, 1, now(), 1, now())"+ result:- namespace: bb.advisorcode: 208status: ERRORtitle: 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.advisorcode: 1107status: WARNtitle: statement.insert.must-specify-columncontent: The INSERT statement must specify columns but "INSERT INTO t VALUES (1, 1, now(), 1, now())" does notline: 1+ run: false+- statement: DELETE FROM tech_book WHERE id = (SELECT max(id) FROM tech_book WHERE name = 'bytebase')+ result:- namespace: bb.advisorcode: 208status: ERRORtitle: 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.advisorcode: 209status: WARNtitle: statement.affected-row-limitcontent: '"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: 1run: false- statement: COMMIT;result:diff --git a/backend/tests/test-data/sql_review_pg.yaml b/backend/tests/test-data/sql_review_pg.yamlindex 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.advisorcode: 301status: WARN@@ -60,25 +79,6 @@title: naming.index.fkcontent: 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.advisorcode: 401status: WARN