Skip to content

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

chore: add rename table function (#8048)

boojackd03949aa4439
diff --git a/frontend/src/components/SchemaDesigner/DatabaseTree.vue b/frontend/src/components/SchemaDesigner/DatabaseTree.vue
index bd19921587..582fd620f8 100644
--- a/frontend/src/components/SchemaDesigner/DatabaseTree.vue
+++ b/frontend/src/components/SchemaDesigner/DatabaseTree.vue
@@ -61,6 +61,7 @@
<TableNameModal
v-if="state.tableNameModalContext !== undefined"
:schema-id="state.tableNameModalContext.schemaId"
+ :table-id="state.tableNameModalContext.tableId"
@close="state.tableNameModalContext = undefined"
/>
</template>
@@ -118,6 +119,7 @@ interface LocalState {
};
tableNameModalContext?: {
schemaId: string;
+ tableId?: string;
};
}
@@ -247,6 +249,10 @@ const contextMenuOptions = computed(() => {
label: t("schema-editor.actions.restore"),
});
} else {
+ options.push({
+ key: "rename",
+ label: t("schema-editor.actions.rename"),
+ });
options.push({
key: "drop",
label: t("schema-editor.actions.drop-table"),
@@ -537,7 +543,12 @@ const handleContextMenuDropdownSelect = async (key: string) => {
};
}
} else if (treeNode.type === "table") {
- if (key === "drop") {
+ if (key === "rename") {
+ state.tableNameModalContext = {
+ schemaId: treeNode.schemaId,
+ tableId: treeNode.tableId,
+ };
+ } else if (key === "drop") {
dropTable(treeNode.schemaId, treeNode.tableId);
} else if (key === "restore") {
const table = getTable(treeNode.schemaId, treeNode.tableId);
diff --git a/frontend/src/components/SchemaDesigner/Modals/TableNameModal.vue b/frontend/src/components/SchemaDesigner/Modals/TableNameModal.vue
index 4c307e2d5e..468ecab32c 100644
--- a/frontend/src/components/SchemaDesigner/Modals/TableNameModal.vue
+++ b/frontend/src/components/SchemaDesigner/Modals/TableNameModal.vue
@@ -1,6 +1,10 @@
<template>
<BBModal
- :title="$t('schema-editor.actions.create-table')"
+ :title="
+ isCreatingTable
+ ? $t('schema-editor.actions.create-table')
+ : $t('schema-editor.actions.rename')
+ "
class="shadow-inner outline outline-gray-200"
@close="dismissModal"
>
@@ -19,23 +23,23 @@
{{ $t("common.cancel") }}
</button>
<button class="btn-primary" @click="handleConfirmButtonClick">
- {{ $t("common.create") }}
+ {{ isCreatingTable ? $t("common.create") : $t("common.save") }}
</button>
</div>
</BBModal>
</template>
<script lang="ts" setup>
-import { reactive } from "vue";
+import { computed, reactive } from "vue";
import { useI18n } from "vue-i18n";
-import { useNotificationStore } from "@/store";
+import { generateUniqueTabId, useNotificationStore } from "@/store";
import {
convertColumnMetadataToColumn,
convertTableMetadataToTable,
} from "@/types";
import { ColumnMetadata, TableMetadata } from "@/types/proto/store/database";
import { Engine } from "@/types/proto/v1/common";
-import { useSchemaDesignerContext } from "../common";
+import { SchemaDesignerTabType, useSchemaDesignerContext } from "../common";
const tableNameFieldRegexp = /^\S+$/;
@@ -43,28 +47,26 @@ interface LocalState {
tableName: string;
}
-const props = defineProps({
- schemaId: {
- type: String,
- default: "",
- },
- tableId: {
- type: String,
- default: undefined,
- },
-});
+const props = defineProps<{
+ schemaId: string;
+ tableId?: string;
+}>();
const emit = defineEmits<{
(event: "close"): void;
}>();
const { t } = useI18n();
-const { engine, editableSchemas } = useSchemaDesignerContext();
+const { engine, editableSchemas, addTab } = useSchemaDesignerContext();
const notificationStore = useNotificationStore();
const state = reactive<LocalState>({
tableName: "",
});
+const isCreatingTable = computed(() => {
+ return props.tableId === undefined;
+});
+
const handleTableNameChange = (event: Event) => {
state.tableName = (event.target as HTMLInputElement).value;
};
@@ -90,25 +92,46 @@ const handleConfirmButtonClick = async () => {
});
return;
}
+ const tableNameList = schema.tableList.map((table) => table.name);
+ if (tableNameList.includes(state.tableName)) {
+ notificationStore.pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("schema-editor.message.duplicated-table-name"),
+ });
+ return;
+ }
- const table = TableMetadata.fromPartial({});
- table.name = state.tableName;
- const column = ColumnMetadata.fromPartial({});
- column.name = "id";
- if (engine.value === Engine.POSTGRES) {
- column.type = "INTEGER";
+ if (isCreatingTable.value) {
+ const table = TableMetadata.fromPartial({});
+ table.name = state.tableName;
+ const column = ColumnMetadata.fromPartial({});
+ column.name = "id";
+ if (engine.value === Engine.POSTGRES) {
+ column.type = "INTEGER";
+ } else {
+ column.type = "INT";
+ }
+ column.comment = "ID";
+ const columnEdit = convertColumnMetadataToColumn(column);
+ columnEdit.status = "created";
+ const tableEdit = convertTableMetadataToTable(table);
+ tableEdit.status = "created";
+ tableEdit.columnList.push(columnEdit);
+ tableEdit.primaryKey.columnIdList.push(columnEdit.id);
+ schema.tableList.push(tableEdit);
+ addTab({
+ id: generateUniqueTabId(),
+ type: SchemaDesignerTabType.TabForTable,
+ schemaId: props.schemaId,
+ tableId: tableEdit.id,
+ });
} else {
- column.type = "INT";
+ const table = schema.tableList.find((table) => table.id === props.tableId);
+ if (table) {
+ table.name = state.tableName;
+ }
}
- column.comment = "ID";
- const columnEdit = convertColumnMetadataToColumn(column);
- columnEdit.status = "created";
- const tableEdit = convertTableMetadataToTable(table);
- tableEdit.status = "created";
- tableEdit.columnList.push(columnEdit);
- tableEdit.primaryKey.columnIdList.push(columnEdit.id);
- schema.tableList.push(tableEdit);
-
dismissModal();
};