Skip to content

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

refactor(sql-editor): decouple selected table from tab connection info (#2839)

Liu Ji7227fd510965
diff --git a/frontend/src/store/modules/sqlEditor.ts b/frontend/src/store/modules/sqlEditor.ts
index 4a470c436a..7ed0e04714 100644
--- a/frontend/src/store/modules/sqlEditor.ts
+++ b/frontend/src/store/modules/sqlEditor.ts
@@ -4,7 +4,6 @@ import {
SQLEditorState,
ConnectionAtom,
QueryInfo,
- ConnectionContext,
Database,
DatabaseId,
ProjectId,
@@ -12,6 +11,7 @@ import {
UNKNOWN_ID,
InstanceId,
Connection,
+ unknown,
} from "@/types";
import { useActivityStore } from "./activity";
import { useDatabaseStore } from "./database";
@@ -22,14 +22,10 @@ import { useProjectStore } from "./project";
import { useTabStore } from "./tab";
import { emptyConnection } from "@/utils";
-export const getDefaultConnectionContext = () => ({
- option: {} as any,
-});
-
export const useSQLEditorStore = defineStore("sqlEditor", {
state: (): SQLEditorState => ({
connectionTree: [],
- connectionContext: getDefaultConnectionContext(),
+ selectedTable: unknown("TABLE"),
isLoadingTree: false,
isShowExecutingHint: false,
shouldFormatContent: false,
@@ -81,9 +77,6 @@ export const useSQLEditorStore = defineStore("sqlEditor", {
setConnectionTree(payload: ConnectionAtom[]) {
this.connectionTree = payload;
},
- setConnectionContext(payload: Partial<ConnectionContext>) {
- Object.assign(this.connectionContext, payload);
- },
setShouldFormatContent(payload: boolean) {
this.shouldFormatContent = payload;
},
diff --git a/frontend/src/store/modules/table.ts b/frontend/src/store/modules/table.ts
index b8368dfbc3..7c28467fcb 100644
--- a/frontend/src/store/modules/table.ts
+++ b/frontend/src/store/modules/table.ts
@@ -81,6 +81,15 @@ export const useTableStore = defineStore("table", {
return table || unknown("TABLE");
},
+ async getOrFetchTableByDatabaseIdAndTableId(
+ databaseId: DatabaseId,
+ tableId: TableId
+ ) {
+ const tableList = await this.getOrFetchTableListByDatabaseId(databaseId);
+ const table = tableList.find((t) => t.id === tableId);
+ return table || unknown("TABLE");
+ },
+
setTableByDatabaseIdAndTableName({
databaseId,
tableName,
diff --git a/frontend/src/types/sqlEditor.ts b/frontend/src/types/sqlEditor.ts
index 1c1a8a37e7..a62b8b0cce 100644
--- a/frontend/src/types/sqlEditor.ts
+++ b/frontend/src/types/sqlEditor.ts
@@ -26,11 +26,6 @@ export enum SortText {
KEYWORD = "3",
}
-// TODO(Jim): refactor <TableSchema> to get rid of this structure totally.
-export type ConnectionContext = {
- option: ConnectionAtom;
-};
-
export interface QueryHistory {
id: ActivityId;
diff --git a/frontend/src/types/store.ts b/frontend/src/types/store.ts
index 761b59e61b..3e55ce51b6 100644
--- a/frontend/src/types/store.ts
+++ b/frontend/src/types/store.ts
@@ -46,7 +46,7 @@ import { Setting, SettingName } from "./setting";
import { Table } from "./table";
import { VCS } from "./vcs";
import { Label } from "./label";
-import { ConnectionAtom, ConnectionContext } from "./sqlEditor";
+import { ConnectionAtom } from "./sqlEditor";
import { TabInfo } from "./tab";
import type { DebugLog } from "@/types/debug";
@@ -193,7 +193,7 @@ export interface LabelState {
export interface SQLEditorState {
connectionTree: ConnectionAtom[];
- connectionContext: ConnectionContext;
+ selectedTable: Table;
shouldFormatContent: boolean;
queryHistoryList: QueryHistory[];
isFetchingQueryHistory: boolean;
diff --git a/frontend/src/types/tab.ts b/frontend/src/types/tab.ts
index cf800938ef..b5e4794347 100644
--- a/frontend/src/types/tab.ts
+++ b/frontend/src/types/tab.ts
@@ -1,11 +1,4 @@
-import {
- Advice,
- DatabaseId,
- InstanceId,
- ProjectId,
- SheetId,
- TableId,
-} from "../types";
+import { Advice, DatabaseId, InstanceId, ProjectId, SheetId } from "../types";
export type ExecuteConfig = {
databaseType: string;
@@ -19,7 +12,6 @@ export type Connection = {
projectId: ProjectId;
instanceId: InstanceId;
databaseId: DatabaseId;
- tableId: TableId;
};
export interface TabInfo {
diff --git a/frontend/src/utils/tab.ts b/frontend/src/utils/tab.ts
index 0b309e1a80..64f0ad857b 100644
--- a/frontend/src/utils/tab.ts
+++ b/frontend/src/utils/tab.ts
@@ -11,7 +11,6 @@ export const emptyConnection = (): Connection => {
projectId: DEFAULT_PROJECT_ID,
instanceId: UNKNOWN_ID,
databaseId: UNKNOWN_ID,
- tableId: UNKNOWN_ID,
};
};
@@ -36,3 +35,7 @@ export const isTempTab = (tab: TabInfo): boolean => {
if (tab.statement) return false;
return true;
};
+
+export const isSameConnection = (a: Connection, b: Connection): boolean => {
+ return a.instanceId === b.instanceId && a.databaseId === b.databaseId;
+};
diff --git a/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue b/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue
index d9d204822f..0727ec5b9a 100644
--- a/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue
@@ -6,7 +6,6 @@
horizontal
class="default-theme"
:dbl-click-splitter="false"
- @resized="handleResized"
>
<Pane :size="databasePaneSize"><DatabaseTree /></Pane>
<Pane :size="FULL_HEIGHT - databasePaneSize">
@@ -22,38 +21,28 @@
</template>
<script lang="ts" setup>
-import { ref, watch } from "vue";
+import { computed } from "vue";
import { useSQLEditorStore } from "@/store";
import DatabaseTree from "./DatabaseTree.vue";
import QueryHistoryContainer from "./QueryHistoryContainer.vue";
import TableSchema from "./TableSchema.vue";
import { Splitpanes, Pane } from "splitpanes";
+import { unknown, UNKNOWN_ID } from "@/types";
const FULL_HEIGHT = 100;
const DATABASE_PANE_SIZE = 60;
const sqlEditorStore = useSQLEditorStore();
-const databasePaneSize = ref(FULL_HEIGHT);
-
-const handleResized = (data: any) => {
- const [top] = data;
- databasePaneSize.value = top.size;
-};
+const databasePaneSize = computed(() => {
+ if (sqlEditorStore.selectedTable.id !== UNKNOWN_ID) {
+ return DATABASE_PANE_SIZE;
+ }
+ return FULL_HEIGHT;
+});
const handleCloseTableSchemaPane = () => {
- databasePaneSize.value = FULL_HEIGHT;
+ sqlEditorStore.selectedTable = unknown("TABLE");
};
-
-watch(
- () => sqlEditorStore.connectionContext.option,
- (option) => {
- if (option && option.type === "table") {
- databasePaneSize.value = DATABASE_PANE_SIZE;
- } else {
- databasePaneSize.value = FULL_HEIGHT;
- }
- }
-);
</script>
<style scoped>
diff --git a/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue b/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
index ef3d39740a..76c913f520 100644
--- a/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
@@ -60,6 +60,7 @@ import {
import {
emptyConnection,
getHighlightHTMLByKeyWords,
+ isSameConnection,
mapConnectionAtom,
} from "@/utils";
import { generateInstanceNode, generateTableItem } from "./utils";
@@ -78,6 +79,7 @@ const { t } = useI18n();
const instanceStore = useInstanceStore();
const databaseStore = useDatabaseStore();
const sqlEditorStore = useSQLEditorStore();
+const tableStore = useTableStore();
const tabStore = useTabStore();
const defaultExpandedKeys = ref<string[]>([]);
@@ -143,6 +145,20 @@ watch(
const setConnection = (option: ConnectionAtom) => {
if (option) {
const conn = emptyConnection();
+ const connect = () => {
+ if (isSameConnection(tabStore.currentTab.connection, conn)) {
+ // Don't go further if the connection doesn't change.
+ return;
+ }
+ if (tabStore.currentTab.sheetId) {
+ // We won't mutate a saved sheet's connection.
+ // So we'll set connection in a temp or new tab.
+ tabStore.selectOrAddTempTab();
+ }
+ tabStore.updateCurrentTab({
+ connection: conn,
+ });
+ };
// If selected item is instance node
if (option.type === "instance") {
@@ -157,25 +173,17 @@ const setConnection = (option: ConnectionAtom) => {
const databaseId = option.parentId;
const databaseInfo = databaseStore.getDatabaseById(databaseId);
const instanceId = databaseInfo.instance.id;
+ const tableId = option.id;
conn.instanceId = instanceId;
conn.databaseId = databaseId;
- conn.tableId = option.id;
- }
-
- if (tabStore.currentTab.sheetId) {
- // We won't mutate a saved sheet's connection.
- // So we'll set connection in a temp or new tab.
- tabStore.selectOrAddTempTab();
+ tableStore
+ .getOrFetchTableByDatabaseIdAndTableId(databaseId, tableId)
+ .then((table) => {
+ sqlEditorStore.selectedTable = table;
+ });
}
- tabStore.updateCurrentTab({
- connection: conn,
- });
- // TODO(Jim): This part is for <TableSchema> only
- // and should be removed after upcoming refactor.
- sqlEditorStore.setConnectionContext({
- option,
- });
+ connect();
}
};
@@ -246,15 +254,6 @@ const nodeProps = (info: { option: ConnectionAtom }) => {
return {
onClick(e: MouseEvent) {
- // TODO(Jim): This part is for <TableSchema> only
- // and should be removed after upcoming refactor.
- const targetEl = e.target as HTMLElement;
- if (option && targetEl.className === "n-tree-node-content__text") {
- sqlEditorStore.setConnectionContext({
- option,
- });
- }
-
setConnection(option);
},
onContextmenu(e: MouseEvent) {
diff --git a/frontend/src/views/sql-editor/AsidePanel/TableSchema.vue b/frontend/src/views/sql-editor/AsidePanel/TableSchema.vue
index 46cd4310e7..e7b3450e93 100644
--- a/frontend/src/views/sql-editor/AsidePanel/TableSchema.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/TableSchema.vue
@@ -1,9 +1,9 @@
<template>
- <div v-if="tableInfo" class="table-schema">
+ <div v-if="table.id !== UNKNOWN_ID" 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 class="font-semibold">{{ tableInfo.name }}</span>
+ <span class="font-semibold">{{ table.name }}</span>
</div>
<div
class="table-schema--header-actions flex-1 flex justify-end space-x-2"
@@ -32,7 +32,7 @@
</div>
<div class="table-schema--meta text-gray-500 text-sm">
<div class="pb-1">
- <span>{{ tableInfo.rowCount }} rows</span>
+ <span>{{ table.rowCount }} rows</span>
</div>
<div class="flex justify-between items-center w-full text-xs py-2">
<div class="table-schema--content-column">
@@ -45,7 +45,7 @@
</div>
<div class="table-schema--content text-sm text-gray-400 overflow-y-auto">
<div
- v-for="(column, index) in tableInfo.columnList"
+ v-for="(column, index) in table.columnList"
:key="index"
class="flex justify-between items-center w-full p-1 hover:bg-link-hover"
>
@@ -64,30 +64,25 @@
</template>
<script lang="ts" setup>
-import { ref, watch } from "vue";
+import { computed } from "vue";
import { stringify } from "qs";
-import { Database } from "@/types";
-import { useSQLEditorStore, useTableStore } from "@/store";
+import { Database, UNKNOWN_ID } from "@/types";
+import { useSQLEditorStore } from "@/store";
const emit = defineEmits<{
(e: "close-pane"): void;
}>();
-const tableStore = useTableStore();
const sqlEditorStore = useSQLEditorStore();
-
-const tableInfo = ref();
+const table = computed(() => sqlEditorStore.selectedTable);
const gotoAlterSchema = () => {
- // TODO(Jim): Remove the dependency of `sqlEditorStore.connectionContext.option`
- // after refactoring <DatabaseTree>
- const { option } = sqlEditorStore.connectionContext;
- if (option.type !== "table") {
+ if (table.value.id === UNKNOWN_ID) {
return;
}
- const tableName = option.label;
- const databaseId = option.parentId;
+ const tableName = table.value.name;
+ const databaseId = table.value.database.id;
const projectId = sqlEditorStore.findProjectIdByDatabaseId(databaseId);
const databaseList =
@@ -109,23 +104,6 @@ const gotoAlterSchema = () => {
const handleClosePane = () => {
emit("close-pane");
};
-
-watch(
- () => sqlEditorStore.connectionContext.option,
- async (option) => {
- if (option && option.type === "table") {
- const res = await tableStore.fetchTableByDatabaseIdAndTableName({
- databaseId: option.parentId as number,
- tableName: option.label as string,
- });
-
- tableInfo.value = res;
- } else {
- tableInfo.value = null;
- }
- },
- { deep: true }
-);
</script>
<style scoped>
diff --git a/frontend/src/views/sql-editor/EditorPanel/EditorAction.vue b/frontend/src/views/sql-editor/EditorPanel/EditorAction.vue
index d7fa487b58..008f3896a4 100644
--- a/frontend/src/views/sql-editor/EditorPanel/EditorAction.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/EditorAction.vue
@@ -60,14 +60,6 @@
<heroicons-outline:database />
<span class="ml-2">{{ selectedDatabase.name }}</span>
</div>
- <div
- v-if="selectedTable.id !== UNKNOWN_ID"
- class="flex items-center"
- >
- &nbsp; / &nbsp;
- <heroicons-outline:table />
- <span class="ml-2">{{ selectedTable.name }}</span>
- </div>
</label>
</template>
<section>
@@ -86,10 +78,6 @@
<h1 class="text-gray-400">{{ $t("common.database") }}:</h1>
<span>{{ selectedDatabase.name }}</span>
</div>
- <div v-if="selectedTable.id !== UNKNOWN_ID" class="flex flex-col">
- <h1 class="text-gray-400">{{ $t("common.table") }}:</h1>
- <span>{{ selectedTable.name }}</span>
- </div>
</div>
</section>
</NPopover>
@@ -132,7 +120,6 @@ import {
useSQLEditorStore,
useInstanceById,
useDatabaseById,
- useTableStore,
} from "@/store";
import { UNKNOWN_ID } from "@/types";
import { instanceSlug } from "@/utils/slug";
@@ -146,7 +133,6 @@ const router = useRouter();
const instanceStore = useInstanceStore();
const tabStore = useTabStore();
const sqlEditorStore = useSQLEditorStore();
-const tableStore = useTableStore();
const connection = computed(() => tabStore.currentTab.connection);
@@ -163,10 +149,6 @@ const selectedInstanceEngine = computed(() => {
const selectedDatabase = useDatabaseById(
computed(() => connection.value.databaseId)
);
-const selectedTable = computed(() => {
- const { databaseId, tableId } = connection.value;
- return tableStore.getTableByDatabaseIdAndTableId(databaseId, tableId);
-});
const hasReadonlyDataSource = computed(() => {
for (const ds of selectedInstance.value.dataSourceList) {