Skip to content

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

chore: view preview (#13517)

Liu Jie57b44d9b290
diff --git a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
index d0a1b9064b..5ab3f987d2 100644
--- a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
@@ -39,7 +39,6 @@
:loading="state.isPreparingMetadata"
:diff-when-ready="false"
:show-last-updater="false"
- :show-table-preview="true"
/>
</NTabPane>
<NTabPane
diff --git a/frontend/src/components/Branch/BranchCreateView.vue b/frontend/src/components/Branch/BranchCreateView.vue
index 3f52710e29..500c05fa6c 100644
--- a/frontend/src/components/Branch/BranchCreateView.vue
+++ b/frontend/src/components/Branch/BranchCreateView.vue
@@ -76,7 +76,6 @@
:branch="branchData?.branch ?? EMPTY_BRANCH"
:readonly="true"
:diff-when-ready="!!branchData?.parent"
- :show-table-preview="true"
/>
<!-- turn on diff-when-ready is useful when the branch is created from a parent branch -->
</div>
diff --git a/frontend/src/components/Branch/BranchDetailView.vue b/frontend/src/components/Branch/BranchDetailView.vue
index 259089f4ec..aab4ea0f1a 100644
--- a/frontend/src/components/Branch/BranchDetailView.vue
+++ b/frontend/src/components/Branch/BranchDetailView.vue
@@ -90,7 +90,6 @@
:readonly="!state.isEditing"
:branch="dirtyBranch"
:disable-diff-coloring="!state.isEditing && !state.showDiff"
- :show-table-preview="true"
@update-is-editing="updateIsEditing"
/>
</div>
diff --git a/frontend/src/components/Branch/BranchRebaseView/RebaseBranchStep.vue b/frontend/src/components/Branch/BranchRebaseView/RebaseBranchStep.vue
index 48fe24de84..282d506bb0 100644
--- a/frontend/src/components/Branch/BranchRebaseView/RebaseBranchStep.vue
+++ b/frontend/src/components/Branch/BranchRebaseView/RebaseBranchStep.vue
@@ -29,7 +29,6 @@
:readonly="true"
:branch="validationState.branch"
:show-last-updater="true"
- :show-table-preview="true"
/>
</div>
<div
diff --git a/frontend/src/components/Branch/BranchRebaseView/SelectBranchStep.vue b/frontend/src/components/Branch/BranchRebaseView/SelectBranchStep.vue
index 61e15e10d1..1c511821fc 100644
--- a/frontend/src/components/Branch/BranchRebaseView/SelectBranchStep.vue
+++ b/frontend/src/components/Branch/BranchRebaseView/SelectBranchStep.vue
@@ -96,7 +96,6 @@
:loading="isLoadingHeadBranch"
:branch="headBranch ?? Branch.fromPartial({})"
:show-last-updater="true"
- :show-table-preview="true"
/>
<MaskSpinner v-if="isLoadingHeadBranch" />
</div>
diff --git a/frontend/src/components/Branch/BranchRolloutView/BranchRolloutView.vue b/frontend/src/components/Branch/BranchRolloutView/BranchRolloutView.vue
index fc20b92ac4..16426926dd 100644
--- a/frontend/src/components/Branch/BranchRolloutView/BranchRolloutView.vue
+++ b/frontend/src/components/Branch/BranchRolloutView/BranchRolloutView.vue
@@ -77,7 +77,6 @@
:branch="virtualBranch"
:loading="isLoadingVirtualBranch"
:show-last-updater="true"
- :show-table-preview="true"
/>
<!-- used as a placeholder -->
diff --git a/frontend/src/components/Branch/SchemaDesignEditorLite.vue b/frontend/src/components/Branch/SchemaDesignEditorLite.vue
index fbb7588ad8..a8ba7c922e 100644
--- a/frontend/src/components/Branch/SchemaDesignEditorLite.vue
+++ b/frontend/src/components/Branch/SchemaDesignEditorLite.vue
@@ -47,7 +47,7 @@
:diff-when-ready="true"
:show-last-updater="true"
:disable-diff-coloring="disableDiffColoring"
- :show-table-preview="showTablePreview"
+ :hide-preview="hidePreview"
@update-is-editing="$emit('update-is-editing', $event)"
/>
</div>
@@ -96,7 +96,7 @@ const props = defineProps<{
readonly?: boolean;
hideSQLCheckButton?: boolean;
disableDiffColoring?: boolean;
- showTablePreview?: boolean;
+ hidePreview?: boolean;
}>();
const { t } = useI18n();
diff --git a/frontend/src/components/Branch/common/BranchComparison.vue b/frontend/src/components/Branch/common/BranchComparison.vue
index 9ba6d821f6..bb4c6d973f 100644
--- a/frontend/src/components/Branch/common/BranchComparison.vue
+++ b/frontend/src/components/Branch/common/BranchComparison.vue
@@ -50,7 +50,6 @@
:branch="virtualBranch ?? Branch.fromPartial({})"
:loading="combinedLoading"
:show-last-updater="true"
- :show-table-preview="true"
/>
</div>
<MaskSpinner v-if="combinedLoading" />
diff --git a/frontend/src/components/Branch/common/BranchDetailViewer.vue b/frontend/src/components/Branch/common/BranchDetailViewer.vue
index f4aec6978a..d0e5966e1c 100644
--- a/frontend/src/components/Branch/common/BranchDetailViewer.vue
+++ b/frontend/src/components/Branch/common/BranchDetailViewer.vue
@@ -26,7 +26,6 @@
:branch="branch ?? Branch.fromPartial({})"
:loading="loading"
:show-last-updater="true"
- :show-table-preview="true"
/>
</div>
<div
diff --git a/frontend/src/components/SchemaEditorLite/Editor.vue b/frontend/src/components/SchemaEditorLite/Editor.vue
index 167d1548f2..c2ec706f90 100644
--- a/frontend/src/components/SchemaEditorLite/Editor.vue
+++ b/frontend/src/components/SchemaEditorLite/Editor.vue
@@ -6,7 +6,10 @@
@update:table-search-pattern="handleTableSearchPattern"
@update:column-search-pattern="handleColumnSearchPattern"
/>
- <div class="w-full flex-1 relative overflow-y-hidden" :data-key="currentTab.id">
+ <div
+ class="w-full flex-1 relative overflow-y-hidden"
+ :data-key="currentTab.id"
+ >
<DatabaseEditor
v-if="currentTab.type === 'database'"
:key="currentTab.id"
@@ -32,7 +35,7 @@
:database="currentTab.metadata.database"
:schema="currentTab.metadata.schema"
:procedure="currentTab.metadata.procedure"
- class="px-2 pb-2"
+ class="pb-2"
/>
<FunctionEditor
v-if="currentTab.type === 'function'"
@@ -41,7 +44,7 @@
:database="currentTab.metadata.database"
:schema="currentTab.metadata.schema"
:func="currentTab.metadata.function"
- class="px-2 pb-2"
+ class="pb-2"
/>
<ViewEditor
v-if="currentTab.type === 'view'"
@@ -50,7 +53,6 @@
:database="currentTab.metadata.database"
:schema="currentTab.metadata.schema"
:view="currentTab.metadata.view"
- class="px-2 pb-2"
/>
</div>
</template>
diff --git a/frontend/src/components/SchemaEditorLite/Panels/CommonCodeEditor.vue b/frontend/src/components/SchemaEditorLite/Panels/CommonCodeEditor.vue
index bce6cf8b37..3087ee3183 100644
--- a/frontend/src/components/SchemaEditorLite/Panels/CommonCodeEditor.vue
+++ b/frontend/src/components/SchemaEditorLite/Panels/CommonCodeEditor.vue
@@ -3,17 +3,20 @@
<div class="flex justify-between items-center text-sm">
<slot name="header-suffix" />
</div>
- <MonacoEditor
- :content="state.code"
- :readonly="!editable"
- :auto-complete-context="{
- instance: db.instance,
- database: db.name,
- scene: 'all',
- }"
- class="border w-full rounded flex-1 relative"
- @update:content="handleUpdateCode"
- />
+ <div class="flex-1 relative px-2 overflow-y-hidden">
+ <MonacoEditor
+ :content="state.code"
+ :readonly="!editable"
+ :auto-complete-context="{
+ instance: db.instance,
+ database: db.name,
+ scene: 'all',
+ }"
+ class="border w-full h-full rounded"
+ @update:content="handleUpdateCode"
+ />
+ </div>
+ <slot name="preview" />
</div>
</template>
diff --git a/frontend/src/components/SchemaEditorLite/Panels/PreviewPane.vue b/frontend/src/components/SchemaEditorLite/Panels/PreviewPane.vue
new file mode 100644
index 0000000000..1542e99663
--- /dev/null
+++ b/frontend/src/components/SchemaEditorLite/Panels/PreviewPane.vue
@@ -0,0 +1,114 @@
+<template>
+ <div
+ v-if="!hidePreview"
+ v-show="layoutReady"
+ class="overflow-hidden flex flex-col items-stretch shrink-0 transition-all"
+ :style="{
+ height: expanded ? `${panelHeight}px` : '1rem',
+ }"
+ :data-parent-height="parentHeight"
+ >
+ <div
+ class="cursor-pointer flex items-center justify-start px-2 gap-1 overflow-y-visible bg-control-bg hover:bg-control-bg-hover"
+ :style="{
+ height: '1rem',
+ }"
+ @click="expanded = !expanded"
+ >
+ <ChevronDownIcon
+ class="w-4 h-4 transition-transform"
+ :class="expanded ? '' : 'rotate-180'"
+ />
+ <span v-if="true || expanded" class="text-xs origin-left">
+ {{ title }}
+ </span>
+ </div>
+ <div class="relative flex-1 overflow-hidden">
+ <MonacoEditor
+ v-if="status === 'pending' || status === 'success'"
+ :readonly="true"
+ :content="data?.trim() ?? ''"
+ :options="{
+ fontSize: '12px',
+ lineHeight: '18px',
+ }"
+ class="w-full h-full"
+ />
+ <pre v-if="expanded && status === 'error'" class="text-sm text-error">{{
+ error?.message ?? "Unknown error"
+ }}</pre>
+ <MaskSpinner v-if="status === 'pending'" />
+ </div>
+ </div>
+</template>
+
+<script setup lang="ts">
+import { useQuery } from "@tanstack/vue-query";
+import {
+ refDebounced,
+ useElementSize,
+ useLocalStorage,
+ useParentElement,
+} from "@vueuse/core";
+import { ChevronDownIcon } from "lucide-vue-next";
+import { computed, toRef } from "vue";
+import { MonacoEditor } from "@/components/MonacoEditor";
+import MaskSpinner from "@/components/misc/MaskSpinner.vue";
+import { sqlServiceClient } from "@/grpcweb";
+import type { ComposedDatabase } from "@/types";
+import {
+ DatabaseMetadata,
+ SchemaMetadata,
+} from "@/types/proto/v1/database_service";
+import { minmax } from "@/utils";
+import { extractGrpcErrorMessage } from "@/utils/grpcweb";
+import { useSchemaEditorContext } from "../context";
+
+const props = defineProps<{
+ db: ComposedDatabase;
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ title: string;
+ mocked: DatabaseMetadata | undefined;
+}>();
+
+const { hidePreview } = useSchemaEditorContext();
+const expanded = useLocalStorage("bb.schema-editor.preview.expanded", true);
+const parentElement = useParentElement();
+const { height: parentHeight } = useElementSize(parentElement);
+const layoutReady = computed(() => parentHeight.value > 0);
+const panelHeight = computed(() => {
+ const min = 8 * 16; // 8rem ~= 6 lines
+ const max = 16 * 16; // 16rem ~= 13 lines
+ const flexible = parentHeight.value * 0.4;
+ return minmax(flexible, min, max);
+});
+
+const engine = computed(() => props.db.instanceResource.engine);
+const mocked = toRef(props, "mocked");
+const debouncedMocked = refDebounced(mocked, 500);
+
+const { status, data, error } = useQuery({
+ queryKey: [engine, debouncedMocked],
+ queryFn: async () => {
+ if (!expanded.value) return "";
+ const metadata = debouncedMocked.value;
+ if (!metadata) return "";
+ console.log("call query");
+ try {
+ const response = await sqlServiceClient.stringifyMetadata(
+ {
+ engine: engine.value,
+ metadata,
+ },
+ {
+ silent: true,
+ }
+ );
+ return response.schema;
+ } catch (err) {
+ return Promise.reject(new Error(extractGrpcErrorMessage(err)));
+ }
+ },
+});
+</script>
diff --git a/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue b/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue
index 5e43e07dcc..f42b41f2c3 100644
--- a/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue
+++ b/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue
@@ -147,12 +147,12 @@
/>
</div>
- <TableSchemaPreview
- v-if="showTablePreview"
+ <PreviewPane
:db="db"
:database="database"
:schema="schema"
- :table="table"
+ :title="$t('schema-editor.preview-schema-text')"
+ :mocked="mocked"
/>
</div>
@@ -200,11 +200,12 @@ import { IndexIcon, TablePartitionIcon } from "@/components/Icon";
import { Drawer, DrawerContent } from "@/components/v2";
import { hasFeature, pushNotification } from "@/store/modules";
import type { ComposedDatabase } from "@/types";
-import type {
+import {
DatabaseMetadata,
- ForeignKeyMetadata,
- SchemaMetadata,
- TableMetadata,
+ SchemaConfig,
+ type ForeignKeyMetadata,
+ type SchemaMetadata,
+ type TableMetadata,
} from "@/types/proto/v1/database_service";
import {
ColumnMetadata,
@@ -234,8 +235,8 @@ import type { EditStatus } from "../types";
import ColumnSelectionSummary from "./ColumnSelectionSummary.vue";
import IndexesEditor from "./IndexesEditor";
import PartitionsEditor from "./PartitionsEditor";
+import PreviewPane from "./PreviewPane.vue";
import TableColumnEditor from "./TableColumnEditor";
-import TableSchemaPreview from "./TableSchemaPreview.vue";
type EditMode = "COLUMNS" | "INDEXES" | "PARTITIONS";
@@ -264,7 +265,6 @@ const {
project,
readonly,
events,
- showTablePreview,
options,
addTab,
markEditStatus,
@@ -576,6 +576,42 @@ const handleReorderColumn = (
arraySwap(columns, index, target);
};
+const mocked = computed(() => {
+ const { db, database, schema, table } = props;
+
+ const mockedTable = cloneDeep(table);
+ mockedTable.columns = mockedTable.columns.filter((column) => {
+ const status = getColumnStatus(db, { database, schema, table, column });
+ return status !== "dropped";
+ });
+ const mockedDatabase = DatabaseMetadata.fromJSON({
+ name: database.name,
+ characterSet: database.characterSet,
+ collation: database.collation,
+ schemas: [
+ {
+ name: schema.name,
+ tables: [mockedTable],
+ },
+ ],
+ });
+ const schemaConfig = database.schemaConfigs.find(
+ (sc) => sc.name === schema.name
+ );
+ const tableConfig = schemaConfig?.tableConfigs.find(
+ (tc) => tc.name === table.name
+ );
+ if (schemaConfig && tableConfig) {
+ mockedDatabase.schemaConfigs = [
+ SchemaConfig.fromJSON({
+ name: schemaConfig.name,
+ tableConfigs: [cloneDeep(tableConfig)],
+ }),
+ ];
+ }
+ return mockedDatabase;
+});
+
const markTableStatus = (status: EditStatus) => {
const oldStatus = statusForTable();
if (
diff --git a/frontend/src/components/SchemaEditorLite/Panels/ViewEditor.vue b/frontend/src/components/SchemaEditorLite/Panels/ViewEditor.vue
index 6e9a381134..27201b1af8 100644
--- a/frontend/src/components/SchemaEditorLite/Panels/ViewEditor.vue
+++ b/frontend/src/components/SchemaEditorLite/Panels/ViewEditor.vue
@@ -5,20 +5,33 @@
:readonly="disallowChangeView"
:status="status"
@update:code="handleUpdateDefinition"
- />
+ >
+ <template #preview>
+ <PreviewPane
+ :db="db"
+ :database="database"
+ :schema="schema"
+ :mocked="mocked"
+ :title="$t('schema-editor.preview-view-definition')"
+ />
+ </template>
+ </CommonCodeEditor>
</template>
<script setup lang="ts">
+import { cloneDeep } from "lodash-es";
import { computed } from "vue";
import type { ComposedDatabase } from "@/types";
-import type {
+import {
DatabaseMetadata,
- ViewMetadata,
- SchemaMetadata,
+ type ViewMetadata,
+ type SchemaMetadata,
+ SchemaConfig,
} from "@/types/proto/v1/database_service";
import { useSchemaEditorContext } from "../context";
import type { EditStatus } from "../types";
import CommonCodeEditor from "./CommonCodeEditor.vue";
+import PreviewPane from "./PreviewPane.vue";
const props = defineProps<{
db: ComposedDatabase;
@@ -62,6 +75,38 @@ const disallowChangeView = computed(() => {
return statusForSchema() === "dropped" || status.value === "dropped";
});
+const mocked = computed(() => {
+ const { database, schema, view } = props;
+
+ const mockedView = cloneDeep(view);
+ const mockedDatabase = DatabaseMetadata.fromJSON({
+ name: database.name,
+ characterSet: database.characterSet,
+ collation: database.collation,
+ schemas: [
+ {
+ name: schema.name,
+ views: [mockedView],
+ },
+ ],
+ });
+ const schemaConfig = database.schemaConfigs.find(
+ (sc) => sc.name === schema.name
+ );
+ const viewConfig = schemaConfig?.viewConfigs.find(
+ (vc) => vc.name === view.name
+ );
+ if (schemaConfig && viewConfig) {
+ mockedDatabase.schemaConfigs = [
+ SchemaConfig.fromJSON({
+ name: schemaConfig.name,
+ tableConfigs: [cloneDeep(viewConfig)],
+ }),
+ ];
+ }
+ return mockedDatabase;
+});
+
const handleUpdateDefinition = (code: string) => {
// eslint-disable-next-line vue/no-mutating-props
props.view.definition = code;
diff --git a/frontend/src/components/SchemaEditorLite/SchemaEditorLite.vue b/frontend/src/components/SchemaEditorLite/SchemaEditorLite.vue
index 194289aba6..78edf1c8f7 100644
--- a/frontend/src/components/SchemaEditorLite/SchemaEditorLite.vue
+++ b/frontend/src/components/SchemaEditorLite/SchemaEditorLite.vue
@@ -48,7 +48,7 @@ const props = defineProps<{
diffWhenReady?: boolean;
showLastUpdater?: boolean;
disableDiffColoring?: boolean;
- showTablePreview?: boolean;
+ hidePreview?: boolean;
}>();
const emit = defineEmits<{
(event: "update:selected-rollout-objects", objects: RolloutObject[]): void;
@@ -106,7 +106,7 @@ const context = provideSchemaEditorContext({
selectedRolloutObjects: toRef(props, "selectedRolloutObjects"),
showLastUpdater: toRef(props, "showLastUpdater"),
disableDiffColoring: toRef(props, "disableDiffColoring"),
- showTablePreview: toRef(props, "showTablePreview"),
+ hidePreview: toRef(props, "hidePreview"),
});
const { rebuildMetadataEdit, applyMetadataEdit, applySelectedMetadataEdit } =
useAlgorithm(context);
diff --git a/frontend/src/components/SchemaEditorLite/context/index.ts b/frontend/src/components/SchemaEditorLite/context/index.ts
index 8b74cb6368..b01c26a2b1 100644
--- a/frontend/src/components/SchemaEditorLite/context/index.ts
+++ b/frontend/src/components/SchemaEditorLite/context/index.ts
@@ -37,7 +37,7 @@ export const provideSchemaEditorContext = (params: {
selectedRolloutObjects: Ref<RolloutObject[] | undefined>;
showLastUpdater: Ref<boolean>;
disableDiffColoring: Ref<boolean>;
- showTablePreview: Ref<boolean>;
+ hidePreview: Ref<boolean>;
options?: Ref<SchemaEditorOptions>;
}) => {
const events = new Emittery() as SchemaEditorEvents;
diff --git a/frontend/src/components/SchemaTemplate/TableTemplateForm.vue b/frontend/src/components/SchemaTemplate/TableTemplateForm.vue
index 6a0290afef..e7622bfe63 100644
--- a/frontend/src/components/SchemaTemplate/TableTemplateForm.vue
+++ b/frontend/src/components/SchemaTemplate/TableTemplateForm.vue
@@ -261,7 +261,7 @@ const context = provideSchemaEditorContext({
selectedRolloutObjects: ref(undefined),
showLastUpdater: ref(false),
disableDiffColoring: ref(true),
- showTablePreview: ref(true),
+ hidePreview: ref(false),
});
const state = reactive<LocalState>({
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 405401bf06..e94ea2a71f 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -2577,7 +2577,8 @@
"views": "Views",
"procedures": "Procedures",
"functions": "Functions",
- "preview-schema-text": "Preview schema text"
+ "preview-schema-text": "Preview schema text",
+ "preview-view-definition": "Preview view definition"
},
"label": {
"empty-label-value": "Empty",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 72fec1cefe..5d2e606e76 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -2577,7 +2577,8 @@
"views": "Puntos de vista",
"procedures": "Trámites",
"functions": "Funciones",
- "preview-schema-text": "Vista previa del texto del esquema"
+ "preview-schema-text": "Vista previa del texto del esquema",
+ "preview-view-definition": "Definición de vista previa"
},
"label": {
"empty-label-value": "Valor",
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index 19e9f6e3e1..5e3e00b221 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -2577,7 +2577,8 @@
"views": "ビュー",
"procedures": "手順",
"functions": "関数",
- "preview-schema-text": "スキーマテキストのプレビュー"
+ "preview-schema-text": "スキーマテキストのプレビュー",
+ "preview-view-definition": "プレビュービューの定義"
},
"label": {
"empty-label-value": "ヌル",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 90d7e46992..8fcebcd0e8 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -2577,7 +2577,8 @@
"views": "视图",
"procedures": "存储过程",
"functions": "函数",
- "preview-schema-text": "预览文本 schema"
+ "preview-schema-text": "预览文本 schema",
+ "preview-view-definition": "预览视图定义"
},
"label": {
"empty-label-value": "空",