Skip to content

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

refactor(schema editor): add unique `id` to atom types (#4110)

boojack8aaaaf211954
diff --git a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
index 18ddc3b89a..3e93e4c8fb 100644
--- a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
@@ -298,7 +298,7 @@ const getDatabaseEditListWithSchemaEditor = () => {
if (!originSchema) {
continue;
}
- const diffSchemaResult = diffSchema(originSchema, schema);
+ const diffSchemaResult = diffSchema(database.id, originSchema, schema);
if (
diffSchemaResult.createTableList.length > 0 ||
diffSchemaResult.alterTableList.length > 0 ||
diff --git a/frontend/src/components/SchemaEditor/DatabaseTree.vue b/frontend/src/components/SchemaEditor/DatabaseTree.vue
index d32cadd915..f87aab327c 100644
--- a/frontend/src/components/SchemaEditor/DatabaseTree.vue
+++ b/frontend/src/components/SchemaEditor/DatabaseTree.vue
@@ -42,7 +42,7 @@
v-if="state.tableNameModalContext !== undefined"
:database-id="state.tableNameModalContext.databaseId"
:schema-name="state.tableNameModalContext.schemaName"
- :table-name="state.tableNameModalContext.tableName"
+ :table-id="state.tableNameModalContext.tableId"
@close="state.tableNameModalContext = undefined"
/>
</template>
@@ -63,8 +63,9 @@ import {
import { getHighlightHTMLByKeyWords, isDescendantOf } from "@/utils";
import InstanceEngineIcon from "@/components/InstanceEngineIcon.vue";
import TableNameModal from "./Modals/TableNameModal.vue";
-import DatabaseIcon from "~icons/heroicons-outline/database";
-import TableIcon from "~icons/heroicons-outline/table";
+import DatabaseIcon from "~icons/heroicons-outline/circle-stack";
+import SchemaIcon from "~icons/heroicons-outline/view-columns";
+import TableIcon from "~icons/heroicons-outline/table-cells";
import EllipsisIcon from "~icons/heroicons-solid/ellipsis-horizontal";
import { isTableChanged } from "./utils/table";
@@ -98,7 +99,7 @@ interface TreeNodeForTable extends BaseTreeNode {
instanceId: InstanceId;
databaseId: DatabaseId;
schemaName: string;
- tableName: string;
+ tableId: string;
}
type TreeNode =
@@ -119,7 +120,7 @@ interface LocalState {
tableNameModalContext?: {
databaseId: DatabaseId;
schemaName: string;
- tableName: string | undefined;
+ tableId: string | undefined;
};
}
@@ -181,7 +182,7 @@ const contextMenuOptions = computed(() => {
} else if (treeNode.type === "table") {
const table = schemaList.value
.find((schema) => schema.name === treeNode.schemaName)
- ?.tableList.find((table) => table.newName === treeNode.tableName);
+ ?.tableList.find((table) => table.id === treeNode.tableId);
if (!table) {
return [];
}
@@ -302,14 +303,14 @@ watch(
databaseTreeNode.children = tableList.map((table) => {
return {
type: "table",
- key: `t-${database.id}-${table.newName}`,
- label: table.newName,
+ key: `t-${database.id}-${table.id}`,
+ label: table.name,
children: [],
isLeaf: true,
instanceId: database.instance.id,
databaseId: database.id,
schemaName: "",
- tableName: table.newName,
+ tableId: table.id,
};
});
}
@@ -336,14 +337,14 @@ watch(
schemaTreeNode.children = schema.tableList.map((table) => {
return {
type: "table",
- key: `t-${database.id}-${table.newName}`,
- label: table.newName,
+ key: `t-${database.id}-${table.id}`,
+ label: table.name,
children: [],
isLeaf: true,
instanceId: database.instance.id,
databaseId: database.id,
- schemaName: "",
- tableName: table.newName,
+ schemaName: schema.name,
+ tableId: table.id,
};
});
}
@@ -382,7 +383,7 @@ watch(
if (!expandedKeysRef.value.includes(databaseTreeNodeKey)) {
expandedKeysRef.value.push(databaseTreeNodeKey);
}
- const tableTreeNodeKey = `t-${currentTab.databaseId}-${currentTab.tableName}`;
+ const tableTreeNodeKey = `t-${currentTab.databaseId}-${currentTab.tableId}`;
selectedKeysRef.value = [tableTreeNodeKey];
}
@@ -433,6 +434,10 @@ const renderPrefix = ({ option: treeNode }: { option: TreeNode }) => {
return h(DatabaseIcon, {
class: "w-4 h-auto text-gray-400",
});
+ } else if (treeNode.type === "schema") {
+ return h(SchemaIcon, {
+ class: "w-4 h-auto text-gray-400",
+ });
} else if (treeNode.type === "table") {
return h(TableIcon, {
class: "w-4 h-auto text-gray-400",
@@ -449,23 +454,23 @@ const renderLabel = ({ option: treeNode }: { option: TreeNode }) => {
const table = editorStore.databaseSchemaById
.get(treeNode.databaseId)
?.schemaList.find((schema) => schema.name === treeNode.schemaName)
- ?.tableList.find(
- (table) => table.newName === treeNode.tableName
- ) as Table;
-
- if (table.status === "created") {
- additionalClassList.push("text-green-700");
- } else if (table.status === "dropped") {
- additionalClassList.push("text-red-700 line-through");
- } else {
- if (
- isTableChanged(
- treeNode.databaseId,
- treeNode.schemaName,
- treeNode.tableName
- )
- ) {
- additionalClassList.push("text-yellow-700");
+ ?.tableList.find((table) => table.id === treeNode.tableId);
+
+ if (table) {
+ if (table.status === "created") {
+ additionalClassList.push("text-green-700");
+ } else if (table.status === "dropped") {
+ additionalClassList.push("text-red-700 line-through");
+ } else {
+ if (
+ isTableChanged(
+ treeNode.databaseId,
+ treeNode.schemaName,
+ treeNode.tableId
+ )
+ ) {
+ additionalClassList.push("text-yellow-700");
+ }
}
}
}
@@ -577,7 +582,7 @@ const nodeProps = ({ option: treeNode }: { option: TreeNode }) => {
type: SchemaEditorTabType.TabForTable,
databaseId: treeNode.databaseId,
schemaName: treeNode.schemaName,
- tableName: treeNode.tableName,
+ tableId: treeNode.tableId,
});
}
@@ -586,7 +591,7 @@ const nodeProps = ({ option: treeNode }: { option: TreeNode }) => {
selectedKeysRef.value = [`d-${treeNode.databaseId}`];
} else if (treeNode.type === "table") {
selectedKeysRef.value = [
- `t-${treeNode.databaseId}-${treeNode.tableId}-${treeNode.label}`,
+ `t-${treeNode.databaseId}-${treeNode.tableId}`,
];
}
state.shouldRelocateTreeNode = true;
@@ -625,7 +630,7 @@ const handleContextMenuDropdownSelect = async (key: string) => {
state.tableNameModalContext = {
databaseId: treeNode.databaseId,
schemaName: "",
- tableName: undefined,
+ tableId: undefined,
};
}
} else if (treeNode?.type === "schema") {
@@ -634,22 +639,20 @@ const handleContextMenuDropdownSelect = async (key: string) => {
state.tableNameModalContext = {
databaseId: treeNode.databaseId,
schemaName: treeNode.schemaName,
- tableName: undefined,
+ tableId: undefined,
};
}
} else if (treeNode?.type === "table") {
const table = editorStore.databaseSchemaById
.get(treeNode.databaseId)
?.schemaList.find((schema) => schema.name === treeNode.schemaName)
- ?.tableList.find(
- (table) => table.newName === treeNode.tableName
- ) as Table;
+ ?.tableList.find((table) => table.id === treeNode.tableId) as Table;
if (key === "rename") {
state.tableNameModalContext = {
databaseId: treeNode.databaseId,
schemaName: treeNode.schemaName,
- tableName: table.newName,
+ tableId: table.id,
};
} else if (key === "drop") {
editorStore.dropTable(treeNode.databaseId, treeNode.schemaName, table);
diff --git a/frontend/src/components/SchemaEditor/Modals/EditColumnForeignKeyModal.vue b/frontend/src/components/SchemaEditor/Modals/EditColumnForeignKeyModal.vue
index 28935bfbc1..52fa760cdd 100644
--- a/frontend/src/components/SchemaEditor/Modals/EditColumnForeignKeyModal.vue
+++ b/frontend/src/components/SchemaEditor/Modals/EditColumnForeignKeyModal.vue
@@ -11,10 +11,10 @@
:item-list="tableList"
:placeholder="$t('schema-editor.table.select')"
:show-prefix-item="true"
- @select-item="(table: Table) => state.referencedTable = table.newName"
+ @select-item="(table: Table) => state.referencedTableId = table.id"
>
- <template #menuItem="{ item: table }">
- {{ table.newName }}
+ <template #menuItem="{ item }">
+ {{ item.name }}
</template>
</BBSelect>
</div>
@@ -25,10 +25,10 @@
:item-list="columnList"
:placeholder="$t('schema-editor.column.select')"
:show-prefix-item="true"
- @select-item="(column: Column) => state.referencedColumn = column.newName"
+ @select-item="(column: Column) => state.referencedColumnId = column.id"
>
<template #menuItem="{ item }">
- {{ item.newName }}
+ {{ item.name }}
</template>
</BBSelect>
</div>
@@ -59,15 +59,20 @@
<script lang="ts" setup>
import { isUndefined } from "lodash-es";
-import { computed, onMounted, PropType, reactive, ref, watch } from "vue";
+import { computed, onMounted, PropType, reactive, watch } from "vue";
import { DatabaseId, UNKNOWN_ID } from "@/types";
import { useSchemaEditorStore } from "@/store";
-import { Column, Schema, Table } from "@/types/schemaEditor/atomType";
+import {
+ Column,
+ ForeignKey,
+ Schema,
+ Table,
+} from "@/types/schemaEditor/atomType";
interface LocalState {
referencedSchema?: string;
- referencedTable?: string;
- referencedColumn?: string;
+ referencedTableId?: string;
+ referencedColumnId?: string;
}
const props = defineProps({
@@ -79,13 +84,13 @@ const props = defineProps({
type: String as PropType<string>,
default: "",
},
- tableName: {
+ tableId: {
type: String as PropType<string>,
default: "",
},
- column: {
- required: true,
- type: Object as PropType<Column>,
+ columnId: {
+ type: String as PropType<string>,
+ default: "",
},
});
@@ -102,9 +107,21 @@ const schema = computed(() => {
return editorStore.getSchema(props.databaseId, props.schemaName) as Schema;
});
+const table = computed(() => {
+ return editorStore.getTable(
+ props.databaseId,
+ props.schemaName,
+ props.tableId
+ );
+});
+
+const propsColumn = computed(() => {
+ return table.value?.columnList.find((column) => column.id === props.columnId);
+});
+
const foreignKeyList = computed(() => {
return schema.value?.foreignKeyList.filter(
- (pk) => pk.table === props.tableName
+ (pk) => pk.tableId === props.tableId
);
});
@@ -112,36 +129,34 @@ const tableList = computed(() => {
return (
editorStore.getSchema(props.databaseId, state.referencedSchema || "")
?.tableList || []
- ).filter((table: Table) => table.newName !== props.tableName);
+ ).filter((table: Table) => table.id !== props.tableId);
});
const selectedTable = computed(() => {
- return tableList.value.find(
- (table) => table.newName === state.referencedTable
- );
+ return tableList.value.find((table) => table.id === state.referencedTableId);
});
const columnList = computed(() => {
return (
tableList.value
- .find((item) => item.newName === state.referencedTable)
+ .find((table) => table.id === state.referencedTableId)
?.columnList.filter(
(column) =>
- column.type.toUpperCase() === props.column.type.toUpperCase()
+ column.type.toUpperCase() === propsColumn.value?.type.toUpperCase()
) || []
);
});
const selectedColumn = computed(() => {
return columnList.value.find(
- (column) => column.newName === state.referencedColumn
+ (column) => column.id === state.referencedColumnId
);
});
const foreignKey = computed(() => {
for (const fk of foreignKeyList.value) {
- const foundIndex = fk.columnList.findIndex(
- (columnRef) => columnRef.value === props.column
+ const foundIndex = fk.columnIdList.findIndex(
+ (columnId) => columnId === props.columnId
);
if (foundIndex > -1) {
return fk;
@@ -152,25 +167,26 @@ const foreignKey = computed(() => {
onMounted(() => {
if (foreignKey.value) {
- const foundIndex = foreignKey.value.columnList.findIndex(
- (columnRef) => columnRef.value === props.column
+ const foundIndex = foreignKey.value.columnIdList.findIndex(
+ (columnId) => columnId === props.columnId
);
if (foundIndex > -1) {
state.referencedSchema = foreignKey.value.referencedSchema;
- state.referencedTable = foreignKey.value.referencedTable;
- state.referencedColumn = foreignKey.value.referencedColumns[foundIndex];
+ state.referencedTableId = foreignKey.value.referencedTableId;
+ state.referencedColumnId =
+ foreignKey.value.referencedColumnIdList[foundIndex];
}
}
});
watch(
- () => state.referencedTable,
+ () => state.referencedTableId,
() => {
const found = columnList.value.find(
- (column) => column.newName === state.referencedColumn
+ (column) => column.id === state.referencedColumnId
);
if (!found) {
- state.referencedColumn = undefined;
+ state.referencedColumnId = undefined;
}
}
);
@@ -180,15 +196,12 @@ const handleRemoveFKButtonClick = async () => {
return;
}
- const column = schema.value.tableList
- .find((table) => table.newName === props.tableName)
- ?.columnList.find((column) => column === props.column) as Column;
- const index = foreignKey.value.columnList.findIndex(
- (columnRef) => columnRef.value === column
+ const index = foreignKey.value.columnIdList.findIndex(
+ (columnId) => columnId === propsColumn.value?.id
);
if (index > -1) {
- foreignKey.value.referencedColumns.splice(index, 1);
- foreignKey.value.columnList.splice(index, 1);
+ foreignKey.value.referencedColumnIdList.splice(index, 1);
+ foreignKey.value.columnIdList.splice(index, 1);
dismissModal();
}
};
@@ -196,29 +209,35 @@ const handleRemoveFKButtonClick = async () => {
const handleConfirmButtonClick = async () => {
if (
isUndefined(state.referencedSchema) ||
- isUndefined(state.referencedTable) ||
- isUndefined(state.referencedColumn)
+ isUndefined(state.referencedTableId) ||
+ isUndefined(state.referencedColumnId)
) {
return;
}
- const column = schema.value.tableList
- .find((table) => table.newName === props.tableName)
- ?.columnList.find((column) => column === props.column) as Column;
-
+ const column = propsColumn.value as Column;
if (isUndefined(foreignKey.value)) {
- const fk = {
+ const fk: ForeignKey = {
+ // TODO(steven): it's a constraint name and should be unique.
name: "",
- table: props.tableName,
- columnList: [ref(column)],
+ tableId: props.tableId,
+ columnIdList: [column.id],
referencedSchema: state.referencedSchema,
- referencedTable: state.referencedTable,
- referencedColumns: [state.referencedColumn],
+ referencedTableId: state.referencedTableId,
+ referencedColumnIdList: [state.referencedColumnId],
};
schema.value.foreignKeyList.push(fk);
} else {
- foreignKey.value.referencedColumns.push(state.referencedColumn);
- foreignKey.value.columnList.push(ref(column));
+ const index = foreignKey.value.columnIdList.findIndex(
+ (columnId) => columnId === column.id
+ );
+ if (index >= 0) {
+ foreignKey.value.referencedColumnIdList[index] = state.referencedColumnId;
+ foreignKey.value.columnIdList[index] = column.id;
+ } else {
+ foreignKey.value.referencedColumnIdList.push(state.referencedColumnId);
+ foreignKey.value.columnIdList.push(column.id);
+ }
}
dismissModal();
};
diff --git a/frontend/src/components/SchemaEditor/Modals/TableNameModal.vue b/frontend/src/components/SchemaEditor/Modals/TableNameModal.vue
index f6a9007c5a..6859d0f78f 100644
--- a/frontend/src/components/SchemaEditor/Modals/TableNameModal.vue
+++ b/frontend/src/components/SchemaEditor/Modals/TableNameModal.vue
@@ -30,10 +30,9 @@
</template>
<script lang="ts" setup>
-import { computed, PropType, reactive } from "vue";
+import { computed, onMounted, PropType, reactive } from "vue";
import { useI18n } from "vue-i18n";
import { DatabaseId, UNKNOWN_ID, SchemaEditorTabType } from "@/types";
-import { TableTabContext } from "@/types/schemaEditor";
import {
useSchemaEditorStore,
useNotificationStore,
@@ -61,7 +60,7 @@ const props = defineProps({
type: String as PropType<string>,
default: "",
},
- tableName: {
+ tableId: {
type: String as PropType<string | undefined>,
default: undefined,
},
@@ -75,11 +74,26 @@ const { t } = useI18n();
const editorStore = useSchemaEditorStore();
const notificationStore = useNotificationStore();
const state = reactive<LocalState>({
- tableName: props.tableName || "",
+ tableName: "",
});
const isCreatingTable = computed(() => {
- return props.tableName === undefined;
+ return props.tableId === undefined;
+});
+
+onMounted(() => {
+ if (props.tableId === undefined) {
+ return;
+ }
+
+ const table = editorStore.getTable(
+ props.databaseId,
+ props.schemaName,
+ props.tableId
+ );
+ if (table) {
+ state.tableName = table.name;
+ }
});
const handleTableNameChange = (event: Event) => {
@@ -100,7 +114,7 @@ const handleConfirmButtonClick = async () => {
const schema = editorStore.databaseSchemaById
.get(databaseId)
?.schemaList.find((schema) => schema.name === props.schemaName) as Schema;
- const tableNameList = schema.tableList.map((table) => table.newName);
+ const tableNameList = schema.tableList.map((table) => table.name);
if (tableNameList.includes(state.tableName)) {
notificationStore.pushNotification({
module: "bytebase",
@@ -112,62 +126,36 @@ const handleConfirmButtonClick = async () => {
if (isCreatingTable.value) {
const table = convertTableMetadataToTable(TableMetadata.fromPartial({}));
- table.oldName = state.tableName;
- table.newName = state.tableName;
+ table.name = state.tableName;
table.status = "created";
const column = convertColumnMetadataToColumn(
ColumnMetadata.fromPartial({})
);
- column.oldName = "id";
- column.newName = "id";
+ column.name = "id";
column.type = "int";
column.comment = "ID";
column.status = "created";
table.columnList.push(column);
+ table.primaryKey.columnIdList.push(column.id);
schema.tableList.push(table);
- schema.primaryKeyList.push({
- table: table.newName,
- columnList: [],
- });
editorStore.addTab({
id: generateUniqueTabId(),
type: SchemaEditorTabType.TabForTable,
databaseId: props.databaseId,
schemaName: props.schemaName,
- tableName: table.newName,
+ tableId: table.id,
});
dismissModal();
} else {
const table = editorStore.getTable(
props.databaseId,
props.schemaName,
- props.tableName ?? ""
+ props.tableId ?? ""
);
if (table) {
- table.newName = state.tableName;
-
- // TODO(steven): use reactive Ref.
- // Update reference objects.
- const tab = editorStore.findTab(
- databaseId,
- props.tableName
- ) as TableTabContext;
- if (tab) {
- tab.tableName = table.newName;
- }
- const primaryKey = schema.primaryKeyList.find(
- (pk) => pk.table === props.tableName
- );
- if (primaryKey) {
- primaryKey.table = table.newName;
- }
- for (const fk of schema.foreignKeyList) {
- if (fk.table === props.tableName) {
- fk.table = table.newName;
- }
- }
+ table.name = state.tableName;
}
dismissModal();
}
diff --git a/frontend/src/components/SchemaEditor/Panels/DatabaseEditor.vue b/frontend/src/components/SchemaEditor/Panels/DatabaseEditor.vue
index b8f4606f58..f52dd343d2 100644
--- a/frontend/src/components/SchemaEditor/Panels/DatabaseEditor.vue
+++ b/frontend/src/components/SchemaEditor/Panels/DatabaseEditor.vue
@@ -64,7 +64,7 @@
<div class="w-full">
<div
v-for="(table, index) in tableList"
- :key="`${index}-${table.newName}`"
+ :key="`${index}-${table.name}`"
class="grid grid-cols-[repeat(6,_minmax(0,_1fr))_32px] text-sm even:bg-gray-50"
:class="
isDroppedTable(table) && 'text-red-700 !bg-red-50 opacity-70'
@@ -75,7 +75,7 @@
class="w-full cursor-pointer leading-6 my-2 hover:text-accent"
>
<span @click="handleTableItemClick(table)">
- {{ table.newName }}
+ {{ table.name }}
</span>
</NEllipsis>
</div>
@@ -261,7 +261,7 @@ watch(
if (!originSchema) {
continue;
}
- const diffSchemaResult = diffSchema(originSchema, schema);
+ const diffSchemaResult = diffSchema(database.id, originSchema, schema);
if (
diffSchemaResult.createTableList.length > 0 ||
diffSchemaResult.alterTableList.length > 0 ||
@@ -324,7 +324,7 @@ const handleTableItemClick = (table: Table) => {
type: SchemaEditorTabType.TabForTable,
databaseId: database.id,
schemaName: state.selectedSchema,
- tableName: table.newName,
+ tableId: table.id,
});
};
diff --git a/frontend/src/components/SchemaEditor/Panels/TableEditor.vue b/frontend/src/components/SchemaEditor/Panels/TableEditor.vue
index 9a47a08724..83034bd7d9 100644
--- a/frontend/src/components/SchemaEditor/Panels/TableEditor.vue
+++ b/frontend/src/components/SchemaEditor/Panels/TableEditor.vue
@@ -71,7 +71,7 @@
<div class="w-full">
<div
v-for="(column, index) in table.columnList"
- :key="`${index}-${column.oldName}`"
+ :key="`${index}-${column.id}`"
class="grid grid-cols-[repeat(4,_minmax(0,_1fr))_repeat(2,_96px)_minmax(0,_1fr)_32px] gr text-sm even:bg-gray-50"
:class="
isDroppedColumn(column) &&
@@ -80,7 +80,7 @@
>
<div class="table-body-item-container">
<input
- v-model="column.newName"
+ v-model="column.name"
:disabled="disableAlterColumn(column)"
placeholder="column name"
class="column-field-input"
@@ -243,8 +243,8 @@
v-if="state.showEditColumnForeignKeyModal && editForeignKeyColumn"
:database-id="currentTab.databaseId"
:schema-name="schema.name"
- :table-name="table.newName"
- :column="editForeignKeyColumn"
+ :table-id="table.id"
+ :column-id="editForeignKeyColumn.id"
@close="state.showEditColumnForeignKeyModal = false"
/>
</template>
@@ -300,15 +300,9 @@ const schema = computed(() => {
(schema) => schema.name === currentTab.schemaName
) as Schema;
});
-const primaryKeyColumnList = computed(() => {
- return (
- schema.value.primaryKeyList.find((pk) => pk.table === currentTab.tableName)
- ?.columnList || []
- );
-});
const foreignKeyList = computed(() => {
return schema.value.foreignKeyList.filter(
- (pk) => pk.table === currentTab.tableName
+ (pk) => pk.tableId === currentTab.tableId
) as ForeignKey[];
});
@@ -327,7 +321,7 @@ const allowResetTable = computed(() => {
isTableChanged(
currentTab.databaseId,
currentTab.schemaName,
- currentTab.tableName
+ currentTab.tableId
) || isDroppedTable.value
);
});
@@ -397,20 +391,24 @@ watch(
currentTab.databaseId,
currentTab.schemaName
) as Schema;
- const diffSchemaResult = diffSchema(originSchema, schema.value);
+ const diffSchemaResult = diffSchema(
+ currentTab.databaseId,
+ originSchema,
+ schema.value
+ );
const databaseEdit: DatabaseEdit = {
databaseId: currentTab.databaseId,
createTableList: diffSchemaResult.createTableList.filter(
- (context) => context.name === table.value.newName
+ (context) => context.name === table.value.name
),
alterTableList: diffSchemaResult.alterTableList.filter(
- (context) => context.name === table.value.newName
+ (context) => context.name === table.value.name
),
renameTableList: diffSchemaResult.renameTableList.filter(
- (context) => context.newName === table.value.newName
+ (context) => context.newName === table.value.name
),
dropTableList: diffSchemaResult.dropTableList.filter(
- (context) => context.name === table.value.oldName
+ (context) => context.name === table.value.name
),
};
state.isFetchingDDL = true;
@@ -436,20 +434,14 @@ watch(
);
const isColumnPrimaryKey = (column: Column): boolean => {
- for (const columnRef of primaryKeyColumnList.value) {
- if (columnRef.value === column) {
- return true;
- }
- }
- return false;
+ return table.value.primaryKey.columnIdList.includes(column.id);
};
const checkColumnHasForeignKey = (column: Column): boolean => {
- const columnRefList = flatten(
- foreignKeyList.value.map((fk) => fk.columnList)
+ const columnIdList = flatten(
+ foreignKeyList.value.map((fk) => fk.columnIdList)
);
- const find = columnRefList.find((columnRef) => columnRef.value === column);
- return find !== undefined;
+ return columnIdList.includes(column.id);
};
const getReferencedForeignKeyName = (column: Column) => {
@@ -458,21 +450,30 @@ const getReferencedForeignKeyName = (column: Column) => {
}
const fk = foreignKeyList.value.find(
(fk) =>
- fk.columnList.find((columnRef) => columnRef.value === column) !==
- undefined
+ fk.columnIdList.find((columnId) => columnId === column.id) !== undefined
);
- const index = fk?.columnList.findIndex(
- (columnRef) => columnRef.value === column
+ const index = fk?.columnIdList.findIndex(
+ (columnId) => columnId === column.id
);
if (isUndefined(fk) || isUndefined(index) || index < 0) {
return;
}
- const referColumnName = fk.referencedColumns[index];
+ const referencedTable = editorStore.getTable(
+ currentTab.databaseId,
+ fk.referencedSchema,
+ fk.referencedTableId
+ );
+ if (!referencedTable) {
+ return;
+ }
+ const referColumn = referencedTable.columnList.find(
+ (column) => column.id === fk.referencedColumnIdList[index]
+ );
if (databaseEngine.value === "MYSQL") {
- return `${fk.referencedTable}(${referColumnName})`;
+ return `${referencedTable.name}(${referColumn?.name})`;
} else {
- return `${fk.referencedSchema}.${fk.referencedTable}(${referColumnName})`;
+ return `${fk.referencedSchema}.${referencedTable.name}(${referColumn?.name})`;
}
};
@@ -487,16 +488,12 @@ const disableAlterColumn = (column: Column): boolean => {
const setColumnPrimaryKey = (column: Column, isPrimaryKey: boolean) => {
if (isPrimaryKey) {
column.nullable = false;
- primaryKeyColumnList.value.push(ref(column));
+ table.value.primaryKey.columnIdList.push(column.id);
} else {
- const primaryKey = schema.value.primaryKeyList.find(
- (pk) => pk.table === currentTab.tableName
- );
- if (primaryKey) {
- primaryKey.columnList = primaryKey.columnList.filter(
- (columnRef) => columnRef.value !== column
+ table.value.primaryKey.columnIdList =
+ table.value.primaryKey.columnIdList.filter(
+ (columnId) => columnId !== column.id
);
- }
}
};
@@ -531,24 +528,21 @@ const handleDropColumn = (column: Column) => {
table.value.columnList = table.value.columnList.filter(
(item) => item !== column
);
- const primaryKey = schema.value.primaryKeyList.find(
- (pk) => pk.table === currentTab.tableName
- );
- if (primaryKey) {
- primaryKey.columnList = primaryKey.columnList.filter(
- (item) => item.value !== column
+ table.value.primaryKey.columnIdList =
+ table.value.primaryKey.columnIdList.filter(
+ (columnId) => columnId !== column.id
);
- }
+
const foreignKeyList = schema.value.foreignKeyList.filter(
- (fk) => fk.table === currentTab.tableName
+ (fk) => fk.tableId === currentTab.tableId
);
for (const foreignKey of foreignKeyList) {
- const columnRefIndex = foreignKey.columnList.findIndex(
- (columnRef) => columnRef.value === column
+ const columnRefIndex = foreignKey.columnIdList.findIndex(
+ (columnId) => columnId === column.id
);
if (columnRefIndex > -1) {
- foreignKey.columnList.splice(columnRefIndex, 1);
- foreignKey.referencedColumns.splice(columnRefIndex, 1);
+ foreignKey.columnIdList.splice(columnRefIndex, 1);
+ foreignKey.referencedColumnIdList.splice(columnRefIndex, 1);
}
}
} else {
@@ -576,58 +570,27 @@ const handleDiscardChanges = () => {
const originTable = editorStore.getOriginTable(
currentTab.databaseId,
currentTab.schemaName,
- table.value.oldName
+ table.value.id
);
- const previousName = table.value.newName;
- table.value.newName = table.value.oldName;
- table.value.columnList = cloneDeep(originTable?.columnList ?? []);
- table.value.status = "normal";
-
- // TODO(steven): use reactive Ref.
- // Update reference objects.
- currentTab.tableName = table.value.oldName;
- const primaryKey = schema.value.primaryKeyList.find(
- (pk) => pk.table === previousName
- );
- if (primaryKey) {
- const originPrimaryKeyColumnNameList = (
- originSchema?.primaryKeyList.find(
- (pk) => pk.table === table.value.oldName
- )?.columnList || []
- ).map((columnRef) => columnRef.value.oldName);
- primaryKey.table = table.value.oldName;
- primaryKey.columnList = [];
- for (const column of table.value.columnList) {
- if (originPrimaryKeyColumnNameList.includes(column.oldName)) {
- primaryKey.columnList.push(ref(column));
- }
- }
+ if (!originTable) {
+ return;
}
+ table.value.name = originTable.name;
+ table.value.columnList = cloneDeep(originTable.columnList);
+ table.value.primaryKey = cloneDeep(originTable.primaryKey);
+ table.value.status = "normal";
+
schema.value.foreignKeyList = schema.value.foreignKeyList.filter(
- (fk) => fk.table !== table.value.oldName
+ (fk) => fk.tableId !== table.value.id
);
const originForeignKeyList =
originSchema?.foreignKeyList.filter(
- (fk) => fk.table === table.value.oldName
+ (fk) => fk.tableId === table.value.id
) || [];
for (const originForeignKey of originForeignKeyList) {
- const tempForeignKey: ForeignKey = {
- ...cloneDeep(originForeignKey),
- columnList: [],
- };
- const originColumnNameList = originForeignKey.columnList.map(
- (columnRef) => columnRef.value.oldName
- );
- for (const column of table.value.columnList) {
- if (originColumnNameList.includes(column.oldName)) {
- tempForeignKey.columnList.push(ref(column));
- }
- }
- if (tempForeignKey.columnList.length > 0) {
- schema.value.foreignKeyList.push(tempForeignKey);
- }
+ schema.value.foreignKeyList.push(cloneDeep(originForeignKey));
}
};
</script>
diff --git a/frontend/src/components/SchemaEditor/TabsContainer.vue b/frontend/src/components/SchemaEditor/TabsContainer.vue
index 7d19f2146f..5f42d2d8db 100644
--- a/frontend/src/components/SchemaEditor/TabsContainer.vue
+++ b/frontend/src/components/SchemaEditor/TabsContainer.vue
@@ -89,7 +89,7 @@ const getTabComputedClassList = (tab: TabContext) => {
return ["text-green-700"];
}
- if (isTableChanged(tab.databaseId, tab.schemaName, tab.tableName)) {
+ if (isTableChanged(tab.databaseId, tab.schemaName, tab.tableId)) {
return ["text-yellow-700"];
}
}
@@ -104,7 +104,12 @@ const getTabName = (tab: TabContext) => {
);
return `${database?.name || "unknown database"}`;
} else if (tab.type === SchemaEditorTabType.TabForTable) {
- return `${tab.tableName}`;
+ const table = editorStore.getTable(
+ tab.databaseId,
+ tab.schemaName,
+ tab.tableId
+ );
+ return `${table?.name || ""}`;
} else {
// Should never reach here.
return "unknown structure";
diff --git a/frontend/src/components/SchemaEditor/utils/table.ts b/frontend/src/components/SchemaEditor/utils/table.ts
index ba960b2269..346794bc36 100644
--- a/frontend/src/components/SchemaEditor/utils/table.ts
+++ b/frontend/src/components/SchemaEditor/utils/table.ts
@@ -1,70 +1,27 @@
import { isEqual } from "lodash-es";
import { useSchemaEditorStore } from "@/store";
import { DatabaseId } from "@/types";
-import { ForeignKey } from "@/types/schemaEditor/atomType";
export const isTableChanged = (
databaseId: DatabaseId,
schemaName: string,
- tableName: string
+ tableId: string
): boolean => {
const editorStore = useSchemaEditorStore();
const originSchema = editorStore.getOriginSchema(databaseId, schemaName);
const schema = editorStore.getSchema(databaseId, schemaName);
- const table = schema?.tableList.find((table) => table.newName === tableName);
+ const table = schema?.tableList.find((table) => table.id === tableId);
const originTable = originSchema?.tableList.find(
- (item) => item.oldName === table?.oldName
+ (table) => table.id === tableId
);
- const originPrimaryKeyColumnList = originSchema?.primaryKeyList
- .find((pk) => pk.table === table?.oldName)
- ?.columnList.map((columnRef) => columnRef.value)
- .sort();
- const primaryKeyColumnList = schema?.primaryKeyList
- .find((pk) => pk.table === tableName)
- ?.columnList.map((columnRef) => columnRef.value)
- .sort();
+
const originForeignKeyList =
- originSchema?.foreignKeyList.filter((pk) => pk.table === table?.oldName) ||
- [];
+ originSchema?.foreignKeyList.filter((fk) => fk.tableId === table?.id) || [];
const foreignKeyList =
- schema?.foreignKeyList.filter((pk) => pk.table === tableName) || [];
+ schema?.foreignKeyList.filter((fk) => fk.tableId === table?.id) || [];
return (
!isEqual(originTable, table) ||
- !isEqual(originPrimaryKeyColumnList, primaryKeyColumnList) ||
- !isEqualForeignKeyList(originForeignKeyList, foreignKeyList)
- );
-};
-
-export const isEqualForeignKeyList = (
- originForeignKeyList: ForeignKey[],
- foreignKeyList: ForeignKey[]
-) => {
- if (originForeignKeyList.length !== foreignKeyList.length) {
- return false;
- }
- for (let i = 0; i < foreignKeyList.length; i++) {
- if (!isEqualForeignKey(originForeignKeyList[i], foreignKeyList[i])) {
- return false;
- }
- }
- return true;
-};
-
-export const isEqualForeignKey = (
- originForeignKey?: ForeignKey,
- foreignKey?: ForeignKey
-) => {
- return isEqual(
- {
- columnList: originForeignKey?.columnList.map(
- (columnRef) => columnRef.value
- ),
- referencedColumns: originForeignKey?.referencedColumns,
- },
- {
- columnList: foreignKey?.columnList.map((columnRef) => columnRef.value),
- referencedColumns: foreignKey?.referencedColumns,
- }
+ !isEqual(originForeignKeyList, foreignKeyList)
);
};
diff --git a/frontend/src/store/modules/schemaEditor.ts b/frontend/src/store/modules/schemaEditor.ts
index 09bd9b5056..8d64322bcd 100644
--- a/frontend/src/store/modules/schemaEditor.ts
+++ b/frontend/src/store/modules/schemaEditor.ts
@@ -77,7 +77,7 @@ export const useSchemaEditorStore = defineStore("SchemaEditor", {
if (
item.type === SchemaEditorTabType.TabForTable &&
item.databaseId === tab.databaseId &&
- item.tableName === (tab as TableTabContext).tableName
+ item.tableId === (tab as TableTabContext).tableId
) {
return true;
}
@@ -121,9 +121,9 @@ export const useSchemaEditorStore = defineStore("SchemaEditor", {
}
this.tabState.tabMap.delete(tabId);
},
- findTab(databaseId: DatabaseId, tableName?: string) {
+ findTab(databaseId: DatabaseId, tableId?: string) {
let tabType = SchemaEditorTabType.TabForDatabase;
- if (tableName !== undefined) {
+ if (tableId !== undefined) {
tabType = SchemaEditorTabType.TabForTable;
}
@@ -136,7 +136,7 @@ export const useSchemaEditorStore = defineStore("SchemaEditor", {
return true;
} else if (
tab.type === SchemaEditorTabType.TabForTable &&
- tab.tableName === tableName
+ tab.tableId === tableId
) {
return true;
}
@@ -195,27 +195,27 @@ export const useSchemaEditorStore = defineStore("SchemaEditor", {
.get(databaseId)
?.originSchemaList.find((schema) => schema.name === schemaName);
},
- getTable(databaseId: DatabaseId, schemaName: string, tableName: string) {
+ getTable(databaseId: DatabaseId, schemaName: string, tableId: string) {
return this.databaseSchemaById
.get(databaseId)
?.schemaList.find((schema) => schema.name === schemaName)
- ?.tableList.find((table) => table.newName === tableName);
+ ?.tableList.find((table) => table.id === tableId);
},
getOriginTable(
databaseId: DatabaseId,
schemaName: string,
- tableName: string
+ tableId: string
) {
return this.databaseSchemaById
.get(databaseId)
?.originSchemaList.find((schema) => schema.name === schemaName)
- ?.tableList.find((table) => table.newName === tableName);
+ ?.tableList.find((table) => table.id === tableId);
},
getTableWithTableTab(tab: TableTabContext) {
return this.databaseSchemaById
.get(tab.databaseId)
?.schemaList.find((schema) => schema.name === tab.schemaName)
- ?.tableList?.find((table) => table.newName === tab.tableName);
+ ?.tableList?.find((table) => table.id === tab.tableId);
},
dropTable(databaseId: DatabaseId, schemaName: string, table: Table) {
// Remove table record and close tab for created table.
@@ -224,11 +224,9 @@ export const useSchemaEditorStore = defineStore("SchemaEditor", {
.get(databaseId)
?.schemaList.find((schema) => schema.name === schemaName)
?.tableList as Table[];
- const index = tableList.findIndex(
- (item) => item.newName === table.newName
- );
+ const index = tableList.findIndex((item) => item.id === table.id);
tableList.splice(index, 1);
- const tab = this.findTab(databaseId, table.newName);
+ const tab = this.findTab(databaseId, table.id);
if (tab) {
this.closeTab(tab.id);
}
diff --git a/frontend/src/types/schemaEditor/atomType.ts b/frontend/src/types/schemaEditor/atomType.ts
index 0ecebc7b9d..bc9b136443 100644
--- a/frontend/src/types/schemaEditor/atomType.ts
+++ b/frontend/src/types/schemaEditor/atomType.ts
@@ -1,64 +1,65 @@
-import { ref, Ref } from "vue";
+import { v1 as uuidv1 } from "uuid";
import {
ColumnMetadata,
SchemaMetadata,
TableMetadata,
} from "../proto/store/database";
-type TableOrColumnStatus = "normal" | "created" | "dropped";
+type AtomResourceStatus = "normal" | "created" | "dropped";
export interface Column {
- oldName: string;
- newName: string;
+ id: string;
+ name: string;
type: string;
nullable: boolean;
comment: string;
default?: string;
- status: TableOrColumnStatus;
+ status: AtomResourceStatus;
+}
+
+export interface PrimaryKey {
+ name: string;
+ columnIdList: string[];
}
export interface Table {
- oldName: string;
- newName: string;
+ id: string;
+ name: string;
engine: string;
collation: string;
rowCount: number;
dataSize: number;
comment: string;
columnList: Column[];
- status: TableOrColumnStatus;
-}
-
-export interface PrimaryKey {
- table: string;
- // column is a ref to Column.
- columnList: Ref<Column>[];
+ // Including column id list.
+ primaryKey: PrimaryKey;
+ status: AtomResourceStatus;
}
export interface ForeignKey {
// Should be an unique name.
name: string;
- table: string;
- columnList: Ref<Column>[];
+ tableId: string;
+ columnIdList: string[];
referencedSchema: string;
- referencedTable: string;
- referencedColumns: string[];
+ referencedTableId: string;
+ referencedColumnIdList: string[];
}
export interface Schema {
// It should be an empty string for MySQL/TiDB.
name: string;
tableList: Table[];
- primaryKeyList: PrimaryKey[];
foreignKeyList: ForeignKey[];
+ status: AtomResourceStatus;
}
export const convertColumnMetadataToColumn = (
columnMetadata: ColumnMetadata
): Column => {
return {
- oldName: columnMetadata.name,
- newName: columnMetadata.name,
+ id: uuidv1(),
+ name: columnMetadata.name,
type: columnMetadata.type,
nullable: columnMetadata.nullable,
comment: columnMetadata.comment,
@@ -70,9 +71,9 @@ export const convertColumnMetadataToColumn = (
export const convertTableMetadataToTable = (
tableMetadata: TableMetadata
): Table => {
- return {
- oldName: tableMetadata.name,
- newName: tableMetadata.name,
+ const table: Table = {
+ id: uuidv1(),
+ name: tableMetadata.name,
engine: tableMetadata.engine,
collation: tableMetadata.collation,
rowCount: tableMetadata.rowCount,
@@ -81,39 +82,47 @@ export const convertTableMetadataToTable = (
columnList: tableMetadata.columns.map((column) =>
convertColumnMetadataToColumn(column)
),
+ primaryKey: {
+ name: "",
+ columnIdList: [],
+ },
status: "normal",
};
+
+ for (const indexMetadata of tableMetadata.indexes) {
+ if (indexMetadata.primary === true) {
+ table.primaryKey.name = indexMetadata.name;
+ for (const columnName of indexMetadata.expressions) {
+ const column = table.columnList.find(
+ (column) => column.name === columnName
+ );
+ if (column) {
+ table.primaryKey.columnIdList.push(column.id);
+ }
+ }
+ break;
+ }
+ }
+
+ return table;
};
export const convertSchemaMetadataToSchema = (
schemaMetadata: SchemaMetadata
): Schema => {
const tableList: Table[] = [];
- const primaryKeyList: PrimaryKey[] = [];
const foreignKeyList: ForeignKey[] = [];
for (const tableMetadata of schemaMetadata.tables) {
const table = convertTableMetadataToTable(tableMetadata);
tableList.push(table);
+ }
- const primaryKey: PrimaryKey = {
- table: tableMetadata.name,
- columnList: [],
- };
- for (const indexMetadata of tableMetadata.indexes) {
- if (indexMetadata.primary === true) {
- for (const columnName of indexMetadata.expressions) {
- const column = table.columnList.find(
- (column) => column.oldName === columnName
- );
- if (column) {
- primaryKey.columnList.push(ref(column));
- }
- }
- break;
- }
+ for (const tableMetadata of schemaMetadata.tables) {
+ const table = tableList.find((table) => table.name === tableMetadata.name);
+ if (!table) {
+ continue;
}
- primaryKeyList.push(primaryKey);
for (const foreignKeyMetadata of tableMetadata.foreignKeys) {
// TODO(steven): remove this after backend return unique fk.
@@ -122,24 +131,39 @@ export const convertSchemaMetadataToSchema = (
) {
continue;
}
+ const referencedTable = tableList.find(
+ (table) => table.name === foreignKeyMetadata.referencedTable
+ );
+ if (!referencedTable) {
+ continue;
+ }
const fk: ForeignKey = {
- table: tableMetadata.name,
name: foreignKeyMetadata.name,
- columnList: [],
+ tableId: table.id,
+ columnIdList: [],
referencedSchema: foreignKeyMetadata.referencedSchema,
- referencedTable: foreignKeyMetadata.referencedTable,
- referencedColumns: foreignKeyMetadata.referencedColumns,
+ referencedTableId: referencedTable.id,
+ referencedColumnIdList: [],
};
for (const columnName of foreignKeyMetadata.columns) {
const column = table.columnList.find(
- (column) => column.oldName === columnName
+ (column) => column.name === columnName
);
if (column) {
- fk.columnList.push(ref(column));
+ fk.columnIdList.push(column.id);
}
}
+ for (const referencedColumnName of foreignKeyMetadata.referencedColumns) {
+ const referencedColumn = referencedTable.columnList.find(
+ (column) => column.name === referencedColumnName
+ );
+ if (referencedColumn) {
+ fk.referencedColumnIdList.push(referencedColumn.id);
+ }
+ }
+
foreignKeyList.push(fk);
}
}
@@ -147,7 +171,7 @@ export const convertSchemaMetadataToSchema = (
return {
name: schemaMetadata.name,
tableList: tableList,
- primaryKeyList: primaryKeyList,
foreignKeyList: foreignKeyList,
+ status: "normal",
};
};
diff --git a/frontend/src/types/schemaEditor/index.ts b/frontend/src/types/schemaEditor/index.ts
index e51e9ce14d..750b696cfc 100644
--- a/frontend/src/types/schemaEditor/index.ts
+++ b/frontend/src/types/schemaEditor/index.ts
@@ -19,7 +19,7 @@ export interface TableTabContext {
type: SchemaEditorTabType.TabForTable;
databaseId: DatabaseId;
schemaName: string;
- tableName: string;
+ tableId: string;
}
export type TabContext = DatabaseTabContext | TableTabContext;
@@ -71,6 +71,7 @@ export interface AlterTableContext {
changeColumnList: ChangeColumnContext[];
dropColumnList: DropColumnContext[];
dropPrimaryKey: boolean;
+ dropPrimaryKeyList: string[];
primaryKeyList?: string[];
dropForeignKeyList: string[];
addForeignKeyList: AddForeignKeyContext[];
@@ -112,6 +113,7 @@ export interface DropColumnContext {
export interface AddForeignKeyContext {
columnList: string[];
+ referencedSchema: string;
referencedTable: string;
referencedColumnList: string[];
}
diff --git a/frontend/src/utils/schemaEditor/diffColumn.test.ts b/frontend/src/utils/schemaEditor/diffColumn.test.ts
index 6cb86878d2..1af5f80549 100644
--- a/frontend/src/utils/schemaEditor/diffColumn.test.ts
+++ b/frontend/src/utils/schemaEditor/diffColumn.test.ts
@@ -21,8 +21,8 @@ it("diff add column list", () => {
originColumnList: [],
columnList: [
{
- oldName: "id",
- newName: "id",
+ id: "1",
+ name: "id",
type: "int",
comment: "",
nullable: false,
@@ -66,8 +66,8 @@ it("diff modify column list", () => {
{
originColumnList: [
{
- oldName: "id",
- newName: "id",
+ id: "1",
+ name: "id",
type: "int",
comment: "",
nullable: true,
@@ -76,8 +76,8 @@ it("diff modify column list", () => {
],
columnList: [
{
- oldName: "id",
- newName: "id",
+ id: "1",
+ name: "id",
type: "varchar",
comment: "",
nullable: false,
@@ -122,8 +122,8 @@ it("diff drop column list", () => {
{
originColumnList: [
{
- oldName: "id",
- newName: "id",
+ id: "1",
+ name: "id",
type: "int",
comment: "",
nullable: true,
@@ -131,8 +131,8 @@ it("diff drop column list", () => {
],
columnList: [
{
- oldName: "id",
- newName: "id",
+ id: "1",
+ name: "id",
type: "int",
comment: "",
nullable: true,
@@ -156,126 +156,3 @@ it("diff drop column list", () => {
expect(result).toStrictEqual(test.wanted);
}
});
-
-it("diff column list", () => {
- const testList: {
- originColumnList: Column[];
- columnList: Column[];
- wanted: {
- addColumnList: AddColumnContext[];
- changeColumnList: ChangeColumnContext[];
- dropColumnList: DropColumnContext[];
- };
- }[] = [
- {
- originColumnList: [
- {
- oldName: "id",
- newName: "id",
- type: "int",
- comment: "",
- nullable: true,
- status: "normal",
- } as any as Column,
- {
- oldName: "name",
- newName: "name",
- type: "varchar",
- comment: "",
- nullable: true,
- status: "normal",
- } as any as Column,
- {
- oldName: "city",
- newName: "city",
- type: "varchar",
- comment: "",
- nullable: true,
- status: "normal",
- } as any as Column,
- ],
- columnList: [
- {
- oldName: "id",
- newName: "id",
- type: "int",
- comment: "this is id",
- nullable: true,
- default: undefined,
- status: "normal",
- } as any as Column,
- {
- oldName: "name",
- newName: "name",
- type: "varchar",
- comment: "",
- nullable: false,
- default: "",
- status: "normal",
- } as Column,
- {
- oldName: "city",
- newName: "city",
- type: "varchar",
- comment: "",
- nullable: true,
- status: "dropped",
- } as any as Column,
- {
- oldName: "birthday",
- newName: "birthday",
- type: "varchar",
- comment: "",
- nullable: false,
- default: "",
- status: "created",
- } as Column,
- ],
- wanted: {
- addColumnList: [
- {
- name: "birthday",
- type: "varchar",
- characterSet: "",
- collation: "",
- comment: "",
- nullable: false,
- default: "",
- },
- ],
- changeColumnList: [
- {
- oldName: "id",
- newName: "id",
- type: "int",
- characterSet: "",
- collation: "",
- comment: "this is id",
- nullable: true,
- default: undefined,
- },
- {
- oldName: "name",
- newName: "name",
- type: "varchar",
- characterSet: "",
- collation: "",
- comment: "",
- nullable: false,
- default: "",
- },
- ],
- dropColumnList: [
- {
- name: "city",
- },
- ],
- },
- },
- ];
-
- for (const test of testList) {
- const result = diffColumnList(test.originColumnList, test.columnList);
- expect(result).toStrictEqual(test.wanted);
- }
-});
diff --git a/frontend/src/utils/schemaEditor/diffColumn.ts b/frontend/src/utils/schemaEditor/diffColumn.ts
index 4db83143bb..fb66b28c2d 100644
--- a/frontend/src/utils/schemaEditor/diffColumn.ts
+++ b/frontend/src/utils/schemaEditor/diffColumn.ts
@@ -30,7 +30,7 @@ export const diffColumnList = (
);
for (const column of changedColumnList) {
const originColumn = originColumnList.find(
- (originColumn) => originColumn.oldName === column.oldName
+ (originColumn) => originColumn.id === column.id
);
if (isUndefined(originColumn)) {
continue;
@@ -47,9 +47,12 @@ export const diffColumnList = (
(column) => column.status === "dropped"
);
for (const column of droppedColumnList) {
- dropColumnContextList.push({
- name: column.oldName,
- });
+ const originColumn = originColumnList.find((item) => item.id === column.id);
+ if (originColumn) {
+ dropColumnContextList.push({
+ name: originColumn.name,
+ });
+ }
}
return {
diff --git a/frontend/src/utils/schemaEditor/diffSchema.ts b/frontend/src/utils/schemaEditor/diffSchema.ts
index 52001fabe3..24393cdb86 100644
--- a/frontend/src/utils/schemaEditor/diffSchema.ts
+++ b/frontend/src/utils/schemaEditor/diffSchema.ts
@@ -1,19 +1,22 @@
-import {
- isEqualForeignKeyList,
- isEqualForeignKey,
-} from "@/components/SchemaEditor/utils/table";
+import { useSchemaEditorStore } from "@/store";
import {
AlterTableContext,
CreateTableContext,
+ DatabaseId,
DropTableContext,
RenameTableContext,
} from "@/types";
-import { Column, ForeignKey, Schema } from "@/types/schemaEditor/atomType";
+import { Schema } from "@/types/schemaEditor/atomType";
import { isEqual, isUndefined } from "lodash-es";
import { diffColumnList } from "./diffColumn";
import { transformTableToCreateTableContext } from "./transform";
-export const diffSchema = (originSchema: Schema, schema: Schema) => {
+export const diffSchema = (
+ databaseId: DatabaseId,
+ originSchema: Schema,
+ schema: Schema
+) => {
+ const editorStore = useSchemaEditorStore();
const createTableContextList: CreateTableContext[] = [];
const createdTableList = schema.tableList.filter(
(table) => table.status === "created"
@@ -22,21 +25,46 @@ export const diffSchema = (originSchema: Schema, schema: Schema) => {
const createTableContext = transformTableToCreateTableContext(table);
const diffColumnListResult = diffColumnList([], table.columnList);
createTableContext.addColumnList = diffColumnListResult.addColumnList;
- createTableContext.primaryKeyList =
- schema.primaryKeyList
- .find((pk) => pk.table === createTableContext.name)
- ?.columnList.map((columnRef) => columnRef.value.newName) ?? [];
+ for (const columnId of table.primaryKey.columnIdList) {
+ const column = table.columnList.find((column) => column.id === columnId);
+ if (column) {
+ createTableContext.primaryKeyList.push(column.name);
+ }
+ }
const foreignKeyList = schema.foreignKeyList.filter(
- (fk) => fk.table === createTableContext.name
+ (fk) => fk.tableId === table.id
);
for (const foreignKey of foreignKeyList) {
- if (foreignKey.columnList.length > 0) {
+ const referencedTable = editorStore.getTable(
+ databaseId,
+ foreignKey.referencedSchema,
+ foreignKey.referencedTableId
+ );
+ if (referencedTable) {
+ const columnNameList: string[] = [];
+ const referencedColumnNameList: string[] = [];
+ for (const columnId of foreignKey.columnIdList) {
+ const column = table.columnList.find(
+ (column) => column.id === columnId
+ );
+ if (column) {
+ columnNameList.push(column.name);
+ }
+ }
+ for (const referencedColumnId of foreignKey.referencedColumnIdList) {
+ const column = referencedTable.columnList.find(
+ (column) => column.id === referencedColumnId
+ );
+ if (column) {
+ referencedColumnNameList.push(column.name);
+ }
+ }
+
createTableContext.addForeignKeyList.push({
- columnList: foreignKey.columnList.map(
- (columnRef) => columnRef.value.newName
- ),
- referencedTable: foreignKey.referencedTable,
- referencedColumnList: foreignKey.referencedColumns,
+ columnList: columnNameList,
+ referencedSchema: foreignKey.referencedSchema,
+ referencedTable: referencedTable.name,
+ referencedColumnList: referencedColumnNameList,
});
}
}
@@ -50,64 +78,155 @@ export const diffSchema = (originSchema: Schema, schema: Schema) => {
);
for (const table of changedTableList) {
const originTable = originSchema.tableList.find(
- (originTable) => originTable.oldName === table.oldName
+ (originTable) => originTable.id === table.id
);
- const originPrimaryKey = originSchema?.primaryKeyList
- .find((pk) => pk.table === table.oldName)
- ?.columnList.map((columnRef) => columnRef.value)
- .sort();
- const primaryKey = schema?.primaryKeyList
- .find((pk) => pk.table === table.newName)
- ?.columnList.filter((column) => column.value.status !== "dropped")
- .map((columnRef) => columnRef.value)
- .sort();
+ if (!originTable) {
+ continue;
+ }
+
+ const originPrimaryKey = originTable.primaryKey;
+ const primaryKey = table.primaryKey;
const originForeignKeyList = originSchema.foreignKeyList.filter(
- (fk) => fk.table === table.oldName
+ (fk) => fk.tableId === table.id
);
const foreignKeyList = schema.foreignKeyList.filter(
- (fk) => fk.table === table.newName
+ (fk) => fk.tableId === table.id
);
if (
!isEqual(originTable, table) ||
!isEqual(originPrimaryKey, primaryKey) ||
- !isEqualForeignKeyList(originForeignKeyList, foreignKeyList)
+ !isEqual(originForeignKeyList, foreignKeyList)
) {
- if (table.newName !== table.oldName) {
+ if (originTable.name !== table.name) {
renameTableContextList.push({
- oldName: table.oldName,
- newName: table.newName,
+ oldName: originTable.name,
+ newName: table.name,
});
}
const columnListDiffResult = diffColumnList(
- originTable?.columnList ?? [],
+ originTable.columnList,
table.columnList
);
if (
!isEqual(originPrimaryKey, primaryKey) ||
- !isEqualForeignKeyList(originForeignKeyList, foreignKeyList) ||
+ !isEqual(originForeignKeyList, foreignKeyList) ||
columnListDiffResult.addColumnList.length > 0 ||
columnListDiffResult.changeColumnList.length > 0 ||
columnListDiffResult.dropColumnList.length > 0
) {
const alterTableContext: AlterTableContext = {
- name: table.newName,
+ name: table.name,
...columnListDiffResult,
dropPrimaryKey: false,
+ dropPrimaryKeyList: [],
dropForeignKeyList: [],
addForeignKeyList: [],
};
- composePrimaryKeyWithAlterTableContext(
- originPrimaryKey || [],
- primaryKey || [],
- alterTableContext
- );
- composeForeignKeyWithAlterTableContext(
- originForeignKeyList || [],
- foreignKeyList || [],
- alterTableContext
- );
+
+ // Compose primary key changes.
+ if (!isEqual(originPrimaryKey, primaryKey)) {
+ const droppedColumnNameList = alterTableContext.dropColumnList.map(
+ (column) => column.name
+ );
+ const filterOriginPrimaryKeyColumnNameList: string[] = [];
+ for (const columnId of originPrimaryKey.columnIdList) {
+ const column = originTable.columnList.find(
+ (column) => column.id === columnId
+ );
+ if (column && !droppedColumnNameList.includes(column.name)) {
+ filterOriginPrimaryKeyColumnNameList.push(column.name);
+ }
+ }
+ const primaryKeyColumnNameList: string[] = [];
+ for (const columnId of primaryKey.columnIdList) {
+ const column = table.columnList.find(
+ (column) => column.id === columnId
+ );
+ if (column) {
+ primaryKeyColumnNameList.push(column.name);
+ }
+ }
+
+ if (
+ !isEqual(
+ filterOriginPrimaryKeyColumnNameList,
+ primaryKeyColumnNameList
+ )
+ ) {
+ if (originPrimaryKey.columnIdList.length !== 0) {
+ alterTableContext.dropPrimaryKey = true;
+ alterTableContext.dropPrimaryKeyList.push(originPrimaryKey.name);
+ }
+ alterTableContext.primaryKeyList = primaryKeyColumnNameList;
+ }
+ }
+
+ // Compose foreign key changes.
+ if (!isEqual(originForeignKeyList, foreignKeyList)) {
+ for (const foreignKey of foreignKeyList) {
+ const originForeignKey = originForeignKeyList.find(
+ (fk) => fk.name === foreignKey.name
+ );
+
+ if (!isEqual(originForeignKey, foreignKey)) {
+ if (!isUndefined(originForeignKey)) {
+ alterTableContext.dropForeignKeyList.push(
+ originForeignKey.name
+ );
+ }
+ if (
+ foreignKey.columnIdList.length > 0 &&
+ foreignKey.columnIdList.length ===
+ foreignKey.referencedColumnIdList.length
+ ) {
+ const referencedSchema = editorStore.getSchema(
+ databaseId,
+ foreignKey.referencedSchema
+ );
+ const referencedTable = referencedSchema?.tableList.find(
+ (table) => table.id === foreignKey.referencedTableId
+ );
+ if (!referencedSchema || !referencedTable) {
+ continue;
+ }
+
+ const columnList: string[] = [];
+ const referencedColumnList: string[] = [];
+ for (const columnId of foreignKey.columnIdList) {
+ const column = table.columnList.find(
+ (column) => column.id === columnId
+ );
+ const columnIndex = foreignKey.columnIdList.findIndex(
+ (item) => item === column?.id
+ );
+ const referencedColumn = referencedTable?.columnList.find(
+ (column) =>
+ column.id ===
+ foreignKey.referencedColumnIdList[columnIndex]
+ );
+ if (
+ column &&
+ referencedColumn &&
+ column.status !== "dropped"
+ ) {
+ columnList.push(column.name);
+ referencedColumnList.push(referencedColumn.name);
+ }
+ }
+
+ alterTableContext.addForeignKeyList.push({
+ columnList,
+ referencedSchema: referencedSchema.name,
+ referencedTable: referencedTable.name,
+ referencedColumnList,
+ });
+ }
+ }
+ }
+ }
+
alterTableContextList.push(alterTableContext);
}
}
@@ -119,7 +238,7 @@ export const diffSchema = (originSchema: Schema, schema: Schema) => {
);
for (const table of droppedTableList) {
dropTableContextList.push({
- name: table.oldName,
+ name: table.name,
});
}
@@ -130,63 +249,3 @@ export const diffSchema = (originSchema: Schema, schema: Schema) => {
dropTableList: dropTableContextList,
};
};
-
-const composePrimaryKeyWithAlterTableContext = (
- originPrimaryKey: Column[],
- primaryKey: Column[],
- alterTableContext: AlterTableContext
-) => {
- const droppedColumnNameList = alterTableContext.dropColumnList.map(
- (column) => column.name
- );
- const filterOriginPrimaryKey = originPrimaryKey.filter(
- (column) => !droppedColumnNameList.includes(column.oldName)
- );
- if (!isEqual(filterOriginPrimaryKey, primaryKey)) {
- if (originPrimaryKey.length !== 0) {
- alterTableContext.dropPrimaryKey = true;
- }
- alterTableContext.primaryKeyList = primaryKey.map(
- (column) => column.newName
- );
- }
-};
-
-const composeForeignKeyWithAlterTableContext = (
- originForeignKeyList: ForeignKey[],
- foreignKeyList: ForeignKey[],
- alterTableContext: AlterTableContext
-) => {
- for (const foreignKey of foreignKeyList) {
- const originForeignKey =
- originForeignKeyList[foreignKeyList.findIndex((fk) => fk === foreignKey)];
-
- if (!isEqualForeignKey(originForeignKey, foreignKey)) {
- if (!isUndefined(originForeignKey)) {
- alterTableContext.dropForeignKeyList.push(originForeignKey.name);
- }
- if (
- foreignKey.columnList.length > 0 &&
- foreignKey.columnList.length === foreignKey.referencedColumns.length
- ) {
- const columnList: string[] = [];
- const referencedColumnList: string[] = [];
- for (const column of foreignKey.columnList) {
- if (column.value.status !== "dropped") {
- columnList.push(column.value.newName);
- referencedColumnList.push(
- foreignKey.referencedColumns[
- foreignKey.columnList.findIndex((item) => item === column)
- ]
- );
- }
- }
- alterTableContext.addForeignKeyList.push({
- referencedTable: foreignKey.referencedTable,
- columnList,
- referencedColumnList,
- });
- }
- }
- }
-};
diff --git a/frontend/src/utils/schemaEditor/transform.ts b/frontend/src/utils/schemaEditor/transform.ts
index c408837ff5..7922dcccdf 100644
--- a/frontend/src/utils/schemaEditor/transform.ts
+++ b/frontend/src/utils/schemaEditor/transform.ts
@@ -10,7 +10,7 @@ export const transformColumnToAddColumnContext = (
column: Column
): AddColumnContext => {
return {
- name: defaultTo(column.newName, ""),
+ name: defaultTo(column.name, ""),
type: defaultTo(column.type, ""),
comment: defaultTo(column.comment, ""),
nullable: defaultTo(column.nullable, false),
@@ -25,8 +25,8 @@ export const transformColumnToChangeColumnContext = (
column: Column
): ChangeColumnContext => {
return {
- oldName: defaultTo(originColumn.oldName, ""),
- newName: defaultTo(column.newName, ""),
+ oldName: defaultTo(originColumn.name, ""),
+ newName: defaultTo(column.name, ""),
type: defaultTo(column.type, ""),
comment: defaultTo(column.comment, ""),
nullable: defaultTo(column.nullable, false),
@@ -40,7 +40,7 @@ export const transformTableToCreateTableContext = (
table: Table
): CreateTableContext => {
return {
- name: defaultTo(table.newName, ""),
+ name: defaultTo(table.name, ""),
engine: defaultTo(table.engine, ""),
collation: defaultTo(table.collation, ""),
comment: defaultTo(table.comment, ""),