Skip to content

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

feat(sql-editor): call SchemaEditor for alter schema (#4635)

Liu Ji316c0fa1f161
diff --git a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
index e57c7eba0b..6a59571e8c 100644
--- a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
@@ -174,6 +174,10 @@ const props = defineProps({
type: String as PropType<"TENANT" | "MULTI_DB" | "SINGLE_DB">,
required: true,
},
+ newWindow: {
+ type: Boolean,
+ default: false,
+ },
});
const emit = defineEmits<{
@@ -491,13 +495,19 @@ const handlePreviewIssue = async () => {
}
}
- router.push({
+ const routeInfo = {
name: "workspace.issue.detail",
params: {
issueSlug: "new",
},
query,
- });
+ };
+ if (props.newWindow) {
+ const route = router.resolve(routeInfo);
+ window.open(route.fullPath, "__blank");
+ } else {
+ router.push(routeInfo);
+ }
};
const generateIssueName = (
diff --git a/frontend/src/store/modules/connectionTree.ts b/frontend/src/store/modules/connectionTree.ts
index 1f032f09d6..b4fbbc160c 100644
--- a/frontend/src/store/modules/connectionTree.ts
+++ b/frontend/src/store/modules/connectionTree.ts
@@ -184,3 +184,18 @@ export const searchConnectionByName = (
return connection;
};
+
+export const isConnectableAtom = (atom: ConnectionAtom): boolean => {
+ if (atom.disabled) {
+ return false;
+ }
+ if (atom.type === "database") {
+ return true;
+ }
+ if (atom.type === "instance") {
+ const instance = useInstanceStore().getInstanceById(atom.id);
+ const { engine } = instance;
+ return engine === "MYSQL" || engine === "TIDB";
+ }
+ return false;
+};
diff --git a/frontend/src/store/modules/project.ts b/frontend/src/store/modules/project.ts
index 25d25dd7e8..4a6aef3f27 100644
--- a/frontend/src/store/modules/project.ts
+++ b/frontend/src/store/modules/project.ts
@@ -17,6 +17,7 @@ import {
ResourceObject,
RowStatus,
unknown,
+ UNKNOWN_ID,
} from "@/types";
import { getPrincipalFromIncludedList } from "./principal";
@@ -138,6 +139,15 @@ export const useProjectStore = defineStore("project", {
return this.projectById.get(projectId) || (unknown("PROJECT") as Project);
},
+ async getOrFetchProjectById(projectId: ProjectId): Promise<Project> {
+ if (projectId === EMPTY_ID) return empty("PROJECT");
+ if (projectId === UNKNOWN_ID) return unknown("PROJECT");
+ if (!this.projectById.has(projectId)) {
+ await this.fetchProjectById(projectId);
+ }
+ return this.getProjectById(projectId);
+ },
+
async fetchAllProjectList() {
const data = (await axios.get(`/api/project`)).data;
const projectList = data.data.map((project: ResourceObject) => {
diff --git a/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue b/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue
index a25a7312d2..2c9ae331b0 100644
--- a/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/AsidePanel.vue
@@ -8,10 +8,10 @@
:dbl-click-splitter="false"
>
<Pane>
- <DatabaseTree />
+ <DatabaseTree @alter-schema="$emit('alter-schema', $event)" />
</Pane>
<Pane v-if="showSchemaPanel" :size="40">
- <SchemaPanel />
+ <SchemaPanel @alter-schema="$emit('alter-schema', $event)" />
</Pane>
</Splitpanes>
</n-tab-pane>
@@ -26,10 +26,10 @@
:dbl-click-splitter="false"
>
<Pane>
- <DatabaseTree />
+ <DatabaseTree @alter-schema="$emit('alter-schema', $event)" />
</Pane>
<Pane v-if="showSchemaPanel" :size="40">
- <SchemaPanel />
+ <SchemaPanel @alter-schema="$emit('alter-schema', $event)" />
</Pane>
</Splitpanes>
</n-tab-pane>
@@ -48,9 +48,16 @@ import DatabaseTree from "./DatabaseTree.vue";
import QueryHistoryContainer from "./QueryHistoryContainer.vue";
import SchemaPanel from "./SchemaPanel/";
import { Splitpanes, Pane } from "splitpanes";
-import { ConnectionTreeMode, UNKNOWN_ID } from "@/types";
+import { ConnectionTreeMode, DatabaseId, UNKNOWN_ID } from "@/types";
import { hasWorkspacePermission } from "@/utils";
+defineEmits<{
+ (
+ event: "alter-schema",
+ params: { databaseId: DatabaseId; schema: string; table: string }
+ ): void;
+}>();
+
const currentUser = useCurrentUser();
const tabStore = useTabStore();
const connectionTreeStore = useConnectionTreeStore();
diff --git a/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue b/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
index 41f15d272e..4adfc92b09 100644
--- a/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/DatabaseTree.vue
@@ -50,7 +50,6 @@
import { ref, computed, h, nextTick, watch } from "vue";
import { NTree, NDropdown, DropdownOption, TreeOption } from "naive-ui";
import { useI18n } from "vue-i18n";
-import { stringify } from "qs";
import type {
ConnectionAtom,
@@ -61,6 +60,7 @@ import type {
} from "@/types";
import { ConnectionTreeState, TabMode, UNKNOWN_ID } from "@/types";
import {
+ isConnectableAtom,
useConnectionTreeStore,
useCurrentUser,
useDatabaseStore,
@@ -86,6 +86,13 @@ type DropdownOptionWithConnectionAtom = DropdownOption & {
item: ConnectionAtom;
};
+const emit = defineEmits<{
+ (
+ event: "alter-schema",
+ params: { databaseId: DatabaseId; schema: string; table: string }
+ ): void;
+}>();
+
const { t } = useI18n();
const databaseStore = useDatabaseStore();
@@ -114,17 +121,26 @@ const dropdownOptions = computed((): DropdownOptionWithConnectionAtom[] => {
if (atom.disabled) {
return [];
}
- const items = [
- {
+
+ const items: DropdownOptionWithConnectionAtom[] = [];
+ if (isConnectableAtom(atom)) {
+ items.push({
key: "connect",
label: t("sql-editor.connect"),
item: atom,
- },
- ];
- if (allowAdmin.value) {
+ });
+ if (allowAdmin.value) {
+ items.push({
+ key: "connect-in-admin-mode",
+ label: t("sql-editor.connect-in-admin-mode"),
+ item: atom,
+ });
+ }
+ }
+ if (atom.type === "database") {
items.push({
- key: "connect-in-admin-mode",
- label: t("sql-editor.connect-in-admin-mode"),
+ key: "alter-schema",
+ label: t("database.alter-schema"),
item: atom,
});
}
@@ -219,32 +235,18 @@ const renderSuffix = ({ option }: { option: TreeOption }) => {
return h(Suffix, { atom });
};
-const gotoAlterSchema = (option: ConnectionAtom) => {
- const databaseId = option.parentId;
- const database = databaseStore.getDatabaseById(databaseId);
- if (database.id === UNKNOWN_ID) {
- return;
- }
-
- const query = {
- template: "bb.issue.database.schema.update",
- name: `[${database.name}] Alter schema`,
- project: database.project.id,
- databaseList: databaseId,
- sql: `ALTER TABLE ${option.label}`,
- };
- const url = `/issue/new?${stringify(query)}`;
- window.open(url, "_blank");
-};
-
const handleSelect = (key: string) => {
const option = dropdownOptions.value.find((item) => item.key === key);
if (!option) {
return;
}
- if (key === "alter-table") {
- gotoAlterSchema(option.item);
+ if (key === "alter-schema") {
+ emit("alter-schema", {
+ databaseId: option.item.id,
+ schema: "",
+ table: "",
+ });
} else if (key === "connect") {
setConnection(option.item);
} else if (key === "connect-in-admin-mode") {
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/AlterSchemaButton.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/AlterSchemaButton.vue
index 410dacc170..4feea69bd8 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/AlterSchemaButton.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/AlterSchemaButton.vue
@@ -6,7 +6,7 @@
size="tiny"
class="!px-1"
v-bind="$attrs"
- @click="gotoAlterSchema"
+ @click="emit('click')"
>
<heroicons-outline:pencil-alt class="w-4 h-4" />
</NButton>
@@ -16,63 +16,21 @@
</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";
+import type { Database } from "@/types";
-const props = defineProps<{
+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");
-};
+const emit = defineEmits<{
+ (event: "click"): void;
+}>();
</script>
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/DatabaseSchema.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/DatabaseSchema.vue
index 3a9ced983e..33e8bd2979 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/DatabaseSchema.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/DatabaseSchema.vue
@@ -18,7 +18,16 @@
:link="`/db/${databaseSlug(database)}`"
:tooltip="$t('common.detail')"
/>
- <AlterSchemaButton :database="database" />
+ <AlterSchemaButton
+ :database="database"
+ @click="
+ emit('alter-schema', {
+ databaseId: database.id,
+ schema: '',
+ table: '',
+ })
+ "
+ />
</div>
</div>
@@ -50,7 +59,7 @@ import type {
SchemaMetadata,
TableMetadata,
} from "@/types/proto/store/database";
-import type { Database } from "@/types";
+import type { Database, DatabaseId } from "@/types";
import { databaseSlug } from "@/utils";
import ExternalLinkButton from "./ExternalLinkButton.vue";
import AlterSchemaButton from "./AlterSchemaButton.vue";
@@ -65,6 +74,10 @@ const props = defineProps<{
const emit = defineEmits<{
(e: "click-header"): void;
(e: "select-table", schema: SchemaMetadata, table: TableMetadata): void;
+ (
+ event: "alter-schema",
+ params: { databaseId: DatabaseId; schema: string; table: string }
+ ): void;
}>();
const engine = computed(() => props.database.instance.engine);
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/SchemaPanel.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/SchemaPanel.vue
index a96c6ec58e..e1b8198dc2 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/SchemaPanel.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/SchemaPanel.vue
@@ -7,6 +7,7 @@
:header-clickable="state.selected !== undefined"
@click-header="state.selected = undefined"
@select-table="handleSelectTable"
+ @alter-schema="emit('alter-schema', $event)"
/>
<Transition name="slide-up" appear>
<TableSchema
@@ -17,6 +18,7 @@
:schema="state.selected.schema"
:table="state.selected.table"
@close="state.selected = undefined"
+ @alter-schema="emit('alter-schema', $event)"
/>
</Transition>
</template>
@@ -34,7 +36,7 @@
import { computed, reactive, ref, watch } from "vue";
import { storeToRefs } from "pinia";
-import { UNKNOWN_ID } from "@/types";
+import { DatabaseId, UNKNOWN_ID } from "@/types";
import {
DatabaseMetadata,
SchemaMetadata,
@@ -48,6 +50,13 @@ type LocalState = {
selected?: { schema: SchemaMetadata; table: TableMetadata };
};
+const emit = defineEmits<{
+ (
+ event: "alter-schema",
+ params: { databaseId: DatabaseId; schema: string; table: string }
+ ): void;
+}>();
+
const state = reactive<LocalState>({
selected: undefined,
});
diff --git a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/TableSchema.vue b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/TableSchema.vue
index ae3e87269b..eabbdd3ee8 100644
--- a/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/TableSchema.vue
+++ b/frontend/src/views/sql-editor/AsidePanel/SchemaPanel/TableSchema.vue
@@ -19,6 +19,13 @@
:database="database"
:schema="schema"
:table="table"
+ @click="
+ emit('alter-schema', {
+ databaseId: database.id,
+ schema: schema.name,
+ table: table.name,
+ })
+ "
/>
</div>
</div>
@@ -47,7 +54,7 @@ import type {
SchemaMetadata,
TableMetadata,
} from "@/types/proto/store/database";
-import type { Database } from "@/types";
+import type { Database, DatabaseId } from "@/types";
import AlterSchemaButton from "./AlterSchemaButton.vue";
import { computed } from "vue";
import { databaseSlug } from "@/utils";
@@ -62,6 +69,10 @@ const props = defineProps<{
const emit = defineEmits<{
(e: "close"): void;
+ (
+ event: "alter-schema",
+ params: { databaseId: DatabaseId; schema: string; table: string }
+ ): void;
}>();
const tableDetailLink = computed((): string => {
diff --git a/frontend/src/views/sql-editor/SQLEditorPage.vue b/frontend/src/views/sql-editor/SQLEditorPage.vue
index 7a943b8621..7fbca998a5 100644
--- a/frontend/src/views/sql-editor/SQLEditorPage.vue
+++ b/frontend/src/views/sql-editor/SQLEditorPage.vue
@@ -3,7 +3,7 @@
<TabList />
<Splitpanes class="default-theme flex flex-col flex-1 overflow-hidden">
<Pane size="20">
- <AsidePanel />
+ <AsidePanel @alter-schema="handleAlterSchema" />
</Pane>
<Pane size="80" class="relative">
<template v-if="allowAccess">
@@ -65,19 +65,28 @@
</div>
</Pane>
</Splitpanes>
+
+ <SchemaEditorModal
+ v-if="alterSchemaState.showModal"
+ :database-id-list="alterSchemaState.databaseIdList"
+ :new-window="true"
+ alter-type="SINGLE_DB"
+ @close="alterSchemaState.showModal = false"
+ />
</div>
</template>
<script lang="ts" setup>
-import { computed } from "vue";
+import { computed, reactive } from "vue";
import { Splitpanes, Pane } from "splitpanes";
-import { TabMode, UNKNOWN_ID } from "@/types";
+import { DatabaseId, TabMode, UNKNOWN_ID } from "@/types";
import {
useConnectionTreeStore,
useCurrentUser,
useDatabaseStore,
useInstanceById,
+ useProjectStore,
useSQLEditorStore,
useTabStore,
} from "@/store";
@@ -88,6 +97,12 @@ import TabList from "./TabList";
import TablePanel from "./TablePanel/TablePanel.vue";
import { isDatabaseAccessible } from "@/utils";
import AdminModeButton from "./EditorCommon/AdminModeButton.vue";
+import SchemaEditorModal from "@/components/AlterSchemaPrepForm/SchemaEditorModal.vue";
+
+type AlterSchemaState = {
+ showModal: boolean;
+ databaseIdList: DatabaseId[];
+};
const tabStore = useTabStore();
const databaseStore = useDatabaseStore();
@@ -125,6 +140,19 @@ const allowReadOnlyMode = computed(() => {
}
return true;
});
+
+const alterSchemaState = reactive<AlterSchemaState>({
+ showModal: false,
+ databaseIdList: [],
+});
+
+const handleAlterSchema = async (params: { databaseId: DatabaseId }) => {
+ const { databaseId } = params;
+ const database = databaseStore.getDatabaseById(databaseId);
+ await useProjectStore().getOrFetchProjectById(database.project.id);
+ alterSchemaState.databaseIdList = [databaseId];
+ alterSchemaState.showModal = true;
+};
</script>
<style>