Skip to content

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

chore: add raw sql preview to branch design (#8643)

boojackda4812517af1
diff --git a/frontend/src/components/Branch/BranchDetailView.vue b/frontend/src/components/Branch/BranchDetailView.vue
index 79f1829866..7c092eb48e 100644
--- a/frontend/src/components/Branch/BranchDetailView.vue
+++ b/frontend/src/components/Branch/BranchDetailView.vue
@@ -74,21 +74,14 @@
</div>
</div>
</div>
- <div class="flex-1 flex flex-row justify-end gap-2">
- <SchemaDesignSQLCheckButton
- class="justify-end"
- :schema-design="schemaDesign"
- />
- </div>
</div>
<div class="w-full h-[32rem]">
- <SchemaEditorV1
+ <SchemaDesignEditor
:key="schemaEditorKey"
- :readonly="!state.isEditing"
:project="project"
- :resource-type="'branch'"
- :branches="[schemaDesign]"
+ :readonly="!state.isEditing"
+ :branch="schemaDesign"
/>
</div>
<!-- Don't show delete button in view mode. -->
@@ -119,7 +112,6 @@ import { CSSProperties, computed, reactive, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
import DatabaseInfo from "@/components/DatabaseInfo.vue";
-import SchemaEditorV1 from "@/components/SchemaEditorV1/index.vue";
import {
pushNotification,
useChangeHistoryStore,
@@ -136,7 +128,7 @@ import {
import { provideSQLCheckContext } from "../SQLCheck";
import { getBaselineMetadataOfBranch } from "../SchemaEditorV1/utils/branch";
import MergeBranchPanel from "./MergeBranchPanel.vue";
-import SchemaDesignSQLCheckButton from "./SchemaDesignSQLCheckButton.vue";
+import SchemaDesignEditor from "./SchemaDesignEditor.vue";
import {
generateForkedBranchName,
mergeSchemaEditToMetadata,
diff --git a/frontend/src/components/Branch/SchemaDesignEditor.vue b/frontend/src/components/Branch/SchemaDesignEditor.vue
new file mode 100644
index 0000000000..d06c20829f
--- /dev/null
+++ b/frontend/src/components/Branch/SchemaDesignEditor.vue
@@ -0,0 +1,178 @@
+<template>
+ <div class="w-full h-full flex flex-col justify-start items-start">
+ <div
+ class="w-full flex flex-row justify-between items-center border-b pl-1 border-b-gray-300"
+ >
+ <div class="flex items-center flex-start">
+ <button
+ class="-mb-px px-3 leading-9 rounded-t-md flex items-center text-sm text-gray-500 border border-b-0 border-transparent cursor-pointer select-none outline-none"
+ :class="
+ state.selectedTab === 'schema-editor' &&
+ 'bg-white !border-gray-300 text-gray-800'
+ "
+ @click="handleChangeTab('schema-editor')"
+ >
+ {{ $t("schema-editor.self") }}
+ </button>
+ <button
+ class="-mb-px px-3 leading-9 rounded-t-md text-sm text-gray-500 border border-b-0 border-transparent cursor-pointer select-none outline-none"
+ :class="
+ state.selectedTab === 'raw-sql-preview' &&
+ 'bg-white !border-gray-300 text-gray-800'
+ "
+ @click="handleChangeTab('raw-sql-preview')"
+ >
+ {{ $t("schema-designer.raw-sql-preview") }}
+ </button>
+ </div>
+ <div v-if="!hideSQLCheckButton" class="flex items-center flex-end">
+ <SchemaDesignSQLCheckButton
+ class="justify-end"
+ :schema-design="branch"
+ />
+ </div>
+ </div>
+ <div class="grow w-full h-auto overflow-auto">
+ <div
+ v-show="state.selectedTab === 'schema-editor'"
+ class="w-full h-full pt-2"
+ >
+ <SchemaEditorV1
+ :project="project"
+ :readonly="readonly"
+ :resource-type="'branch'"
+ :branches="[branch]"
+ />
+ </div>
+ <div
+ v-show="state.selectedTab === 'raw-sql-preview'"
+ class="w-full h-full pt-2 overflow-y-auto"
+ >
+ <MonacoEditor
+ class="w-full h-full border rounded-lg overflow-auto"
+ data-label="bb-schema-editor-sql-editor"
+ :value="rawSQLPreviewState.value"
+ :readonly="true"
+ :auto-focus="false"
+ />
+ </div>
+ </div>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { cloneDeep } from "lodash-es";
+import { computed, reactive } from "vue";
+import { useI18n } from "vue-i18n";
+import { schemaDesignServiceClient } from "@/grpcweb";
+import {
+ pushNotification,
+ useDatabaseV1Store,
+ useSchemaEditorV1Store,
+} from "@/store";
+import { SchemaDesign } from "@/types/proto/v1/schema_design_service";
+import { ComposedProject } from "@/types/v1";
+import {
+ mergeSchemaEditToMetadata,
+ validateDatabaseMetadata,
+} from "../SchemaEditorV1/utils";
+import { getBaselineMetadataOfBranch } from "../SchemaEditorV1/utils/branch";
+
+type TabType = "schema-editor" | "raw-sql-preview";
+
+interface LocalState {
+ selectedTab: TabType;
+}
+
+const props = defineProps<{
+ project: ComposedProject;
+ branch: SchemaDesign;
+ readonly?: boolean;
+ hideSQLCheckButton?: boolean;
+}>();
+
+const { t } = useI18n();
+const state = reactive<LocalState>({
+ selectedTab: "schema-editor",
+});
+const databaseStore = useDatabaseV1Store();
+const schemaEditorV1Store = useSchemaEditorV1Store();
+const rawSQLPreviewState = reactive({
+ value: "",
+ isFetching: false,
+});
+
+const baselineDatabase = computed(() => {
+ return databaseStore.getDatabaseByName(props.branch.baselineDatabase);
+});
+
+const handleChangeTab = async (tab: TabType) => {
+ state.selectedTab = tab;
+ if (tab === "raw-sql-preview") {
+ await fetchRawSQLPreview();
+ }
+};
+
+const fetchRawSQLPreview = async () => {
+ if (rawSQLPreviewState.isFetching) {
+ return;
+ }
+
+ const sourceMetadata = getBaselineMetadataOfBranch(props.branch);
+ const branchSchema = schemaEditorV1Store.resourceMap["branch"].get(
+ props.branch.name
+ );
+ if (!branchSchema) {
+ return undefined;
+ }
+
+ const baselineMetadata = getBaselineMetadataOfBranch(branchSchema.branch);
+ const metadata = mergeSchemaEditToMetadata(
+ branchSchema.schemaList,
+ cloneDeep(baselineMetadata)
+ );
+ if (!metadata) {
+ rawSQLPreviewState.value = "";
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("schema-editor.message.invalid-schema"),
+ });
+ return;
+ }
+ const validationMessages = validateDatabaseMetadata(metadata);
+ if (validationMessages.length > 0) {
+ rawSQLPreviewState.value = "";
+ pushNotification({
+ module: "bytebase",
+ style: "WARN",
+ title: "Invalid schema structure",
+ description: validationMessages.join("\n"),
+ });
+ return;
+ }
+
+ try {
+ rawSQLPreviewState.isFetching = true;
+ const diffResponse = await schemaDesignServiceClient.diffMetadata(
+ {
+ sourceMetadata,
+ targetMetadata: metadata,
+ engine: baselineDatabase.value.instanceEntity.engine,
+ },
+ {
+ silent: true,
+ }
+ );
+ rawSQLPreviewState.value = diffResponse.diff;
+ } catch {
+ pushNotification({
+ module: "bytebase",
+ style: "WARN",
+ title: t("schema-editor.message.invalid-schema"),
+ });
+ rawSQLPreviewState.value = "";
+ }
+ rawSQLPreviewState.isFetching = false;
+};
+</script>
diff --git a/frontend/src/components/Branch/SchemaDesignSQLCheckButton.vue b/frontend/src/components/Branch/SchemaDesignSQLCheckButton.vue
index 07a0db2f41..715c1890ec 100644
--- a/frontend/src/components/Branch/SchemaDesignSQLCheckButton.vue
+++ b/frontend/src/components/Branch/SchemaDesignSQLCheckButton.vue
@@ -5,6 +5,9 @@
:statement="statement"
:errors="errors"
class="justify-end"
+ :button-style="{
+ height: '28px',
+ }"
>
<template #result="{ advices, isRunning }">
<SQLCheckSummary
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index e56709b030..461b325113 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -3021,6 +3021,7 @@
"save-draft": "Save draft",
"merge-branch": "Merge branch",
"delete-branch-after-merged": "Delete branch after merged",
+ "raw-sql-preview": "Raw SQL Preview",
"diff-editor": {
"self": "Diff editor",
"resolve": "Resolve",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 264e05f704..11795ccf08 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -3019,6 +3019,7 @@
"save-draft": "Guardar borrador",
"merge-branch": "Fusionar rama",
"delete-branch-after-merged": "Eliminar rama después de la fusión",
+ "raw-sql-preview": "vista previa de sql sin formato",
"diff-editor": {
"self": "Editor de diferencias",
"resolve": "Resolver",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 326944ec34..5884da639a 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -3014,6 +3014,7 @@
"save-draft": "保存草稿",
"merge-branch": "合并分支",
"delete-branch-after-merged": "合并后删除分支",
+ "raw-sql-preview": "原始 SQL 预览",
"diff-editor": {
"self": "Diff 编辑器",
"resolve": "解决",