Skip to content

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

refactor(frontend): branch tree and database tree (#10222)

Liu Ji0e0e2dcf0f71
diff --git a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
index 96825f4819..58aa8b05e9 100644
--- a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
@@ -2,7 +2,14 @@
<BBModal
:title="$t('database.edit-schema')"
:trap-focus="false"
- class="schema-editor-modal-container !w-[96rem] h-auto overflow-auto !max-w-[calc(100vw-40px)] !max-h-[calc(100vh-40px)]"
+ class="schema-editor-modal-container overflow-auto"
+ style="
+ width: calc(100vw - 40px);
+ max-width: calc(100vw - 40px);
+ height: calc(100vh - 40px);
+ max-height: calc(100vh - 40px);
+ "
+ container-class="h-full flex flex-col gap-y-4"
@close="dismissModal"
>
<MaskSpinner
@@ -17,108 +24,95 @@
</span>
</MaskSpinner>
- <div class="w-full h-[46rem] max-h-full overflow-auto flex flex-col">
- <div class="flex-1">
- <NTabs
- v-model:value="state.selectedTab"
- type="card"
- style="height: 100%"
- >
- <NTabPane
- name="schema-editor"
- :tab="$t('schema-editor.self')"
- style="height: 100%"
- display-directive="show:lazy"
- >
- <SchemaEditorLite
- ref="schemaEditorRef"
- resource-type="database"
- :project="project"
- :targets="state.targets"
- :loading="state.isPreparingMetadata"
- :diff-when-ready="false"
- />
- </NTabPane>
- <NTabPane
- name="raw-sql"
- :tab="$t('schema-editor.raw-sql')"
- style="height: 100%"
- display-directive="show:lazy"
- >
- <div
- class="w-full h-full grid grid-rows-[50px,_1fr] overflow-y-auto"
- >
- <div
- class="w-full h-full shrink-0 flex flex-row justify-between items-center"
- >
- <div>{{ $t("sql-editor.self") }}</div>
- <div class="flex flex-row justify-end items-center space-x-3">
- <NButton @click="onUploaderClick">
- <template #icon>
- <heroicons-outline:arrow-up-tray
- class="w-4 h-auto text-gray-500"
- />
- </template>
- {{ $t("issue.upload-sql") }}
- <input
- id="sql-file-input"
- ref="sqlFileUploader"
- type="file"
- accept=".sql,.txt,application/sql,text/plain"
- class="hidden"
- @change="handleUploadFile"
- />
- </NButton>
- <NButton @click="handleSyncSQLFromSchemaEditor">
- <template #icon>
- <heroicons-outline:arrow-path
- class="w-4 h-auto text-gray-500"
- />
- </template>
- {{ $t("schema-editor.sync-sql-from-schema-editor") }}
- </NButton>
- </div>
- </div>
- <MonacoEditor
- v-model:content="state.editStatement"
- class="border w-[calc(100%-2px)] h-[calc(100%-2px)]"
- data-label="bb-schema-editor-sql-editor"
- :auto-focus="false"
- :dialect="dialectOfEngineV1(databaseEngine)"
- />
- </div>
- </NTabPane>
- <template #suffix>
- <SchemaEditorSQLCheckButton
- :database-list="databaseList"
- :get-statement="generateOrGetEditingDDL"
- />
- </template>
- </NTabs>
- </div>
-
- <div class="w-full flex flex-row justify-between items-center mt-4 pr-px">
- <div class="">
+ <NTabs v-model:value="state.selectedTab" type="card" style="flex: 1">
+ <NTabPane
+ name="schema-editor"
+ :tab="$t('schema-editor.self')"
+ style="flex: 1"
+ display-directive="show:lazy"
+ >
+ <SchemaEditorLite
+ ref="schemaEditorRef"
+ resource-type="database"
+ :project="project"
+ :targets="state.targets"
+ :loading="state.isPreparingMetadata"
+ :diff-when-ready="false"
+ />
+ </NTabPane>
+ <NTabPane
+ name="raw-sql"
+ :tab="$t('schema-editor.raw-sql')"
+ style="flex: 1"
+ display-directive="show:lazy"
+ >
+ <div class="w-full h-full grid grid-rows-[50px,_1fr] overflow-y-auto">
<div
- v-if="isBatchMode"
- class="flex flex-row items-center text-sm text-gray-500"
+ class="w-full h-full shrink-0 flex flex-row justify-between items-center"
>
- <heroicons-outline:exclamation-circle class="w-4 h-auto mr-1" />
- {{ $t("schema-editor.tenant-mode-tips") }}
+ <div>{{ $t("sql-editor.self") }}</div>
+ <div class="flex flex-row justify-end items-center space-x-3">
+ <NButton @click="onUploaderClick">
+ <template #icon>
+ <heroicons-outline:arrow-up-tray
+ class="w-4 h-auto text-gray-500"
+ />
+ </template>
+ {{ $t("issue.upload-sql") }}
+ <input
+ id="sql-file-input"
+ ref="sqlFileUploader"
+ type="file"
+ accept=".sql,.txt,application/sql,text/plain"
+ class="hidden"
+ @change="handleUploadFile"
+ />
+ </NButton>
+ <NButton @click="handleSyncSQLFromSchemaEditor">
+ <template #icon>
+ <heroicons-outline:arrow-path
+ class="w-4 h-auto text-gray-500"
+ />
+ </template>
+ {{ $t("schema-editor.sync-sql-from-schema-editor") }}
+ </NButton>
+ </div>
</div>
+ <MonacoEditor
+ v-model:content="state.editStatement"
+ class="border w-[calc(100%-2px)] h-[calc(100%-2px)]"
+ data-label="bb-schema-editor-sql-editor"
+ :auto-focus="false"
+ :dialect="dialectOfEngineV1(databaseEngine)"
+ />
</div>
- <div class="flex justify-end items-center space-x-3">
- <NButton @click="dismissModal">
- {{ $t("common.cancel") }}
- </NButton>
- <NButton
- type="primary"
- :disabled="!allowPreviewIssue"
- @click="handlePreviewIssue"
- >
- {{ $t("schema-editor.preview-issue") }}
- </NButton>
- </div>
+ </NTabPane>
+ <template #suffix>
+ <SchemaEditorSQLCheckButton
+ :database-list="databaseList"
+ :get-statement="generateOrGetEditingDDL"
+ />
+ </template>
+ </NTabs>
+
+ <div class="w-full flex flex-row justify-between items-center">
+ <div class="flex flex-row items-center text-sm text-gray-500">
+ <template v-if="isBatchMode">
+ <heroicons-outline:exclamation-circle class="w-4 h-auto mr-1" />
+ {{ $t("schema-editor.tenant-mode-tips") }}
+ </template>
+ </div>
+ <div class="flex justify-end items-center space-x-3">
+ <NButton @click="dismissModal">
+ {{ $t("common.cancel") }}
+ </NButton>
+ <NButton
+ type="primary"
+ :disabled="!allowPreviewIssue"
+ @click="handlePreviewIssue"
+ >
+ {{ $t("schema-editor.preview-issue") }}
+ </NButton>
</div>
</div>
</BBModal>
@@ -564,10 +558,3 @@ const generateIssueName = (
return issueNameParts.join(" ");
};
</script>
-
-<style lang="postcss">
-.schema-editor-modal-container > .modal-container {
- @apply w-full h-[46rem] overflow-auto grid;
- grid-template-rows: min-content 1fr min-content;
-}
-</style>
diff --git a/frontend/src/components/SchemaEditorLite/Aside/Aside.vue b/frontend/src/components/SchemaEditorLite/Aside/Aside.vue
index ef0719692a..91706b02e7 100644
--- a/frontend/src/components/SchemaEditorLite/Aside/Aside.vue
+++ b/frontend/src/components/SchemaEditorLite/Aside/Aside.vue
@@ -1,12 +1,7 @@
<template>
- <BranchTree v-if="resourceType === 'branch'" />
- <DatabaseTree v-else-if="resourceType === 'database'" />
+ <Tree />
</template>
<script lang="ts" setup>
-import { useSchemaEditorContext } from "../context";
-import BranchTree from "./BranchTree.vue";
-import DatabaseTree from "./DatabaseTree.vue";
-
-const { resourceType } = useSchemaEditorContext();
+import Tree from "./Tree.vue";
</script>
diff --git a/frontend/src/components/SchemaEditorLite/Aside/Tree.vue b/frontend/src/components/SchemaEditorLite/Aside/Tree.vue
new file mode 100644
index 0000000000..fa06c3bd0e
--- /dev/null
+++ b/frontend/src/components/SchemaEditorLite/Aside/Tree.vue
@@ -0,0 +1,808 @@
+<template>
+ <div
+ class="w-full h-full pl-1 pr-2 flex flex-col gap-y-2 relative overflow-y-hidden"
+ >
+ <div class="w-full sticky top-0 pt-2 h-12 bg-white z-10">
+ <NInput
+ v-model:value="searchPattern"
+ :placeholder="
+ resourceType === 'branch'
+ ? $t('schema-designer.search-tables')
+ : $t('schema-editor.search-database-and-table')
+ "
+ >
+ <template #prefix>
+ <heroicons-outline:search class="w-4 h-auto text-gray-300" />
+ </template>
+ </NInput>
+ </div>
+ <div
+ ref="treeContainerElRef"
+ class="schema-editor-tree flex-1 pb-1 text-sm overflow-hidden select-none"
+ :data-height="treeContainerHeight"
+ >
+ <NTree
+ v-if="treeContainerHeight > 0"
+ ref="treeRef"
+ block-line
+ virtual-scroll
+ :style="{
+ height: `${treeContainerHeight}px`,
+ }"
+ :data="treeDataRef"
+ :pattern="searchPattern"
+ :render-prefix="renderPrefix"
+ :render-label="renderLabel"
+ :render-suffix="renderSuffix"
+ :node-props="nodeProps"
+ :expanded-keys="expandedKeysRef"
+ :selected-keys="selectedKeysRef"
+ :show-irrelevant-nodes="false"
+ :theme-overrides="{ nodeHeight: '28px' }"
+ @update:expanded-keys="handleExpandedKeysChange"
+ />
+ <NDropdown
+ trigger="manual"
+ placement="bottom-start"
+ :show="contextMenu.showDropdown"
+ :options="contextMenuOptions"
+ :x="contextMenu.clientX"
+ :y="contextMenu.clientY"
+ to="body"
+ @select="handleContextMenuDropdownSelect"
+ @clickoutside="handleDropdownClickoutside"
+ />
+ </div>
+ </div>
+
+ <SchemaNameModal
+ v-if="state.schemaNameModalContext !== undefined"
+ :database="state.schemaNameModalContext.db"
+ :metadata="state.schemaNameModalContext.database"
+ @close="state.schemaNameModalContext = undefined"
+ />
+
+ <TableNameModal
+ v-if="state.tableNameModalContext !== undefined"
+ :database="state.tableNameModalContext.db"
+ :metadata="state.tableNameModalContext.database"
+ :schema="state.tableNameModalContext.schema"
+ :table="state.tableNameModalContext.table"
+ @close="state.tableNameModalContext = undefined"
+ />
+</template>
+
+<script lang="ts" setup>
+import { useElementSize } from "@vueuse/core";
+import { cloneDeep, debounce, escape, head } from "lodash-es";
+import { MoreHorizontalIcon, CopyIcon } from "lucide-vue-next";
+import {
+ TreeOption,
+ NInput,
+ NDropdown,
+ NTree,
+ DropdownOption,
+ NPerformantEllipsis,
+} from "naive-ui";
+import {
+ computed,
+ onMounted,
+ watch,
+ ref,
+ h,
+ reactive,
+ nextTick,
+ VNodeChild,
+} from "vue";
+import { useI18n } from "vue-i18n";
+import { DatabaseIcon, SchemaIcon, TableIcon } from "@/components/Icon";
+import { InstanceV1EngineIcon } from "@/components/v2";
+import { useEmitteryEventListener } from "@/composables/useEmitteryEventListener";
+import { ComposedDatabase } from "@/types";
+import { Engine } from "@/types/proto/v1/common";
+import {
+ DatabaseMetadata,
+ SchemaMetadata,
+ TableMetadata,
+} from "@/types/proto/v1/database_service";
+import { getHighlightHTMLByKeyWords, isDescendantOf } from "@/utils";
+import SchemaNameModal from "../Modals/SchemaNameModal.vue";
+import TableNameModal from "../Modals/TableNameModal.vue";
+import { useSchemaEditorContext } from "../context";
+import { keyForResource, keyForResourceName } from "../context/common";
+import { engineHasSchema } from "../engine-specs";
+import NodeCheckbox from "./NodeCheckbox";
+import {
+ TreeNode,
+ TreeNodeForInstance,
+ TreeNodeForSchema,
+ TreeNodeForTable,
+ buildTree,
+} from "./common";
+
+interface TreeContextMenu {
+ showDropdown: boolean;
+ clientX: number;
+ clientY: number;
+ treeNode?: TreeNode;
+}
+interface LocalState {
+ shouldRelocateTreeNode: boolean;
+ schemaNameModalContext?: {
+ db: ComposedDatabase;
+ database: DatabaseMetadata;
+ schema?: SchemaMetadata;
+ };
+ tableNameModalContext?: {
+ db: ComposedDatabase;
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ table?: TableMetadata;
+ };
+}
+
+const { t } = useI18n();
+const {
+ events,
+ resourceType,
+ targets,
+ readonly,
+ currentTab,
+ addTab,
+ markEditStatus,
+ removeEditStatus,
+ getSchemaStatus,
+ getTableStatus,
+ getColumnStatus,
+ selectionEnabled,
+ upsertTableConfig,
+ queuePendingScrollToTable,
+ queuePendingScrollToColumn,
+} = useSchemaEditorContext();
+const state = reactive<LocalState>({
+ shouldRelocateTreeNode: false,
+});
+
+const contextMenu = reactive<TreeContextMenu>({
+ showDropdown: false,
+ clientX: 0,
+ clientY: 0,
+ treeNode: undefined,
+});
+const treeContainerElRef = ref<HTMLElement>();
+const { height: treeContainerHeight } = useElementSize(
+ treeContainerElRef,
+ undefined,
+ {
+ box: "content-box",
+ }
+);
+const treeRef = ref<InstanceType<typeof NTree>>();
+const searchPattern = ref("");
+const expandedKeysRef = ref<string[]>([]);
+const selectedKeysRef = ref<string[]>([]);
+const treeDataRef = ref<TreeNode[]>([]);
+const treeNodeMap = new Map<string, TreeNode>();
+
+const contextMenuOptions = computed(() => {
+ const treeNode = contextMenu.treeNode;
+ if (!treeNode) return [];
+ if (treeNode.type === "instance") return [];
+ const { engine } = treeNode.db.instanceEntity;
+ if (treeNode.type === "database") {
+ const options: DropdownOption[] = [];
+ if (engineHasSchema(engine)) {
+ options.push({
+ key: "create-schema",
+ label: t("schema-editor.actions.create-schema"),
+ });
+ } else {
+ options.push({
+ key: "create-table",
+ label: t("schema-editor.actions.create-table"),
+ });
+ }
+ return options;
+ } else if (treeNode.type === "schema") {
+ const options: DropdownOption[] = [];
+ if (engine === Engine.POSTGRES) {
+ const status = getSchemaStatus(treeNode.db, treeNode.metadata);
+ if (status === "dropped") {
+ options.push({
+ key: "restore",
+ label: t("schema-editor.actions.restore"),
+ });
+ } else {
+ options.push({
+ key: "create-table",
+ label: t("schema-editor.actions.create-table"),
+ });
+ options.push({
+ key: "rename",
+ label: t("schema-editor.actions.rename"),
+ });
+ options.push({
+ key: "drop-schema",
+ label: t("schema-editor.actions.drop-schema"),
+ });
+ }
+ }
+ return options;
+ } else if (treeNode.type === "table") {
+ const options: DropdownOption[] = [];
+ const status = getTableStatus(treeNode.db, treeNode.metadata);
+ if (status === "dropped") {
+ options.push({
+ key: "restore",
+ label: t("schema-editor.actions.restore"),
+ });
+ } else {
+ options.push({
+ key: "rename",
+ label: t("schema-editor.actions.rename"),
+ });
+ options.push({
+ key: "drop",
+ label: t("schema-editor.actions.drop-table"),
+ });
+ }
+ return options;
+ }
+
+ return [];
+});
+
+const upsertExpandedKeys = (key: string) => {
+ if (expandedKeysRef.value.includes(key)) return;
+ expandedKeysRef.value.push(key);
+};
+const expandNodeRecursively = (node: TreeNode | undefined) => {
+ if (!node) return;
+ if (node.type === "column") {
+ // column nodes are not expandable
+ expandNodeRecursively(node.parent);
+ }
+ if (node.type === "table") {
+ const key = keyForResource(node.db, node.metadata);
+ upsertExpandedKeys(key);
+ expandNodeRecursively(node.parent);
+ }
+ if (node.type === "schema") {
+ const key = keyForResource(node.db, node.metadata);
+ upsertExpandedKeys(key);
+ expandNodeRecursively(node.parent);
+ }
+ if (node.type === "database") {
+ const key = node.db.name;
+ upsertExpandedKeys(key);
+ expandNodeRecursively(node.parent);
+ }
+ if (node.type === "instance") {
+ const key = node.instance.name;
+ upsertExpandedKeys(key);
+ }
+};
+const findFirstSchemaNode = (
+ nodeList: TreeNodeForSchema[] | TreeNodeForInstance[]
+) => {
+ const first = nodeList[0];
+ if (!first) return undefined;
+ if (first.type === "schema") {
+ return first;
+ }
+ const firstDatabaseNode = head(first.children);
+ return head(firstDatabaseNode?.children);
+};
+const buildDatabaseTreeData = (openFirstChild: boolean) => {
+ const treeNodeList = buildTree(targets.value, treeNodeMap, {
+ byInstance: true,
+ });
+ treeDataRef.value = treeNodeList;
+
+ if (openFirstChild) {
+ const firstSchemaNode = findFirstSchemaNode(treeNodeList);
+ if (firstSchemaNode) {
+ nextTick(() => {
+ // Auto expand the first tree node.
+ openTabForTreeNode(firstSchemaNode);
+ });
+ }
+ }
+};
+const debouncedBuildDatabaseTreeData = debounce(buildDatabaseTreeData, 100);
+useEmitteryEventListener(events, "rebuild-tree", (params) => {
+ debouncedBuildDatabaseTreeData(params.openFirstChild);
+});
+
+const tabWatchKey = computed(() => {
+ const tab = currentTab.value;
+ if (!tab) return undefined;
+ if (tab.type === "database") {
+ return keyForResourceName(tab.database.name, tab.selectedSchema);
+ }
+ return keyForResource(tab.database, tab.metadata);
+});
+// Sync tree expandedKeys and selectedKeys with tabs
+watch(tabWatchKey, () => {
+ requestAnimationFrame(() => {
+ const tab = currentTab.value;
+ if (!tab) {
+ selectedKeysRef.value = [];
+ return;
+ }
+
+ if (tab.type === "database") {
+ const { database, selectedSchema: schema } = tab;
+ if (schema) {
+ const key = keyForResourceName(database.name, schema);
+ const node = treeNodeMap.get(key);
+ if (node) {
+ expandNodeRecursively(node);
+ }
+ selectedKeysRef.value = [key];
+ }
+ } else if (tab.type === "table") {
+ const {
+ database,
+ metadata: { schema, table },
+ } = tab;
+ const schemaKey = keyForResource(database, { schema });
+ const schemaNode = treeNodeMap.get(schemaKey);
+ if (schemaNode) {
+ expandNodeRecursively(schemaNode);
+ }
+ const tableKey = keyForResource(database, { schema, table });
+ selectedKeysRef.value = [tableKey];
+ }
+
+ if (state.shouldRelocateTreeNode) {
+ nextTick(() => {
+ treeRef.value?.scrollTo({
+ key: selectedKeysRef.value[0],
+ });
+ });
+ }
+ });
+});
+
+const openTabForTreeNode = (node: TreeNode) => {
+ state.shouldRelocateTreeNode = false;
+
+ if (node.type === "column") {
+ openTabForTreeNode(node.parent);
+ queuePendingScrollToColumn({
+ db: node.db,
+ metadata: node.metadata,
+ });
+ return;
+ } else if (node.type === "table") {
+ expandNodeRecursively(node);
+ addTab({
+ type: "table",
+ database: node.db,
+ metadata: node.metadata,
+ });
+ } else if (node.type === "schema") {
+ expandNodeRecursively(node);
+ addTab({
+ type: "database",
+ database: node.db,
+ metadata: node.metadata,
+ selectedSchema: node.metadata.schema.name,
+ });
+ }
+
+ state.shouldRelocateTreeNode = true;
+};
+
+onMounted(() => {
+ buildDatabaseTreeData(/* openFirstChild */ true);
+});
+
+// Render prefix icons before label text.
+const renderPrefix = ({ option }: { option: TreeOption }) => {
+ const treeNode = option as TreeNode;
+
+ if (treeNode.type === "instance") {
+ const { instance } = treeNode;
+ const children = [
+ h(InstanceV1EngineIcon, {
+ instance,
+ }),
+ h(
+ "span",
+ {
+ class: "text-gray-500 text-sm",
+ },
+ `(${instance.environmentEntity.title})`
+ ),
+ ];
+
+ return h("span", { class: "flex items-center gap-x-1" }, children);
+ } else if (treeNode.type === "database") {
+ return h("span", { class: "flex items-center gap-x-1" }, [
+ h(DatabaseIcon, {
+ class: "w-4 h-auto text-gray-400",
+ }),
+ h(
+ "span",
+ {
+ class: "text-gray-500 text-sm",
+ },
+ `(${treeNode.db.effectiveEnvironmentEntity.title})`
+ ),
+ ]);
+ }
+
+ const children: VNodeChild[] = [];
+ if (selectionEnabled.value) {
+ children.push(
+ h(NodeCheckbox, {
+ node: treeNode,
+ })
+ );
+ }
+ if (treeNode.type === "schema") {
+ children.push(
+ h(SchemaIcon, {
+ class: "w-4 h-auto text-gray-400",
+ })
+ );
+ } else if (treeNode.type === "table") {
+ children.push(
+ h(TableIcon, {
+ class: "w-4 h-auto text-gray-400",
+ })
+ );
+ }
+ if (children.length > 0) {
+ return h("div", { class: "flex flex-row items-center gap-x-1" }, children);
+ }
+ return null;
+};
+
+// Render label text.
+const renderLabel = ({ option }: { option: TreeOption }) => {
+ const treeNode = option as TreeNode;
+ const additionalClassList: string[] = ["select-none"];
+ let label = treeNode.label;
+
+ if (treeNode.type === "schema") {
+ const { db, metadata } = treeNode;
+ additionalClassList.push(getSchemaStatus(db, metadata));
+
+ if (db.instanceEntity.engine !== Engine.POSTGRES) {
+ label = t("db.tables");
+ } else {
+ label = metadata.schema.name;
+ }
+ } else if (treeNode.type === "table") {
+ const { db, metadata } = treeNode;
+ additionalClassList.push(getTableStatus(db, metadata));
+ label = metadata.table.name;
+ } else if (treeNode.type === "column") {
+ const { db, metadata } = treeNode;
+ additionalClassList.push(getColumnStatus(db, metadata));
+ const { name } = metadata.column;
+ if (name) {
+ label = name;
+ } else {
+ label = `<${t("common.untitled")}>`;
+ additionalClassList.push("text-control-placeholder italic");
+ }
+ }
+ return h(
+ NPerformantEllipsis,
+ {
+ class: additionalClassList.join(" "),
+ },
+ () => [
+ h("span", {
+ innerHTML: getHighlightHTMLByKeyWords(
+ escape(label),
+ escape(searchPattern.value)
+ ),
+ }),
+ ]
+ );
+};
+
+// Render a 'menu' icon in the right of the node
+const renderSuffix = ({ option }: { option: TreeOption }) => {
+ if (readonly.value) {
+ return null;
+ }
+ const treeNode = option as TreeNode;
+ const menuIcon = h(MoreHorizontalIcon, {
+ class: "w-4 h-auto text-gray-600",
+ onClick: (e) => {
+ handleShowDropdown(e, treeNode);
+ },
+ });
+ if (treeNode.type === "database") {
+ return menuIcon;
+ } else if (treeNode.type === "schema") {
+ const { engine } = treeNode.db.instanceEntity;
+ if (engine === Engine.POSTGRES) {
+ return menuIcon;
+ }
+ } else if (treeNode.type === "table") {
+ const icons = [menuIcon];
+
+ if (!readonly.value) {
+ const duplicateIcon = h(CopyIcon, {
+ class: "w-4 h-auto mr-2 text-gray-600",
+ onClick: (e) => {
+ e.preventDefault();
+ e.stopPropagation();
+ e.stopImmediatePropagation();
+
+ handleDuplicateTable(treeNode);
+ },
+ });
+ icons.unshift(duplicateIcon);
+ }
+ return icons;
+ }
+ return null;
+};
+
+const handleDuplicateTable = (treeNode: TreeNodeForTable) => {
+ const { db } = treeNode;
+ const { schema, table } = treeNode.metadata;
+ const flattenTableList = treeNode.metadata.database.schemas.flatMap(
+ (schema) => schema.tables
+ );
+ const getOriginalName = (name: string): string => {
+ return name.replace(/_copy[0-9]{0,}$/, "");
+ };
+
+ const extractDuplicateNumber = (name: string): number => {
+ const match = /_copy[0-9]{0,}$/.exec(name);
+ if (!match) {
+ return -1;
+ }
+
+ const num = Number(match[0].replace("_copy", "0"));
+ if (Number.isNaN(num)) {
+ return -1;
+ }
+ return num;
+ };
+
+ const getDuplicateName = (name: string): string => {
+ const num = extractDuplicateNumber(name);
+ if (num < 0) {
+ return `${name}_copy`;
+ }
+ return `${getOriginalName(name)}_copy${num + 1}`;
+ };
+
+ const matchPattern = new RegExp(
+ `^${getOriginalName(table.name)}` + "(_copy[0-9]{0,}){0,1}$"
+ );
+ const copiedTableNames = flattenTableList
+ .filter((table) => {
+ return matchPattern.test(table.name);
+ })
+ .sort((t1, t2) => {
+ return extractDuplicateNumber(t1.name) - extractDuplicateNumber(t2.name);
+ });
+ const targetName = copiedTableNames.slice(-1)[0]?.name ?? table.name;
+
+ const newTable = cloneDeep(table);
+ newTable.name = getDuplicateName(targetName);
+ schema.tables.push(newTable);
+ markEditStatus(
+ db,
+ {
+ ...treeNode.metadata,
+ table: newTable,
+ },
+ "created"
+ );
+ newTable.columns.forEach((newColumn) => {
+ markEditStatus(
+ db,
+
+ {
+ ...treeNode.metadata,
+ table: newTable,
+ column: newColumn,
+ },
+ "created"
+ );
+ });
+ const tableConfig = treeNode.metadata.database.schemaConfigs
+ .find((sc) => sc.name === schema.name)
+ ?.tableConfigs.find((tc) => tc.name === table.name);
+ if (tableConfig) {
+ const tableConfigCopy = cloneDeep(tableConfig);
+ tableConfigCopy.name = newTable.name;
+ upsertTableConfig(
+ db,
+ {
+ ...treeNode.metadata,
+ table: newTable,
+ },
+ (config) => {
+ Object.assign(config, tableConfigCopy);
+ }
+ );
+ }
+ addTab({
+ type: "table",
+ database: db,
+ metadata: {
+ ...treeNode.metadata,
+ table: newTable,
+ },
+ });
+ queuePendingScrollToTable({
+ db,
+ metadata: {
+ ...treeNode.metadata,
+ table: newTable,
+ },
+ });
+ events.emit("rebuild-tree", {
+ openFirstChild: false,
+ });
+};
+
+const handleShowDropdown = (e: MouseEvent, treeNode: TreeNode) => {
+ e.preventDefault();
+ e.stopPropagation();
+ contextMenu.treeNode = treeNode;
+ contextMenu.showDropdown = true;
+ contextMenu.clientX = e.clientX;
+ contextMenu.clientY = e.clientY;
+ selectedKeysRef.value = [treeNode.key];
+};
+
+// Set event handler to tree nodes.
+const nodeProps = ({ option }: { option: TreeOption }) => {
+ const treeNode = option as TreeNode;
+ return {
+ async onClick(e: MouseEvent) {
+ // Check if clicked on the content part.
+ // And ignore the fold/unfold arrow.
+ if (isDescendantOf(e.target as Element, ".n-tree-node-content")) {
+ state.shouldRelocateTreeNode = false;
+ if (treeNode.type === "instance") {
+ expandNodeRecursively(treeNode);
+ } else if (treeNode.type === "database") {
+ expandNodeRecursively(treeNode);
+ addTab({
+ type: "database",
+ database: treeNode.db,
+ metadata: treeNode.metadata,
+ selectedSchema: head(treeNode.metadata.database.schemas)?.name,
+ });
+ } else {
+ openTabForTreeNode(treeNode);
+ }
+ } else {
+ nextTick(() => {
+ selectedKeysRef.value = [];
+ });
+ }
+ },
+ oncontextmenu(e: MouseEvent) {
+ handleShowDropdown(e, treeNode);
+ },
+ };
+};
+
+const handleContextMenuDropdownSelect = async (key: string) => {
+ const treeNode = contextMenu.treeNode;
+ if (!treeNode) return;
+ if (treeNode.type === "database") {
+ const engine = treeNode.db.instanceEntity.engine;
+ if (key === "create-table") {
+ if (!engineHasSchema(engine)) {
+ const schema = head(treeNode.metadata.database.schemas);
+ if (!schema) {
+ return;
+ }
+ state.tableNameModalContext = {
+ db: treeNode.db,
+ database: treeNode.metadata.database,
+ schema: schema,
+ table: undefined,
+ };
+ }
+ } else if (key === "create-schema") {
+ if (engineHasSchema(engine)) {
+ state.schemaNameModalContext = {
+ db: treeNode.db,
+ database: treeNode.metadata.database,
+ schema: undefined,
+ };
+ }
+ }
+ } else if (treeNode.type === "schema") {
+ if (key === "create-table") {
+ state.tableNameModalContext = {
+ db: treeNode.db,
+ database: treeNode.metadata.database,
+ schema: treeNode.metadata.schema,
+ table: undefined,
+ };
+ } else if (key === "drop-schema") {
+ markEditStatus(treeNode.db, treeNode.metadata, "dropped");
+ } else if (key === "restore") {
+ removeEditStatus(treeNode.db, treeNode.metadata, /* recursive */ false);
+ }
+ } else if (treeNode.type === "table") {
+ if (key === "rename") {
+ state.tableNameModalContext = {
+ db: treeNode.db,
+ database: treeNode.metadata.database,
+ schema: treeNode.metadata.schema,
+ table: treeNode.metadata.table,
+ };
+ } else if (key === "drop") {
+ markEditStatus(treeNode.db, treeNode.metadata, "dropped");
+ } else if (key === "restore") {
+ removeEditStatus(treeNode.db, treeNode.metadata, /* recursive */ false);
+ }
+ }
+ contextMenu.showDropdown = false;
+};
+
+const handleDropdownClickoutside = (e: MouseEvent) => {
+ if (
+ !isDescendantOf(e.target as Element, ".n-tree-node-wrapper") ||
+ e.button !== 2
+ ) {
+ selectedKeysRef.value = [];
+ contextMenu.showDropdown = false;
+ }
+};
+
+const handleExpandedKeysChange = (expandedKeys: string[]) => {
+ expandedKeysRef.value = expandedKeys;
+};
+</script>
+
+<style lang="postcss" scoped>
+.schema-editor-tree :deep(.n-tree-node-wrapper) {
+ @apply !p-0;
+}
+.schema-editor-tree :deep(.n-tree-node-content) {
+ @apply !pl-2 text-sm;
+}
+.schema-editor-tree :deep(.n-tree-node-indent) {
+ width: 0.25rem;
+}
+.schema-editor-tree :deep(.n-tree-node-content__prefix) {
+ @apply shrink-0 !mr-1;
+}
+.schema-editor-tree
+ :deep(.n-tree-node-wrapper:hover .n-tree-node-content__suffix) {
+ @apply !flex;
+}
+.schema-editor-tree
+ :deep(.n-tree-node-wrapper
+ .n-tree-node--selected
+ .n-tree-node-content__suffix) {
+ @apply !flex;
+}
+
+.schema-editor-tree :deep(.n-tree-node-content__text) {
+ @apply truncate mr-1;
+}
+.schema-editor-tree :deep(.n-tree-node-switcher) {
+ @apply px-0 !w-4 !h-7;
+}
+.schema-editor-tree :deep(.n-tree-node-content .created) {
+ @apply text-green-700;
+}
+.schema-editor-tree :deep(.n-tree-node-content .dropped) {
+ @apply text-red-700 line-through;
+}
+.schema-editor-tree :deep(.n-tree-node-content .updated) {
+ @apply text-yellow-700;
+}
+</style>
diff --git a/frontend/src/components/SchemaEditorLite/Aside/common.ts b/frontend/src/components/SchemaEditorLite/Aside/common.ts
new file mode 100644
index 0000000000..8398cd7832
--- /dev/null
+++ b/frontend/src/components/SchemaEditorLite/Aside/common.ts
@@ -0,0 +1,236 @@
+import { TreeOption } from "naive-ui";
+import { ComposedDatabase, ComposedInstance } from "@/types";
+import {
+ ColumnMetadata,
+ DatabaseMetadata,
+ SchemaMetadata,
+ TableMetadata,
+} from "@/types/proto/v1/database_service";
+import { groupBy } from "@/utils";
+import { keyForResource } from "../context/common";
+import { EditTarget } from "../types";
+
+export interface BaseTreeNode extends TreeOption {
+ key: string;
+ label: string;
+ isLeaf: boolean;
+ children?: TreeNode[];
+}
+
+export interface TreeNodeForInstance extends BaseTreeNode {
+ type: "instance";
+ instance: ComposedInstance;
+ children: TreeNodeForDatabase[];
+}
+
+export interface TreeNodeForDatabase extends BaseTreeNode {
+ type: "database";
+ db: ComposedDatabase;
+ parent?: TreeNodeForInstance;
+ metadata: {
+ database: DatabaseMetadata;
+ };
+ children: TreeNodeForSchema[];
+}
+
+export interface TreeNodeForSchema extends BaseTreeNode {
+ type: "schema";
+ db: ComposedDatabase;
+ parent?: TreeNodeForDatabase;
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ };
+ children: TreeNodeForTable[];
+}
+
+export interface TreeNodeForTable extends BaseTreeNode {
+ type: "table";
+ db: ComposedDatabase;
+ parent: TreeNodeForSchema;
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ table: TableMetadata;
+ };
+ children: TreeNodeForColumn[];
+}
+
+export interface TreeNodeForColumn extends BaseTreeNode {
+ type: "column";
+ db: ComposedDatabase;
+ parent: TreeNodeForTable;
+ metadata: {
+ database: DatabaseMetadata;
+ schema: SchemaMetadata;
+ table: TableMetadata;
+ column: ColumnMetadata;
+ };
+ children: undefined;
+ isLeaf: true;
+}
+
+export type TreeNode =
+ | TreeNodeForInstance
+ | TreeNodeForDatabase
+ | TreeNodeForSchema
+ | TreeNodeForTable
+ | TreeNodeForColumn;
+
+export type BuildTreeOptions = {
+ byInstance: boolean;
+};
+export const buildTree = (
+ targets: EditTarget[],
+ map: Map<string, TreeNode>,
+ options: BuildTreeOptions
+) => {
+ map.clear();
+ if (options.byInstance) {
+ return buildInstanceNodeList(targets, map);
+ } else {
+ const target = targets[0];
+ return buildSchemaNodeList(
+ target.metadata.schemas,
+ map,
+ target.database,
+ target.metadata,
+ undefined
+ );
+ }
+};
+
+const buildInstanceNodeList = (
+ targets: EditTarget[],
+ map: Map<string, TreeNode>
+) => {
+ const groupedByInstance = groupBy(
+ targets,
+ (target) => target.database.instance
+ );
+ return Array.from(groupedByInstance).map(([_, targets]) => {
+ const instance = targets[0].database.instanceEntity;
+ const instanceNode: TreeNodeForInstance = {
+ type: "instance",
+ key: instance.name,
+ label: instance.title,
+ isLeaf: false,
+ instance,
+ children: [],
+ };
+ map.set(instanceNode.key, instanceNode);
+ instanceNode.children = buildDatabaseNodeList(targets, map, instanceNode);
+ return instanceNode;
+ });
+};
+
+const buildDatabaseNodeList = (
+ targets: EditTarget[],
+ map: Map<string, TreeNode>,
+ parent: TreeNodeForInstance | undefined
+) => {
+ return targets.map((target) => {
+ const db = target.database;
+ const databaseNode: TreeNodeForDatabase = {
+ type: "database",
+ key: db.name,
+ parent,
+ label: db.databaseName,
+ isLeaf: false,
+ db,
+ metadata: {
+ database: target.metadata,
+ },
+ children: [],
+ };
+ map.set(databaseNode.key, databaseNode);
+ databaseNode.children = buildSchemaNodeList(
+ target.metadata.schemas,
+ map,
+ db,
+ target.metadata,
+ databaseNode
+ );
+ return databaseNode;
+ });
+};
+const buildSchemaNodeList = (
+ schemas: SchemaMetadata[],
+ map: Map<string, TreeNode>,
+ db: ComposedDatabase,
+ database: DatabaseMetadata,
+ parent: TreeNodeForDatabase | undefined
+) => {
+ return schemas.map((schema) => {
+ const metadata = {
+ database,
+ schema,
+ };
+ const schemaNode: TreeNodeForSchema = {
+ type: "schema",
+ parent: parent,
+ key: keyForResource(db, metadata),
+ label: schema.name,
+ isLeaf: false,
+ db,
+ metadata,
+ children: [],
+ };
+ map.set(schemaNode.key, schemaNode);
+ schemaNode.children = buildTableNodeList(schema.tables, map, schemaNode);
+ return schemaNode;
+ });
+};
+
+const buildTableNodeList = (
+ tables: TableMetadata[],
+ map: Map<string, TreeNode>,
+ parent: TreeNodeForSchema
+) => {
+ return tables.map((table) => {
+ const { db } = parent;
+ const metadata = {
+ ...parent.metadata,
+ table,
+ };
+ const tableNode: TreeNodeForTable = {
+ type: "table",
+ parent,
+ key: keyForResource(db, metadata),
+ label: table.name,
+ isLeaf: false,
+ db,
+ metadata,
+ children: [],
+ };
+ map.set(tableNode.key, tableNode);
+ tableNode.children = buildColumnNodeList(table.columns, map, tableNode);
+ return tableNode;
+ });
+};
+
+const buildColumnNodeList = (
+ columns: ColumnMetadata[],
+ map: Map<string, TreeNode>,
+ parent: TreeNodeForTable
+) => {
+ return columns.map((column) => {
+ const { db } = parent;
+ const metadata = {
+ ...parent.metadata,
+ column,
+ };
+ const columnNode: TreeNodeForColumn = {
+ type: "column",
+ key: keyForResource(db, metadata),
+ parent,
+ label: column.name,
+ db,
+ metadata,
+ isLeaf: true,
+ children: undefined,
+ };
+ map.set(columnNode.key, columnNode);
+ return columnNode;
+ });
+};
diff --git a/frontend/src/components/SchemaEditorLite/Aside/types.ts b/frontend/src/components/SchemaEditorLite/Aside/types.ts
deleted file mode 100644
index da252c32d8..0000000000
--- a/frontend/src/components/SchemaEditorLite/Aside/types.ts
+++ /dev/null
@@ -1,62 +0,0 @@
-import { TreeOption } from "naive-ui";
-import { ComposedDatabase } from "@/types";
-import {
- ColumnMetadata,
- DatabaseMetadata,
- SchemaMetadata,
- TableMetadata,
-} from "@/types/proto/v1/database_service";
-
-export interface BaseTreeNode extends TreeOption {
- key: string;
- label: string;
- isLeaf: boolean;
- children?: TreeNode[];
-}
-
-export interface TreeNodeForDatabase extends BaseTreeNode {
- type: "database";
- db: ComposedDatabase;
- metadata: {
- database: DatabaseMetadata;
- };
-}
-
-export interface TreeNodeForSchema extends BaseTreeNode {
- type: "schema";
- db: ComposedDatabase;
- parent?: TreeNodeForDatabase;
- metadata: {
- database: DatabaseMetadata;
- schema: SchemaMetadata;
- };
-}
-
-export interface TreeNodeForTable extends BaseTreeNode {
- type: "table";
- db: ComposedDatabase;
- parent: TreeNodeForSchema;
- metadata: {
- database: DatabaseMetadata;
- schema: SchemaMetadata;
- table: TableMetadata;
- };
-}
-
-export interface TreeNodeForColumn extends BaseTreeNode {
- type: "column";
- db: ComposedDatabase;
- parent: TreeNodeForTable;
- metadata: {
- database: DatabaseMetadata;
- schema: SchemaMetadata;
- table: TableMetadata;
- column: ColumnMetadata;
- };
-}
-
-export type TreeNode =
- | TreeNodeForDatabase
- | TreeNodeForSchema
- | TreeNodeForTable
- | TreeNodeForColumn;