Skip to content

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

feat(sql-editor): database schema panel (#4352)

Liu Jie758b13d38a2
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 9b575a35c8..d737bfb64e 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -945,6 +945,7 @@
"index-size": "Index size",
"column": "Column",
"columns": "Columns",
+ "data-type": "Data type",
"indexes": "Indexes",
"row-count-est": "Row count est.",
"incorrect-project-warning": "SQL editor can only query databases in projects available to the user. You should first transfer this database into a project.",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index a230d209a6..3466154b5d 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -945,6 +945,7 @@
"index-size": "索引大小",
"column": "列",
"columns": "列",
+ "data-type": "数据类型",
"indexes": "索引",
"row-count-est": "估计行数",
"incorrect-project-warning": "SQL 编辑器只能查询用户可用的项目中的数据库。您应该首先将此数据库转移到一个可用的项目中。",
diff --git a/frontend/src/store/modules/connectionTree.ts b/frontend/src/store/modules/connectionTree.ts
index 8ac4634b9a..8db1552a05 100644
--- a/frontend/src/store/modules/connectionTree.ts
+++ b/frontend/src/store/modules/connectionTree.ts
@@ -17,8 +17,6 @@ import { useDatabaseStore } from "./database";
import { useInstanceStore } from "./instance";
import { emptyConnection } from "@/utils";
import { useDBSchemaStore } from "./dbSchema";
-import { TableMetadata } from "@/types/proto/store/database";
-import { has, uniqueId } from "lodash-es";
export const useConnectionTreeStore = defineStore("connectionTree", () => {
// states
@@ -78,12 +76,11 @@ export const useConnectionTreeStore = defineStore("connectionTree", () => {
};
// utilities
const mapAtom = (
- item: Project | Instance | Database | TableMetadata,
+ item: Project | Instance | Database,
type: ConnectionAtomType,
parentId: number
) => {
- const id =
- type !== "table" && has(item, "id") ? (item as any).id : uniqueId();
+ const id = item.id;
const key = `${type}-${id}`;
const connectionAtom: ConnectionAtom = {
parentId,
@@ -91,7 +88,7 @@ export const useConnectionTreeStore = defineStore("connectionTree", () => {
key,
label: item.name,
type,
- isLeaf: type === "table",
+ isLeaf: type === "database",
};
return connectionAtom;
};
diff --git a/frontend/src/types/connectionTree.ts b/frontend/src/types/connectionTree.ts
index 33ec50409e..34bda30e91 100644
--- a/frontend/src/types/connectionTree.ts
+++ b/frontend/src/types/connectionTree.ts
@@ -1,11 +1,6 @@
import { InstanceId, DatabaseId, ProjectId } from "../types";
-export type ConnectionAtomType =
- | "project"
- | "instance"
- | "database"
- | "table"
- | "view";
+export type ConnectionAtomType = "project" | "instance" | "database";
export enum ConnectionTreeState {
UNSET,
@@ -27,8 +22,4 @@ export interface ConnectionAtom {
children?: ConnectionAtom[];
disabled?: boolean;
isLeaf?: boolean;
- table?: {
- schema: string;
- name: string;
- };
}
diff --git a/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue b/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue
index 52dba0152f..6b9a890446 100644
--- a/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue
@@ -7,11 +7,11 @@
class="default-theme"
:dbl-click-splitter="false"
>
- <Pane :size="databasePaneSize">
+ <Pane>
<DatabaseTree />
</Pane>
- <Pane :size="FULL_HEIGHT - databasePaneSize">
- <TableSchema @close-pane="handleCloseTableSchemaPane" />
+ <Pane v-if="showSchemaPanel" :size="40">
+ <SchemaPanel />
</Pane>
</Splitpanes>
</n-tab-pane>
@@ -25,11 +25,11 @@
class="default-theme"
:dbl-click-splitter="false"
>
- <Pane :size="databasePaneSize">
+ <Pane>
<DatabaseTree />
</Pane>
- <Pane :size="FULL_HEIGHT - databasePaneSize">
- <TableSchema @close-pane="handleCloseTableSchemaPane" />
+ <Pane v-if="showSchemaPanel" :size="40">
+ <SchemaPanel />
</Pane>
</Splitpanes>
</n-tab-pane>
@@ -42,20 +42,17 @@
<script lang="ts" setup>
import { computed, ref, watchEffect } from "vue";
-import { isUndefined } from "lodash-es";
-import { useConnectionTreeStore, useCurrentUser } from "@/store";
+import { useConnectionTreeStore, useCurrentUser, useTabStore } from "@/store";
import DatabaseTree from "./DatabaseTree.vue";
import QueryHistoryContainer from "./QueryHistoryContainer.vue";
-import TableSchema from "./TableSchema.vue";
+import SchemaPanel from "./SchemaPanel/";
import { Splitpanes, Pane } from "splitpanes";
-import { ConnectionTreeMode } from "@/types";
+import { ConnectionTreeMode, UNKNOWN_ID } from "@/types";
import { hasWorkspacePermission } from "@/utils";
-const FULL_HEIGHT = 100;
-const DATABASE_PANE_SIZE = 60;
-
const currentUser = useCurrentUser();
+const tabStore = useTabStore();
const tab = ref<"projects" | "instances" | "history">("projects");
@@ -68,16 +65,10 @@ const hasInstanceView = computed((): boolean => {
const connectionTreeStore = useConnectionTreeStore();
-const databasePaneSize = computed(() => {
- if (!isUndefined(connectionTreeStore.selectedTableAtom)) {
- return DATABASE_PANE_SIZE;
- }
- return FULL_HEIGHT;
+const showSchemaPanel = computed(() => {
+ return tabStore.currentTab.connection.databaseId !== UNKNOWN_ID;
});
-const handleCloseTableSchemaPane = () => {
- connectionTreeStore.selectedTableAtom = undefined;
-};
watchEffect(() => {
if (tab.value === "projects") {
connectionTreeStore.tree.mode = ConnectionTreeMode.PROJECT;
diff --git a/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue b/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
index 3fa702b34d..78a14f5fcb 100644
--- a/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
@@ -25,7 +25,6 @@
:render-prefix="renderPrefix"
:render-suffix="renderSuffix"
:node-props="nodeProps"
- :on-load="loadSubTree"
:on-update:expanded-keys="updateExpandedKeys"
/>
</div>
@@ -64,7 +63,6 @@ import {
useConnectionTreeStore,
useCurrentUser,
useDatabaseStore,
- useDBSchemaStore,
useIsLoggedIn,
useTabStore,
} from "@/store";
@@ -75,7 +73,6 @@ import {
isDescendantOf,
isSimilarTab,
} from "@/utils";
-import { generateTableItem } from "./utils";
import { scrollIntoViewIfNeeded } from "@/bbkit/BBUtil";
import { Prefix, Label, Suffix } from "./TreeNode";
@@ -92,7 +89,6 @@ const { t } = useI18n();
const databaseStore = useDatabaseStore();
const connectionTreeStore = useConnectionTreeStore();
-const dbSchemaStore = useDBSchemaStore();
const tabStore = useTabStore();
const isLoggedIn = useIsLoggedIn();
const currentUser = useCurrentUser();
@@ -111,15 +107,6 @@ const dropdownOptions = computed((): DropdownOptionWithConnectionAtom[] => {
}
if (atom.type === "project") {
return [];
- }
- if (atom.type === "table") {
- return [
- {
- key: "alter-table",
- label: t("sql-editor.alter-table"),
- item: atom,
- },
- ];
} else {
// Don't show any context menu actions for disabled
// instances/databases
@@ -206,17 +193,6 @@ const setConnection = (
const database = databaseStore.getDatabaseById(option.id);
conn.instanceId = database.instance.id;
conn.databaseId = database.id;
- } else if (option.type === "table") {
- // If selected item is table node
- const databaseId = option.parentId;
- const database = databaseStore.getDatabaseById(databaseId);
- const instanceId = database.instance.id;
- conn.instanceId = instanceId;
- conn.databaseId = databaseId;
- if (database.instance.engine !== "MONGODB") {
- // A MongoDB connection has no schema, so it shouldn't be clickable.
- connectionTreeStore.selectedTableAtom = option;
- }
}
connect();
@@ -224,8 +200,6 @@ const setConnection = (
};
// dynamic render the highlight keywords
-// and render the selected table node in bold font
-
const renderLabel = ({ option }: { option: TreeOption }) => {
const atom = option as any as ConnectionAtom;
return h(Label, { atom, keyword: searchPattern.value ?? "" });
@@ -244,41 +218,6 @@ const renderSuffix = ({ option }: { option: TreeOption }) => {
return h(Suffix, { atom });
};
-const loadSubTree = async (option: TreeOption): Promise<void> => {
- const item = option as any as ConnectionAtom;
- if (item.type === "database") {
- const database = databaseStore.getDatabaseById(item.id);
- const schemaList = await dbSchemaStore.getOrFetchSchemaListByDatabaseId(
- item.id as DatabaseId
- );
- item.children = schemaList.flatMap((schema) => {
- return schema.tables.map((table) => {
- const node = generateTableItem(
- connectionTreeStore.mapAtom(table, "table", item.id)
- );
- node.table = {
- schema: schema.name,
- name: table.name,
- };
- if (schema.name) {
- node.label = `${schema.name}.${node.label}`;
- }
- if (database.instance.engine === "MONGODB") {
- node.disabled = true;
- }
-
- return node;
- });
- });
- if (item.children.length === 0) {
- // No tables in the db
- item.isLeaf = true;
- // TODO: this might be a little bit confusing
- // Better add a dummy "no tables" node in the future
- }
- }
-};
-
const gotoAlterSchema = (option: ConnectionAtom) => {
const databaseId = option.parentId;
const database = databaseStore.getDatabaseById(databaseId);
@@ -384,22 +323,6 @@ const scrollToConnectedNode = (
});
};
-// Hide the selected table schema panel when switching to another database
-// or instance.
-watch(
- [
- () => connectionTreeStore.selectedTableAtom,
- () => tabStore.currentTab.connection.databaseId,
- ],
- ([tableAtom, databaseId]) => {
- if (tableAtom) {
- if (tableAtom.parentId !== databaseId) {
- connectionTreeStore.selectedTableAtom = undefined;
- }
- }
- }
-);
-
// Open corresponding tree node when the connection changed.
watch(
[
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/AlterSchemaButton.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/AlterSchemaButton.vue
new file mode 100644
index 0000000000..74c0725f0f
--- /dev/null
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/AlterSchemaButton.vue
@@ -0,0 +1,72 @@
+<template>
+ <NTooltip trigger="hover" :delay="500">
+ <template #trigger>
+ <NButton quaternary size="tiny" v-bind="$attrs" @click="gotoAlterSchema">
+ <heroicons-outline:pencil-alt class="w-4 h-4" />
+ </NButton>
+ </template>
+ {{ $t("database.alter-schema") }}
+ </NTooltip>
+</template>
+
+<script lang="ts" setup>
+import { computed } from "vue";
+import { stringify } from "qs";
+import { NButton } from "naive-ui";
+
+import { useRepositoryStore } from "@/store";
+import type {
+ SchemaMetadata,
+ TableMetadata,
+} from "@/types/proto/store/database";
+import type { Database, Repository } from "@/types";
+import { baseDirectoryWebUrl } from "@/types";
+
+const props = defineProps<{
+ database: Database;
+ schema?: SchemaMetadata;
+ table?: TableMetadata;
+}>();
+
+const exampleSQL = computed(() => {
+ const { schema, table } = props;
+ if (schema && table) {
+ if (schema.name) {
+ return `ALTER TABLE ${schema.name}.${table.name}`;
+ }
+ return `ALTER TABLE ${table.name}`;
+ }
+ return `ALTER TABLE`;
+});
+
+const gotoAlterSchema = () => {
+ const { database } = props;
+
+ const { project } = database;
+ if (project.workflowType === "VCS") {
+ useRepositoryStore()
+ .fetchRepositoryByProjectId(database.project.id)
+ .then((repository: Repository) => {
+ window.open(
+ baseDirectoryWebUrl(repository, {
+ DB_NAME: database.name,
+ ENV_NAME: database.instance.environment.name,
+ TYPE: "ddl",
+ }),
+ "_blank"
+ );
+ });
+ return;
+ }
+
+ const query = {
+ template: "bb.issue.database.schema.update",
+ name: `[${database.name}] Alter schema`,
+ project: database.project.id,
+ databaseList: database.id,
+ sql: exampleSQL.value,
+ };
+ const url = `/issue/new?${stringify(query)}`;
+ window.open(url, "_blank");
+};
+</script>
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/DatabaseSchema.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/DatabaseSchema.vue
new file mode 100644
index 0000000000..b449623ba7
--- /dev/null
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/DatabaseSchema.vue
@@ -0,0 +1,115 @@
+<template>
+ <div v-if="databaseMetadata" class="h-full overflow-hidden flex flex-col">
+ <div class="flex items-center justify-between p-2 border-b">
+ <div class="flex items-center truncate">
+ <heroicons-outline:database class="h-4 w-4 mr-1" />
+ <a
+ :href="`/db/${databaseSlug(database)}`"
+ target="__BLANK"
+ class="font-semibold anchor-link"
+ >
+ {{ databaseMetadata.name }}
+ </a>
+ </div>
+ <div class="flex justify-end">
+ <SchemaDiagramButton
+ :database="database"
+ :database-metadata="databaseMetadata"
+ />
+ <AlterSchemaButton :database="database" />
+ </div>
+ </div>
+
+ <div class="px-2 py-2 border-b text-gray-500 text-xs space-y-1">
+ <div v-if="showCharset" class="flex items-center justify-between">
+ <span>
+ {{
+ engine == "POSTGRES" ? $t("db.encoding") : $t("db.character-set")
+ }}
+ </span>
+ <span>{{ database.characterSet }}</span>
+ </div>
+ <div v-if="showCollation" class="flex items-center justify-between">
+ <span>
+ {{ $t("db.collation") }}
+ </span>
+ <span>{{ database.collation }}</span>
+ </div>
+ </div>
+
+ <div class="flex-1 px-2 overflow-y-auto flex flex-col gap-y-2">
+ <div class="mt-2 text-sm text-gray-500">
+ {{
+ database.instance.engine !== "MONGODB"
+ ? $t("db.tables")
+ : $t("db.collections")
+ }}
+ </div>
+
+ <template v-for="(schema, i) in databaseMetadata.schemas" :key="i">
+ <div v-for="(table, j) in schema.tables" :key="j" class="text-xs">
+ <div
+ class="inline-block text-gray-600 whitespace-pre-wrap break-words"
+ :class="
+ rowClickable && [
+ 'hover:text-[var(--color-accent)]',
+ 'cursor-pointer',
+ ]
+ "
+ @click="handleClickTable(schema, table)"
+ >
+ <span v-if="schema.name">{{ schema.name }}.</span>
+ <span>{{ table.name }}</span>
+ </div>
+ </div>
+ </template>
+ </div>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { computed } from "vue";
+
+import type {
+ DatabaseMetadata,
+ SchemaMetadata,
+ TableMetadata,
+} from "@/types/proto/store/database";
+import type { Database } from "@/types";
+import { databaseSlug } from "@/utils";
+import AlterSchemaButton from "./AlterSchemaButton.vue";
+import SchemaDiagramButton from "./SchemaDiagramButton.vue";
+
+const props = defineProps<{
+ database: Database;
+ databaseMetadata: DatabaseMetadata;
+}>();
+
+const emit = defineEmits<{
+ (e: "select-table", schema: SchemaMetadata, table: TableMetadata): void;
+}>();
+
+const engine = computed(() => props.database.instance.engine);
+
+const showCharset = computed(
+ () =>
+ engine.value !== "CLICKHOUSE" &&
+ engine.value !== "SNOWFLAKE" &&
+ engine.value !== "MONGODB"
+);
+const showCollation = computed(
+ () =>
+ engine.value !== "CLICKHOUSE" &&
+ engine.value !== "SNOWFLAKE" &&
+ engine.value !== "MONGODB"
+);
+
+const rowClickable = computed(() => engine.value !== "MONGODB");
+
+const handleClickTable = (schema: SchemaMetadata, table: TableMetadata) => {
+ if (!rowClickable.value) {
+ return;
+ }
+ emit("select-table", schema, table);
+};
+</script>
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/SchemaDiagramButton.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/SchemaDiagramButton.vue
new file mode 100644
index 0000000000..d0bdec1942
--- /dev/null
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/SchemaDiagramButton.vue
@@ -0,0 +1,53 @@
+<template>
+ <NTooltip trigger="hover" :delay="500">
+ <template #trigger>
+ <NButton
+ quaternary
+ size="tiny"
+ v-bind="$attrs"
+ @click="state.show = true"
+ >
+ <SchemaDiagramIcon />
+ </NButton>
+ </template>
+ {{ $t("schema-diagram.self") }}
+ </NTooltip>
+
+ <BBModal
+ v-if="state.show"
+ :title="$t('schema-diagram.self')"
+ class="h-[calc(100vh-40px)] !max-h-[calc(100vh-40px)]"
+ header-class="!border-0"
+ container-class="flex-1 !pt-0"
+ @close="state.show = false"
+ >
+ <div class="w-[80vw] h-full">
+ <SchemaDiagram
+ :database="database"
+ :database-metadata="databaseMetadata"
+ />
+ </div>
+ </BBModal>
+</template>
+
+<script lang="ts" setup>
+import { reactive } from "vue";
+import { NButton } from "naive-ui";
+
+import type { DatabaseMetadata } from "@/types/proto/store/database";
+import type { Database } from "@/types";
+import { SchemaDiagram, SchemaDiagramIcon } from "@/components/SchemaDiagram";
+
+type LocalState = {
+ show: boolean;
+};
+
+defineProps<{
+ database: Database;
+ databaseMetadata: DatabaseMetadata;
+}>();
+
+const state = reactive<LocalState>({
+ show: false,
+});
+</script>
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/SchemaPanel.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/SchemaPanel.vue
new file mode 100644
index 0000000000..b00865b3cd
--- /dev/null
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/SchemaPanel.vue
@@ -0,0 +1,77 @@
+<template>
+ <div class="w-full h-full relative overflow-hidden">
+ <template v-if="databaseMetadata">
+ <DatabaseSchema
+ v-show="!state.selected"
+ :database="database"
+ :database-metadata="databaseMetadata"
+ @select-table="handleSelectTable"
+ />
+ <TableSchema
+ v-if="state.selected"
+ :database="database"
+ :database-metadata="databaseMetadata"
+ :schema="state.selected.schema"
+ :table="state.selected.table"
+ @close="state.selected = undefined"
+ />
+ </template>
+
+ <div
+ v-else
+ class="absolute inset-0 bg-white/50 flex flex-col items-center justify-center"
+ >
+ <BBSpin />
+ </div>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { computed, reactive, ref, watch } from "vue";
+import { storeToRefs } from "pinia";
+
+import { UNKNOWN_ID } from "@/types";
+import {
+ DatabaseMetadata,
+ SchemaMetadata,
+ TableMetadata,
+} from "@/types/proto/store/database";
+import { useDatabaseById, useDBSchemaStore, useTabStore } from "@/store";
+import DatabaseSchema from "./DatabaseSchema.vue";
+import TableSchema from "./TableSchema.vue";
+
+type LocalState = {
+ selected?: { schema: SchemaMetadata; table: TableMetadata };
+};
+
+const state = reactive<LocalState>({
+ selected: undefined,
+});
+
+const dbSchemaStore = useDBSchemaStore();
+const { currentTab } = storeToRefs(useTabStore());
+const conn = computed(() => currentTab.value.connection);
+
+const database = useDatabaseById(computed(() => conn.value.databaseId));
+const databaseMetadata = ref<DatabaseMetadata>();
+
+const handleSelectTable = (schema: SchemaMetadata, table: TableMetadata) => {
+ state.selected = { schema, table };
+};
+
+watch(
+ () => database.value.id,
+ async (databaseId) => {
+ state.selected = undefined;
+ databaseMetadata.value = undefined;
+ if (databaseId !== UNKNOWN_ID) {
+ databaseMetadata.value =
+ await dbSchemaStore.getOrFetchDatabaseMetadataById(
+ databaseId,
+ /* !skipCache */ false
+ );
+ }
+ },
+ { immediate: true }
+);
+</script>
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/TableSchema.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/TableSchema.vue
new file mode 100644
index 0000000000..7ac3319a81
--- /dev/null
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/TableSchema.vue
@@ -0,0 +1,98 @@
+<template>
+ <div class="h-full overflow-hidden flex flex-col">
+ <div class="flex items-center justify-between p-2 border-b">
+ <div class="flex items-center truncate">
+ <heroicons-outline:table class="h-4 w-4 mr-1" />
+ <a
+ :href="tableDetailLink"
+ target="__BLANK"
+ class="font-semibold anchor-link"
+ >
+ <span v-if="schema.name">{{ schema.name }}.</span>
+ <span>{{ table.name }}</span>
+ </a>
+ </div>
+
+ <div class="flex justify-end">
+ <AlterSchemaButton
+ :database="database"
+ :schema="schema"
+ :table="table"
+ />
+
+ <NButton quaternary size="tiny" @click="handleClose">
+ <heroicons-outline:x class="w-4 h-4" />
+ </NButton>
+ </div>
+ </div>
+
+ <div class="px-2 py-2 border-b text-gray-500 text-xs space-y-1">
+ <div class="flex items-center justify-between">
+ <span class="mr-1">{{ $t("database.row-count-est") }}</span>
+ <span>{{ table.rowCount }}</span>
+ </div>
+ </div>
+
+ <div
+ class="grid px-2 overflow-y-auto gap-x-1 gap-y-2"
+ style="grid-template-columns: minmax(4rem, 2fr) minmax(4rem, 1fr)"
+ >
+ <div class="mt-2 text-sm text-gray-500">{{ $t("database.columns") }}</div>
+ <div class="mt-2 text-right text-sm text-gray-500">
+ {{ $t("database.data-type") }}
+ </div>
+
+ <template v-for="(column, index) in table.columns" :key="index">
+ <div class="text-xs text-gray-600whitespace-pre-wrap break-words">
+ {{ column.name }}
+ </div>
+ <div
+ class="text-right text-xs text-gray-400 overflow-x-hidden whitespace-nowrap"
+ >
+ {{ column.type }}
+ </div>
+ </template>
+ </div>
+ </div>
+</template>
+
+<script lang="ts" setup>
+import { NButton } from "naive-ui";
+
+import type {
+ DatabaseMetadata,
+ SchemaMetadata,
+ TableMetadata,
+} from "@/types/proto/store/database";
+import type { Database } from "@/types";
+import AlterSchemaButton from "./AlterSchemaButton.vue";
+import { computed } from "vue";
+import { databaseSlug } from "@/utils";
+
+const props = defineProps<{
+ database: Database;
+ databaseMetadata: DatabaseMetadata;
+ schema: SchemaMetadata;
+ table: TableMetadata;
+}>();
+
+const emit = defineEmits<{
+ (e: "close"): void;
+}>();
+
+const tableDetailLink = computed((): string => {
+ const { database, schema, table } = props;
+ let url = `/db/${databaseSlug(database)}/table/${encodeURIComponent(
+ table.name
+ )}`;
+ if (schema.name) {
+ url += `?schema=${encodeURIComponent(schema.name)}`;
+ }
+
+ return url;
+});
+
+const handleClose = () => {
+ emit("close");
+};
+</script>
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/index.ts b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/index.ts
new file mode 100644
index 0000000000..0715cc5e56
--- /dev/null
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/index.ts
@@ -0,0 +1,3 @@
+import SchemaPanel from "./SchemaPanel.vue";
+
+export default SchemaPanel;
diff --git a/frontend/src/views/sql-editor/AsidePanel/TableSchema.vue b/frontend/src/views/sql-editor/AsidePanel/TableSchema.vue
deleted file mode 100644
index ec4c34db1e..0000000000
--- a/frontend/src/views/sql-editor/AsidePanel/TableSchema.vue
+++ /dev/null
@@ -1,162 +0,0 @@
-<template>
- <div v-if="schema && table" class="table-schema">
- <div class="table-schema--header">
- <div class="table-schema--header-title mr-1 flex items-center">
- <heroicons-outline:table class="h-4 w-4 mr-1" />
- <span v-if="schema.name" class="font-semibold">{{ schema.name }}.</span>
- <span class="font-semibold">{{ table.name }}</span>
- </div>
- <div
- class="table-schema--header-actions flex-1 flex justify-end space-x-2"
- >
- <div class="action-edit flex items-center">
- <NTooltip trigger="hover">
- <template #trigger>
- <NButton text @click="gotoAlterSchema">
- <heroicons-outline:pencil-alt class="w-4 h-4" />
- </NButton>
- </template>
- {{ $t("database.alter-schema") }}
- </NTooltip>
- </div>
- <div class="action-close flex items-center">
- <NTooltip trigger="hover">
- <template #trigger>
- <NButton text @click="handleClosePane">
- <heroicons-outline:x class="w-4 h-4" />
- </NButton>
- </template>
- {{ $t("sql-editor.close-pane") }}
- </NTooltip>
- </div>
- </div>
- </div>
- <div class="table-schema--meta text-gray-500 text-sm">
- <div class="pb-1">
- <span class="mr-1">{{ $t("database.row-count-est") }}</span>
- <span>{{ table.rowCount }}</span>
- </div>
- <div class="flex justify-between items-center w-full text-xs py-2">
- <div class="table-schema--content-column">
- <span>{{ $t("database.columns") }}</span>
- </div>
- <div class="table-schema--content-column">
- <span>Data Type</span>
- </div>
- </div>
- </div>
- <div class="table-schema--content text-sm text-gray-400 overflow-y-auto">
- <div
- v-for="(column, index) in table.columns"
- :key="index"
- class="flex justify-between items-center w-full p-1 hover:bg-link-hover"
- >
- <div class="table-schema--content-column text-gray-600">
- <span>{{ column.name }}</span>
- </div>
- <div class="table-schema--content-column">
- <span>{{ column.type }}</span>
- </div>
- </div>
- </div>
- </div>
- <div v-else class="h-full flex justify-center items-center">
- {{ $t("sql-editor.table-schema-placeholder") }}
- </div>
-</template>
-
-<script lang="ts" setup>
-import { isUndefined } from "lodash-es";
-import { computed } from "vue";
-import { stringify } from "qs";
-import type { Repository } from "@/types";
-import { baseDirectoryWebUrl } from "@/types";
-import {
- useConnectionTreeStore,
- useDatabaseStore,
- useDBSchemaStore,
- useRepositoryStore,
-} from "@/store";
-
-const emit = defineEmits<{
- (e: "close-pane"): void;
-}>();
-
-const connectionTreeStore = useConnectionTreeStore();
-const databaseStore = useDatabaseStore();
-const dbSchemaStore = useDBSchemaStore();
-const tableAtom = computed(() => connectionTreeStore.selectedTableAtom);
-const schema = computed(() => {
- const atom = tableAtom.value;
- if (isUndefined(atom)) {
- return undefined;
- }
- const schemaList = dbSchemaStore.getSchemaListByDatabaseId(atom.parentId);
- return schemaList.find((schema) => schema.name === atom.table?.schema);
-});
-const table = computed(() => {
- const atom = tableAtom.value;
- if (isUndefined(atom)) {
- return undefined;
- }
- return schema.value?.tables.find((table) => table.name === atom.table?.name);
-});
-
-const gotoAlterSchema = () => {
- if (isUndefined(tableAtom.value) || isUndefined(table.value)) {
- return;
- }
-
- const database = databaseStore.getDatabaseById(tableAtom.value.parentId);
- const { project } = database;
- if (project.workflowType === "VCS") {
- useRepositoryStore()
- .fetchRepositoryByProjectId(database.project.id)
- .then((repository: Repository) => {
- window.open(
- baseDirectoryWebUrl(repository, {
- DB_NAME: database.name,
- ENV_NAME: database.instance.environment.name,
- TYPE: "ddl",
- }),
- "_blank"
- );
- });
- return;
- }
-
- const query = {
- template: "bb.issue.database.schema.update",
- name: `[${database.name}] Alter schema`,
- project: database.project.id,
- databaseList: database.id,
- sql: `ALTER TABLE ${table.value.name}`,
- };
- const url = `/issue/new?${stringify(query)}`;
- window.open(url, "_blank");
-};
-
-const handleClosePane = () => {
- emit("close-pane");
-};
-</script>
-
-<style scoped>
-.table-schema {
- @apply h-full space-y-2;
-}
-
-.table-schema--header {
- @apply flex items-center p-2 border-b;
-}
-
-.table-schema--meta {
- @apply px-2 py-1;
- @apply border-b;
-}
-
-.table-schema--content {
- @apply px-2 py-1;
- height: calc(100% - 116px);
-}
-</style>
diff --git a/frontend/src/views/sql-editor/AsidePanel/TreeNode/Label.vue b/frontend/src/views/sql-editor/AsidePanel/TreeNode/Label.vue
index 849c23e143..211dd90fef 100644
--- a/frontend/src/views/sql-editor/AsidePanel/TreeNode/Label.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/TreeNode/Label.vue
@@ -1,6 +1,6 @@
<template>
<!-- eslint-disable-next-line vue/no-v-html -->
- <span :id="id" class="truncate" :class="classes" v-html="html" />
+ <span :id="id" class="truncate" v-html="html" />
</template>
<script lang="ts" setup>
@@ -9,7 +9,6 @@ import { useI18n } from "vue-i18n";
import { escape } from "lodash-es";
import { ConnectionAtom, DEFAULT_PROJECT_ID } from "@/types";
-import { useConnectionTreeStore } from "@/store";
import { getHighlightHTMLByKeyWords } from "@/utils";
const props = defineProps<{
@@ -17,18 +16,6 @@ const props = defineProps<{
keyword: string;
}>();
const { t } = useI18n();
-const connectionTreeStore = useConnectionTreeStore();
-
-const classes = computed(() => {
- const classes = ["truncate"];
- const { atom } = props;
- if (atom.type === "table") {
- if (atom === connectionTreeStore.selectedTableAtom) {
- classes.push("font-bold");
- }
- }
- return classes;
-});
// render an unique id for every node
// for auto scroll to the node when tab switches
diff --git a/frontend/src/views/sql-editor/AsidePanel/TreeNode/Prefix.vue b/frontend/src/views/sql-editor/AsidePanel/TreeNode/Prefix.vue
index 51333198d0..ebad6c856f 100644
--- a/frontend/src/views/sql-editor/AsidePanel/TreeNode/Prefix.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/TreeNode/Prefix.vue
@@ -18,9 +18,6 @@
<heroicons-outline:database class="w-4 h-4" />
</span>
</template>
- <template v-else-if="atom.type === 'table'">
- <heroicons-outline:table class="w-4 h-4" />
- </template>
</template>
<script lang="ts" setup>
diff --git a/frontend/src/views/sql-editor/AsidePanel/utils.ts b/frontend/src/views/sql-editor/AsidePanel/utils.ts
deleted file mode 100644
index 3b1622df02..0000000000
--- a/frontend/src/views/sql-editor/AsidePanel/utils.ts
+++ /dev/null
@@ -1,26 +0,0 @@
-import { ConnectionAtom } from "@/types";
-import type { useInstanceStore } from "@/store";
-
-export const generateInstanceNode = (
- item: ConnectionAtom,
- instanceStore: ReturnType<typeof useInstanceStore>
-) => {
- return {
- ...item,
- children: item.children?.map(generateDatabaseItem),
- };
-};
-
-export const generateDatabaseItem = (item: ConnectionAtom) => {
- return {
- ...item,
- children: item.children?.map(generateTableItem),
- };
-};
-
-export const generateTableItem = (item: ConnectionAtom) => {
- return {
- ...item,
- isLeaf: true,
- };
-};