Skip to content

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

chore(schema system): implement the drop table naming convention rule (#1867)

ecmadao9fd84ce64582
diff --git a/frontend/src/components/Issue/TaskCheckRunPanel.vue b/frontend/src/components/Issue/TaskCheckRunPanel.vue
index 13d5b076cc..93e639a501 100644
--- a/frontend/src/components/Issue/TaskCheckRunPanel.vue
+++ b/frontend/src/components/Issue/TaskCheckRunPanel.vue
@@ -37,11 +37,11 @@
<BBTableCell class="w-64">
{{ checkResult.content }}
<a
- v-if="errorCodeLink(checkResult.code)"
+ v-if="errorCodeLink(checkResult)"
class="normal-link"
- :href="errorCodeLink(checkResult.code)?.url"
- :target="errorCodeLink(checkResult.code)?.target"
- >{{ errorCodeLink(checkResult.code)?.title }}</a
+ :href="errorCodeLink(checkResult)?.url"
+ :target="errorCodeLink(checkResult)?.target"
+ >{{ errorCodeLink(checkResult)?.title }}</a
>
</BBTableCell>
</template>
@@ -58,10 +58,10 @@ import {
TaskCheckRun,
TaskCheckResult,
ErrorCode,
- ERROR_LIST,
GeneralErrorCode,
ruleTemplateMap,
getRuleLocalization,
+ CompatibilityErrorCode,
SQLReviewPolicyErrorCode,
RuleType,
} from "@/types";
@@ -117,6 +117,7 @@ export default defineComponent({
title: t("common.error"),
code: props.taskCheckRun.code,
content: props.taskCheckRun.result.detail,
+ namespace: "bb.core",
},
];
} else if (props.taskCheckRun.status == "CANCELED") {
@@ -126,6 +127,7 @@ export default defineComponent({
title: t("common.canceled"),
code: props.taskCheckRun.code,
content: "",
+ namespace: "bb.core",
},
];
}
@@ -140,6 +142,7 @@ export default defineComponent({
case SQLReviewPolicyErrorCode.EMPTY_POLICY:
title = checkResult.title;
break;
+ // SchemaReviewPolicyErrorCode
case SQLReviewPolicyErrorCode.STATEMENT_NO_WHERE:
case SQLReviewPolicyErrorCode.STATEMENT_NO_SELECT_ALL:
case SQLReviewPolicyErrorCode.STATEMENT_LEADING_WILDCARD_LIKE:
@@ -152,6 +155,20 @@ export default defineComponent({
case SQLReviewPolicyErrorCode.COLUMN_CANBE_NULL:
case SQLReviewPolicyErrorCode.NOT_INNODB_ENGINE:
case SQLReviewPolicyErrorCode.NO_PK_IN_TABLE:
+ case SQLReviewPolicyErrorCode.FK_IN_TABLE:
+ case SQLReviewPolicyErrorCode.TABLE_DROP_NAMING_CONVENTION:
+ // CompatibilityErrorCode
+ case CompatibilityErrorCode.COMPATIBILITY_DROP_DATABASE:
+ case CompatibilityErrorCode.COMPATIBILITY_RENAME_TABLE:
+ case CompatibilityErrorCode.COMPATIBILITY_DROP_TABLE:
+ case CompatibilityErrorCode.COMPATIBILITY_RENAME_COLUMN:
+ case CompatibilityErrorCode.COMPATIBILITY_DROP_COLUMN:
+ case CompatibilityErrorCode.COMPATIBILITY_ADD_PRIMARY_KEY:
+ case CompatibilityErrorCode.COMPATIBILITY_ADD_UNIQUE_KEY:
+ case CompatibilityErrorCode.COMPATIBILITY_ADD_FOREIGN_KEY:
+ case CompatibilityErrorCode.COMPATIBILITY_ADD_CHECK:
+ case CompatibilityErrorCode.COMPATIBILITY_ALTER_CHECK:
+ case CompatibilityErrorCode.COMPATIBILITY_ALTER_COLUMN:
const rule = ruleTemplateMap.get(checkResult.title as RuleType);
if (rule) {
@@ -168,8 +185,10 @@ export default defineComponent({
return title ? `${title} (${checkResult.code})` : checkResult.title;
};
- const errorCodeLink = (code: ErrorCode): ErrorCodeLink | undefined => {
- switch (code) {
+ const errorCodeLink = (
+ checkResult: TaskCheckResult
+ ): ErrorCodeLink | undefined => {
+ switch (checkResult.code) {
case GeneralErrorCode.OK:
return;
case SQLReviewPolicyErrorCode.EMPTY_POLICY:
@@ -179,14 +198,13 @@ export default defineComponent({
url: "/setting/sql-review",
};
default:
- const error = ERROR_LIST.find((item) => item.code == code);
- if (!error) {
- return;
- }
+ const url = `https://www.bytebase.com/docs/reference/error-code/${
+ checkResult.namespace === "bb.advisor" ? "advisor" : "core"
+ }?source=console`;
return {
title: t("common.view-doc"),
target: "__blank",
- url: `https://bytebase.com/docs/reference/error-code/core#${error.hash}?source=console`,
+ url: url,
};
}
};
diff --git a/frontend/src/locales/sql-review/en-US.json b/frontend/src/locales/sql-review/en-US.json
index 6675c50bc4..fff0d022d8 100644
--- a/frontend/src/locales/sql-review/en-US.json
+++ b/frontend/src/locales/sql-review/en-US.json
@@ -28,6 +28,15 @@
"title": "No foreign key",
"description": "Disallow the foreign key in the table."
},
+ "table-drop-naming-convention": {
+ "title": "Drop table with naming convention",
+ "description": "Only tables named with specific patterns can be deleted. The requires users to do a rename before dropping the table. The table name must have \"_delete\" suffix by default.",
+ "component": {
+ "name_format": {
+ "title": "Table name format (regex)"
+ }
+ }
+ },
"naming-table": {
"title": "Table naming check",
"description": "Enforce the table name format and length limit. Default snake_lower_case with 64 characters.",
diff --git a/frontend/src/locales/sql-review/zh-CN.json b/frontend/src/locales/sql-review/zh-CN.json
index be2b058ccd..495ca4536b 100644
--- a/frontend/src/locales/sql-review/zh-CN.json
+++ b/frontend/src/locales/sql-review/zh-CN.json
@@ -28,6 +28,15 @@
"title": "禁止外键",
"description": "禁止给表创建外键。"
},
+ "table-drop-naming-convention": {
+ "title": "待删除表的命名规范",
+ "description": "只有符合命名规范的表才可以被删除,通过强制用户在删除前重命名来避免误删。默认情况下待删除表名必须以 \"_delete\" 结尾。",
+ "component": {
+ "name_format": {
+ "title": "命名规则(正则)"
+ }
+ }
+ },
"naming-table": {
"title": "表名命名检查",
"description": "限制表名命名风格和长度,默认为小写字母_下划线,且不超过 64 个字符。",
diff --git a/frontend/src/types/error.ts b/frontend/src/types/error.ts
index d60c0d55ff..1f080e9556 100644
--- a/frontend/src/types/error.ts
+++ b/frontend/src/types/error.ts
@@ -22,33 +22,35 @@ export enum MigrationErrorCode {
}
export enum SQLReviewPolicyErrorCode {
- EMPTY_POLICY = 401,
- STATEMENT_NO_WHERE = 10101,
- STATEMENT_NO_SELECT_ALL = 10102,
- STATEMENT_LEADING_WILDCARD_LIKE = 10103,
- TABLE_NAMING_DISMATCH = 10201,
- COLUMN_NAMING_DISMATCH = 10202,
- INDEX_NAMING_DISMATCH = 10203,
- UK_NAMING_DISMATCH = 10204,
- FK_NAMING_DISMATCH = 10205,
- NO_REQUIRED_COLUMN = 10301,
- COLUMN_CANBE_NULL = 10302,
- NOT_INNODB_ENGINE = 10401,
- NO_PK_IN_TABLE = 10501,
+ EMPTY_POLICY = 2,
+ STATEMENT_NO_WHERE = 202,
+ STATEMENT_NO_SELECT_ALL = 203,
+ STATEMENT_LEADING_WILDCARD_LIKE = 204,
+ TABLE_NAMING_DISMATCH = 301,
+ COLUMN_NAMING_DISMATCH = 302,
+ INDEX_NAMING_DISMATCH = 303,
+ UK_NAMING_DISMATCH = 304,
+ FK_NAMING_DISMATCH = 305,
+ NO_REQUIRED_COLUMN = 401,
+ COLUMN_CANBE_NULL = 402,
+ NOT_INNODB_ENGINE = 501,
+ NO_PK_IN_TABLE = 601,
+ FK_IN_TABLE = 602,
+ TABLE_DROP_NAMING_CONVENTION = 603,
}
export enum CompatibilityErrorCode {
- COMPATIBILITY_DROP_DATABASE = 10001,
- COMPATIBILITY_RENAME_TABLE = 10002,
- COMPATIBILITY_DROP_TABLE = 10003,
- COMPATIBILITY_RENAME_COLUMN = 10004,
- COMPATIBILITY_DROP_COLUMN = 10005,
- COMPATIBILITY_ADD_PRIMARY_KEY = 10006,
- COMPATIBILITY_ADD_UNIQUE_KEY = 10007,
- COMPATIBILITY_ADD_FOREIGN_KEY = 10008,
- COMPATIBILITY_ADD_CHECK = 10009,
- COMPATIBILITY_ALTER_CHECK = 10010,
- COMPATIBILITY_ALTER_COLUMN = 10011,
+ COMPATIBILITY_DROP_DATABASE = 101,
+ COMPATIBILITY_RENAME_TABLE = 102,
+ COMPATIBILITY_DROP_TABLE = 103,
+ COMPATIBILITY_RENAME_COLUMN = 104,
+ COMPATIBILITY_DROP_COLUMN = 105,
+ COMPATIBILITY_ADD_PRIMARY_KEY = 106,
+ COMPATIBILITY_ADD_UNIQUE_KEY = 107,
+ COMPATIBILITY_ADD_FOREIGN_KEY = 108,
+ COMPATIBILITY_ADD_CHECK = 109,
+ COMPATIBILITY_ALTER_CHECK = 110,
+ COMPATIBILITY_ALTER_COLUMN = 111,
}
export type ErrorCode =
diff --git a/frontend/src/types/pipeline/task.ts b/frontend/src/types/pipeline/task.ts
index 6025fca66f..faf4fdb078 100644
--- a/frontend/src/types/pipeline/task.ts
+++ b/frontend/src/types/pipeline/task.ts
@@ -225,11 +225,14 @@ export type TaskCheckDatabaseSchemaUpdateGhostCutoverPayload = {
export type TaskCheckStatus = "SUCCESS" | "WARN" | "ERROR";
+export type TaskCheckNamespace = "bb.advisor" | "bb.core";
+
export type TaskCheckResult = {
status: TaskCheckStatus;
code: ErrorCode;
title: string;
content: string;
+ namespace: TaskCheckNamespace;
};
export type TaskCheckRunResultPayload = {
diff --git a/frontend/src/types/sqlReview.ts b/frontend/src/types/sqlReview.ts
index 2ba1c08692..f1730c312b 100644
--- a/frontend/src/types/sqlReview.ts
+++ b/frontend/src/types/sqlReview.ts
@@ -75,6 +75,7 @@ export type RuleType =
| "engine.mysql.use-innodb"
| "table.require-pk"
| "table.no-foreign-key"
+ | "table.drop-naming-convention"
| "naming.table"
| "naming.column"
| "naming.index.uk"
@@ -91,7 +92,7 @@ export type RuleType =
// Used by the backend.
interface NamingFormatPayload {
format: string;
- maxLength: number;
+ maxLength?: number;
}
// The naming format rule payload.
@@ -203,71 +204,82 @@ export const convertPolicyRuleToRuleTemplate = (
return res;
}
+ const stringComponent = ruleTemplate.componentList.find(
+ (c) => c.payload.type === "STRING"
+ );
+ const numberComponent = ruleTemplate.componentList.find(
+ (c) => c.payload.type === "NUMBER"
+ );
+ const templateComponent = ruleTemplate.componentList.find(
+ (c) => c.payload.type === "TEMPLATE"
+ );
+
switch (ruleTemplate.type) {
+ case "table.drop-naming-convention":
+ if (!stringComponent) {
+ throw new Error(`Invalid rule ${ruleTemplate.type}`);
+ }
+
+ return {
+ ...res,
+ componentList: [
+ {
+ ...stringComponent,
+ payload: {
+ ...stringComponent.payload,
+ value: (policyRule.payload as NamingFormatPayload).format,
+ } as StringPayload,
+ },
+ ],
+ };
case "naming.column":
case "naming.table":
- const stringComponent = ruleTemplate.componentList.find(
- (c) => c.payload.type === "STRING"
- );
- const numberComponent = ruleTemplate.componentList.find(
- (c) => c.payload.type === "NUMBER"
- );
if (!stringComponent || !numberComponent) {
throw new Error(`Invalid rule ${ruleTemplate.type}`);
}
- const namingRulepayload = {
- ...stringComponent.payload,
- value: (policyRule.payload as NamingFormatPayload).format,
- } as StringPayload;
- const numberPayload = {
- ...numberComponent.payload,
- value: (policyRule.payload as NamingFormatPayload).maxLength,
- } as NumberPayload;
return {
...res,
componentList: [
{
...stringComponent,
- payload: namingRulepayload,
+ payload: {
+ ...stringComponent.payload,
+ value: (policyRule.payload as NamingFormatPayload).format,
+ } as StringPayload,
},
{
...numberComponent,
- payload: numberPayload,
+ payload: {
+ ...numberComponent.payload,
+ value: (policyRule.payload as NamingFormatPayload).maxLength,
+ } as NumberPayload,
},
],
};
case "naming.index.idx":
case "naming.index.uk":
case "naming.index.fk":
- const templateComponent = ruleTemplate.componentList.find(
- (c) => c.payload.type === "TEMPLATE"
- );
- const lengthLimitComponent = ruleTemplate.componentList.find(
- (c) => c.payload.type === "NUMBER"
- );
- if (!templateComponent || !lengthLimitComponent) {
+ if (!templateComponent || !numberComponent) {
throw new Error(`Invalid rule ${ruleTemplate.type}`);
}
- const indexRulePayload = {
- ...templateComponent.payload,
- value: (policyRule.payload as NamingFormatPayload).format,
- } as TemplatePayload;
- const lengthLimitPayload = {
- ...lengthLimitComponent.payload,
- value: (policyRule.payload as NamingFormatPayload).maxLength,
- } as NumberPayload;
return {
...res,
componentList: [
{
...templateComponent,
- payload: indexRulePayload,
+ payload: {
+ ...templateComponent.payload,
+ value: (policyRule.payload as NamingFormatPayload).format,
+ } as TemplatePayload,
},
{
- ...lengthLimitComponent,
- payload: lengthLimitPayload,
+ ...numberComponent,
+ payload: {
+ ...numberComponent.payload,
+ value: (policyRule.payload as NamingFormatPayload).maxLength,
+ } as NumberPayload,
},
],
};
@@ -304,15 +316,30 @@ export const convertRuleTemplateToPolicyRule = (
return base;
}
+ const stringPayload = rule.componentList.find(
+ (c) => c.payload.type === "STRING"
+ )?.payload as StringPayload | undefined;
+ const numberPayload = rule.componentList.find(
+ (c) => c.payload.type === "NUMBER"
+ )?.payload as NumberPayload | undefined;
+ const templatePayload = rule.componentList.find(
+ (c) => c.payload.type === "TEMPLATE"
+ )?.payload as TemplatePayload | undefined;
+
switch (rule.type) {
+ case "table.drop-naming-convention":
+ if (!stringPayload) {
+ throw new Error(`Invalid rule ${rule.type}`);
+ }
+
+ return {
+ ...base,
+ payload: {
+ format: stringPayload.value ?? stringPayload.default,
+ },
+ };
case "naming.column":
case "naming.table":
- const stringPayload = rule.componentList.find(
- (c) => c.payload.type === "STRING"
- )?.payload as StringPayload | undefined;
- const numberPayload = rule.componentList.find(
- (c) => c.payload.type === "NUMBER"
- )?.payload as NumberPayload | undefined;
if (!stringPayload || !numberPayload) {
throw new Error(`Invalid rule ${rule.type}`);
}
@@ -327,21 +354,14 @@ export const convertRuleTemplateToPolicyRule = (
case "naming.index.idx":
case "naming.index.uk":
case "naming.index.fk":
- const templatePayload = rule.componentList.find(
- (c) => c.payload.type === "TEMPLATE"
- )?.payload as TemplatePayload | undefined;
- const lengthPayload = rule.componentList.find(
- (c) => c.payload.type === "NUMBER"
- )?.payload as NumberPayload | undefined;
-
- if (!templatePayload || !lengthPayload) {
+ if (!templatePayload || !numberPayload) {
throw new Error(`Invalid rule ${rule.type}`);
}
return {
...base,
payload: {
format: templatePayload.value ?? templatePayload.default,
- maxLength: lengthPayload.value ?? lengthPayload.default,
+ maxLength: numberPayload.value ?? numberPayload.default,
},
};
case "column.required":
diff --git a/frontend/src/types/sqlReviewConfig.yaml b/frontend/src/types/sqlReviewConfig.yaml
index 8f7ed0a897..650e3481a0 100644
--- a/frontend/src/types/sqlReviewConfig.yaml
+++ b/frontend/src/types/sqlReviewConfig.yaml
@@ -24,6 +24,15 @@ templateList:
engine: COMMON
level: ERROR
componentList: []
+ - type: table.drop-naming-convention
+ category: TABLE
+ engine: COMMON
+ level: ERROR
+ componentList:
+ - key: name_format
+ payload:
+ type: STRING
+ default: "_delete$"
- type: statement.select.no-select-all
category: STATEMENT
engine: COMMON
@@ -156,6 +165,15 @@ templateList:
engine: COMMON
level: WARNING
componentList: []
+ - type: table.drop-naming-convention
+ category: TABLE
+ engine: COMMON
+ level: ERROR
+ componentList:
+ - key: name_format
+ payload:
+ type: STRING
+ default: "_delete$"
- type: naming.table
category: NAMING
engine: COMMON
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.go
index 02a3269f02..80ca82f67d 100644
--- a/plugin/advisor/advisor.go
+++ b/plugin/advisor/advisor.go
@@ -90,6 +90,9 @@ const (
// MySQLTableNoFK is an advisor type for MySQL table disallow foreign key.
MySQLTableNoFK Type = "bb.plugin.advisor.mysql.table.no-foreign-key"
+ // MySQLTableDropNamingConvention is an advisor type for MySQL table drop with naming convention.
+ MySQLTableDropNamingConvention Type = "bb.plugin.advisor.mysql.table.drop-naming-convention"
+
// PostgreSQL Advisor
// PostgreSQLSyntax is an advisor type for PostgreSQL syntax.
diff --git a/plugin/advisor/code.go b/plugin/advisor/code.go
index 79946335ef..12218ad0ea 100644
--- a/plugin/advisor/code.go
+++ b/plugin/advisor/code.go
@@ -48,8 +48,9 @@ const (
NotInnoDBEngine Code = 501
// 601 ~ 699 table rule advisor error code
- TableNoPK Code = 601
- TableHasFK Code = 602
+ TableNoPK Code = 601
+ TableHasFK Code = 602
+ TableDropNamingConventionMismatch Code = 603
)
// Int returns the int type of code.
diff --git a/plugin/advisor/mysql/advisor_table_drop_naming_convention.go b/plugin/advisor/mysql/advisor_table_drop_naming_convention.go
new file mode 100644
index 0000000000..bdd8fd0872
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_table_drop_naming_convention.go
@@ -0,0 +1,88 @@
+package mysql
+
+import (
+ "fmt"
+ "regexp"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/pingcap/tidb/parser/ast"
+)
+
+var (
+ _ advisor.Advisor = (*TableDropNamingConventionAdvisor)(nil)
+)
+
+func init() {
+ advisor.Register(advisor.MySQL, advisor.MySQLTableDropNamingConvention, &TableDropNamingConventionAdvisor{})
+ advisor.Register(advisor.TiDB, advisor.MySQLTableDropNamingConvention, &TableDropNamingConventionAdvisor{})
+}
+
+// TableDropNamingConventionAdvisor is the advisor checking the MySQLTableDropNamingConvention rule.
+type TableDropNamingConventionAdvisor struct {
+}
+
+// Check checks for drop table naming convention.
+func (adv *TableDropNamingConventionAdvisor) Check(ctx advisor.Context, statement string) ([]advisor.Advice, error) {
+ root, errAdvice := parseStatement(statement, ctx.Charset, ctx.Collation)
+ if errAdvice != nil {
+ return errAdvice, nil
+ }
+
+ level, err := advisor.NewStatusBySQLReviewRuleLevel(ctx.Rule.Level)
+ if err != nil {
+ return nil, err
+ }
+ format, _, err := advisor.UnamrshalNamingRulePayloadAsRegexp(ctx.Rule.Payload)
+ if err != nil {
+ return nil, err
+ }
+
+ checker := &namingDropTableConventionChecker{
+ level: level,
+ title: string(ctx.Rule.Type),
+ format: format,
+ }
+ for _, stmtNode := range root {
+ (stmtNode).Accept(checker)
+ }
+
+ if len(checker.adviceList) == 0 {
+ checker.adviceList = append(checker.adviceList, advisor.Advice{
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ })
+ }
+ return checker.adviceList, nil
+}
+
+type namingDropTableConventionChecker struct {
+ adviceList []advisor.Advice
+ level advisor.Status
+ title string
+ format *regexp.Regexp
+}
+
+// Enter implements the ast.Visitor interface
+func (v *namingDropTableConventionChecker) Enter(in ast.Node) (ast.Node, bool) {
+ if node, ok := in.(*ast.DropTableStmt); ok {
+ for _, table := range node.Tables {
+ if !v.format.MatchString(table.Name.O) {
+ v.adviceList = append(v.adviceList, advisor.Advice{
+ Status: v.level,
+ Code: advisor.TableDropNamingConventionMismatch,
+ Title: v.title,
+ Content: fmt.Sprintf("`%s` mismatches drop table naming convention, naming format should be %q", table.Name.O, v.format),
+ })
+ }
+ }
+ }
+
+ return in, false
+}
+
+// Leave implements the ast.Visitor interface
+func (v *namingDropTableConventionChecker) Leave(in ast.Node) (ast.Node, bool) {
+ return in, true
+}
diff --git a/plugin/advisor/mysql/advisor_table_drop_naming_convention_test.go b/plugin/advisor/mysql/advisor_table_drop_naming_convention_test.go
new file mode 100644
index 0000000000..0b4e69a12a
--- /dev/null
+++ b/plugin/advisor/mysql/advisor_table_drop_naming_convention_test.go
@@ -0,0 +1,57 @@
+package mysql
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/bytebase/bytebase/plugin/advisor"
+ "github.com/stretchr/testify/require"
+)
+
+func TestMySQLTableDropNamingConvention(t *testing.T) {
+ tests := []advisor.TestCase{
+ {
+ Statement: "DROP TABLE IF EXISTS foo_delete",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Success,
+ Code: advisor.Ok,
+ Title: "OK",
+ Content: "",
+ },
+ },
+ },
+ {
+ Statement: "DROP TABLE IF EXISTS foo",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: advisor.TableDropNamingConventionMismatch,
+ Title: "table.drop-naming-convention",
+ Content: "`foo` mismatches drop table naming convention, naming format should be \"_delete$\"",
+ },
+ },
+ },
+ {
+ Statement: "DROP TABLE IF EXISTS foo_delete, bar",
+ Want: []advisor.Advice{
+ {
+ Status: advisor.Error,
+ Code: advisor.TableDropNamingConventionMismatch,
+ Title: "table.drop-naming-convention",
+ Content: "`bar` mismatches drop table naming convention, naming format should be \"_delete$\"",
+ },
+ },
+ },
+ }
+
+ payload, err := json.Marshal(advisor.NamingRulePayload{
+ Format: "_delete$",
+ })
+ require.NoError(t, err)
+ advisor.RunSchemaReviewRuleTests(t, tests, &TableDropNamingConventionAdvisor{}, &advisor.SQLReviewRule{
+ Type: advisor.SchemaRuleTableDropNamingConvention,
+ Level: advisor.SchemaRuleLevelError,
+ Payload: string(payload),
+ }, &advisor.MockCatalogService{})
+}
diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.go
index 555d0f7991..33f13cfc1d 100644
--- a/plugin/advisor/sql_review.go
+++ b/plugin/advisor/sql_review.go
@@ -55,6 +55,8 @@ const (
SchemaRuleTableRequirePK SQLReviewRuleType = "table.require-pk"
// SchemaRuleTableNoFK require the table disallow the foreign key.
SchemaRuleTableNoFK SQLReviewRuleType = "table.no-foreign-key"
+ // SchemaRuleTableDropNamingConvention require only the table following the naming convention can be deleted.
+ SchemaRuleTableDropNamingConvention SQLReviewRuleType = "table.drop-naming-convention"
// SchemaRuleRequiredColumn enforce the required columns in each table.
SchemaRuleRequiredColumn SQLReviewRuleType = "column.required"
@@ -367,6 +369,11 @@ func getAdvisorTypeByRule(ruleType SQLReviewRuleType, engine DBType) (Type, erro
case MySQL, TiDB:
return MySQLTableNoFK, nil
}
+ case SchemaRuleTableDropNamingConvention:
+ switch engine {
+ case MySQL, TiDB:
+ return MySQLTableDropNamingConvention, nil
+ }
case SchemaRuleMySQLEngine:
if engine == MySQL {
return MySQLUseInnoDB, nil
diff --git a/tests/sql_review_test.go b/tests/sql_review_test.go
index 12a7edbd2b..96994dbbdd 100644
--- a/tests/sql_review_test.go
+++ b/tests/sql_review_test.go
@@ -231,6 +231,13 @@ func TestSQLReview(t *testing.T) {
{
statement: "DROP TABLE user",
result: []api.TaskCheckResult{
+ {
+ Status: api.TaskCheckStatusError,
+ Namespace: api.AdvisorNamespace,
+ Code: advisor.TableDropNamingConventionMismatch.Int(),
+ Title: "table.drop-naming-convention",
+ Content: "`user` mismatches drop table naming convention, naming format should be \"_delete$\"",
+ },
{
Status: api.TaskCheckStatusWarn,
Namespace: api.AdvisorNamespace,
diff --git a/tests/tests.go b/tests/tests.go
index 65db606a13..5ec329074a 100644
--- a/tests/tests.go
+++ b/tests/tests.go
@@ -1432,6 +1432,10 @@ func setDefaultSchemaReviewRulePayload(ruleTp advisor.SQLReviewRuleType) (string
case advisor.SchemaRuleTableNoFK:
case advisor.SchemaRuleColumnNotNull:
case advisor.SchemaRuleSchemaBackwardCompatibility:
+ case advisor.SchemaRuleTableDropNamingConvention:
+ payload, err = json.Marshal(advisor.NamingRulePayload{
+ Format: "_delete$",
+ })
case advisor.SchemaRuleTableNaming:
fallthrough
case advisor.SchemaRuleColumnNaming:
@@ -1519,6 +1523,10 @@ func prodTemplateSchemaReviewPolicy() (string, error) {
Type: advisor.SchemaRuleTableNoFK,
Level: advisor.SchemaRuleLevelError,
},
+ {
+ Type: advisor.SchemaRuleTableDropNamingConvention,
+ Level: advisor.SchemaRuleLevelError,
+ },
{
Type: advisor.SchemaRuleRequiredColumn,
Level: advisor.SchemaRuleLevelWarning,