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: support run sql checks when previewing issue (#8367)
Liu Ji2cd75079697f
diff --git a/frontend/src/components/IssueV1/IssueDetailPage.vue b/frontend/src/components/IssueV1/IssueDetailPage.vueindex 87f2965854..39064e910e 100644--- a/frontend/src/components/IssueV1/IssueDetailPage.vue+++ b/frontend/src/components/IssueV1/IssueDetailPage.vue@@ -18,6 +18,7 @@<div class="w-full border-t my-4" />+ <SQLCheckSection v-if="isCreating" /><PlanCheckSection v-if="!isCreating" /><StatementSection />diff --git a/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckBadge.vue b/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckBadge.vuenew file mode 100644index 0000000000..d89c526043--- /dev/null+++ b/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckBadge.vue@@ -0,0 +1,108 @@+<template>+ <button+ class="inline-flex items-center px-3 py-0.5 rounded-full text-sm border border-control-border"+ :class="buttonClasses"+ @click="handleClick"+ >+ <template v-if="status === 'RUNNING'">+ <TaskSpinner class="-ml-1 mr-1.5 h-4 w-4 text-info" />+ </template>+ <template v-else-if="status === 'SUCCESS'">+ <heroicons-outline:check+ class="-ml-1 mr-1.5 mt-0.5 h-4 w-4 text-success"+ />+ </template>+ <template v-else-if="status === 'WARNING'">+ <heroicons-outline:exclamation+ class="-ml-1 mr-1.5 mt-0.5 h-4 w-4 text-warning"+ />+ </template>+ <template v-else-if="status === 'ERROR'">+ <span class="mr-1.5 font-medium text-error" aria-hidden="true"> ! </span>+ </template>++ <span>{{ $t("task.check-type.sql-review") }}</span>++ <SQLCheckPanel+ v-if="showDetailPanel"+ :advices="advices"+ @close="showDetailPanel = false"+ />+ </button>+</template>++<script setup lang="ts">+import { computed, ref } from "vue";+import { Advice, Advice_Status } from "@/types/proto/v1/sql_service";+import SQLCheckPanel from "./SQLCheckPanel.vue";++const props = defineProps<{+ isRunning: boolean;+ advices: Advice[];+}>();++defineEmits<{+ (event: "click"): void;+}>();++const showDetailPanel = ref(false);++const status = computed(() => {+ const { isRunning, advices } = props;+ if (isRunning) {+ return "RUNNING";+ }+ if (advices.some((adv) => adv.status === Advice_Status.ERROR)) {+ return "ERROR";+ }+ if (advices.some((adv) => adv.status === Advice_Status.WARNING)) {+ return "WARNING";+ }+ return "SUCCESS";+});++const clickable = computed(() => {+ return status.value === "ERROR" || status.value === "WARNING";+});++const buttonClasses = computed(() => {+ let bgColor = "";+ let bgHoverColor = "";+ let textColor = "";+ switch (status.value) {+ case "RUNNING":+ bgColor = "bg-blue-100";+ bgHoverColor = "bg-blue-300";+ textColor = "text-blue-800";+ break;+ case "SUCCESS":+ bgColor = "bg-gray-100";+ bgHoverColor = "bg-gray-300";+ textColor = "text-gray-800";+ break;+ case "WARNING":+ bgColor = "bg-yellow-100";+ bgHoverColor = "bg-yellow-300";+ textColor = "text-yellow-800";+ break;+ case "ERROR":+ bgColor = "bg-red-100";+ bgHoverColor = "bg-red-300";+ textColor = "text-red-800";+ break;+ }++ const styleList: string[] = [textColor, bgColor];+ if (clickable.value) {+ styleList.push("cursor-pointer", `hover:${bgHoverColor}`);+ } else {+ styleList.push("cursor-default");+ }++ return styleList.join(" ");+});++const handleClick = () => {+ showDetailPanel.value = true;+};+</script>diff --git a/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckDetail.vue b/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckDetail.vuenew file mode 100644index 0000000000..086353af8d--- /dev/null+++ b/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckDetail.vue@@ -0,0 +1,264 @@+<template>+ <BBGrid+ :column-list="COLUMN_LIST"+ :data-source="tableRows"+ :show-header="false"+ :row-clickable="false"+ class="border"+ >+ <template #item="{ item: row }: BBGridRow<TableRow>">+ <div class="bb-grid-cell">+ <div+ class="relative w-5 h-5 flex flex-shrink-0 items-center justify-center rounded-full select-none"+ :class="statusIconClass(row.advice.status)"+ >+ <template v-if="row.advice.status === Advice_Status.SUCCESS">+ <heroicons-solid:check class="w-4 h-4" />+ </template>+ <template v-if="row.advice.status === Advice_Status.WARNING">+ <heroicons-outline:exclamation class="h-4 w-4" />+ </template>+ <template v-else-if="row.advice.status === Advice_Status.ERROR">+ <span class="text-white font-medium text-base" aria-hidden="true"+ >!</span+ >+ </template>+ </div>+ </div>+ <div v-if="showCategoryColumn" class="bb-grid-cell">+ {{ row.category }}+ </div>+ <div class="bb-grid-cell">+ {{ row.title }}+ </div>+ <div class="bb-grid-cell">+ <div>+ <span>{{ row.advice.content }}</span>+ <template v-if="getActiveRule(row.advice.title as RuleType)">+ <span+ class="ml-1 normal-link"+ @click="setActiveRule(row.advice.title as RuleType)"+ >{{ $t("sql-review.rule-detail") }}</span+ >+ <span class="border-r border-control-border ml-1"></span>+ </template>++ <a+ v-if="row.link"+ class="ml-1 normal-link"+ :href="row.link.url"+ :target="row.link.target"+ >+ {{ row.link.title }}+ </a>+ </div>+ </div>+ </template>+ </BBGrid>++ <SQLRuleEditDialog+ v-if="state.activeRule"+ :editable="false"+ :rule="state.activeRule.rule"+ :payload="state.activeRule.payload"+ :disabled="false"+ @cancel="state.activeRule = undefined"+ />+</template>++<script setup lang="ts">+import { computed, reactive } from "vue";+import { useI18n } from "vue-i18n";+import { BBGridColumn, BBGridRow, BBGrid } from "@/bbkit";+import { LocalizedSQLRuleErrorCodes } from "@/components/Issue/const";+import { databaseForTask, useIssueContext } from "@/components/IssueV1/logic";+import { SQLRuleEditDialog } from "@/components/SQLReview/components";+import { PayloadValueType } from "@/components/SQLReview/components/RuleConfigComponents";+import { useReviewPolicyByEnvironmentId } from "@/store";+import {+ GeneralErrorCode,+ RuleTemplate,+ RuleType,+ SQLReviewPolicyErrorCode,+ findRuleTemplate,+ getRuleLocalization,+ ruleTemplateMap,+} from "@/types";+import { Advice, Advice_Status } from "@/types/proto/v1/sql_service";++// import PlanCheckResultDefinitionModal from "./PlanCheckResultDefinitionModal.vue";++interface ErrorCodeLink {+ title: string;+ target: string;+ url: string;+}++type PreviewSQLReviewRule = {+ rule: RuleTemplate;+ payload: PayloadValueType[];+};++type TableRow = {+ advice: Advice;+ category: string;+ title: string;+ link: ErrorCodeLink | undefined;+};++type LocalState = {+ activeRule?: PreviewSQLReviewRule;+ activeResultDefinition?: string;+};++const props = defineProps<{+ advices: Advice[];+}>();++const { t } = useI18n();+const state = reactive<LocalState>({+ activeRule: undefined,+ activeResultDefinition: undefined,+});+const { issue, selectedTask } = useIssueContext();++const statusIconClass = (status: Advice_Status) => {+ switch (status) {+ case Advice_Status.SUCCESS:+ return "bg-success text-white";+ case Advice_Status.WARNING:+ return "bg-warning text-white";+ case Advice_Status.ERROR:+ return "bg-error text-white";+ }+};++const categoryAndTitle = (advice: Advice): [string, string] => {+ const code = advice.code;+ if (code === SQLReviewPolicyErrorCode.EMPTY_POLICY) {+ const title = messageWithCode(advice.title, code);+ return ["", title];+ }+ if (LocalizedSQLRuleErrorCodes.has(code)) {+ const rule = ruleTemplateMap.get(advice.title as RuleType);+ if (rule) {+ const ruleLocalization = getRuleLocalization(rule.type);+ const key = `sql-review.category.${rule.category.toLowerCase()}`;+ const category = t(key);+ const title = messageWithCode(ruleLocalization.title, code);+ return [category, title];+ } else {+ return ["", messageWithCode(advice.title, code)];+ }+ }+ return ["", advice.title];+};++const messageWithCode = (message: string, code: number) => {+ return `${message} #${code}`;+};++const errorCodeLink = (advice: Advice): ErrorCodeLink | undefined => {+ const code = advice.code;+ switch (code) {+ case undefined:+ return;+ case GeneralErrorCode.OK:+ return;+ case SQLReviewPolicyErrorCode.EMPTY_POLICY:+ return {+ title: t("sql-review.configure-policy"),+ target: "_self",+ url: "/setting/sql-review",+ };+ default: {+ const errorCodeNamespace = "advisor";+ const domain = "https://www.bytebase.com";+ const path = `/docs/reference/error-code/${errorCodeNamespace}/`;+ const query = `source=console#${code}`;+ const url = `${domain}${path}?${query}`;+ return {+ title: t("common.view-doc"),+ target: "__blank",+ url: url,+ };+ }+ }+};++const tableRows = computed(() => {+ return props.advices.map<TableRow>((advice) => {+ const [category, title] = categoryAndTitle(advice);+ const link = errorCodeLink(advice);+ return {+ advice,+ category,+ title,+ link,+ };+ });+});++const showCategoryColumn = computed((): boolean =>+ tableRows.value.some((row) => row.category !== "")+);++const COLUMN_LIST = computed(() => {+ const STATUS: BBGridColumn = {+ title: "Status",+ width: "auto",+ };+ const CATEGORY: BBGridColumn = {+ title: "Category",+ width: "minmax(4rem, 6rem)",+ };+ const TITLE: BBGridColumn = {+ title: "Title",+ width: "minmax(12rem, 1fr)",+ };+ const CONTENT: BBGridColumn = {+ title: "Detail",+ width: "2fr",+ };+ if (showCategoryColumn.value) {+ return [STATUS, CATEGORY, TITLE, CONTENT];+ }+ return [STATUS, TITLE, CONTENT];+});++const reviewPolicy = useReviewPolicyByEnvironmentId(+ computed(() => {+ const task = selectedTask.value;+ const database = databaseForTask(issue.value, task);+ return database.effectiveEnvironmentEntity.uid;+ })+);+const getActiveRule = (type: RuleType): PreviewSQLReviewRule | undefined => {+ const rule = reviewPolicy.value?.ruleList.find((rule) => rule.type === type);+ if (!rule) {+ return undefined;+ }++ const ruleTemplate = findRuleTemplate(type);+ if (!ruleTemplate) {+ return undefined;+ }+ ruleTemplate.comment = rule.comment;+ const { componentList } = ruleTemplate;+ const payload = componentList.reduce<PayloadValueType[]>(+ (list, component) => {+ list.push(component.payload.value ?? component.payload.default);+ return list;+ },+ []+ );++ return {+ rule: ruleTemplate,+ payload: payload,+ };+};+const setActiveRule = (type: RuleType) => {+ state.activeRule = getActiveRule(type);+};+</script>diff --git a/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckPanel.vue b/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckPanel.vuenew file mode 100644index 0000000000..4e09b7e8a6--- /dev/null+++ b/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckPanel.vue@@ -0,0 +1,24 @@+<template>+ <BBModal+ :title="$t('task.check-result.title-general')"+ class="!w-[56rem]"+ header-class="whitespace-pre-wrap break-all gap-x-1"+ container-class="!pt-0 -mt-px"+ @close="$emit('close')"+ >+ <SQLCheckDetail :advices="advices" />+ </BBModal>+</template>++<script setup lang="ts">+import { Advice } from "@/types/proto/v1/sql_service";+import SQLCheckDetail from "./SQLCheckDetail.vue";++defineProps<{+ advices: Advice[];+}>();++defineEmits<{+ (event: "close"): void;+}>();+</script>diff --git a/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckSection.vue b/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckSection.vuenew file mode 100644index 0000000000..d1ae4487a3--- /dev/null+++ b/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckSection.vue@@ -0,0 +1,54 @@+<template>+ <div v-if="show" class="flex items-center gap-x-4 px-4 py-2">+ <div class="textlabel h-[26px] inline-flex items-center">+ {{ $t("issue.sql-check.sql-checks") }}+ </div>++ <SQLCheckButton+ v-if="database"+ :statement="sheetStatement"+ :database="database"+ :button-props="{+ size: 'tiny',+ }"+ button-style="--n-padding: 0 8px 0 6px; --n-icon-margin: 3px;"+ class="justify-between flex-1"+ >+ <template #result="{ advices, isRunning }">+ <template v-if="advices === undefined">+ <span class="textinfolabel">+ {{ $t("issue.sql-check.not-executed-yet") }}+ </span>+ </template>+ <SQLCheckBadge v-else :is-running="isRunning" :advices="advices" />+ </template>+ </SQLCheckButton>+ </div>+</template>++<script lang="ts" setup>+import { computed } from "vue";+import { useTaskSheet } from "@/components/IssueV1/components/StatementSection/useTaskSheet";+import { useIssueContext, databaseForTask } from "@/components/IssueV1/logic";+import { SQLCheckButton } from "@/components/SQLCheck";+import { TaskTypeListWithStatement } from "@/types";+import { Task_Type } from "@/types/proto/v1/rollout_service";+import SQLCheckBadge from "./SQLCheckBadge.vue";++const { issue, selectedTask } = useIssueContext();+const { sheetStatement } = useTaskSheet();+const database = computed(() => {+ return databaseForTask(issue.value, selectedTask.value);+});++const show = computed(() => {+ const type = selectedTask.value.type;+ if (type === Task_Type.DATABASE_SCHEMA_BASELINE) {+ return false;+ }+ if (TaskTypeListWithStatement.includes(type)) {+ return true;+ }+ return false;+});+</script>diff --git a/frontend/src/components/IssueV1/components/SQLCheckSection/index.ts b/frontend/src/components/IssueV1/components/SQLCheckSection/index.tsnew file mode 100644index 0000000000..2028b2b13c--- /dev/null+++ b/frontend/src/components/IssueV1/components/SQLCheckSection/index.ts@@ -0,0 +1,3 @@+import SQLCheckSection from "./SQLCheckSection.vue";++export default SQLCheckSection;diff --git a/frontend/src/components/SQLCheck/SQLCheckButton.vue b/frontend/src/components/SQLCheck/SQLCheckButton.vuenew file mode 100644index 0000000000..6318c9bf94--- /dev/null+++ b/frontend/src/components/SQLCheck/SQLCheckButton.vue@@ -0,0 +1,69 @@+<template>+ <div class="flex flex-row items-center gap-2">+ <slot name="result" :advices="advices" :is-running="isRunning">+ <div class="flex flex-col text-xs">+ <span>statement: {{ statement }}</span>+ <pre v-for="(advice, i) in advices" :key="i">{{+ Advice.toJSON(advice)+ }}</pre>+ </div>+ </slot>++ <NButton+ style="--n-padding: 0 14px 0 10px"+ :disabled="statement.trim().length === 0"+ :style="buttonStyle"+ v-bind="buttonProps"+ @click="runChecks"+ >+ <template #icon>+ <BBSpin v-if="isRunning" class="w-4 h-4" />+ <heroicons-outline:play v-else class="w-4 h-4" />+ </template>+ <template #default>+ <template v-if="isRunning">+ {{ $t("task.checking") }}+ </template>+ <template v-else>+ {{ $t("task.run-checks") }}+ </template>+ </template>+ </NButton>+ </div>+</template>++<script lang="ts" setup>+import { ButtonProps, NButton } from "naive-ui";+import { ref } from "vue";+import { CSSProperties } from "vue";+import { sqlServiceClient } from "@/grpcweb";+import { ComposedDatabase } from "@/types";+import { Advice } from "@/types/proto/v1/sql_service";++const props = defineProps<{+ statement: string;+ database: ComposedDatabase;+ buttonProps?: ButtonProps;+ buttonStyle?: string | CSSProperties;+}>();++const isRunning = ref(false);+const advices = ref<Advice[]>();++const runChecks = async () => {+ const { statement, database } = props;+ isRunning.value = true;+ if (!advices.value) {+ advices.value = [];+ }+ try {+ const result = await sqlServiceClient.check({+ statement,+ database: database.name,+ });+ advices.value = result.advices;+ } finally {+ isRunning.value = false;+ }+};+</script>diff --git a/frontend/src/components/SQLCheck/index.ts b/frontend/src/components/SQLCheck/index.tsnew file mode 100644index 0000000000..0daadef86c--- /dev/null+++ b/frontend/src/components/SQLCheck/index.ts@@ -0,0 +1,3 @@+import SQLCheckButton from "./SQLCheckButton.vue";++export { SQLCheckButton };diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex c46ca21e3c..a606289e9c 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -949,7 +949,11 @@"task-is-x-status": "The task is {status}","task-is-running-cancel-first": "The task is running, cancel task before changing this value"},- "you-are-not-allowed-to-change-this-value": "You are not allowed to change this value"+ "you-are-not-allowed-to-change-this-value": "You are not allowed to change this value",+ "sql-check": {+ "sql-checks": "SQL Checks",+ "not-executed-yet": "Not executed yet"+ }},"alter-schema": {"vcs-enabled": "This project has enabled VCS based version control and selecting database below will navigate you to the corresponding Git repository to initiate the change process.",diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.jsonindex dc1af8fc04..7e3b476471 100644--- a/frontend/src/locales/es-ES.json+++ b/frontend/src/locales/es-ES.json@@ -948,7 +948,11 @@"task-is-x-status": "La tarea es {status}","task-is-running-cancel-first": "La tarea se está ejecutando, cancele la tarea antes de cambiar este valor"},- "you-are-not-allowed-to-change-this-value": "No tienes permitido cambiar este valor."+ "you-are-not-allowed-to-change-this-value": "No tienes permitido cambiar este valor.",+ "sql-check": {+ "sql-checks": "Comprobaciones SQL",+ "not-executed-yet": "Aún no ejecutado"+ }},"alter-schema": {"vcs-enabled": "Este proyecto ha habilitado el control de versiones basado en VCS y seleccionar la base de datos a continuación lo llevará al repositorio Git correspondiente para iniciar el proceso de cambio.",diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex e1e50793ee..f85443ca15 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -946,7 +946,11 @@"task-is-x-status": "任务状态是{status}","task-is-running-cancel-first": "任务运行中,需要取消任务才能修改这项值"},- "you-are-not-allowed-to-change-this-value": "您无权更改这项值"+ "you-are-not-allowed-to-change-this-value": "您无权更改这项值",+ "sql-check": {+ "sql-checks": "SQL 检查项",+ "not-executed-yet": "尚未执行"+ }},"alter-schema": {"vcs-enabled": "该项目开启了基于 VCS 的版本管理,选择下面的数据库会将您导航到相应的 Git 仓库以发起变更流程。",