Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: make builtin SQL review rule level configurable (#19214)
p0ny8f2bef904a57
diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex 2b2f72e483..4c9b9e8182 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -103,8 +103,16 @@ func SQLReviewCheck(builtinOnly := len(ruleList) == 0if !checkContext.NoAppendBuiltin {- // Append builtin rules to the rule list.- ruleList = append(ruleList, GetBuiltinRules(checkContext.DBType)...)+ // Append builtin rules only if the user hasn't overridden them in their config.+ userRuleTypes := make(map[storepb.SQLReviewRule_Type]bool, len(ruleList))+ for _, r := range ruleList {+ userRuleTypes[r.Type] = true+ }+ for _, r := range GetBuiltinRules(checkContext.DBType) {+ if !userRuleTypes[r.Type] {+ ruleList = append(ruleList, r)+ }+ }}if asts == nil || len(ruleList) == 0 {diff --git a/backend/plugin/advisor/template_test.go b/backend/plugin/advisor/template_test.goindex a65bfe372c..9006c311a2 100644--- a/backend/plugin/advisor/template_test.go+++ b/backend/plugin/advisor/template_test.go@@ -13,8 +13,6 @@ import ("github.com/pkg/errors""github.com/stretchr/testify/require""gopkg.in/yaml.v3"-- storepb "github.com/bytebase/bytebase/backend/generated-go/store")// TestTemplateAdvisorRegistrations validates that all SQL review rules in frontend templates@@ -371,6 +369,6 @@ func buildRuleTypeMapping() map[string]string {"SYSTEM_FUNCTION_DISALLOW_CREATE": "storepb.SQLReviewRule_SYSTEM_FUNCTION_DISALLOW_CREATE","SYSTEM_FUNCTION_DISALLOWED_LIST": "storepb.SQLReviewRule_SYSTEM_FUNCTION_DISALLOWED_LIST","ADVICE_ONLINE_MIGRATION": "storepb.SQLReviewRule_ADVICE_ONLINE_MIGRATION",- "BUILTIN_PRIOR_BACKUP_CHECK": storepb.SQLReviewRule_BUILTIN_PRIOR_BACKUP_CHECK.String(),+ "BUILTIN_PRIOR_BACKUP_CHECK": "storepb.SQLReviewRule_BUILTIN_PRIOR_BACKUP_CHECK",}}diff --git a/frontend/src/components/SQLReview/SQLReviewCreation.vue b/frontend/src/components/SQLReview/SQLReviewCreation.vueindex a1b1479266..8dba297af2 100644--- a/frontend/src/components/SQLReview/SQLReviewCreation.vue+++ b/frontend/src/components/SQLReview/SQLReviewCreation.vue@@ -66,6 +66,8 @@ import {TEMPLATE_LIST_V2 as builtInTemplateList,convertRuleMapToPolicyRuleList,getRuleMapByEngine,+ isBuiltinRule,+ withBuiltinRules,} from "@/types";import { Engine } from "@/types/proto-es/v1/common_pb";import { SQLReviewRule_Type } from "@/types/proto-es/v1/review_config_service_pb";@@ -128,7 +130,9 @@ const state = reactive<LocalState>({name: props.name || t("sql-review.create.basic-info.display-name-default"),resourceId: props.policy ? getReviewConfigId(props.policy.id) : "",attachedResources: props.selectedResources,- selectedRuleMapByEngine: getRuleMapByEngine(props.selectedRuleList),+ selectedRuleMapByEngine: withBuiltinRules(+ getRuleMapByEngine(props.selectedRuleList)+ ),selectedTemplateId: props.policy? getTemplateId(props.policy): builtInTemplateList[0]?.id,@@ -145,7 +149,9 @@ const onTemplateApply = (template: SQLReviewPolicyTemplateV2 | undefined) => {state.selectedTemplateId = template.id;state.pendingApplyTemplate = undefined;- state.selectedRuleMapByEngine = getRuleMapByEngine(template.ruleList);+ state.selectedRuleMapByEngine = withBuiltinRules(+ getRuleMapByEngine(template.ruleList)+ );};const onCancel = (newPolicy: SQLReviewPolicy | undefined = undefined) => {@@ -257,6 +263,9 @@ const tryApplyTemplate = (template: SQLReviewPolicyTemplateV2) => {};const removeRole = (rule: RuleTemplateV2) => {+ if (isBuiltinRule(rule)) {+ return;+ }state.selectedRuleMapByEngine.get(rule.engine)?.delete(rule.type);if (state.selectedRuleMapByEngine.get(rule.engine)?.size === 0) {state.selectedRuleMapByEngine.delete(rule.engine);diff --git a/frontend/src/components/SQLReview/components/SQLReviewRulesSelectPanel.vue b/frontend/src/components/SQLReview/components/SQLReviewRulesSelectPanel.vueindex 3fa3e36bee..b8020ffd59 100644--- a/frontend/src/components/SQLReview/components/SQLReviewRulesSelectPanel.vue+++ b/frontend/src/components/SQLReview/components/SQLReviewRulesSelectPanel.vue@@ -48,7 +48,7 @@ import { Drawer, DrawerContent } from "@/components/v2";import { type Engine } from "@/types/proto-es/v1/common_pb";import type { SQLReviewRule_Type } from "@/types/proto-es/v1/review_config_service_pb";import type { RuleTemplateV2 } from "@/types/sqlReview";-import { ruleTemplateMapV2 } from "@/types/sqlReview";+import { isBuiltinRule, ruleTemplateMapV2 } from "@/types/sqlReview";import SQLReviewTabsByEngine from "./SQLReviewTabsByEngine.vue";import SQLRuleTableWithFilter from "./SQLRuleTableWithFilter.vue";import { getRuleKey } from "./utils";@@ -99,6 +99,10 @@ const onSelectedRuleKeysUpdateForEngine = (engine: Engine, keys: string[]) => {const [engineStr, ruleKey] = oldKey.split(":");const engineNum = parseInt(engineStr) as Engine;const ruleType = parseInt(ruleKey) as SQLReviewRule_Type;+ const template = ruleTemplateMapV2.get(engineNum)?.get(ruleType);+ if (template && isBuiltinRule(template)) {+ continue;+ }const rule = props.selectedRuleMap.get(engineNum)?.get(ruleType);if (rule) {emit("rule-remove", rule);diff --git a/frontend/src/components/SQLReview/components/SQLRuleTable.vue b/frontend/src/components/SQLReview/components/SQLRuleTable.vueindex 90f39360c3..d67448c1c0 100644--- a/frontend/src/components/SQLReview/components/SQLRuleTable.vue+++ b/frontend/src/components/SQLReview/components/SQLRuleTable.vue@@ -61,7 +61,7 @@@click="setActiveRule(rule)"/><NButton- v-if="editable"+ v-if="editable && !isBuiltinRule(rule)"secondarytype="error"size="small"@@ -102,7 +102,7 @@ import { NButton, NCheckbox, NDataTable, NDivider } from "naive-ui";import { computed, reactive } from "vue";import { useI18n } from "vue-i18n";import type { RuleTemplateV2 } from "@/types";-import { getRuleLocalization, ruleTypeToString } from "@/types";+import { getRuleLocalization, isBuiltinRule, ruleTypeToString } from "@/types";import { SQLReviewRule_Level } from "@/types/proto-es/v1/review_config_service_pb";import RuleConfig from "./RuleConfigComponents/RuleConfig.vue";import RuleLevelSwitch from "./RuleLevelSwitch.vue";@@ -248,7 +248,7 @@ const columns = computed(() => {<NButton onClick={() => setActiveRule(rule)}>{props.editable ? t("common.edit") : t("common.view")}</NButton>- {props.editable && (+ {props.editable && !isBuiltinRule(rule) && (<NButtonsecondarytype="error"diff --git a/frontend/src/types/sql-review-schema.yaml b/frontend/src/types/sql-review-schema.yamlindex 325c50a1ca..b974b9e705 100644--- a/frontend/src/types/sql-review-schema.yaml+++ b/frontend/src/types/sql-review-schema.yaml@@ -2012,3 +2012,18 @@- type: STATEMENT_OBJECT_OWNER_CHECKcategory: STATEMENTengine: POSTGRES+- type: BUILTIN_PRIOR_BACKUP_CHECK+ category: BUILTIN+ engine: MYSQL+- type: BUILTIN_PRIOR_BACKUP_CHECK+ category: BUILTIN+ engine: POSTGRES+- type: BUILTIN_PRIOR_BACKUP_CHECK+ category: BUILTIN+ engine: TIDB+- type: BUILTIN_PRIOR_BACKUP_CHECK+ category: BUILTIN+ engine: MSSQL+- type: BUILTIN_PRIOR_BACKUP_CHECK+ category: BUILTIN+ engine: ORACLEdiff --git a/frontend/src/types/sqlReview.ts b/frontend/src/types/sqlReview.tsindex 6aa2eb2628..4f5031e93b 100644--- a/frontend/src/types/sqlReview.ts+++ b/frontend/src/types/sqlReview.ts@@ -119,6 +119,33 @@ export const ruleTypeToString = (type: SQLReviewRule_Type): string => {return SQLReviewRule_Type[type] as string;};+const BUILTIN_CATEGORY = "BUILTIN";++export const isBuiltinRule = (rule: RuleTemplateV2): boolean => {+ return rule.category === BUILTIN_CATEGORY;+};++// Returns a new rule map with builtin rules injected for engines already present.+// Builtin rules are always active and cannot be removed by users.+export const withBuiltinRules = (+ ruleMap: Map<Engine, Map<SQLReviewRule_Type, RuleTemplateV2>>+): Map<Engine, Map<SQLReviewRule_Type, RuleTemplateV2>> => {+ const result = new Map<Engine, Map<SQLReviewRule_Type, RuleTemplateV2>>();+ for (const [engine, engineMap] of ruleMap) {+ const copy = new Map(engineMap);+ const engineTemplates = ruleTemplateMapV2.get(engine);+ if (engineTemplates) {+ for (const [type, template] of engineTemplates) {+ if (template.category === BUILTIN_CATEGORY && !copy.has(type)) {+ copy.set(type, { ...template });+ }+ }+ }+ result.set(engine, copy);+ }+ return result;+};+export const getRuleMapByEngine = (ruleList: RuleTemplateV2[]): Map<Engine, Map<SQLReviewRule_Type, RuleTemplateV2>> => {diff --git a/frontend/src/views/SettingWorkspaceSQLReviewDetail.vue b/frontend/src/views/SettingWorkspaceSQLReviewDetail.vueindex 5ae0b64086..4f5bef0e20 100644--- a/frontend/src/views/SettingWorkspaceSQLReviewDetail.vue+++ b/frontend/src/views/SettingWorkspaceSQLReviewDetail.vue@@ -197,7 +197,9 @@ import type { RuleTemplateV2 } from "@/types";import {convertRuleMapToPolicyRuleList,getRuleMapByEngine,+ isBuiltinRule,UNKNOWN_ID,+ withBuiltinRules,} from "@/types";import type { Engine } from "@/types/proto-es/v1/common_pb";import { SQLReviewRule_Type } from "@/types/proto-es/v1/review_config_service_pb";@@ -288,7 +290,7 @@ const ruleListOfPolicy = computed((): RuleTemplateV2[] => {watch(ruleListOfPolicy,(ruleList) => {- state.ruleMapByEngine = getRuleMapByEngine(ruleList);+ state.ruleMapByEngine = withBuiltinRules(getRuleMapByEngine(ruleList));},{ immediate: true });@@ -331,6 +333,9 @@ const markChange = (};const removeRule = (rule: RuleTemplateV2) => {+ if (isBuiltinRule(rule)) {+ return;+ }state.ruleMapByEngine.get(rule.engine)?.delete(rule.type);if (state.ruleMapByEngine.get(rule.engine)?.size === 0) {state.ruleMapByEngine.delete(rule.engine);@@ -339,7 +344,9 @@ const removeRule = (rule: RuleTemplateV2) => {};const onCancelChanges = () => {- state.ruleMapByEngine = getRuleMapByEngine(ruleListOfPolicy.value);+ state.ruleMapByEngine = withBuiltinRules(+ getRuleMapByEngine(ruleListOfPolicy.value)+ );state.rulesUpdated = false;};