Skip to content

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

refactor: update schema editor v1 state (#8185)

boojack16faf9f6399f
diff --git a/frontend/src/components/SchemaDesigner/CreateSchemaDesignPanel.vue b/frontend/src/components/SchemaDesigner/CreateSchemaDesignPanel.vue
index 679387f06f..cd4c8f4eee 100644
--- a/frontend/src/components/SchemaDesigner/CreateSchemaDesignPanel.vue
+++ b/frontend/src/components/SchemaDesigner/CreateSchemaDesignPanel.vue
@@ -47,15 +47,15 @@
:baseline-schema="state.baselineSchema"
@update="handleBaselineSchemaChange"
/>
- <SchemaDesigner
- ref="schemaDesignerRef"
+ <SchemaEditorV1
+ ref="schemaEditorRef"
:key="refreshId"
class="!mt-6"
:readonly="true"
:engine="state.schemaDesign.engine"
- :baseline-schema-metadata="state.schemaDesign.baselineSchemaMetadata"
- :schema-metadata="state.schemaDesign.schemaMetadata"
:project="project"
+ :resource-type="'branch'"
+ :branches="[state.schemaDesign]"
/>
</div>
@@ -86,11 +86,12 @@ import { cloneDeep, uniqueId } from "lodash-es";
import { NButton, NDrawer, NDrawerContent, NDivider } from "naive-ui";
import { computed, reactive, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
-import SchemaDesigner from "@/components/SchemaEditorV1/index.vue";
+import SchemaEditorV1 from "@/components/SchemaEditorV1/index.vue";
import {
pushNotification,
useDatabaseV1Store,
useProjectV1Store,
+ useSchemaEditorV1Store,
useSheetV1Store,
} from "@/store";
import { useSchemaDesignStore } from "@/store/modules/schemaDesign";
@@ -137,7 +138,7 @@ const emit = defineEmits<{
}>();
const { t } = useI18n();
-const schemaDesignerRef = ref<InstanceType<typeof SchemaDesigner>>();
+const schemaEditorRef = ref<InstanceType<typeof SchemaEditorV1>>();
const projectStore = useProjectV1Store();
const databaseStore = useDatabaseV1Store();
const schemaDesignStore = useSchemaDesignStore();
@@ -229,7 +230,7 @@ const handleConfirm = async () => {
return;
}
- const designerState = schemaDesignerRef.value;
+ const designerState = schemaEditorRef.value;
if (!designerState) {
// Should not happen.
throw new Error("schema designer is undefined");
@@ -246,9 +247,16 @@ const handleConfirm = async () => {
const database = useDatabaseV1Store().getDatabaseByUID(
state.baselineSchema.databaseId || ""
);
+ const schemaEditorV1Store = useSchemaEditorV1Store();
+ const branchSchema = schemaEditorV1Store.resourceMap["branch"].get(
+ state.schemaDesign.name
+ );
+ if (!branchSchema) {
+ return;
+ }
const metadata = mergeSchemaEditToMetadata(
- designerState.editableSchemas,
+ branchSchema.schemaList,
cloneDeep(
state.schemaDesign.baselineSchemaMetadata ||
DatabaseMetadata.fromPartial({})
diff --git a/frontend/src/components/SchemaDesigner/EditSchemaDesignPanel.vue b/frontend/src/components/SchemaDesigner/EditSchemaDesignPanel.vue
index 5f9e707cce..53a2ab0ea2 100644
--- a/frontend/src/components/SchemaDesigner/EditSchemaDesignPanel.vue
+++ b/frontend/src/components/SchemaDesigner/EditSchemaDesignPanel.vue
@@ -147,13 +147,14 @@
</template>
</div>
- <SchemaDesigner
- ref="schemaDesignerRef"
+ <SchemaEditorV1
+ :key="schemaEditorKey"
+ ref="schemaEditorRef"
:readonly="!state.isEditing"
:engine="schemaDesign.engine"
- :baseline-schema-metadata="schemaDesign.baselineSchemaMetadata"
- :schema-metadata="schemaDesign.schemaMetadata"
:project="project"
+ :resource-type="'branch'"
+ :branches="[schemaDesign]"
/>
<!-- Don't show delete button in view mode. -->
<div v-if="!viewMode">
@@ -191,7 +192,7 @@
<script lang="ts" setup>
import dayjs from "dayjs";
-import { cloneDeep, isEqual } from "lodash-es";
+import { cloneDeep, isEqual, uniqueId } from "lodash-es";
import { Pen, X, Check } from "lucide-vue-next";
import {
NButton,
@@ -207,12 +208,13 @@ import { computed, reactive, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
import DatabaseInfo from "@/components/DatabaseInfo.vue";
-import SchemaDesigner from "@/components/SchemaEditorV1/index.vue";
+import SchemaEditorV1 from "@/components/SchemaEditorV1/index.vue";
import {
pushNotification,
useChangeHistoryStore,
useCurrentUserV1,
useDatabaseV1Store,
+ useSchemaEditorV1Store,
} from "@/store";
import { useSchemaDesignStore } from "@/store/modules/schemaDesign";
import { DatabaseMetadata } from "@/types/proto/v1/database_service";
@@ -260,7 +262,8 @@ const state = reactive<LocalState>({
showDiffEditor: false,
});
const createdBranchName = ref<string>("");
-const schemaDesignerRef = ref<InstanceType<typeof SchemaDesigner>>();
+const schemaEditorRef = ref<InstanceType<typeof SchemaEditorV1>>();
+const schemaEditorKey = ref<string>(uniqueId());
const schemaDesign = computed(() => {
return schemaDesignStore.getSchemaDesignByName(state.schemaDesignName || "");
@@ -423,8 +426,17 @@ const handleEdit = async () => {
};
const handleCancelEdit = async () => {
+ const schemaEditorV1Store = useSchemaEditorV1Store();
+ const branchSchema = schemaEditorV1Store.resourceMap["branch"].get(
+ schemaDesign.value.name
+ );
+ if (!branchSchema) {
+ return;
+ }
+
+ const editableSchemas = branchSchema.schemaList;
const metadata = mergeSchemaEditToMetadata(
- schemaDesignerRef.value?.editableSchemas || [],
+ editableSchemas,
cloneDeep(
schemaDesign.value.schemaMetadata || DatabaseMetadata.fromPartial({})
)
@@ -464,9 +476,9 @@ const handleCancelEdit = async () => {
}
}
- // If the metadata is changed, we need to rebuild the editing state.
if (!isEqual(metadata, schemaDesign.value.schemaMetadata)) {
- schemaDesignerRef.value?.rebuildEditingState();
+ // If the metadata is changed, we need to rebuild the editing state.
+ schemaEditorKey.value = uniqueId();
}
state.isEditing = false;
};
@@ -476,7 +488,7 @@ const handleSaveSchemaDesignDraft = async () => {
return;
}
- const designerState = schemaDesignerRef.value;
+ const designerState = schemaEditorRef.value;
if (!designerState) {
throw new Error("schema designer is undefined");
}
@@ -496,8 +508,16 @@ const handleSaveSchemaDesignDraft = async () => {
updateMask.push("title");
}
}
+
+ const schemaEditorV1Store = useSchemaEditorV1Store();
+ const branchSchema = schemaEditorV1Store.resourceMap["branch"].get(
+ schemaDesign.value.name
+ );
+ if (!branchSchema) {
+ return;
+ }
const mergedMetadata = mergeSchemaEditToMetadata(
- designerState.editableSchemas,
+ branchSchema.schemaList,
cloneDeep(
schemaDesign.value.baselineSchemaMetadata ||
DatabaseMetadata.fromPartial({})
diff --git a/frontend/src/components/SchemaDesigner/utils.ts b/frontend/src/components/SchemaDesigner/utils.ts
index 926eb4d700..a0649b9b2c 100644
--- a/frontend/src/components/SchemaDesigner/utils.ts
+++ b/frontend/src/components/SchemaDesigner/utils.ts
@@ -1,14 +1,5 @@
import { cloneDeep, isEqual, uniq } from "lodash-es";
import { useCurrentUserV1 } from "@/store";
-import {
- Column,
- ForeignKey,
- Schema,
- Table,
- convertColumnMetadataToColumn,
- convertSchemaMetadataToSchema,
- convertTableMetadataToTable,
-} from "@/types";
import {
ColumnMetadata,
DatabaseMetadata,
@@ -18,6 +9,15 @@ import {
TableMetadata,
} from "@/types/proto/v1/database_service";
import { SchemaDesign } from "@/types/proto/v1/schema_design_service";
+import {
+ Column,
+ ForeignKey,
+ Schema,
+ Table,
+ convertColumnMetadataToColumn,
+ convertSchemaMetadataToSchema,
+ convertTableMetadataToTable,
+} from "@/types/v1/schemaEditor";
import { randomString } from "@/utils";
export const mergeSchemaEditToMetadata = (
@@ -118,79 +118,6 @@ export const mergeSchemaEditToMetadata = (
}
}
}
-
- // Build foreign keys.
- for (const foreignKey of schemaEdit.foreignKeyList) {
- const schema = metadata.schemas.find(
- (schema) => schema.name === schemaEdit.name
- );
- if (!schema) {
- continue;
- }
- const tableEdit = schemaEdit.tableList.find(
- (table) => table.id === foreignKey.tableId
- );
- const table = schema.tables.find(
- (table) => table.name === tableEdit?.name
- );
- if (!tableEdit || !table) {
- continue;
- }
- const referencedSchema = metadata.schemas.find(
- (schema) =>
- schema.name ===
- schemaEdits.find(
- (schemaEdit) => schemaEdit.id === foreignKey.referencedSchemaId
- )?.name
- );
- if (!referencedSchema) {
- continue;
- }
- const referencedTableEdit = schemaEdits
- .find((schemaEdit) => schemaEdit.id === foreignKey.referencedSchemaId)
- ?.tableList.find((table) => table.id === foreignKey.referencedTableId);
- const referencedTable = referencedSchema.tables.find(
- (table) => table.name === referencedTableEdit?.name
- );
- if (!referencedTableEdit || !referencedTable) {
- continue;
- }
-
- const foreignKeyName = foreignKey.name
- ? foreignKey.name
- : `${table.name}-fk-${randomString(8).toLowerCase()}`;
- const fk = ForeignKeyMetadata.fromPartial({
- name: foreignKeyName,
- referencedSchema: referencedSchema.name,
- referencedTable: referencedTable.name,
- });
- if (table.foreignKeys.find((fk) => fk.name === foreignKeyName)) {
- continue;
- }
- if (
- foreignKey.columnIdList.length !==
- foreignKey.referencedColumnIdList.length
- ) {
- continue;
- }
- for (const columnId of foreignKey.columnIdList) {
- const column = tableEdit.columnList.find(
- (column) => column.id === columnId
- );
- if (column) {
- fk.columns.push(column.name);
- }
- }
- for (const columnId of foreignKey.referencedColumnIdList) {
- const column = referencedTableEdit.columnList.find(
- (column) => column.id === columnId
- );
- if (column) {
- fk.referencedColumns.push(column.name);
- }
- }
- table.foreignKeys.push(fk);
- }
}
for (const schema of metadata.schemas) {
const schemaEdit = schemaEdits.find((item) => item.name === schema.name);
@@ -198,6 +125,84 @@ export const mergeSchemaEditToMetadata = (
metadata.schemas = metadata.schemas.filter(
(item) => item.name !== schema.name
);
+ continue;
+ }
+
+ for (const tableEdit of schemaEdit.tableList) {
+ // Build foreign keys.
+ for (const foreignKey of tableEdit.foreignKeyList) {
+ const schema = metadata.schemas.find(
+ (schema) => schema.name === schemaEdit.name
+ );
+ if (!schema) {
+ continue;
+ }
+ const tableEdit = schemaEdit.tableList.find(
+ (table) => table.id === foreignKey.tableId
+ );
+ const table = schema.tables.find(
+ (table) => table.name === tableEdit?.name
+ );
+ if (!tableEdit || !table) {
+ continue;
+ }
+ const referencedSchema = metadata.schemas.find(
+ (schema) =>
+ schema.name ===
+ schemaEdits.find(
+ (schemaEdit) => schemaEdit.id === foreignKey.referencedSchemaId
+ )?.name
+ );
+ if (!referencedSchema) {
+ continue;
+ }
+ const referencedTableEdit = schemaEdits
+ .find((schemaEdit) => schemaEdit.id === foreignKey.referencedSchemaId)
+ ?.tableList.find(
+ (table) => table.id === foreignKey.referencedTableId
+ );
+ const referencedTable = referencedSchema.tables.find(
+ (table) => table.name === referencedTableEdit?.name
+ );
+ if (!referencedTableEdit || !referencedTable) {
+ continue;
+ }
+
+ const foreignKeyName = foreignKey.name
+ ? foreignKey.name
+ : `${table.name}-fk-${randomString(8).toLowerCase()}`;
+ const fk = ForeignKeyMetadata.fromPartial({
+ name: foreignKeyName,
+ referencedSchema: referencedSchema.name,
+ referencedTable: referencedTable.name,
+ });
+ if (table.foreignKeys.find((fk) => fk.name === foreignKeyName)) {
+ continue;
+ }
+ if (
+ foreignKey.columnIdList.length !==
+ foreignKey.referencedColumnIdList.length
+ ) {
+ continue;
+ }
+ for (const columnId of foreignKey.columnIdList) {
+ const column = tableEdit.columnList.find(
+ (column) => column.id === columnId
+ );
+ if (column) {
+ fk.columns.push(column.name);
+ }
+ }
+ for (const columnId of foreignKey.referencedColumnIdList) {
+ const column = referencedTableEdit.columnList.find(
+ (column) => column.id === columnId
+ );
+ if (column) {
+ fk.referencedColumns.push(column.name);
+ }
+ }
+ table.foreignKeys.push(fk);
+ }
}
}
@@ -429,7 +434,6 @@ export const rebuildEditableSchemas = (
continue;
}
- const foreignKeyList: ForeignKey[] = [];
for (const table of schema.tables) {
const editableTable = editableSchema.tableList.find(
(item) => item.name === table.name
@@ -438,6 +442,7 @@ export const rebuildEditableSchemas = (
continue;
}
+ const foreignKeyList: ForeignKey[] = [];
for (const foreignKeyMetadata of table.foreignKeys) {
const referencedSchema = editableSchemas.find(
(schema) => schema.name === foreignKeyMetadata.referencedSchema
@@ -476,8 +481,8 @@ export const rebuildEditableSchemas = (
foreignKeyList.push(fk);
}
+ editableTable.foreignKeyList = foreignKeyList;
}
- editableSchema.foreignKeyList = foreignKeyList;
}
return editableSchemas;
diff --git a/frontend/src/components/SchemaEditor/Panels/TableEditor.vue b/frontend/src/components/SchemaEditor/Panels/TableEditor.vue
index c02085d9eb..0950431723 100644
--- a/frontend/src/components/SchemaEditor/Panels/TableEditor.vue
+++ b/frontend/src/components/SchemaEditor/Panels/TableEditor.vue
@@ -319,8 +319,8 @@ import {
useSettingV1Store,
} from "@/store/modules";
import { TableTabContext } from "@/types";
-import { ColumnMetadata } from "@/types/proto/store/database";
import { Engine } from "@/types/proto/v1/common";
+import { ColumnMetadata } from "@/types/proto/v1/database_service";
import { SchemaTemplateSetting_FieldTemplate } from "@/types/proto/v1/setting_service";
import { DatabaseEdit, SchemaEditorTabType } from "@/types/schemaEditor";
import {
diff --git a/frontend/src/components/SchemaEditorV1/DatabaseTree.vue b/frontend/src/components/SchemaEditorV1/Aside/BranchTree.vue
similarity index 82%
rename from frontend/src/components/SchemaEditorV1/DatabaseTree.vue
rename to frontend/src/components/SchemaEditorV1/Aside/BranchTree.vue
index 4de791d39e..ce7adc3bd6 100644
--- a/frontend/src/components/SchemaEditorV1/DatabaseTree.vue
+++ b/frontend/src/components/SchemaEditorV1/Aside/BranchTree.vue
@@ -53,12 +53,13 @@
<SchemaNameModal
v-if="state.schemaNameModalContext !== undefined"
- :database-id="state.schemaNameModalContext.databaseId"
+ :parent-name="state.schemaNameModalContext.parentName"
@close="state.schemaNameModalContext = undefined"
/>
<TableNameModal
v-if="state.tableNameModalContext !== undefined"
+ :parent-name="state.tableNameModalContext.parentName"
:schema-id="state.tableNameModalContext.schemaId"
:table-id="state.tableNameModalContext.tableId"
@close="state.tableNameModalContext = undefined"
@@ -66,23 +67,23 @@
</template>
<script lang="ts" setup>
-import { escape, isUndefined } from "lodash-es";
+import { escape, head, isUndefined } from "lodash-es";
import { TreeOption, NEllipsis, NInput, NDropdown, NTree } from "naive-ui";
import { v1 as uuidv1 } from "uuid";
-import { computed, watch, ref, h, reactive, nextTick } from "vue";
+import { computed, watch, ref, h, reactive, nextTick, onMounted } from "vue";
import { useI18n } from "vue-i18n";
import DuplicateIcon from "~icons/heroicons-outline/document-duplicate";
import TableIcon from "~icons/heroicons-outline/table-cells";
import SchemaIcon from "~icons/heroicons-outline/view-columns";
import EllipsisIcon from "~icons/heroicons-solid/ellipsis-horizontal";
-import { generateUniqueTabId } from "@/store";
-import { Table } from "@/types";
+import { generateUniqueTabId, useSchemaEditorV1Store } from "@/store";
import { Engine } from "@/types/proto/v1/common";
+import { Table } from "@/types/v1/schemaEditor";
+import { BranchSchema, SchemaEditorTabType } from "@/types/v1/schemaEditor";
import { getHighlightHTMLByKeyWords, isDescendantOf } from "@/utils";
-import SchemaNameModal from "./Modals/SchemaNameModal.vue";
-import TableNameModal from "./Modals/TableNameModal.vue";
-import { useSchemaEditorContext, SchemaEditorTabType } from "./common";
-import { isTableChanged } from "./utils";
+import SchemaNameModal from "../Modals/SchemaNameModal.vue";
+import TableNameModal from "../Modals/TableNameModal.vue";
+import { isTableChanged } from "../utils";
interface BaseTreeNode extends TreeOption {
key: string;
@@ -114,41 +115,49 @@ interface TreeContextMenu {
interface LocalState {
shouldRelocateTreeNode: boolean;
schemaNameModalContext?: {
- databaseId: string;
+ parentName: string;
};
tableNameModalContext?: {
+ parentName: string;
schemaId: string;
tableId?: string;
};
}
const { t } = useI18n();
-const {
- readonly,
- engine,
- editableSchemas,
- tabState,
- addTab,
- getTable,
- getCurrentTab,
- dropTable,
-} = useSchemaEditorContext();
+const schemaEditorV1Store = useSchemaEditorV1Store();
const state = reactive<LocalState>({
shouldRelocateTreeNode: false,
});
-const contextMenu = reactive<TreeContextMenu>({
- showDropdown: false,
- clientX: 0,
- clientY: 0,
- treeNode: undefined,
-});
+const engine = computed(() => schemaEditorV1Store.engine);
+const readonly = computed(() => schemaEditorV1Store.readonly);
+const currentTab = computed(() => schemaEditorV1Store.currentTab);
+
const treeRef = ref<InstanceType<typeof NTree>>();
const searchPattern = ref("");
const expandedKeysRef = ref<string[]>([]);
const selectedKeysRef = ref<string[]>([]);
// Trigger re-render when the tree data is changed.
const treeKeyRef = ref<string>("");
+const contextMenu = reactive<TreeContextMenu>({
+ showDropdown: false,
+ clientX: 0,
+ clientY: 0,
+ treeNode: undefined,
+});
+
+// NOTE: we only support editing one branch for now.
+const branchSchema = computed(
+ () =>
+ head(
+ Array.from(schemaEditorV1Store.resourceMap.branch.values())
+ ) as BranchSchema
+);
+const schemaList = computed(() => branchSchema.value.schemaList);
+const tableList = computed(() =>
+ schemaList.value.map((schema) => schema.tableList).flat()
+);
const treeData = computed(() => {
const treeNodeList: TreeNode[] = [];
if (engine.value === Engine.MYSQL) {
@@ -193,10 +202,6 @@ const treeData = computed(() => {
return treeNodeList;
});
-const schemaList = computed(() => editableSchemas.value);
-const tableList = computed(() =>
- schemaList.value.map((schema) => schema.tableList).flat()
-);
const contextMenuOptions = computed(() => {
const treeNode = contextMenu.treeNode;
if (isUndefined(treeNode)) {
@@ -264,6 +269,12 @@ const contextMenuOptions = computed(() => {
return [];
});
+onMounted(() => {
+ if (!branchSchema.value) {
+ throw new Error("branch should not be empty");
+ }
+});
+
watch(
() => treeData.value,
() => {
@@ -276,20 +287,19 @@ watch(
);
watch(
- () => tabState.value.currentTabId,
+ () => currentTab.value,
() => {
- const currentTab = getCurrentTab();
- if (!currentTab) {
+ if (!currentTab.value) {
selectedKeysRef.value = [];
return;
}
- if (currentTab.type === SchemaEditorTabType.TabForTable) {
- const schemaTreeNodeKey = `s-${currentTab.schemaId}`;
+ if (currentTab.value.type === SchemaEditorTabType.TabForTable) {
+ const schemaTreeNodeKey = `s-${currentTab.value.schemaId}`;
if (!expandedKeysRef.value.includes(schemaTreeNodeKey)) {
expandedKeysRef.value.push(schemaTreeNodeKey);
}
- const tableTreeNodeKey = `t-${currentTab.schemaId}-${currentTab.tableId}`;
+ const tableTreeNodeKey = `t-${currentTab.value.schemaId}-${currentTab.value.tableId}`;
selectedKeysRef.value = [tableTreeNodeKey];
}
@@ -304,7 +314,8 @@ watch(
);
// Render prefix icons before label text.
-const renderPrefix = ({ option: treeNode }: { option: TreeNode }) => {
+const renderPrefix = ({ option }: { option: TreeOption }) => {
+ const treeNode = option as TreeNode;
if (treeNode.type === "schema") {
return h(SchemaIcon, {
class: "w-4 h-auto text-gray-400",
@@ -318,7 +329,8 @@ const renderPrefix = ({ option: treeNode }: { option: TreeNode }) => {
};
// Render label text.
-const renderLabel = ({ option: treeNode }: { option: TreeNode }) => {
+const renderLabel = ({ option }: { option: TreeOption }) => {
+ const treeNode = option as TreeNode;
const additionalClassList: string[] = ["select-none"];
if (treeNode.type === "schema") {
@@ -333,7 +345,13 @@ const renderLabel = ({ option: treeNode }: { option: TreeNode }) => {
additionalClassList.push("text-green-700");
} else if (table.status === "dropped") {
additionalClassList.push("text-red-700 line-through");
- } else if (isTableChanged(treeNode.schemaId, treeNode.tableId)) {
+ } else if (
+ isTableChanged(
+ branchSchema.value.branch.name,
+ treeNode.schemaId,
+ treeNode.tableId
+ )
+ ) {
additionalClassList.push("text-yellow-700");
}
}
@@ -356,11 +374,12 @@ const renderLabel = ({ option: treeNode }: { option: TreeNode }) => {
};
// Render a 'menu' icon in the right of the node
-const renderSuffix = ({ option: treeNode }: { option: TreeNode }) => {
+const renderSuffix = ({ option }: { option: TreeOption }) => {
if (readonly.value) {
return null;
}
+ const treeNode = option as TreeNode;
const menuIcon = h(EllipsisIcon, {
class: "w-4 h-auto text-gray-600",
onClick: (e) => {
@@ -432,8 +451,7 @@ const renderSuffix = ({ option: treeNode }: { option: TreeNode }) => {
}
schema.tableList.push(newTable);
-
- openTabForTable(treeNode, newTable.id);
+ openTabForTable(treeNode);
},
});
if (treeNode.type === "schema") {
@@ -447,7 +465,7 @@ const renderSuffix = ({ option: treeNode }: { option: TreeNode }) => {
}
return icons;
}
- return null;
+ throw new Error(`Unknown tree node type`);
};
const getOriginalName = (name: string): string => {
@@ -487,21 +505,23 @@ const handleShowDropdown = (e: MouseEvent, treeNode: TreeNode) => {
const handleCreateTable = () => {
if (engine.value === Engine.MYSQL) {
- const schema = editableSchemas.value[0];
+ const schema = schemaList.value[0];
state.tableNameModalContext = {
+ parentName: branchSchema.value.branch.name,
schemaId: schema.id,
};
}
};
// Set event handler to tree nodes.
-const nodeProps = ({ option: treeNode }: { option: TreeNode }) => {
+const nodeProps = ({ option }: { option: TreeOption }) => {
+ const treeNode = option as TreeNode;
return {
onClick(e: MouseEvent) {
// Check if clicked on the content part.
// And ignore the fold/unfold arrow.
if (isDescendantOf(e.target as Element, ".n-tree-node-content")) {
- openTabForTable(treeNode, treeNode.tableId as string);
+ openTabForTable(treeNode);
} else {
nextTick(() => {
selectedKeysRef.value = [];
@@ -514,15 +534,16 @@ const nodeProps = ({ option: treeNode }: { option: TreeNode }) => {
};
};
-const openTabForTable = (treeNode: TreeNode, tableId: string) => {
+const openTabForTable = (treeNode: TreeNode) => {
state.shouldRelocateTreeNode = false;
if (treeNode.type === "table") {
- addTab({
+ schemaEditorV1Store.addTab({
id: generateUniqueTabId(),
type: SchemaEditorTabType.TabForTable,
+ parentName: branchSchema.value.branch.name,
schemaId: treeNode.schemaId,
- tableId: tableId,
+ tableId: treeNode.tableId,
});
}
@@ -538,19 +559,29 @@ const handleContextMenuDropdownSelect = async (key: string) => {
if (treeNode.type === "schema") {
if (key === "create-table") {
state.tableNameModalContext = {
+ parentName: branchSchema.value.branch.name,
schemaId: treeNode.schemaId,
};
}
} else if (treeNode.type === "table") {
if (key === "rename") {
state.tableNameModalContext = {
+ parentName: branchSchema.value.branch.name,
schemaId: treeNode.schemaId,
tableId: treeNode.tableId,
};
} else if (key === "drop") {
- dropTable(treeNode.schemaId, treeNode.tableId);
+ schemaEditorV1Store.dropTable(
+ branchSchema.value.branch.name,
+ treeNode.schemaId,
+ treeNode.tableId
+ );
} else if (key === "restore") {
- const table = getTable(treeNode.schemaId, treeNode.tableId);
+ const table = schemaEditorV1Store.getTable(
+ branchSchema.value.branch.name,
+ treeNode.schemaId,
+ treeNode.tableId
+ );
if (!table) {
return;
}
diff --git a/frontend/src/components/SchemaEditorV1/Aside/index.vue b/frontend/src/components/SchemaEditorV1/Aside/index.vue
new file mode 100644
index 0000000000..09f7fcf4fe
--- /dev/null
+++ b/frontend/src/components/SchemaEditorV1/Aside/index.vue
@@ -0,0 +1,10 @@
+<template>
+ <BranchTree v-if="schemaEditorV1Store.resourceType === 'branch'" />
+</template>
+
+<script lang="ts" setup>
+import { useSchemaEditorV1Store } from "@/store";
+import BranchTree from "./BranchTree.vue";
+
+const schemaEditorV1Store = useSchemaEditorV1Store();
+</script>
diff --git a/frontend/src/components/SchemaEditorV1/AsidePanel.vue b/frontend/src/components/SchemaEditorV1/AsidePanel.vue
deleted file mode 100644
index 3bba344d8a..0000000000
--- a/frontend/src/components/SchemaEditorV1/AsidePanel.vue
+++ /dev/null
@@ -1,7 +0,0 @@
-<template>
- <DatabaseTree />
-</template>
-
-<script lang="ts" setup>
-import DatabaseTree from "./DatabaseTree.vue";
-</script>
diff --git a/frontend/src/components/SchemaEditorV1/Designer.vue b/frontend/src/components/SchemaEditorV1/Editor.vue
similarity index 68%
rename from frontend/src/components/SchemaEditorV1/Designer.vue
rename to frontend/src/components/SchemaEditorV1/Editor.vue
index 89d168ca9d..4d7859973f 100644
--- a/frontend/src/components/SchemaEditorV1/Designer.vue
+++ b/frontend/src/components/SchemaEditorV1/Editor.vue
@@ -14,10 +14,11 @@
<script lang="ts" setup>
import { computed } from "vue";
+import { useSchemaEditorV1Store } from "@/store/modules/v1/schemaEditor";
+import { SchemaEditorTabType } from "@/types/v1/schemaEditor";
import TableEditor from "./Panels/TableEditor.vue";
import TabsContainer from "./TabsContainer.vue";
-import { SchemaEditorTabType, useSchemaEditorContext } from "./common";
-const { getCurrentTab } = useSchemaEditorContext();
-const currentTab = computed(() => getCurrentTab());
+const schemaEditorV1Store = useSchemaEditorV1Store();
+const currentTab = computed(() => schemaEditorV1Store.currentTab);
</script>
diff --git a/frontend/src/components/SchemaEditorV1/Modals/EditColumnForeignKeyModal.vue b/frontend/src/components/SchemaEditorV1/Modals/EditColumnForeignKeyModal.vue
index ee5eafbd74..d6f5513552 100644
--- a/frontend/src/components/SchemaEditorV1/Modals/EditColumnForeignKeyModal.vue
+++ b/frontend/src/components/SchemaEditorV1/Modals/EditColumnForeignKeyModal.vue
@@ -8,7 +8,7 @@
<p class="mb-2">{{ $t("schema-editor.select-reference-schema") }}</p>
<BBSelect
:selected-item="selectedSchema"
- :item-list="editableSchemas"
+ :item-list="schemas"
:placeholder="$t('schema-editor.schema.select')"
:show-prefix-item="true"
@select-item="(schema) => (state.referencedSchemaId = schema.id)"
@@ -75,9 +75,9 @@
import { isUndefined } from "lodash-es";
import { computed, onMounted, reactive, watch } from "vue";
import { BBModal, BBSelect } from "@/bbkit";
+import { useSchemaEditorV1Store } from "@/store";
import { Column, ForeignKey } from "@/types";
import { Engine } from "@/types/proto/v1/common";
-import { useSchemaEditorContext } from "../common";
interface LocalState {
referencedSchemaId?: string;
@@ -85,32 +85,36 @@ interface LocalState {
referencedColumnId?: string;
}
-const props = defineProps({
- schemaId: {
- type: String,
- default: "",
- },
- tableId: {
- type: String,
- default: "",
- },
- columnId: {
- type: String,
- default: "",
- },
-});
+const props = defineProps<{
+ parentName: string;
+ schemaId: string;
+ tableId: string;
+ columnId: string;
+}>();
const emit = defineEmits<{
(event: "close"): void;
}>();
-const { engine, editableSchemas } = useSchemaEditorContext();
+const schemaEditorV1Store = useSchemaEditorV1Store();
const state = reactive<LocalState>({
referencedSchemaId: props.schemaId,
});
+const engine = computed(() => schemaEditorV1Store.engine);
+
+const parentResource = computed(() => {
+ return schemaEditorV1Store.resourceMap[schemaEditorV1Store.resourceType].get(
+ props.parentName
+ );
+});
+
+const schemas = computed(() => {
+ return parentResource.value?.schemaList || [];
+});
+
const schema = computed(() => {
- return editableSchemas.value.find((item) => item.id === props.schemaId);
+ return schemaEditorV1Store.getSchema(props.parentName, props.schemaId);
});
const table = computed(() => {
@@ -122,7 +126,7 @@ const propsColumn = computed(() => {
});
const foreignKeyList = computed(() => {
- return schema.value?.foreignKeyList || [];
+ return table.value?.foreignKeyList || [];
});
const shouldShowSchemaSelector = computed(() => {
@@ -130,7 +134,7 @@ const shouldShowSchemaSelector = computed(() => {
});
const selectedSchema = computed(() => {
- return editableSchemas.value.find(
+ return parentResource.value?.schemaList.find(
(schema) => schema.id === state.referencedSchemaId
);
});
@@ -232,7 +236,7 @@ const handleConfirmButtonClick = async () => {
referencedTableId: state.referencedTableId,
referencedColumnIdList: [state.referencedColumnId],
};
- schema.value?.foreignKeyList.push(fk);
+ table.value?.foreignKeyList.push(fk);
} else {
const index = foreignKey.value.columnIdList.findIndex(
(id) => id === column.id
diff --git a/frontend/src/components/SchemaEditorV1/Modals/SchemaNameModal.vue b/frontend/src/components/SchemaEditorV1/Modals/SchemaNameModal.vue
index cfee8bb33a..270e58eedd 100644
--- a/frontend/src/components/SchemaEditorV1/Modals/SchemaNameModal.vue
+++ b/frontend/src/components/SchemaEditorV1/Modals/SchemaNameModal.vue
@@ -28,9 +28,9 @@
<script lang="ts" setup>
import { reactive } from "vue";
import { useI18n } from "vue-i18n";
-import { useNotificationStore } from "@/store";
-import { SchemaMetadata } from "@/types/proto/store/database";
-import { useSchemaEditorContext } from "../common";
+import { useNotificationStore, useSchemaEditorV1Store } from "@/store";
+import { SchemaMetadata } from "@/types/proto/v1/database_service";
+import { convertSchemaMetadataToSchema } from "@/types/v1/schemaEditor";
const schemaNameFieldRegexp = /^\S+$/;
@@ -38,12 +38,16 @@ interface LocalState {
schemaName: string;
}
+const props = defineProps<{
+ parentName: string;
+}>();
+
const emit = defineEmits<{
(event: "close"): void;
}>();
const { t } = useI18n();
-const { metadata } = useSchemaEditorContext();
+const schemaEditorV1Store = useSchemaEditorV1Store();
const notificationStore = useNotificationStore();
const state = reactive<LocalState>({
schemaName: "",
@@ -63,8 +67,22 @@ const handleConfirmButtonClick = async () => {
return;
}
- const schema = SchemaMetadata.fromPartial({});
- metadata.value.schemas.push(schema);
+ const parentResource = schemaEditorV1Store.resourceMap[
+ schemaEditorV1Store.resourceType
+ ].get(props.parentName);
+ if (!parentResource) {
+ throw new Error(
+ `Failed to find parent resource ${props.parentName} of type ${schemaEditorV1Store.resourceType}`
+ );
+ }
+
+ const schema = convertSchemaMetadataToSchema(
+ SchemaMetadata.fromPartial({
+ name: state.schemaName,
+ })
+ );
+ schema.status = "created";
+ parentResource.schemaList.push(schema);
// TODO(steven): Open the schema tab.
dismissModal();
};
diff --git a/frontend/src/components/SchemaEditorV1/Modals/TableNameModal.vue b/frontend/src/components/SchemaEditorV1/Modals/TableNameModal.vue
index 08276b02e5..20fcf93edf 100644
--- a/frontend/src/components/SchemaEditorV1/Modals/TableNameModal.vue
+++ b/frontend/src/components/SchemaEditorV1/Modals/TableNameModal.vue
@@ -32,14 +32,21 @@
<script lang="ts" setup>
import { computed, reactive } from "vue";
import { useI18n } from "vue-i18n";
-import { generateUniqueTabId, useNotificationStore } from "@/store";
import {
+ generateUniqueTabId,
+ useNotificationStore,
+ useSchemaEditorV1Store,
+} from "@/store";
+import { Engine } from "@/types/proto/v1/common";
+import {
+ ColumnMetadata,
+ TableMetadata,
+} from "@/types/proto/v1/database_service";
+import {
+ SchemaEditorTabType,
convertColumnMetadataToColumn,
convertTableMetadataToTable,
-} from "@/types";
-import { ColumnMetadata, TableMetadata } from "@/types/proto/store/database";
-import { Engine } from "@/types/proto/v1/common";
-import { SchemaEditorTabType, useSchemaEditorContext } from "../common";
+} from "@/types/v1/schemaEditor";
const tableNameFieldRegexp = /^\S+$/;
@@ -48,6 +55,7 @@ interface LocalState {
}
const props = defineProps<{
+ parentName: string;
schemaId: string;
tableId?: string;
}>();
@@ -57,12 +65,14 @@ const emit = defineEmits<{
}>();
const { t } = useI18n();
-const { engine, editableSchemas, addTab } = useSchemaEditorContext();
+const schemaEditorV1Store = useSchemaEditorV1Store();
const notificationStore = useNotificationStore();
const state = reactive<LocalState>({
tableName: "",
});
+const engine = computed(() => schemaEditorV1Store.engine);
+
const isCreatingTable = computed(() => {
return props.tableId === undefined;
});
@@ -81,8 +91,9 @@ const handleConfirmButtonClick = async () => {
return;
}
- const schema = editableSchemas.value.find(
- (schema) => schema.id === props.schemaId
+ const schema = schemaEditorV1Store.getSchema(
+ props.parentName,
+ props.schemaId
);
if (!schema) {
notificationStore.pushNotification({
@@ -120,9 +131,10 @@ const handleConfirmButtonClick = async () => {
tableEdit.columnList.push(columnEdit);
tableEdit.primaryKey.columnIdList.push(columnEdit.id);
schema.tableList.push(tableEdit);
- addTab({
+ schemaEditorV1Store.addTab({
id: generateUniqueTabId(),
type: SchemaEditorTabType.TabForTable,
+ parentName: props.parentName,
schemaId: props.schemaId,
tableId: tableEdit.id,
});
diff --git a/frontend/src/components/SchemaEditorV1/Panels/TableEditor.vue b/frontend/src/components/SchemaEditorV1/Panels/TableEditor.vue
index f9fcf286a7..234a97ce31 100644
--- a/frontend/src/components/SchemaEditorV1/Panels/TableEditor.vue
+++ b/frontend/src/components/SchemaEditorV1/Panels/TableEditor.vue
@@ -241,6 +241,7 @@
<EditColumnForeignKeyModal
v-if="state.showEditColumnForeignKeyModal && editForeignKeyColumn"
+ :parent-name="currentTab.parentName"
:schema-id="schema.id"
:table-id="table.id"
:column-id="editForeignKeyColumn.id"
@@ -288,9 +289,10 @@ import {
hasFeature,
generateUniqueTabId,
useSettingV1Store,
+ useSchemaEditorV1Store,
} from "@/store/modules";
-import { ColumnMetadata } from "@/types/proto/store/database";
import { Engine } from "@/types/proto/v1/common";
+import { ColumnMetadata } from "@/types/proto/v1/database_service";
import { SchemaTemplateSetting_FieldTemplate } from "@/types/proto/v1/setting_service";
import {
Column,
@@ -298,20 +300,15 @@ import {
Schema,
convertColumnMetadataToColumn,
ForeignKey,
-} from "@/types/schemaEditor/atomType";
+ SchemaEditorTabType,
+} from "@/types/v1/schemaEditor";
+import { TableTabContext } from "@/types/v1/schemaEditor";
import { getDataTypeSuggestionList } from "@/utils";
import FieldTemplates from "@/views/SchemaTemplate/FieldTemplates.vue";
import EditColumnForeignKeyModal from "../Modals/EditColumnForeignKeyModal.vue";
-import {
- SchemaEditorTabType,
- TableTabContext,
- useSchemaEditorContext,
-} from "../common";
import { isColumnChanged } from "../utils";
interface LocalState {
- isFetchingDDL: boolean;
- statement: string;
showEditColumnForeignKeyModal: boolean;
showSchemaTemplateDrawer: boolean;
showFeatureModal: boolean;
@@ -319,13 +316,20 @@ interface LocalState {
}
const { t } = useI18n();
-const { readonly, engine, project, editableSchemas, getCurrentTab, addTab } =
- useSchemaEditorContext();
+const schemaEditorV1Store = useSchemaEditorV1Store();
const settingStore = useSettingV1Store();
-const currentTab = computed(() => getCurrentTab() as TableTabContext);
+const currentTab = computed(
+ () => schemaEditorV1Store.currentTab as TableTabContext
+);
+const parentResouce = computed(() => {
+ return schemaEditorV1Store.resourceMap[schemaEditorV1Store.resourceType].get(
+ currentTab.value.parentName
+ )!;
+});
+const engine = computed(() => schemaEditorV1Store.engine);
+const readonly = computed(() => schemaEditorV1Store.readonly);
+const project = computed(() => schemaEditorV1Store.project);
const state = reactive<LocalState>({
- isFetchingDDL: false,
- statement: "",
showEditColumnForeignKeyModal: false,
showSchemaTemplateDrawer: false,
showFeatureModal: false,
@@ -333,8 +337,9 @@ const state = reactive<LocalState>({
});
const schema = computed(() => {
- return editableSchemas.value.find(
- (schema) => schema.id === currentTab.value.schemaId
+ return schemaEditorV1Store.getSchema(
+ currentTab.value.parentName,
+ currentTab.value.schemaId
) as Schema;
});
const table = computed(
@@ -344,7 +349,7 @@ const table = computed(
) as Table
);
const foreignKeyList = computed(() => {
- return schema.value.foreignKeyList.filter(
+ return table.value.foreignKeyList.filter(
(pk) => pk.tableId === currentTab.value.tableId
) as ForeignKey[];
});
@@ -442,6 +447,7 @@ const getColumnItemComputedClassList = (column: Column) => {
return ["text-green-700", "!bg-green-50"];
} else if (
isColumnChanged(
+ currentTab.value.parentName,
currentTab.value.schemaId,
currentTab.value.tableId,
column.id
@@ -489,7 +495,7 @@ const getReferencedForeignKeyName = (column: Column) => {
if (isUndefined(fk) || isUndefined(index) || index < 0) {
return;
}
- const referencedSchema = editableSchemas.value.find(
+ const referencedSchema = parentResouce.value.schemaList.find(
(schema) => schema.id === fk.referencedSchemaId
);
const referencedTable = referencedSchema?.tableList.find(
@@ -592,7 +598,7 @@ const gotoForeignKeyReferencedTable = (column: Column) => {
return;
}
- const referencedSchema = editableSchemas.value.find(
+ const referencedSchema = parentResouce.value.schemaList.find(
(schema) => schema.id === fk.referencedSchemaId
);
const referencedTable = referencedSchema?.tableList.find(
@@ -608,9 +614,10 @@ const gotoForeignKeyReferencedTable = (column: Column) => {
return;
}
- addTab({
+ schemaEditorV1Store.addTab({
id: generateUniqueTabId(),
type: SchemaEditorTabType.TabForTable,
+ parentName: currentTab.value.parentName,
schemaId: referencedSchema.id,
tableId: referencedTable.id,
});
@@ -642,7 +649,7 @@ const handleDropColumn = (column: Column) => {
(columnId) => columnId !== column.id
);
- const foreignKeyList = schema.value.foreignKeyList.filter(
+ const foreignKeyList = table.value.foreignKeyList.filter(
(fk) => fk.tableId === currentTab.value.tableId
);
for (const foreignKey of foreignKeyList) {
@@ -663,7 +670,6 @@ const handleRestoreColumn = (column: Column) => {
if (column.status === "created") {
return;
}
-
column.status = "normal";
};
diff --git a/frontend/src/components/SchemaEditorV1/TabsContainer.vue b/frontend/src/components/SchemaEditorV1/TabsContainer.vue
index c49aec45a9..074fecf1fa 100644
--- a/frontend/src/components/SchemaEditorV1/TabsContainer.vue
+++ b/frontend/src/components/SchemaEditorV1/TabsContainer.vue
@@ -43,24 +43,20 @@
</template>
<script lang="ts" setup>
-import { head } from "lodash-es";
import { NEllipsis } from "naive-ui";
import scrollIntoView from "scroll-into-view-if-needed";
import { computed, nextTick, ref, watch } from "vue";
-import {
- useSchemaEditorContext,
- TabContext,
- SchemaEditorTabType,
-} from "./common";
+import { useSchemaEditorV1Store } from "@/store";
+import { SchemaEditorTabType, TabContext } from "@/types/v1/schemaEditor";
import { isTableChanged } from "./utils";
-const { tabState, getCurrentTab, getTable } = useSchemaEditorContext();
+const schemaEditorV1Store = useSchemaEditorV1Store();
const tabsContainerRef = ref();
const tabList = computed(() => {
- return Array.from(tabState.value.tabMap.values());
+ return schemaEditorV1Store.tabList;
});
const currentTab = computed(() => {
- return getCurrentTab();
+ return schemaEditorV1Store.currentTab;
});
watch(
@@ -81,7 +77,7 @@ watch(
const getTabComputedClassList = (tab: TabContext) => {
if (tab.type === SchemaEditorTabType.TabForTable) {
- const table = getTable(tab.schemaId, tab.tableId);
+ const table = schemaEditorV1Store.getTableWithTableTab(tab);
if (!table) {
return [];
}
@@ -92,7 +88,7 @@ const getTabComputedClassList = (tab: TabContext) => {
if (table.status === "created") {
return ["text-green-700"];
}
- if (isTableChanged(tab.schemaId, tab.tableId)) {
+ if (isTableChanged(tab.parentName, tab.schemaId, tab.tableId)) {
return ["text-yellow-700"];
}
}
@@ -101,8 +97,8 @@ const getTabComputedClassList = (tab: TabContext) => {
const getTabName = (tab: TabContext) => {
if (tab.type === SchemaEditorTabType.TabForTable) {
- const table = getTable(tab.schemaId, tab.tableId);
- return table.name || "Uknown tab";
+ const table = schemaEditorV1Store.getTableWithTableTab(tab);
+ return table?.name || "Uknown tab";
} else {
// Should never reach here.
return "unknown structure";
@@ -110,17 +106,10 @@ const getTabName = (tab: TabContext) => {
};
const handleSelectTab = (tab: TabContext) => {
- tabState.value.currentTabId = tab.id;
+ schemaEditorV1Store.setCurrentTab(tab.id);
};
const handleCloseTab = (tab: TabContext) => {
- tabState.value.tabMap.delete(tab.id);
- if (tab.id === currentTab.value?.id) {
- tabState.value.currentTabId = undefined;
- } else {
- tabState.value.currentTabId = head(
- Array.from(tabState.value.tabMap.values())
- )?.id;
- }
+ schemaEditorV1Store.closeTab(tab.id);
};
</script>
diff --git a/frontend/src/components/SchemaEditorV1/common/context.ts b/frontend/src/components/SchemaEditorV1/common/context.ts
deleted file mode 100644
index c370a300ce..0000000000
--- a/frontend/src/components/SchemaEditorV1/common/context.ts
+++ /dev/null
@@ -1,120 +0,0 @@
-import { head, isUndefined } from "lodash-es";
-import { inject, InjectionKey, provide } from "vue";
-import { SchemaEditorContext, SchemaEditorTabType, TabContext } from "./type";
-
-export const KEY = Symbol(
- "bb.schema-editor"
-) as InjectionKey<SchemaEditorContext>;
-
-export const useSchemaEditorContext = () => {
- return inject(KEY)!;
-};
-
-export const provideSchemaEditorContext = (
- context: Pick<
- SchemaEditorContext,
- | "readonly"
- | "engine"
- | "baselineMetadata"
- | "metadata"
- | "tabState"
- | "originalSchemas"
- | "editableSchemas"
- | "project"
- >
-) => {
- const { editableSchemas, tabState } = context;
-
- provide(KEY, {
- ...context,
-
- // Tab related functions.
- getCurrentTab: (): TabContext | undefined => {
- if (isUndefined(tabState.value.currentTabId)) {
- return undefined;
- }
- return tabState.value.tabMap.get(tabState.value.currentTabId);
- },
- addTab(tab: TabContext, setAsCurrentTab = true) {
- const tabCache = Array.from(tabState.value.tabMap.values()).find(
- (item) => {
- if (item.type !== tab.type) {
- return false;
- }
-
- if (
- item.type === SchemaEditorTabType.TabForTable &&
- item.schemaId === tab.schemaId &&
- item.tableId === tab.tableId
- ) {
- return true;
- }
- return false;
- }
- );
-
- if (tabCache !== undefined) {
- tab = {
- ...tabCache,
- ...tab,
- id: tabCache.id,
- };
- }
- tabState.value.tabMap.set(tab.id, tab);
-
- if (setAsCurrentTab) {
- tabState.value.currentTabId = tab.id;
- }
- },
-
- // Table related functions.
- getTable: (schemaId: string, tableId: string) => {
- const schema = editableSchemas.value.find((item) => item.id === schemaId);
- if (schema === undefined) {
- throw new Error(`Schema ${schemaId} not found.`);
- }
-
- const tableItem = schema.tableList.find((item) => item.id === tableId);
- if (tableItem === undefined) {
- throw new Error(`Table ${tableId} not found.`);
- }
-
- return tableItem;
- },
- dropTable: (schemaId: string, tableId: string) => {
- const tabList = Array.from(tabState.value.tabMap.values());
- for (const tab of tabList) {
- if (
- tab.type === SchemaEditorTabType.TabForTable &&
- tab.schemaId === schemaId &&
- tab.tableId === tableId
- ) {
- tabState.value.tabMap.delete(tab.id);
- if (tabState.value.currentTabId === tab.id) {
- tabState.value.currentTabId = head(
- Array.from(tabState.value.tabMap.values())
- )?.id;
- }
- }
- }
-
- const schemaItem = editableSchemas.value.find(
- (item) => item.id === schemaId
- );
- if (schemaItem === undefined) {
- return;
- }
- const table = schemaItem.tableList.find((item) => item.id === tableId);
- if (table === undefined) {
- return;
- }
- if (table.status === "created") {
- schemaItem.tableList = schemaItem.tableList.filter(
- (item) => item.id !== tableId
- );
- } else {
- table.status = "dropped";
- }
- },
- });
-};
diff --git a/frontend/src/components/SchemaEditorV1/common/index.ts b/frontend/src/components/SchemaEditorV1/common/index.ts
deleted file mode 100644
index 003f7d11bf..0000000000
--- a/frontend/src/components/SchemaEditorV1/common/index.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-export * from "./context";
-export * from "./type";
diff --git a/frontend/src/components/SchemaEditorV1/common/type.ts b/frontend/src/components/SchemaEditorV1/common/type.ts
deleted file mode 100644
index e4706663b3..0000000000
--- a/frontend/src/components/SchemaEditorV1/common/type.ts
+++ /dev/null
@@ -1,43 +0,0 @@
-import { Ref } from "vue";
-import { Schema, Table, ComposedProject } from "@/types";
-import { Engine } from "@/types/proto/v1/common";
-import { DatabaseMetadata } from "@/types/proto/v1/database_service";
-
-export enum SchemaEditorTabType {
- TabForTable = "table",
-}
-
-// Tab context for editing table.
-export interface TableTabContext {
- id: string;
- type: SchemaEditorTabType.TabForTable;
- schemaId: string;
- tableId: string;
- selectedSubtab?: string;
-}
-
-export type TabContext = TableTabContext;
-
-export interface SchemaEditorTabState {
- tabMap: Map<string, TabContext>;
- currentTabId?: string;
-}
-
-export interface SchemaEditorContext {
- readonly: Ref<boolean>;
- baselineMetadata: Ref<DatabaseMetadata>;
- engine: Ref<Engine>;
- metadata: Ref<DatabaseMetadata>;
- tabState: Ref<SchemaEditorTabState>;
- originalSchemas: Ref<Schema[]>;
- editableSchemas: Ref<Schema[]>;
- project: Ref<ComposedProject>;
-
- // Tab related functions.
- getCurrentTab: () => TabContext | undefined;
- addTab: (tab: TabContext, setAsCurrentTab?: boolean) => void;
-
- // Table related functions.
- getTable: (schemaId: string, tableId: string) => Table;
- dropTable: (schemaId: string, tableId: string) => void;
-}
diff --git a/frontend/src/components/SchemaEditorV1/index.vue b/frontend/src/components/SchemaEditorV1/index.vue
index d958736215..7a84ca8b6d 100644
--- a/frontend/src/components/SchemaEditorV1/index.vue
+++ b/frontend/src/components/SchemaEditorV1/index.vue
@@ -1,35 +1,29 @@
<template>
- <div class="w-full h-[32rem] border rounded-lg">
+ <div v-if="state.initialized" class="w-full h-[32rem] border rounded-lg">
<Splitpanes
class="default-theme w-full h-full flex flex-row overflow-hidden"
>
<Pane min-size="15" size="25">
- <AsidePanel />
+ <Aside />
</Pane>
<Pane min-size="60" size="75">
- <Designer />
+ <Editor />
</Pane>
</Splitpanes>
</div>
</template>
<script lang="ts" setup>
-import { cloneDeep, isEqual } from "lodash-es";
import { Splitpanes, Pane } from "splitpanes";
-import { onMounted, ref, watch } from "vue";
-import { useSettingV1Store } from "@/store";
-import {
- Schema,
- convertSchemaMetadataList,
- ComposedProject,
- unknownProject,
-} from "@/types";
+import { onMounted, watch, reactive } from "vue";
+import { useSchemaEditorV1Store, useSettingV1Store } from "@/store";
+import { ComposedProject, ComposedDatabase } from "@/types";
import { Engine } from "@/types/proto/v1/common";
import { DatabaseMetadata } from "@/types/proto/v1/database_service";
-import AsidePanel from "./AsidePanel.vue";
-import Designer from "./Designer.vue";
-import { provideSchemaEditorContext, SchemaEditorTabState } from "./common";
-import { rebuildEditableSchemas } from "./utils";
+import { SchemaDesign } from "@/types/proto/v1/schema_design_service";
+import Aside from "./Aside/index.vue";
+import Editor from "./Editor.vue";
+import { convertBranchToBranchSchema } from "./utils/branch";
const props = defineProps<{
readonly: boolean;
@@ -37,91 +31,82 @@ const props = defineProps<{
project: ComposedProject;
baselineSchemaMetadata?: DatabaseMetadata;
schemaMetadata?: DatabaseMetadata;
+ resourceType: "database" | "branch";
+ databases?: ComposedDatabase[];
+ // NOTE: we only support editing one branch for now.
+ branches?: SchemaDesign[];
}>();
+interface LocalState {
+ initialized: boolean;
+}
+
const settingStore = useSettingV1Store();
-const readonly = ref(props.readonly);
-const engine = ref(props.engine);
-const metadata = ref<DatabaseMetadata>(DatabaseMetadata.fromPartial({}));
-const originalSchemas = ref<Schema[]>([]);
-const editableSchemas = ref<Schema[]>([]);
-const project = ref<ComposedProject>(unknownProject());
-const baselineMetadata = ref<DatabaseMetadata>(
- DatabaseMetadata.fromPartial({})
-);
-const tabState = ref<SchemaEditorTabState>({
- tabMap: new Map(),
+const schemaEditorV1Store = useSchemaEditorV1Store();
+const state = reactive<LocalState>({
+ initialized: false,
});
-// Prepare schema template contexts.
-onMounted(async () => {
- await settingStore.getOrFetchSettingByName("bb.workspace.schema-template");
-});
+const updateSchemaEditorState = () => {
+ schemaEditorV1Store.setState({
+ engine: props.engine,
+ project: props.project,
+ readonly: props.readonly,
+ resourceType: props.resourceType,
+ // NOTE: this will clear all tabs. We will restore tabs as needed later.
+ tabState: {
+ tabMap: new Map(),
+ },
+ });
-const rebuildEditingState = () => {
- originalSchemas.value = convertSchemaMetadataList(
- baselineMetadata.value.schemas
- );
- editableSchemas.value = rebuildEditableSchemas(
- originalSchemas.value,
- metadata.value.schemas
- );
- tabState.value = {
- tabMap: new Map(),
- };
+ if (props.resourceType === "database") {
+ schemaEditorV1Store.setState({
+ resourceMap: {
+ // NOTE: we will dynamically fetch schema list for each database in database tree view.
+ database: new Map(
+ (props.databases || []).map((database) => [
+ database.name,
+ {
+ database,
+ schemaList: [],
+ originSchemaList: [],
+ },
+ ])
+ ),
+ branch: new Map(),
+ },
+ });
+ } else {
+ schemaEditorV1Store.setState({
+ resourceMap: {
+ database: new Map(),
+ branch: new Map(
+ (props.branches || []).map((branch) => [
+ branch.name,
+ convertBranchToBranchSchema(branch),
+ ])
+ ),
+ },
+ });
+ }
};
-provideSchemaEditorContext({
- readonly: readonly,
- baselineMetadata: baselineMetadata,
- engine: engine,
- metadata: metadata,
- tabState: tabState,
- originalSchemas: originalSchemas,
- editableSchemas: editableSchemas,
- project,
+// Prepare schema template contexts.
+onMounted(async () => {
+ await settingStore.getOrFetchSettingByName("bb.workspace.schema-template");
+ updateSchemaEditorState();
+ state.initialized = true;
});
watch(
() => props,
() => {
- baselineMetadata.value =
- cloneDeep(props.baselineSchemaMetadata) ||
- DatabaseMetadata.fromPartial({});
- metadata.value =
- cloneDeep(props.schemaMetadata) || DatabaseMetadata.fromPartial({});
- readonly.value = props.readonly;
- engine.value = props.engine;
- project.value = props.project;
- },
- {
- immediate: true,
- deep: true,
- }
-);
-
-watch(
- () => metadata.value,
- (value, oldValue) => {
- // NOTE: regenerate editing state in the following cases:
- // * change baseline schema.
- // * change selected schema design.
- if (!isEqual(value, oldValue)) {
- rebuildEditingState();
- }
+ updateSchemaEditorState();
},
{
- immediate: true,
deep: true,
}
);
-
-defineExpose({
- metadata,
- baselineMetadata,
- editableSchemas,
- rebuildEditingState,
-});
</script>
<style>
diff --git a/frontend/src/components/SchemaEditorV1/utils/branch.ts b/frontend/src/components/SchemaEditorV1/utils/branch.ts
new file mode 100644
index 0000000000..8a608e1d68
--- /dev/null
+++ b/frontend/src/components/SchemaEditorV1/utils/branch.ts
@@ -0,0 +1,23 @@
+import { SchemaDesign } from "@/types/proto/v1/schema_design_service";
+import {
+ BranchSchema,
+ convertSchemaMetadataList,
+} from "@/types/v1/schemaEditor";
+import { rebuildEditableSchemas } from "./metadataV1";
+
+export const convertBranchToBranchSchema = (
+ branch: SchemaDesign
+): BranchSchema => {
+ const originalSchemas = convertSchemaMetadataList(
+ branch.baselineSchemaMetadata?.schemas || []
+ );
+ const editableSchemas = rebuildEditableSchemas(
+ originalSchemas,
+ branch.schemaMetadata?.schemas || []
+ );
+ return {
+ branch,
+ schemaList: editableSchemas,
+ originSchemaList: originalSchemas,
+ };
+};
diff --git a/frontend/src/components/SchemaEditorV1/utils/column.ts b/frontend/src/components/SchemaEditorV1/utils/column.ts
index dc3662aaa7..fc5133baf1 100644
--- a/frontend/src/components/SchemaEditorV1/utils/column.ts
+++ b/frontend/src/components/SchemaEditorV1/utils/column.ts
@@ -1,19 +1,18 @@
import { isEqual, isUndefined } from "lodash-es";
-import { useSchemaEditorContext } from "../common";
+import { useSchemaEditorV1Store } from "@/store";
export const isColumnChanged = (
+ parentName: string,
schemaId: string,
tableId: string,
columnId: string
): boolean => {
- const { originalSchemas, editableSchemas } = useSchemaEditorContext();
- const originSchema = originalSchemas.value.find(
- (schema) => schema.id === schemaId
- );
- const schema = editableSchemas.value.find((schema) => schema.id === schemaId);
- const table = schema?.tableList.find((table) => table.id === tableId);
- const originTable = originSchema?.tableList.find(
- (table) => table.id === tableId
+ const schemaEditorV1Store = useSchemaEditorV1Store();
+ const table = schemaEditorV1Store.getTable(parentName, schemaId, tableId);
+ const originTable = schemaEditorV1Store.getOriginTable(
+ parentName,
+ schemaId,
+ tableId
);
const column = table?.columnList.find((column) => column.id === columnId);
const originColumn = originTable?.columnList.find(
@@ -24,10 +23,10 @@ export const isColumnChanged = (
const isPrimaryKeyOrigin =
originTable?.primaryKey.columnIdList.includes(columnId);
- const originForeignKey = originSchema?.foreignKeyList.find(
+ const originForeignKey = originTable?.foreignKeyList.find(
(fk) => fk.tableId === table?.id && fk.columnIdList.includes(columnId)
);
- const foreignKey = schema?.foreignKeyList.find(
+ const foreignKey = table?.foreignKeyList.find(
(fk) => fk.tableId === table?.id && fk.columnIdList.includes(columnId)
);
const originIndex = originForeignKey?.columnIdList.findIndex(
diff --git a/frontend/src/components/SchemaEditorV1/utils/database.ts b/frontend/src/components/SchemaEditorV1/utils/database.ts
new file mode 100644
index 0000000000..8adde78323
--- /dev/null
+++ b/frontend/src/components/SchemaEditorV1/utils/database.ts
@@ -0,0 +1,39 @@
+import { cloneDeep } from "lodash-es";
+import {
+ useDBSchemaV1Store,
+ useDatabaseV1Store,
+ useSchemaEditorV1Store,
+} from "@/store";
+import { Engine } from "@/types/proto/v1/common";
+import { SchemaMetadata } from "@/types/proto/v1/database_service";
+import {
+ convertSchemaMetadataList,
+ convertSchemaMetadataToSchema,
+} from "@/types/v1/schemaEditor";
+
+export const fetchSchemaListByDatabaseName = async (
+ databaseName: string,
+ skipCache = false
+) => {
+ const schemaEditorV1Store = useSchemaEditorV1Store();
+ const database = useDatabaseV1Store().getDatabaseByName(databaseName);
+ const schemaMetadataList = await useDBSchemaV1Store().getOrFetchSchemaList(
+ database.name,
+ skipCache
+ );
+ const schemaList = convertSchemaMetadataList(schemaMetadataList);
+ if (
+ schemaList.length === 0 &&
+ database.instanceEntity.engine === Engine.MYSQL
+ ) {
+ schemaList.push(
+ convertSchemaMetadataToSchema(SchemaMetadata.fromPartial({}))
+ );
+ }
+
+ schemaEditorV1Store.resourceMap.database.set(database.name, {
+ database,
+ schemaList: schemaList,
+ originSchemaList: cloneDeep(schemaList),
+ });
+};
diff --git a/frontend/src/components/SchemaEditorV1/utils/metadataV1.ts b/frontend/src/components/SchemaEditorV1/utils/metadataV1.ts
new file mode 100644
index 0000000000..849ea27639
--- /dev/null
+++ b/frontend/src/components/SchemaEditorV1/utils/metadataV1.ts
@@ -0,0 +1,513 @@
+import { cloneDeep, isEqual, uniq } from "lodash-es";
+import {
+ ColumnMetadata,
+ DatabaseMetadata,
+ ForeignKeyMetadata,
+ IndexMetadata,
+ SchemaMetadata,
+ TableMetadata,
+} from "@/types/proto/v1/database_service";
+import {
+ Column,
+ ForeignKey,
+ Schema,
+ Table,
+ convertColumnMetadataToColumn,
+ convertSchemaMetadataToSchema,
+ convertTableMetadataToTable,
+} from "@/types/v1/schemaEditor";
+import { randomString } from "@/utils";
+
+export const mergeSchemaEditToMetadata = (
+ schemaEdits: Schema[],
+ metadata: DatabaseMetadata
+): DatabaseMetadata => {
+ for (const schemaEdit of schemaEdits) {
+ if (schemaEdit.status === "created") {
+ // Remove schema if it exists.
+ metadata.schemas = metadata.schemas.filter(
+ (item) => item.name !== schemaEdit.name
+ );
+ metadata.schemas.push(transformSchemaEditToMetadata(schemaEdit));
+ continue;
+ } else if (schemaEdit.status === "dropped") {
+ metadata.schemas = metadata.schemas.filter(
+ (item) => item.name !== schemaEdit.name
+ );
+ continue;
+ } else {
+ const schema = metadata.schemas.find(
+ (item) => item.name === schemaEdit.name
+ );
+ if (!schema) {
+ metadata.schemas.push(transformSchemaEditToMetadata(schemaEdit));
+ continue;
+ }
+ for (const tableEdit of schemaEdit.tableList) {
+ if (tableEdit.status === "created") {
+ // Remove table if it exists.
+ schema.tables = schema.tables.filter(
+ (item) => item.name !== tableEdit.name
+ );
+ schema.tables.push(transformTableEditToMetadata(tableEdit));
+ } else if (tableEdit.status === "dropped") {
+ schema.tables = schema.tables.filter(
+ (item) => item.name !== tableEdit.name
+ );
+ continue;
+ } else {
+ const table = schema.tables.find(
+ (item) => item.name === tableEdit.name
+ );
+ if (!table) {
+ schema.tables.push(transformTableEditToMetadata(tableEdit));
+ continue;
+ }
+ for (const columnEdit of tableEdit.columnList) {
+ if (columnEdit.status === "created") {
+ // Remove column if it exists.
+ table.columns = table.columns.filter(
+ (item) => item.name !== columnEdit.name
+ );
+ table.columns.push(transformColumnEditToMetadata(columnEdit));
+ continue;
+ } else if (columnEdit.status === "dropped") {
+ table.columns = table.columns.filter(
+ (item) => item.name !== columnEdit.name
+ );
+ continue;
+ } else {
+ const column = table.columns.find(
+ (item) => item.name === columnEdit.name
+ );
+ if (!column) {
+ table.columns.push(transformColumnEditToMetadata(columnEdit));
+ continue;
+ }
+ column.type = columnEdit.type;
+ column.nullable = columnEdit.nullable;
+ column.comment = columnEdit.comment;
+ column.userComment = columnEdit.userComment;
+ column.classification = columnEdit.classification ?? "";
+ column.default = columnEdit.default;
+ }
+ }
+ for (const column of table.columns) {
+ const columnEdit = tableEdit.columnList.find(
+ (item) => item.name === column.name
+ );
+ if (!columnEdit) {
+ table.columns = table.columns.filter(
+ (item) => item.name !== column.name
+ );
+ }
+ }
+ }
+ }
+ for (const table of schema.tables) {
+ const tableEdit = schemaEdit.tableList.find(
+ (item) => item.name === table.name
+ );
+ if (!tableEdit) {
+ schema.tables = schema.tables.filter(
+ (item) => item.name !== table.name
+ );
+ }
+ }
+ }
+ }
+ for (const schema of metadata.schemas) {
+ const schemaEdit = schemaEdits.find((item) => item.name === schema.name);
+ if (!schemaEdit) {
+ metadata.schemas = metadata.schemas.filter(
+ (item) => item.name !== schema.name
+ );
+ continue;
+ }
+
+ // Build foreign keys.
+ for (const tableEdit of schemaEdit.tableList) {
+ for (const foreignKey of tableEdit.foreignKeyList) {
+ const schema = metadata.schemas.find(
+ (schema) => schema.name === schemaEdit.name
+ );
+ if (!schema) {
+ continue;
+ }
+ const tableEdit = schemaEdit.tableList.find(
+ (table) => table.id === foreignKey.tableId
+ );
+ const table = schema.tables.find(
+ (table) => table.name === tableEdit?.name
+ );
+ if (!tableEdit || !table) {
+ continue;
+ }
+ const referencedSchema = metadata.schemas.find(
+ (schema) =>
+ schema.name ===
+ schemaEdits.find(
+ (schemaEdit) => schemaEdit.id === foreignKey.referencedSchemaId
+ )?.name
+ );
+ if (!referencedSchema) {
+ continue;
+ }
+ const referencedTableEdit = schemaEdits
+ .find((schemaEdit) => schemaEdit.id === foreignKey.referencedSchemaId)
+ ?.tableList.find(
+ (table) => table.id === foreignKey.referencedTableId
+ );
+ const referencedTable = referencedSchema.tables.find(
+ (table) => table.name === referencedTableEdit?.name
+ );
+ if (!referencedTableEdit || !referencedTable) {
+ continue;
+ }
+
+ const foreignKeyName = foreignKey.name
+ ? foreignKey.name
+ : `${table.name}-fk-${randomString(8).toLowerCase()}`;
+ const fk = ForeignKeyMetadata.fromPartial({
+ name: foreignKeyName,
+ referencedSchema: referencedSchema.name,
+ referencedTable: referencedTable.name,
+ });
+ if (table.foreignKeys.find((fk) => fk.name === foreignKeyName)) {
+ continue;
+ }
+ if (
+ foreignKey.columnIdList.length !==
+ foreignKey.referencedColumnIdList.length
+ ) {
+ continue;
+ }
+ for (const columnId of foreignKey.columnIdList) {
+ const column = tableEdit.columnList.find(
+ (column) => column.id === columnId
+ );
+ if (column) {
+ fk.columns.push(column.name);
+ }
+ }
+ for (const columnId of foreignKey.referencedColumnIdList) {
+ const column = referencedTableEdit.columnList.find(
+ (column) => column.id === columnId
+ );
+ if (column) {
+ fk.referencedColumns.push(column.name);
+ }
+ }
+ table.foreignKeys.push(fk);
+ }
+ }
+ }
+
+ return metadata;
+};
+
+const transformSchemaEditToMetadata = (schemaEdit: Schema): SchemaMetadata => {
+ const schemaMetadata = SchemaMetadata.fromPartial({
+ name: schemaEdit.name,
+ tables: [],
+ });
+
+ for (const table of schemaEdit.tableList) {
+ const tableMetadata = TableMetadata.fromPartial({
+ name: table.name,
+ columns: [],
+ indexes: [],
+ foreignKeys: [],
+ });
+
+ for (const column of table.columnList) {
+ tableMetadata.columns.push(
+ ColumnMetadata.fromPartial({
+ name: column.name,
+ type: column.type,
+ nullable: column.nullable,
+ default: column.default,
+ comment: column.comment,
+ userComment: column.userComment,
+ classification: column.classification,
+ })
+ );
+ }
+
+ if (table.primaryKey.columnIdList.length > 0) {
+ const primaryIndex = IndexMetadata.fromPartial({
+ name: `${table.name}-pk-${randomString(8).toLowerCase()}`,
+ primary: true,
+ expressions: [],
+ });
+ for (const columnId of table.primaryKey.columnIdList) {
+ const column = table.columnList.find(
+ (column) => column.id === columnId
+ );
+ if (column) {
+ primaryIndex.expressions.push(column.name);
+ }
+ }
+ if (primaryIndex.expressions.length > 0) {
+ tableMetadata.indexes.push(primaryIndex);
+ }
+ }
+
+ schemaMetadata.tables.push(tableMetadata);
+ }
+
+ return schemaMetadata;
+};
+
+const transformTableEditToMetadata = (tableEdit: Table): TableMetadata => {
+ const tableMetadata = TableMetadata.fromPartial({
+ name: tableEdit.name,
+ columns: [],
+ indexes: [],
+ foreignKeys: [],
+ });
+
+ for (const column of tableEdit.columnList) {
+ tableMetadata.columns.push(
+ ColumnMetadata.fromPartial({
+ name: column.name,
+ type: column.type,
+ nullable: column.nullable,
+ default: column.default,
+ comment: column.comment,
+ userComment: column.userComment,
+ classification: column.classification,
+ })
+ );
+ }
+
+ if (tableEdit.primaryKey.columnIdList.length > 0) {
+ const primaryIndex = IndexMetadata.fromPartial({
+ name: `${tableEdit.name}-pk-${randomString(8).toLowerCase()}`,
+ primary: true,
+ expressions: [],
+ });
+ for (const columnId of tableEdit.primaryKey.columnIdList) {
+ const column = tableEdit.columnList.find(
+ (column) => column.id === columnId
+ );
+ if (column) {
+ primaryIndex.expressions.push(column.name);
+ }
+ }
+ if (primaryIndex.expressions.length > 0) {
+ tableMetadata.indexes.push(primaryIndex);
+ }
+ }
+
+ return tableMetadata;
+};
+
+const transformColumnEditToMetadata = (columnEdit: Column): ColumnMetadata => {
+ return ColumnMetadata.fromPartial({
+ name: columnEdit.name,
+ type: columnEdit.type,
+ nullable: columnEdit.nullable,
+ default: columnEdit.default,
+ comment: columnEdit.comment,
+ userComment: columnEdit.userComment,
+ classification: columnEdit.classification,
+ });
+};
+
+export const rebuildEditableSchemas = (
+ originalSchemas: Schema[],
+ schemas: SchemaMetadata[]
+): Schema[] => {
+ const editableSchemas = cloneDeep(originalSchemas);
+
+ for (const editableSchema of editableSchemas) {
+ const schema = schemas.find(
+ (schema) => schema.name === editableSchema.name
+ );
+ if (!schema) {
+ editableSchema.status = "dropped";
+ continue;
+ }
+
+ for (const editableTable of editableSchema.tableList) {
+ const table = schema.tables.find(
+ (table) => table.name === editableTable.name
+ );
+ if (!table) {
+ editableTable.status = "dropped";
+ continue;
+ }
+
+ for (const editableColumn of editableTable.columnList) {
+ const column = table.columns.find(
+ (column) => column.name === editableColumn.name
+ );
+ if (!column) {
+ editableColumn.status = "dropped";
+ continue;
+ }
+ if (isEqual(transformColumnEditToMetadata(editableColumn), column)) {
+ continue;
+ }
+
+ editableColumn.type = column.type;
+ editableColumn.nullable = column.nullable;
+ editableColumn.comment = column.comment;
+ editableColumn.userComment = column.userComment;
+ editableColumn.classification = column.classification;
+ editableColumn.default = column.default;
+ }
+
+ for (const column of table.columns) {
+ const editableColumn = editableTable.columnList.find(
+ (item) => item.name === column.name
+ );
+ if (!editableColumn) {
+ const newColumn = convertColumnMetadataToColumn(column);
+ newColumn.status = "created";
+ editableTable.columnList.push(newColumn);
+ }
+ }
+
+ // Rebuild primary key from primary index.
+ const primaryIndex = table.indexes.find(
+ (index) => index.primary === true
+ );
+ if (primaryIndex) {
+ editableTable.primaryKey.name = primaryIndex.name;
+ for (const columnName of primaryIndex.expressions) {
+ const column = editableTable.columnList.find(
+ (column) => column.name === columnName
+ );
+ if (column) {
+ editableTable.primaryKey.columnIdList.push(column.id);
+ }
+ }
+ editableTable.primaryKey.columnIdList = uniq(
+ editableTable.primaryKey.columnIdList
+ );
+ }
+ }
+
+ for (const table of schema.tables) {
+ const editableTable = editableSchema.tableList.find(
+ (item) => item.name === table.name
+ );
+ if (!editableTable) {
+ const newTable = convertTableMetadataToTable(table);
+ newTable.status = "created";
+ for (const column of newTable.columnList) {
+ column.status = "created";
+ }
+ editableSchema.tableList.push(newTable);
+ }
+ }
+ }
+
+ for (const schema of schemas) {
+ const editableSchema = editableSchemas.find(
+ (item) => item.name === schema.name
+ );
+ if (!editableSchema) {
+ const newSchema = convertSchemaMetadataToSchema(schema);
+ newSchema.status = "created";
+ for (const table of newSchema.tableList) {
+ table.status = "created";
+ for (const column of table.columnList) {
+ column.status = "created";
+ }
+ }
+ editableSchemas.push(newSchema);
+ }
+ }
+
+ // Build foreign keys for schema and referenced schema.
+ for (const schema of schemas) {
+ const editableSchema = editableSchemas.find(
+ (schema) => schema.name === schema.name
+ );
+ if (!editableSchema) {
+ continue;
+ }
+
+ for (const table of schema.tables) {
+ const editableTable = editableSchema.tableList.find(
+ (item) => item.name === table.name
+ );
+ if (!editableTable) {
+ continue;
+ }
+
+ const foreignKeyList: ForeignKey[] = [];
+ for (const foreignKeyMetadata of table.foreignKeys) {
+ const referencedSchema = editableSchemas.find(
+ (schema) => schema.name === foreignKeyMetadata.referencedSchema
+ );
+ const referencedTable = referencedSchema?.tableList.find(
+ (table) => table.name === foreignKeyMetadata.referencedTable
+ );
+ if (!referencedSchema || !referencedTable) {
+ continue;
+ }
+
+ const fk: ForeignKey = {
+ name: foreignKeyMetadata.name,
+ tableId: editableTable.id,
+ columnIdList: [],
+ referencedSchemaId: referencedSchema.id,
+ referencedTableId: referencedTable.id,
+ referencedColumnIdList: [],
+ };
+ for (const columnName of foreignKeyMetadata.columns) {
+ const column = editableTable.columnList.find(
+ (column) => column.name === columnName
+ );
+ if (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);
+ }
+ editableTable.foreignKeyList = foreignKeyList;
+ }
+ }
+
+ return editableSchemas;
+};
+
+export const validateDatabaseMetadata = (
+ databaseMetadata: DatabaseMetadata
+): string[] => {
+ const messages: string[] = [];
+
+ for (const schema of databaseMetadata.schemas) {
+ for (const table of schema.tables) {
+ if (!table.name) {
+ messages.push(`Table name is required.`);
+ continue;
+ }
+
+ for (const column of table.columns) {
+ if (!column.name) {
+ messages.push(`Column name is required.`);
+ continue;
+ }
+ if (!column.type) {
+ messages.push(`Column ${column.name} type is required.`);
+ }
+ }
+ }
+ }
+
+ return uniq(messages);
+};
diff --git a/frontend/src/components/SchemaEditorV1/utils/schema.ts b/frontend/src/components/SchemaEditorV1/utils/schema.ts
index 6ad1ccedb7..8551ce5a05 100644
--- a/frontend/src/components/SchemaEditorV1/utils/schema.ts
+++ b/frontend/src/components/SchemaEditorV1/utils/schema.ts
@@ -1,11 +1,15 @@
import { isEqual } from "lodash-es";
-import { useSchemaEditorContext } from "../common";
+import { useSchemaEditorV1Store } from "@/store";
-export const isSchemaChanged = (schemaId: string): boolean => {
- const { originalSchemas, editableSchemas } = useSchemaEditorContext();
- const originSchema = originalSchemas.value.find(
- (schema) => schema.id === schemaId
+export const isSchemaChanged = (
+ parentName: string,
+ schemaId: string
+): boolean => {
+ const schemaEditorV1Store = useSchemaEditorV1Store();
+ const editorSchema = schemaEditorV1Store.getSchema(parentName, schemaId);
+ const originSchema = schemaEditorV1Store.getOriginSchema(
+ parentName,
+ schemaId
);
- const schema = editableSchemas.value.find((schema) => schema.id === schemaId);
- return !isEqual(originSchema, schema);
+ return !isEqual(originSchema, editorSchema);
};
diff --git a/frontend/src/components/SchemaEditorV1/utils/table.ts b/frontend/src/components/SchemaEditorV1/utils/table.ts
index 1afe93af3d..1172d12f37 100644
--- a/frontend/src/components/SchemaEditorV1/utils/table.ts
+++ b/frontend/src/components/SchemaEditorV1/utils/table.ts
@@ -1,23 +1,21 @@
import { isEqual } from "lodash-es";
-import { useSchemaEditorContext } from "../common";
+import { useSchemaEditorV1Store } from "@/store";
-export const isTableChanged = (schemaId: string, tableId: string): boolean => {
- const { originalSchemas, editableSchemas } = useSchemaEditorContext();
- const originSchema = originalSchemas.value.find(
- (schema) => schema.id === schemaId
+export const isTableChanged = (
+ parentName: string,
+ schemaId: string,
+ tableId: string
+): boolean => {
+ const schemaEditorV1Store = useSchemaEditorV1Store();
+ const editorTable = schemaEditorV1Store.getTable(
+ parentName,
+ schemaId,
+ tableId
);
- const schema = editableSchemas.value.find((schema) => schema.id === schemaId);
- const originTable = originSchema?.tableList.find(
- (table) => table.id === tableId
- );
- const table = schema?.tableList.find((table) => table.id === tableId);
- const originForeignKeyList =
- originSchema?.foreignKeyList.filter((fk) => fk.tableId === table?.id) || [];
- const foreignKeyList =
- schema?.foreignKeyList.filter((fk) => fk.tableId === table?.id) || [];
-
- return (
- !isEqual(originTable, table) ||
- !isEqual(originForeignKeyList, foreignKeyList)
+ const originTable = schemaEditorV1Store.getOriginTable(
+ parentName,
+ schemaId,
+ tableId
);
+ return !isEqual(originTable, editorTable);
};
diff --git a/frontend/src/store/modules/schemaEditor.ts b/frontend/src/store/modules/schemaEditor.ts
index 1632b51dbc..ace6941114 100644
--- a/frontend/src/store/modules/schemaEditor.ts
+++ b/frontend/src/store/modules/schemaEditor.ts
@@ -1,5 +1,5 @@
import axios from "axios";
-import { cloneDeep, isUndefined, uniqueId } from "lodash-es";
+import { cloneDeep, isUndefined } from "lodash-es";
import { defineStore } from "pinia";
import {
ComposedDatabase,
@@ -20,10 +20,6 @@ import {
} from "@/types/schemaEditor/atomType";
import { useDatabaseV1Store, useDBSchemaV1Store } from ".";
-export const generateUniqueTabId = () => {
- return uniqueId();
-};
-
const getDefaultSchemaEditorState = (): SchemaEditorState => {
return {
tabState: {
diff --git a/frontend/src/store/modules/v1/index.ts b/frontend/src/store/modules/v1/index.ts
index 2da9b50b7c..3d5103725b 100644
--- a/frontend/src/store/modules/v1/index.ts
+++ b/frontend/src/store/modules/v1/index.ts
@@ -24,3 +24,4 @@ export * from "./activity";
export * from "./bookmark";
export * from "./inbox";
export * from "./anomaly";
+export * from "./schemaEditor";
diff --git a/frontend/src/store/modules/schemaEditorV1.ts b/frontend/src/store/modules/v1/schemaEditor.ts
similarity index 87%
rename from frontend/src/store/modules/schemaEditorV1.ts
rename to frontend/src/store/modules/v1/schemaEditor.ts
index ccbebd6f71..0bcc74dcf3 100644
--- a/frontend/src/store/modules/schemaEditorV1.ts
+++ b/frontend/src/store/modules/v1/schemaEditor.ts
@@ -1,13 +1,14 @@
import { isUndefined, uniqueId } from "lodash-es";
import { defineStore } from "pinia";
-import { ComposedDatabase } from "@/types";
+import { ComposedDatabase, emptyProject } from "@/types";
+import { Engine } from "@/types/proto/v1/common";
import {
SchemaEditorV1State,
Table,
TabContext,
SchemaEditorTabType,
TableTabContext,
-} from "@/types/schemaEditorV1";
+} from "@/types/v1/schemaEditor";
export const generateUniqueTabId = () => {
return uniqueId();
@@ -15,19 +16,22 @@ export const generateUniqueTabId = () => {
const getDefaultSchemaEditorState = (): SchemaEditorV1State => {
return {
+ engine: Engine.MYSQL,
+ project: emptyProject(),
+ readonly: false,
resourceType: "database",
resourceMap: {
- database: new Map(),
- branch: new Map(),
+ database: new Map([]),
+ branch: new Map([]),
},
tabState: {
- tabMap: new Map<string, TabContext>(),
+ tabMap: new Map<string, TabContext>([]),
currentTabId: "",
},
};
};
-export const useSchemaEditorStore = defineStore("SchemaEditorV1", {
+export const useSchemaEditorV1Store = defineStore("SchemaEditorV1", {
state: (): SchemaEditorV1State => {
return getDefaultSchemaEditorState();
},
@@ -48,6 +52,9 @@ export const useSchemaEditorStore = defineStore("SchemaEditorV1", {
},
},
actions: {
+ setState(state: Partial<SchemaEditorV1State>) {
+ Object.assign(this, state);
+ },
addTab(tab: TabContext, setAsCurrentTab = true) {
const tabCache = this.tabList.find((item) => {
if (
@@ -165,20 +172,20 @@ export const useSchemaEditorStore = defineStore("SchemaEditorV1", {
schema.status = "dropped";
}
},
- restoreSchema(databaseId: string, schemaId: string) {
- const schema = this.getSchema(databaseId, schemaId);
+ restoreSchema(parentName: string, schemaId: string) {
+ const schema = this.getSchema(parentName, schemaId);
if (!schema) {
return;
}
schema.status = "normal";
},
- getTable(databaseId: string, schemaId: string, tableId: string) {
- return this.getSchema(databaseId, schemaId)?.tableList.find(
+ getTable(parentName: string, schemaId: string, tableId: string) {
+ return this.getSchema(parentName, schemaId)?.tableList.find(
(table) => table.id === tableId
);
},
- getOriginTable(databaseId: string, schemaId: string, tableId: string) {
- return this.getOriginSchema(databaseId, schemaId)?.tableList.find(
+ getOriginTable(parentName: string, schemaId: string, tableId: string) {
+ return this.getOriginSchema(parentName, schemaId)?.tableList.find(
(table) => table.id === tableId
);
},
diff --git a/frontend/src/types/schemaEditorV1/atomType.ts b/frontend/src/types/v1/schemaEditor/atomType.ts
similarity index 99%
rename from frontend/src/types/schemaEditorV1/atomType.ts
rename to frontend/src/types/v1/schemaEditor/atomType.ts
index fcf29c5c3c..f11a73cb96 100644
--- a/frontend/src/types/schemaEditorV1/atomType.ts
+++ b/frontend/src/types/v1/schemaEditor/atomType.ts
@@ -4,7 +4,7 @@ import {
SchemaMetadata,
TableMetadata,
ColumnMetadata,
-} from "../proto/v1/database_service";
+} from "../../proto/v1/database_service";
type Status = "normal" | "created" | "dropped";
diff --git a/frontend/src/types/schemaEditorV1/index.ts b/frontend/src/types/v1/schemaEditor/index.ts
similarity index 100%
rename from frontend/src/types/schemaEditorV1/index.ts
rename to frontend/src/types/v1/schemaEditor/index.ts
diff --git a/frontend/src/types/schemaEditorV1/state.ts b/frontend/src/types/v1/schemaEditor/state.ts
similarity index 82%
rename from frontend/src/types/schemaEditorV1/state.ts
rename to frontend/src/types/v1/schemaEditor/state.ts
index 03dce5422f..ebbfd067b6 100644
--- a/frontend/src/types/schemaEditorV1/state.ts
+++ b/frontend/src/types/v1/schemaEditor/state.ts
@@ -1,5 +1,6 @@
-import { SchemaDesign } from "../proto/v1/schema_design_service";
-import { ComposedDatabase } from "../v1";
+import { ComposedDatabase, ComposedProject } from "..";
+import { Engine } from "../../proto/v1/common";
+import { SchemaDesign } from "../../proto/v1/schema_design_service";
import { Schema } from "./atomType";
export enum SchemaEditorTabType {
@@ -41,6 +42,9 @@ export interface BranchSchema {
}
export interface SchemaEditorV1State {
+ engine: Engine;
+ project: ComposedProject;
+ readonly: boolean;
resourceType: "database" | "branch";
resourceMap: {
database: Map<string, DatabaseSchema>;