Skip to content

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

chore(sql-editor): schema tree improvements (#13527)

Liu Jif228294942b6
diff --git a/frontend/src/assets/css/tailwind.css b/frontend/src/assets/css/tailwind.css
index e60df85418..d3b8cd8449 100644
--- a/frontend/src/assets/css/tailwind.css
+++ b/frontend/src/assets/css/tailwind.css
@@ -287,6 +287,13 @@
.writing-vertical-lr {
writing-mode: vertical-lr;
}
+
+ .text-size-adjust-none {
+ text-size-adjust: none;
+ -webkit-text-size-adjust: none;
+ -moz-text-size-adjust: none;
+ -ms-text-size-adjust: none;
+ }
}
.fade-enter-active,
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/SchemaPane.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/SchemaPane.vue
index 449288f019..b0a2d7c19e 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/SchemaPane.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/SchemaPane.vue
@@ -70,10 +70,17 @@
<script setup lang="ts">
import { computedAsync, useElementSize, useMounted } from "@vueuse/core";
-import { uniq } from "lodash-es";
-import { NDropdown, NEmpty, NTree, type TreeOption } from "naive-ui";
+import { head, uniq } from "lodash-es";
+import {
+ NDropdown,
+ NEmpty,
+ NTree,
+ type TreeInst,
+ type TreeOption,
+} from "naive-ui";
import { storeToRefs } from "pinia";
import { computed, h, nextTick, ref, watch } from "vue";
+import { watchEffect } from "vue";
import { BBModal } from "@/bbkit";
import TableSchemaViewer from "@/components/TableSchemaViewer.vue";
import MaskSpinner from "@/components/misc/MaskSpinner.vue";
@@ -87,6 +94,7 @@ import {
import { isValidDatabaseName } from "@/types";
import { DatabaseMetadataView } from "@/types/proto/v1/database_service";
import { extractDatabaseResourceName, isDescendantOf } from "@/utils";
+import { useEditorPanelContext } from "../../EditorPanel";
import { useSQLEditorContext } from "../../context";
import SyncSchemaButton from "./SyncSchemaButton.vue";
import { Label } from "./TreeNode";
@@ -100,6 +108,7 @@ import {
} from "./common";
const mounted = useMounted();
+const treeRef = ref<TreeInst>();
const treeContainerElRef = ref<HTMLElement>();
const { height: treeContainerHeight } = useElementSize(
treeContainerElRef,
@@ -109,6 +118,7 @@ const { height: treeContainerHeight } = useElementSize(
}
);
const searchPattern = ref("");
+const { viewState: panelViewState } = useEditorPanelContext();
const { schemaViewer } = useSQLEditorContext();
const {
show: showDropdown,
@@ -120,7 +130,7 @@ const {
} = useDropdown();
const { selectAllFromTableOrView, viewDetail } = useActions();
const { currentTab } = storeToRefs(useSQLEditorTabStore());
-const { connection, database } = useConnectionOfCurrentSQLEditorTab();
+const { database } = useConnectionOfCurrentSQLEditorTab();
const isFetchingMetadata = ref(false);
const metadata = computedAsync(
async () => {
@@ -222,9 +232,55 @@ const renderLabel = ({ option }: { option: TreeOption }) => {
const selectedKeys = computed(() => {
const db = database.value;
if (!isValidDatabaseName(db.name)) return [];
- const { schema, table } = connection.value;
- if (!table) return [];
- return [`${db.name}/schemas/${schema}/tables/${table}`];
+ if (!panelViewState.value) return [];
+ const {
+ schema,
+ detail: {
+ table,
+ column,
+ view,
+ procedure,
+ func,
+ externalTable,
+ partition,
+ index,
+ foreignKey,
+ },
+ } = panelViewState.value;
+
+ const parts = [db.name, `schemas/${schema}`];
+ if (table) {
+ parts.push(`tables/${table}`);
+ if (column) {
+ parts.push(`columns/${column}`);
+ } else if (index) {
+ parts.push(`indexes/${index}`);
+ } else if (partition) {
+ parts.push(`partitionTables/${partition}`);
+ } else if (foreignKey) {
+ parts.push(`foreignKeys/${foreignKey}`);
+ }
+ } else if (view) {
+ parts.push(`views/${view}`);
+ } else if (procedure) {
+ parts.push(`procedures/${procedure}`);
+ } else if (func) {
+ parts.push(`functions/${func}`);
+ } else if (externalTable) {
+ parts.push(`externalTables/${externalTable}`);
+ }
+ if (parts.length <= 2) {
+ // don't highlight if only the root (schema) node is picked
+ return [];
+ }
+
+ return [parts.join("/")];
+});
+watchEffect(() => {
+ const key = head(selectedKeys.value);
+ if (key) {
+ treeRef.value?.scrollTo({ key });
+ }
});
// bottom-up recursively
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ColumnNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ColumnNode.vue
index 5c21fadbfc..efc57ae82b 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ColumnNode.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/ColumnNode.vue
@@ -10,10 +10,31 @@
<IndexIcon v-else-if="isIndex" class="!w-4 !h-4 text-accent/80" />
<ColumnIcon v-else class="w-4 h-4" />
</template>
+ <template #suffix>
+ <div
+ class="flex items-center justify-end gap-1 overflow-hidden whitespace-nowrap shrink opacity-80 !font-normal"
+ >
+ <NTag
+ size="small"
+ class="text-size-adjust-none"
+ style="--n-height: 16px; --n-padding: 0 3px; --n-font-size: 10px"
+ >
+ {{ target.column.nullable ? "NULL" : "NOT NULL" }}
+ </NTag>
+ <NTag
+ size="small"
+ class="text-size-adjust-none"
+ style="--n-height: 16px; --n-padding: 0 3px; --n-font-size: 10px"
+ >
+ {{ target.column.type }}
+ </NTag>
+ </div>
+ </template>
</CommonNode>
</template>
<script setup lang="ts">
+import { NTag } from "naive-ui";
import { computed } from "vue";
import { ColumnIcon, IndexIcon, PrimaryKeyIcon } from "@/components/Icon";
import type { TreeNode } from "../common";
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/CommonNode.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/CommonNode.vue
index 3e7ff6173e..dd71709041 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/CommonNode.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/TreeNode/CommonNode.vue
@@ -22,6 +22,7 @@
/>
<span v-else class="pl-[2px]">{{ text }}</span>
</slot>
+ <slot name="suffix" />
</slot>
</div>
</template>
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/common.ts b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/common.ts
index 179303a54d..7cc1304959 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPane/common.ts
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPane/common.ts
@@ -166,7 +166,7 @@ export const keyForNodeTarget = <T extends NodeType>(
return [
db.name,
`schemas/${schema.name}`,
- `externalTables/${externalTable.externalServerName}/${externalTable.externalDatabaseName}/${externalTable.name}`,
+ `externalTables/${externalTable.name}`,
].join("/");
}
if (type === "column") {
@@ -200,13 +200,13 @@ export const keyForNodeTarget = <T extends NodeType>(
].join("/");
}
if (type === "partition-table") {
- const { db, schema, table, parentPartition, partition } =
+ const { db, schema, table, partition } =
target as NodeTarget<"partition-table">;
return [
db.name,
`schemas/${schema.name}`,
`tables/${table.name}`,
- `partitionTables/${parentPartition?.name ?? ""}/${partition.name}`,
+ `partitionTables/${partition.name}`,
].join("/");
}
if (type === "view") {
@@ -489,26 +489,27 @@ const mapExternalTableNodes = (
parent: TreeNode
) => {
const { schema } = target;
- const folderNode = createExpandableTextNode("column", parent, () =>
- t("database.columns")
- );
- folderNode.children = schema.externalTables.map((externalTable) => {
+ const externalTableNodes = schema.externalTables.map((externalTable) => {
const node = mapTreeNodeByType(
"external-table",
{ ...target, externalTable },
parent
);
+ const folderNode = createExpandableTextNode("column", node, () =>
+ t("database.columns")
+ );
+ node.children = [folderNode];
// columns
- node.children = mapColumnNodes(
+ folderNode.children = mapColumnNodes(
node.meta.target,
externalTable.columns,
- node
+ folderNode
);
return node;
});
- return [folderNode];
+ return externalTableNodes;
};
// Map partition-table-level partitions.
const mapPartitionTableNodes = (