Skip to content

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

feat(frontend): SQL Check improvements (#8448)

Liu Ji2f991f67e7bc
diff --git a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
index 1797676be6..d3155ad9cd 100644
--- a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
@@ -166,6 +166,7 @@ import { TenantMode } from "@/types/proto/v1/project_service";
import { allowGhostMigrationV1 } from "@/utils";
import { validateDatabaseEdit } from "@/utils/schemaEditor/validate";
import MonacoEditor from "../MonacoEditor";
+import { provideSQLCheckContext } from "../SQLCheck";
import {
mergeSchemaEditToMetadata,
validateDatabaseMetadata,
@@ -216,6 +217,7 @@ const dbSchemaV1Store = useDBSchemaV1Store();
const notificationStore = useNotificationStore();
const statementFromSchemaEditor = ref<string>();
const ghostDialog = ref<InstanceType<typeof GhostDialog>>();
+const { runSQLCheck } = provideSQLCheckContext();
const allowPreviewIssue = computed(() => {
if (state.selectedTab === "schema-editor") {
@@ -425,6 +427,11 @@ const handleUploadFile = (e: Event) => {
};
const handlePreviewIssue = async () => {
+ const check = runSQLCheck.value;
+ if (check && !(await check())) {
+ return;
+ }
+
const query: Record<string, any> = {
template: "bb.issue.database.schema.update",
project: project.value.uid,
diff --git a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorSQLCheckButton/SchemaEditorSQLCheckButton.vue b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorSQLCheckButton/SchemaEditorSQLCheckButton.vue
index 3045b00b96..80b505c665 100644
--- a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorSQLCheckButton/SchemaEditorSQLCheckButton.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorSQLCheckButton/SchemaEditorSQLCheckButton.vue
@@ -21,7 +21,7 @@
<script lang="ts" setup>
import { debounce } from "lodash-es";
import { ref, toRefs, watch } from "vue";
-import { SQLCheckButton } from "@/components/SQLCheck";
+import { SQLCheckButton, SQLCheckSummary } from "@/components/SQLCheck";
import { ComposedDatabase } from "@/types";
import { useSchemaEditorSQLCheck } from "./useSchemaEditorSQLCheck";
diff --git a/frontend/src/components/Branch/BranchDetailView.vue b/frontend/src/components/Branch/BranchDetailView.vue
index 7ec1fa64a5..3d4ef72d63 100644
--- a/frontend/src/components/Branch/BranchDetailView.vue
+++ b/frontend/src/components/Branch/BranchDetailView.vue
@@ -141,6 +141,7 @@ import {
SchemaDesign_Type,
} from "@/types/proto/v1/schema_design_service";
import { projectV1Slug } from "@/utils";
+import { provideSQLCheckContext } from "../SQLCheck";
import { getBaselineMetadataOfBranch } from "../SchemaEditorV1/utils/branch";
import MergeBranchPanel from "./MergeBranchPanel.vue";
import SchemaDesignSQLCheckButton from "./SchemaDesignSQLCheckButton.vue";
@@ -171,6 +172,7 @@ const router = useRouter();
const databaseStore = useDatabaseV1Store();
const changeHistoryStore = useChangeHistoryStore();
const schemaDesignStore = useSchemaDesignStore();
+const { runSQLCheck } = provideSQLCheckContext();
const dialog = useDialog();
const state = reactive<LocalState>({
schemaDesignTitle: "",
@@ -328,6 +330,10 @@ const handleSaveBranch = async () => {
if (!state.isEditing) {
return;
}
+ const check = runSQLCheck.value;
+ if (check && !(await check())) {
+ return;
+ }
const updateMask = [];
const schemaEditorV1Store = useSchemaEditorV1Store();
diff --git a/frontend/src/components/IssueV1/IssueDetailPage.vue b/frontend/src/components/IssueV1/IssueDetailPage.vue
index 39064e910e..c8c659cf28 100644
--- a/frontend/src/components/IssueV1/IssueDetailPage.vue
+++ b/frontend/src/components/IssueV1/IssueDetailPage.vue
@@ -54,6 +54,7 @@
<script setup lang="ts">
import { ref } from "vue";
import { Task } from "@/types/proto/v1/rollout_service";
+import { provideSQLCheckContext } from "../SQLCheck";
import {
BannerSection,
HeaderSection,
@@ -109,4 +110,6 @@ events.on("perform-task-rollout-action", async ({ action, tasks }) => {
taskList: tasks,
};
});
+
+provideSQLCheckContext();
</script>
diff --git a/frontend/src/components/IssueV1/components/HeaderSection/Actions/create/CreateButton.vue b/frontend/src/components/IssueV1/components/HeaderSection/Actions/create/CreateButton.vue
index e3236a9418..b1711d65e4 100644
--- a/frontend/src/components/IssueV1/components/HeaderSection/Actions/create/CreateButton.vue
+++ b/frontend/src/components/IssueV1/components/HeaderSection/Actions/create/CreateButton.vue
@@ -39,6 +39,7 @@ import {
useIssueContext,
} from "@/components/IssueV1/logic";
import formatSQL from "@/components/MonacoEditor/sqlFormatter";
+import { useSQLCheckContext } from "@/components/SQLCheck";
import { issueServiceClient, rolloutServiceClient } from "@/grpcweb";
import { useDatabaseV1Store, useSheetV1Store } from "@/store";
import { ComposedIssue, dialectOfEngineV1, languageOfEngineV1 } from "@/types";
@@ -56,6 +57,7 @@ const MAX_FORMATTABLE_STATEMENT_SIZE = 10000; // 10K characters
const router = useRouter();
const { issue, formatOnSave } = useIssueContext();
+const { runSQLCheck } = useSQLCheckContext();
const loading = ref(false);
const issueCreateErrorList = computed(() => {
@@ -73,7 +75,13 @@ const issueCreateErrorList = computed(() => {
});
const doCreateIssue = async () => {
+ const check = runSQLCheck.value;
+ if (check && !(await check())) {
+ return;
+ }
+
loading.value = true;
+
try {
await createSheets();
const createdPlan = await createPlan();
diff --git a/frontend/src/components/SQLCheck/SQLCheckButton.vue b/frontend/src/components/SQLCheck/SQLCheckButton.vue
index 9f67a11934..0f7bca1106 100644
--- a/frontend/src/components/SQLCheck/SQLCheckButton.vue
+++ b/frontend/src/components/SQLCheck/SQLCheckButton.vue
@@ -43,22 +43,36 @@
</template>
</i18n-t>
</template>
- <ErrorList v-else :errors="errors ?? []" />
+ <ErrorList v-else :errors="combinedErrors" />
</template>
</NPopover>
+
+ <SQLCheckPanel
+ v-if="database && advices && confirmDialog"
+ :database="database"
+ :advices="advices"
+ :confirm="confirmDialog"
+ :override-title="$t('issue.sql-check.sql-review-violations')"
+ @close="confirmDialog = undefined"
+ />
</div>
</template>
<script lang="ts" setup>
+import { debounce } from "lodash-es";
import { ButtonProps, NButton, NPopover } from "naive-ui";
-import { CSSProperties, computed, ref } from "vue";
+import { CSSProperties, computed, onUnmounted, ref, watch } from "vue";
+import { onMounted } from "vue";
+import { useI18n } from "vue-i18n";
import { sqlServiceClient } from "@/grpcweb";
import { usePolicyByParentAndType } from "@/store";
import { ComposedDatabase } from "@/types";
import { PolicyType } from "@/types/proto/v1/org_policy_service";
-import { Advice } from "@/types/proto/v1/sql_service";
-import { useWorkspacePermissionV1 } from "@/utils";
+import { Advice, Advice_Status } from "@/types/proto/v1/sql_service";
+import { Defer, defer, useWorkspacePermissionV1 } from "@/utils";
import ErrorList from "../misc/ErrorList.vue";
+import SQLCheckPanel from "./SQLCheckPanel.vue";
+import { useSQLCheckContext } from "./context";
const props = defineProps<{
statement: string;
@@ -68,8 +82,13 @@ const props = defineProps<{
errors?: string[];
}>();
+const { t } = useI18n();
+const SKIP_CHECK_THRESHOLD = 500000;
const isRunning = ref(false);
const advices = ref<Advice[]>();
+const checkRunCounter = ref(0);
+const context = useSQLCheckContext();
+const confirmDialog = ref<Defer<boolean>>();
const reviewPolicy = usePolicyByParentAndType(
computed(() => ({
@@ -93,29 +112,96 @@ const noReviewPolicyTips = computed(() => {
return "";
});
+const isStatementTooLarge = computed(() => {
+ return props.statement.length > SKIP_CHECK_THRESHOLD;
+});
+
const buttonDisabled = computed(() => {
if (noReviewPolicyTips.value) return true;
if (!props.statement) return true;
+ if (isStatementTooLarge.value) return true;
return props.errors && props.errors.length > 0;
});
const tooltipDisabled = computed(() => {
return !buttonDisabled.value;
});
-const runChecks = async () => {
+const combinedErrors = computed(() => {
+ if (isStatementTooLarge.value) {
+ return [t("issue.sql-check.statement-is-too-large")];
+ }
+ return props.errors ?? [];
+});
+
+const runCheckInternal = async () => {
const { statement, database } = props;
+ const result = await sqlServiceClient.check({
+ statement,
+ database: database.name,
+ });
+ checkRunCounter.value++;
+ return result;
+};
+
+const runChecks = async () => {
+ if (buttonDisabled.value) {
+ return;
+ }
+
isRunning.value = true;
if (!advices.value) {
advices.value = [];
}
try {
- const result = await sqlServiceClient.check({
- statement,
- database: database.name,
- });
+ const result = await runCheckInternal();
advices.value = result.advices;
} finally {
isRunning.value = false;
}
};
+
+watch(
+ [() => props.statement, () => props.database.name, () => props.errors],
+
+ debounce(runChecks, 1000),
+ {
+ immediate: true,
+ }
+);
+
+onMounted(() => {
+ if (!context) return;
+ context.runSQLCheck.value = async () => {
+ if (buttonDisabled.value) {
+ // If SQL Check is disabled, we will do nothing to stop the user.
+ return true;
+ }
+
+ if (checkRunCounter.value === 0) {
+ await runChecks();
+ }
+
+ const hasError = advices.value?.some(
+ (advice) =>
+ advice.status === Advice_Status.ERROR ||
+ advice.status === Advice_Status.WARNING
+ );
+ if (hasError) {
+ const d = defer<boolean>();
+ confirmDialog.value = d;
+ d.promise.finally(() => {
+ confirmDialog.value = undefined;
+ });
+
+ return d.promise;
+ }
+
+ return true;
+ };
+});
+
+onUnmounted(() => {
+ if (!context) return;
+ context.runSQLCheck.value = undefined;
+});
</script>
diff --git a/frontend/src/components/SQLCheck/SQLCheckPanel.vue b/frontend/src/components/SQLCheck/SQLCheckPanel.vue
index b4efaa2359..8e8fed6197 100644
--- a/frontend/src/components/SQLCheck/SQLCheckPanel.vue
+++ b/frontend/src/components/SQLCheck/SQLCheckPanel.vue
@@ -1,23 +1,41 @@
<template>
<BBModal
- :title="$t('task.check-result.title-general')"
+ :title="overrideTitle ?? $t('task.check-result.title-general')"
+ :show-close="!confirm"
+ :esc-closable="!confirm"
class="!w-[56rem]"
header-class="whitespace-pre-wrap break-all gap-x-1"
container-class="!pt-0 -mt-px"
@close="$emit('close')"
>
<SQLCheckDetail :database="database" :advices="advices" />
+
+ <div
+ v-if="confirm"
+ class="flex flex-row justify-end items-center gap-x-3 mt-4"
+ >
+ <NButton @click="confirm.resolve(false)">
+ {{ $t("issue.sql-check.back-to-edit") }}
+ </NButton>
+ <NButton type="primary" @click="confirm.resolve(true)">
+ {{ $t("issue.sql-check.continue-anyway") }}
+ </NButton>
+ </div>
</BBModal>
</template>
<script setup lang="ts">
+import { NButton } from "naive-ui";
import { ComposedDatabase } from "@/types";
import { Advice } from "@/types/proto/v1/sql_service";
+import { Defer } from "@/utils";
import SQLCheckDetail from "./SQLCheckDetail.vue";
defineProps<{
database: ComposedDatabase;
advices: Advice[];
+ overrideTitle?: string;
+ confirm?: Defer<boolean>;
}>();
defineEmits<{
diff --git a/frontend/src/components/SQLCheck/context.ts b/frontend/src/components/SQLCheck/context.ts
new file mode 100644
index 0000000000..93bfd8236a
--- /dev/null
+++ b/frontend/src/components/SQLCheck/context.ts
@@ -0,0 +1,23 @@
+import { InjectionKey, Ref, inject, provide, ref } from "vue";
+
+export type SQLCheckContext = {
+ runSQLCheck: Ref<(() => Promise<boolean>) | undefined>;
+};
+
+export const KEY = Symbol(
+ "bb.sql-check.context"
+) as InjectionKey<SQLCheckContext>;
+
+export const useSQLCheckContext = () => {
+ return inject(KEY)!;
+};
+
+export const provideSQLCheckContext = () => {
+ const context: SQLCheckContext = {
+ runSQLCheck: ref(),
+ };
+
+ provide(KEY, context);
+
+ return context;
+};
diff --git a/frontend/src/components/SQLCheck/index.ts b/frontend/src/components/SQLCheck/index.ts
index fde01094ba..ffa6133839 100644
--- a/frontend/src/components/SQLCheck/index.ts
+++ b/frontend/src/components/SQLCheck/index.ts
@@ -2,4 +2,5 @@ import SQLCheckButton from "./SQLCheckButton.vue";
import SQLCheckPanel from "./SQLCheckPanel.vue";
import SQLCheckSummary from "./SQLCheckSummary.vue";
+export * from "./context";
export { SQLCheckButton, SQLCheckPanel, SQLCheckSummary };
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index a69fcd12e2..dc96fbea3b 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -977,7 +977,11 @@
"no-configured-sql-review-policy": {
"admin": "{environment} environment has not configured SQL Review Policy. {link}",
"developer": "{environment} environment has not configured SQL Review Policy. Please ask your DBA to configure."
- }
+ },
+ "statement-is-too-large": "The statement is too large",
+ "back-to-edit": "Back to edit",
+ "continue-anyway": "Continue anyway",
+ "sql-review-violations": "SQL Review Violations"
}
},
"alter-schema": {
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 50c50991e7..adbe55ebcb 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -976,7 +976,11 @@
"no-configured-sql-review-policy": {
"admin": "El entorno {environment} no ha configurado la política de revisión de SQL. {link}",
"developer": "El entorno {environment} no ha configurado la política de revisión de SQL. \nSolicite a su DBA que configure."
- }
+ },
+ "statement-is-too-large": "La declaración es demasiado grande.",
+ "back-to-edit": "Volver a editar",
+ "continue-anyway": "De todas maneras, continúe",
+ "sql-review-violations": "Infracciones de revisión de SQL"
}
},
"alter-schema": {
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index f58c74c8fe..45318230df 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -974,7 +974,11 @@
"no-configured-sql-review-policy": {
"admin": "{environment} 环境没有配置 SQL 审核策略。{link}",
"developer": "{environment} 环境没有配置 SQL 审核策略,请让您的 DBA 配置。"
- }
+ },
+ "statement-is-too-large": "语句过大",
+ "back-to-edit": "返回编辑",
+ "continue-anyway": "仍然继续",
+ "sql-review-violations": "违反的 SQL 审核规则"
}
},
"alter-schema": {