Skip to content

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

refactor(sql-editor): remove Connection.projectId (#2847)

Liu Jic7412aa5233e
diff --git a/frontend/src/store/modules/sqlEditor.ts b/frontend/src/store/modules/sqlEditor.ts
index bbcb4ba90b..16b2b83ca6 100644
--- a/frontend/src/store/modules/sqlEditor.ts
+++ b/frontend/src/store/modules/sqlEditor.ts
@@ -4,9 +4,7 @@ import type {
SQLEditorState,
ConnectionAtom,
QueryInfo,
- Database,
DatabaseId,
- ProjectId,
QueryHistory,
InstanceId,
Connection,
@@ -18,9 +16,7 @@ import { useDatabaseStore } from "./database";
import { useInstanceStore } from "./instance";
import { useTableStore } from "./table";
import { useSQLStore } from "./sql";
-import { useProjectStore } from "./project";
import { useTabStore } from "./tab";
-import { emptyConnection } from "@/utils";
export const useSQLEditorStore = defineStore("sqlEditor", {
state: (): SQLEditorState => ({
@@ -35,41 +31,6 @@ export const useSQLEditorStore = defineStore("sqlEditor", {
isFetchingSheet: false,
}),
- getters: {
- // TODO: remove this after a refactor to <TableSchema>
- connectionInfo() {
- const projectStore = useProjectStore();
- const instanceStore = useInstanceStore();
- const databaseStore = useDatabaseStore();
- const tableStore = useTableStore();
-
- return {
- projectListById: projectStore.projectById,
- instanceListById: instanceStore.instanceById,
- databaseListByInstanceId: databaseStore.databaseListByInstanceId,
- databaseListByProjectId: databaseStore.databaseListByProjectId,
- tableListByDatabaseId: tableStore.tableListByDatabaseId,
- };
- },
- findProjectIdByDatabaseId:
- (state) =>
- (databaseId: DatabaseId): ProjectId => {
- const databaseStore = useDatabaseStore();
- let projectId = UNKNOWN_ID;
- const databaseListByProjectId = databaseStore.databaseListByProjectId;
- for (const [id, databaseList] of databaseListByProjectId) {
- const idx = databaseList.findIndex(
- (database: Database) => database.id === databaseId
- );
- if (idx !== -1) {
- projectId = id;
- break;
- }
- }
- return projectId;
- },
- },
-
actions: {
setSQLEditorState(payload: Partial<SQLEditorState>) {
Object.assign(this, payload);
@@ -104,15 +65,13 @@ export const useSQLEditorStore = defineStore("sqlEditor", {
instanceId: InstanceId,
databaseId: DatabaseId
): Promise<Connection> {
- const [database] = await Promise.all([
+ await Promise.all([
useDatabaseStore().getOrFetchDatabaseById(databaseId),
useInstanceStore().getOrFetchInstanceById(instanceId),
useTableStore().getOrFetchTableListByDatabaseId(databaseId),
]);
return {
- ...emptyConnection(),
- projectId: database.project.id,
instanceId,
databaseId,
};
@@ -132,8 +91,8 @@ export const useSQLEditorStore = defineStore("sqlEditor", {
);
return {
- ...emptyConnection(),
instanceId,
+ databaseId: UNKNOWN_ID,
};
},
async fetchQueryHistoryList() {
diff --git a/frontend/src/types/tab.ts b/frontend/src/types/tab.ts
index b5e4794347..332f766078 100644
--- a/frontend/src/types/tab.ts
+++ b/frontend/src/types/tab.ts
@@ -1,4 +1,4 @@
-import { Advice, DatabaseId, InstanceId, ProjectId, SheetId } from "../types";
+import { Advice, DatabaseId, InstanceId, SheetId } from "../types";
export type ExecuteConfig = {
databaseType: string;
@@ -9,7 +9,6 @@ export type ExecuteOption = {
};
export type Connection = {
- projectId: ProjectId;
instanceId: InstanceId;
databaseId: DatabaseId;
};
diff --git a/frontend/src/utils/tab.ts b/frontend/src/utils/tab.ts
index 64f0ad857b..a5520fb7f3 100644
--- a/frontend/src/utils/tab.ts
+++ b/frontend/src/utils/tab.ts
@@ -2,13 +2,13 @@ import dayjs from "dayjs";
import { computed } from "vue";
import { v1 as uuidv1 } from "uuid";
import { t } from "../plugins/i18n";
-import { Connection, DEFAULT_PROJECT_ID, TabInfo, UNKNOWN_ID } from "@/types";
+import type { Connection, TabInfo } from "@/types";
+import { UNKNOWN_ID } from "@/types";
export const defaultTabName = computed(() => t("sql-editor.untitled-sheet"));
export const emptyConnection = (): Connection => {
return {
- projectId: DEFAULT_PROJECT_ID,
instanceId: UNKNOWN_ID,
databaseId: UNKNOWN_ID,
};
diff --git a/frontend/src/views/sql-editor/AsidePanel/TableSchema.vue b/frontend/src/views/sql-editor/AsidePanel/TableSchema.vue
index e7b3450e93..bb0a675202 100644
--- a/frontend/src/views/sql-editor/AsidePanel/TableSchema.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/TableSchema.vue
@@ -67,7 +67,7 @@
import { computed } from "vue";
import { stringify } from "qs";
-import { Database, UNKNOWN_ID } from "@/types";
+import { UNKNOWN_ID } from "@/types";
import { useSQLEditorStore } from "@/store";
const emit = defineEmits<{
@@ -81,21 +81,14 @@ const gotoAlterSchema = () => {
if (table.value.id === UNKNOWN_ID) {
return;
}
- const tableName = table.value.name;
- const databaseId = table.value.database.id;
- const projectId = sqlEditorStore.findProjectIdByDatabaseId(databaseId);
- const databaseList =
- sqlEditorStore.connectionInfo.databaseListByProjectId.get(projectId) as any;
- const databaseName = databaseList.find(
- (database: Database) => database.id === databaseId
- ).name;
+ const { database } = table.value;
const query = {
template: "bb.issue.database.schema.update",
- name: `[${databaseName}] Alter schema`,
- project: projectId,
- databaseList: databaseId,
- sql: `ALTER TABLE ${tableName}`,
+ 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");
diff --git a/frontend/src/views/sql-editor/EditorPanel/EditorPanel.vue b/frontend/src/views/sql-editor/EditorPanel/EditorPanel.vue
index ce7f7f2f11..f0fba733d0 100644
--- a/frontend/src/views/sql-editor/EditorPanel/EditorPanel.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/EditorPanel.vue
@@ -41,6 +41,7 @@ import {
useSQLEditorStore,
useSheetStore,
useInstanceStore,
+ useDatabaseStore,
} from "@/store";
import { defaultTabName } from "@/utils/tab";
import EditorAction from "./EditorAction.vue";
@@ -54,6 +55,7 @@ const tabStore = useTabStore();
const sqlEditorStore = useSQLEditorStore();
const sheetStore = useSheetStore();
const instanceStore = useInstanceStore();
+const databaseStore = useDatabaseStore();
const isShowSaveSheetModal = ref(false);
@@ -100,14 +102,14 @@ const handleSaveSheet = async (sheetName?: string) => {
sheetName = sheetName ? sheetName : name;
const conn = tabStore.currentTab.connection;
+ const database = await databaseStore.getOrFetchDatabaseById(conn.databaseId);
const sheetUpsert = {
id: sheetId,
- projectId: conn.projectId,
+ projectId: database.project.id,
databaseId: conn.databaseId,
name: sheetName,
statement: statement,
};
-
const sheet = await sheetStore.upsertSheet(sheetUpsert);
tabStore.updateCurrentTab({