Skip to content

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

chore: make SQL checks manual (#9597)

Liu Jia2a6920393f1
diff --git a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorSQLCheckButton/SchemaEditorSQLCheckButton.vue b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorSQLCheckButton/SchemaEditorSQLCheckButton.vue
index 80b505c665..faa367876f 100644
--- a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorSQLCheckButton/SchemaEditorSQLCheckButton.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorSQLCheckButton/SchemaEditorSQLCheckButton.vue
@@ -2,8 +2,7 @@
<SQLCheckButton
v-if="show"
:database="database"
- :statement="statement"
- :errors="errors"
+ :get-statement="getStatement"
:button-props="{
size: 'small',
}"
@@ -19,8 +18,7 @@
</SQLCheckButton>
</template>
<script lang="ts" setup>
-import { debounce } from "lodash-es";
-import { ref, toRefs, watch } from "vue";
+import { toRefs } from "vue";
import { SQLCheckButton, SQLCheckSummary } from "@/components/SQLCheck";
import { ComposedDatabase } from "@/types";
import { useSchemaEditorSQLCheck } from "./useSchemaEditorSQLCheck";
@@ -31,18 +29,5 @@ const props = defineProps<{
editStatement: string;
}>();
-const { show, database, watchKey, getStatement } = useSchemaEditorSQLCheck(
- toRefs(props)
-);
-const errors = ref<string[]>([]);
-const statement = ref("");
-
-const update = async () => {
- const result = await getStatement();
- errors.value = result.errors;
- statement.value = result.statement;
-};
-
-watch(watchKey, debounce(update, 250));
-update();
+const { show, database, getStatement } = useSchemaEditorSQLCheck(toRefs(props));
</script>
diff --git a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorSQLCheckButton/useSchemaEditorSQLCheck.ts b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorSQLCheckButton/useSchemaEditorSQLCheck.ts
index 2dfc095cd4..9628b0b763 100644
--- a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorSQLCheckButton/useSchemaEditorSQLCheck.ts
+++ b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorSQLCheckButton/useSchemaEditorSQLCheck.ts
@@ -29,17 +29,6 @@ export const useSchemaEditorSQLCheck = (params: {
return databaseList.value[0];
});
- const watchKey = computed(() => {
- if (selectedTab.value === "raw-sql") {
- return editStatement.value;
- } else {
- const databaseSchema = schemaEditorV1Store.resourceMap["database"].get(
- database.value.name
- );
- return JSON.stringify(databaseSchema);
- }
- });
-
const fetchDatabaseEditStatement = async (): Promise<{
errors: string[];
statement: string;
@@ -127,5 +116,5 @@ export const useSchemaEditorSQLCheck = (params: {
}
};
- return { show, database, watchKey, getStatement };
+ return { show, database, getStatement };
};
diff --git a/frontend/src/components/Branch/SchemaDesignSQLCheckButton.vue b/frontend/src/components/Branch/SchemaDesignSQLCheckButton.vue
index b37bd77abd..ad6e1c7848 100644
--- a/frontend/src/components/Branch/SchemaDesignSQLCheckButton.vue
+++ b/frontend/src/components/Branch/SchemaDesignSQLCheckButton.vue
@@ -2,8 +2,7 @@
<SQLCheckButton
v-if="database"
:database="database"
- :statement="statement"
- :errors="errors"
+ :get-statement="getStatement"
class="justify-end"
:button-style="{
height: '28px',
@@ -20,9 +19,8 @@
</template>
<script lang="ts" setup>
-import { asyncComputed } from "@vueuse/core";
-import { cloneDeep, debounce } from "lodash-es";
-import { computed, ref, watch } from "vue";
+import { cloneDeep } from "lodash-es";
+import { computed } from "vue";
import { useI18n } from "vue-i18n";
import { SQLCheckButton, SQLCheckSummary } from "@/components/SQLCheck";
import {
@@ -31,7 +29,6 @@ import {
} from "@/components/SchemaEditorV1/utils";
import { schemaDesignServiceClient } from "@/grpcweb";
import { useDatabaseV1Store, useSchemaEditorV1Store } from "@/store";
-import { DatabaseMetadata } from "@/types/proto/v1/database_service";
import { SchemaDesign } from "@/types/proto/v1/schema_design_service";
import { fetchBaselineMetadataOfBranch } from "../SchemaEditorV1/utils/branch";
@@ -46,13 +43,12 @@ const database = computed(() => {
return databaseStore.getDatabaseByName(props.schemaDesign.baselineDatabase);
});
-const sourceMetadata = asyncComputed(async () => {
+const getSourceMetadata = async () => {
const branch = props.schemaDesign;
return await fetchBaselineMetadataOfBranch(branch);
-}, undefined);
-
-const editingMetadata = asyncComputed(async () => {
+};
+const getEditingMetadata = async () => {
const branchSchema = schemaEditorV1Store.resourceMap["branch"].get(
props.schemaDesign.name
);
@@ -68,43 +64,29 @@ const editingMetadata = asyncComputed(async () => {
);
return metadata;
-}, undefined);
-
-const targetMetadata = ref<DatabaseMetadata>();
-const statement = ref("");
-const errors = ref<string[]>([]);
-
-const evaluateTargetMetadataAndDiff = async () => {
- const metadata = editingMetadata.value;
+};
- const setState = (
- metadata: DatabaseMetadata | undefined,
- stmt: string,
- errs: string[]
- ) => {
- targetMetadata.value = metadata;
- statement.value = stmt;
- errors.value = errs;
- };
+const getStatement = async () => {
+ const editingMetadata = await getEditingMetadata();
- if (!metadata) {
- setState(undefined, "", [t("schema-editor.message.invalid-schema")]);
- return;
+ if (!editingMetadata) {
+ return {
+ statement: "",
+ errors: [t("schema-editor.message.invalid-schema")],
+ };
}
- const validationMessages = validateDatabaseMetadata(metadata);
+ const validationMessages = validateDatabaseMetadata(editingMetadata);
if (validationMessages.length > 0) {
- setState(undefined, "", validationMessages);
- return;
+ return {
+ statement: "",
+ errors: validationMessages,
+ };
}
// Prepare to diff
- setState(metadata, "", []);
const db = database.value;
- const source = sourceMetadata.value;
- const target = targetMetadata.value;
- if (!db) return;
- if (!source) return;
- if (!target) return;
+ const source = await getSourceMetadata();
+ const target = editingMetadata;
try {
const diffResponse = await schemaDesignServiceClient.diffMetadata(
@@ -119,44 +101,16 @@ const evaluateTargetMetadataAndDiff = async () => {
);
const diff = diffResponse.diff;
const errs = diff.length === 0 ? [t("schema-editor.nothing-changed")] : [];
- setState(metadata, diff, errs);
+ return {
+ statement: diff,
+ errors: errs,
+ };
} catch {
// The grpc error message is too long not readable. So we won't use it here.
- setState(metadata, "", [t("schema-editor.message.invalid-schema")]);
+ return {
+ statement: "",
+ errors: [t("schema-editor.message.invalid-schema")],
+ };
}
};
-
-watch(
- editingMetadata,
- (metadata) => {
- if (!metadata) {
- errors.value = [t("schema-editor.message.invalid-schema")];
- } else {
- errors.value = validateDatabaseMetadata(metadata);
- }
- },
- {
- immediate: true,
- }
-);
-
-const watchKey = computed(() => {
- return [
- database.value?.name,
- JSON.stringify(
- DatabaseMetadata.toJSON(
- sourceMetadata.value ?? DatabaseMetadata.fromPartial({})
- )
- ),
- JSON.stringify(
- DatabaseMetadata.toJSON(
- editingMetadata.value ?? DatabaseMetadata.fromPartial({})
- )
- ),
- ].join("\n");
-});
-
-evaluateTargetMetadataAndDiff();
-// Won't update too frequently since this costs pretty high.
-watch(watchKey, debounce(evaluateTargetMetadataAndDiff, 250));
</script>
diff --git a/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckSection.vue b/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckSection.vue
index 8bd8177b8b..20b521e252 100644
--- a/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckSection.vue
+++ b/frontend/src/components/IssueV1/components/SQLCheckSection/SQLCheckSection.vue
@@ -7,7 +7,7 @@
<SQLCheckButton
v-if="database"
:key="selectedTask.uid"
- :statement="sheetStatement"
+ :get-statement="getStatement"
:database="database"
:button-props="{
size: 'tiny',
@@ -42,6 +42,13 @@ const database = computed(() => {
return databaseForTask(issue.value, selectedTask.value);
});
+const getStatement = async () => {
+ return {
+ statement: sheetStatement.value,
+ errors: [],
+ };
+};
+
const show = computed(() => {
const type = selectedTask.value.type;
if (type === Task_Type.DATABASE_SCHEMA_BASELINE) {
diff --git a/frontend/src/components/IssueV1/components/StatementSection/EditorView/EditorView.vue b/frontend/src/components/IssueV1/components/StatementSection/EditorView/EditorView.vue
index d3af452a53..bf1267774d 100644
--- a/frontend/src/components/IssueV1/components/StatementSection/EditorView/EditorView.vue
+++ b/frontend/src/components/IssueV1/components/StatementSection/EditorView/EditorView.vue
@@ -119,15 +119,15 @@
<div class="whitespace-pre-wrap overflow-hidden min-h-[120px] relative">
<MonacoEditor
- class="w-full h-auto max-h-[360px] min-h-[120px] border rounded-[3px]"
- :filename="`${selectedTask.name}.sql`"
+ class="w-full h-auto max-h-[240px] min-h-[120px] border rounded-[3px]"
+ :filename="filename"
:content="state.statement"
- :language="'sql'"
+ :language="language"
:auto-focus="false"
:readonly="isEditorReadonly"
:dialect="dialect"
:advices="isEditorReadonly ? markers : []"
- :auto-height="{ min: 120, max: 360 }"
+ :auto-height="{ min: 120, max: 240 }"
:auto-complete-context="{
instance: database.instance,
database: database.name,
@@ -180,6 +180,7 @@ import {
isDeploymentConfigChangeTaskV1,
} from "@/components/IssueV1/logic";
import { MonacoEditor } from "@/components/MonacoEditor";
+import { extensionNameOfLanguage } from "@/components/MonacoEditor/utils";
import DownloadSheetButton from "@/components/Sheet/DownloadSheetButton.vue";
import UploadProgressButton from "@/components/misc/UploadProgressButton.vue";
import { rolloutServiceClient } from "@/grpcweb";
@@ -237,6 +238,11 @@ const database = computed(() => {
const language = useInstanceV1EditorLanguage(
computed(() => database.value.instanceEntity)
);
+const filename = computed(() => {
+ return `${selectedTask.value.name}.${extensionNameOfLanguage(
+ language.value
+ )}`;
+});
const dialect = computed((): SQLDialect => {
const db = database.value;
return dialectOfEngineV1(db.instanceEntity.engine);
diff --git a/frontend/src/components/SQLCheck/SQLCheckButton.vue b/frontend/src/components/SQLCheck/SQLCheckButton.vue
index 12925d0a19..b859ad60c3 100644
--- a/frontend/src/components/SQLCheck/SQLCheckButton.vue
+++ b/frontend/src/components/SQLCheck/SQLCheckButton.vue
@@ -2,11 +2,11 @@
<div class="flex flex-row items-center gap-2">
<slot name="result" :advices="advices" :is-running="isRunning" />
- <NPopover :disabled="tooltipDisabled" to="body">
+ <NPopover :disabled="policyErrors.length === 0" to="body">
<template #trigger>
<NButton
style="--n-padding: 0 14px 0 10px"
- :disabled="buttonDisabled"
+ :disabled="policyErrors.length > 0"
:style="buttonStyle"
tag="div"
v-bind="buttonProps"
@@ -43,7 +43,7 @@
</template>
</i18n-t>
</template>
- <ErrorList v-else :errors="combinedErrors" />
+ <ErrorList v-else :errors="policyErrors" />
</template>
</NPopover>
@@ -59,9 +59,8 @@
</template>
<script lang="ts" setup>
-import { debounce } from "lodash-es";
import { ButtonProps, NButton, NPopover } from "naive-ui";
-import { computed, onUnmounted, ref, watch } from "vue";
+import { computed, onUnmounted, ref } from "vue";
import { onMounted } from "vue";
import { useI18n } from "vue-i18n";
import { sqlServiceClient } from "@/grpcweb";
@@ -76,16 +75,14 @@ import { useSQLCheckContext } from "./context";
const props = withDefaults(
defineProps<{
- statement: string;
+ getStatement: () => Promise<{ errors: string[]; statement: string }>;
database: ComposedDatabase;
buttonProps?: ButtonProps;
buttonStyle?: VueStyle;
- errors?: string[];
}>(),
{
buttonProps: undefined,
buttonStyle: undefined,
- errors: undefined,
}
);
@@ -120,29 +117,13 @@ const noReviewPolicyTips = computed(() => {
return "";
});
-const isStatementTooLarge = computed(() => {
- return props.statement.length > SKIP_CHECK_THRESHOLD;
+const policyErrors = computed(() => {
+ if (noReviewPolicyTips.value) return [noReviewPolicyTips.value];
+ return [];
});
-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 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 runCheckInternal = async (statement: string) => {
+ const { database } = props;
const result = await sqlServiceClient.check({
statement,
database: database.name,
@@ -152,35 +133,48 @@ const runCheckInternal = async () => {
};
const runChecks = async () => {
- if (buttonDisabled.value) {
+ if (policyErrors.value.length > 0) {
return;
}
+ const handleErrors = (errors: string[]) => {
+ // Mock the pre-check errors to advices
+ advices.value = errors.map((err) =>
+ Advice.fromPartial({
+ title: "Pre check",
+ status: Advice_Status.WARNING,
+ content: err,
+ })
+ );
+ isRunning.value = false;
+ };
+
isRunning.value = true;
if (!advices.value) {
advices.value = [];
}
+ const { statement, errors } = await props.getStatement();
+ if (statement.length === 0) {
+ return handleErrors([t("common.nothing-changed")]);
+ }
+ if (statement.length > SKIP_CHECK_THRESHOLD) {
+ return handleErrors([t("issue.sql-check.statement-is-too-large")]);
+ }
+ if (errors.length > 0) {
+ return handleErrors(errors);
+ }
try {
- const result = await runCheckInternal();
+ const result = await runCheckInternal(statement);
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 (policyErrors.value.length > 0) {
// If SQL Check is disabled, we will do nothing to stop the user.
return true;
}
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 6974866a5e..658eb86d5c 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -276,7 +276,8 @@
"license": "License",
"show-all": "Show all",
"view-details": "View details",
- "force-verb": "Force {verb}"
+ "force-verb": "Force {verb}",
+ "nothing-changed": "Nothing changed"
},
"error-page": {
"go-back-home": "Go back home"
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index d212559f77..9bc921b5ff 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -276,7 +276,8 @@
"license": "Licencia",
"show-all": "Mostrar todo",
"view-details": "Ver detalles",
- "force-verb": "forzar {verbo}"
+ "force-verb": "forzar {verbo}",
+ "nothing-changed": "nada ha cambiado"
},
"error-page": {
"go-back-home": "Volver al inicio"
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index b3a6eb30e5..1e44318c74 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -274,7 +274,8 @@
"license": "证书",
"show-all": "显示全部",
"view-details": "查看详情",
- "force-verb": "强制{verb}"
+ "force-verb": "强制{verb}",
+ "nothing-changed": "没有改变"
},
"error-page": {
"go-back-home": "回到主页"