Skip to content

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

refactor: extract schema editor v1 components (#8145)

boojack2a5910bdb1cc
diff --git a/frontend/src/components/SchemaDesigner/CreateSchemaDesignPanel.vue b/frontend/src/components/SchemaDesigner/CreateSchemaDesignPanel.vue
index 66eeca61f8..17e24089ec 100644
--- a/frontend/src/components/SchemaDesigner/CreateSchemaDesignPanel.vue
+++ b/frontend/src/components/SchemaDesigner/CreateSchemaDesignPanel.vue
@@ -85,6 +85,7 @@ 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 {
pushNotification,
useDatabaseV1Store,
@@ -108,8 +109,7 @@ import {
Sheet_Visibility,
} from "@/types/proto/v1/sheet_service";
import BaselineSchemaSelector from "./BaselineSchemaSelector.vue";
-import { mergeSchemaEditToMetadata, validateBranchName } from "./common/util";
-import SchemaDesigner from "./index.vue";
+import { mergeSchemaEditToMetadata, validateBranchName } from "./utils";
interface BaselineSchema {
// The uid of database.
diff --git a/frontend/src/components/SchemaDesigner/EditSchemaDesignPanel.vue b/frontend/src/components/SchemaDesigner/EditSchemaDesignPanel.vue
index 6c44fe9c60..ae13b9f3d9 100644
--- a/frontend/src/components/SchemaDesigner/EditSchemaDesignPanel.vue
+++ b/frontend/src/components/SchemaDesigner/EditSchemaDesignPanel.vue
@@ -206,6 +206,7 @@ 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 {
pushNotification,
useChangeHistoryStore,
@@ -225,8 +226,7 @@ import {
mergeSchemaEditToMetadata,
validateDatabaseMetadata,
validateBranchName,
-} from "./common/util";
-import SchemaDesigner from "./index.vue";
+} from "./utils";
interface LocalState {
schemaDesignTitle: string;
diff --git a/frontend/src/components/SchemaDesigner/common/util.ts b/frontend/src/components/SchemaDesigner/utils.ts
similarity index 100%
rename from frontend/src/components/SchemaDesigner/common/util.ts
rename to frontend/src/components/SchemaDesigner/utils.ts
diff --git a/frontend/src/components/SchemaDesigner/AsidePanel.vue b/frontend/src/components/SchemaEditorV1/AsidePanel.vue
similarity index 100%
rename from frontend/src/components/SchemaDesigner/AsidePanel.vue
rename to frontend/src/components/SchemaEditorV1/AsidePanel.vue
diff --git a/frontend/src/components/SchemaDesigner/DatabaseTree.vue b/frontend/src/components/SchemaEditorV1/DatabaseTree.vue
similarity index 98%
rename from frontend/src/components/SchemaDesigner/DatabaseTree.vue
rename to frontend/src/components/SchemaEditorV1/DatabaseTree.vue
index 26872c59be..4de791d39e 100644
--- a/frontend/src/components/SchemaDesigner/DatabaseTree.vue
+++ b/frontend/src/components/SchemaEditorV1/DatabaseTree.vue
@@ -81,8 +81,8 @@ import { Engine } from "@/types/proto/v1/common";
import { getHighlightHTMLByKeyWords, isDescendantOf } from "@/utils";
import SchemaNameModal from "./Modals/SchemaNameModal.vue";
import TableNameModal from "./Modals/TableNameModal.vue";
-import { useSchemaDesignerContext, SchemaDesignerTabType } from "./common";
-import { isTableChanged } from "./utils/table";
+import { useSchemaEditorContext, SchemaEditorTabType } from "./common";
+import { isTableChanged } from "./utils";
interface BaseTreeNode extends TreeOption {
key: string;
@@ -132,7 +132,7 @@ const {
getTable,
getCurrentTab,
dropTable,
-} = useSchemaDesignerContext();
+} = useSchemaEditorContext();
const state = reactive<LocalState>({
shouldRelocateTreeNode: false,
});
@@ -284,7 +284,7 @@ watch(
return;
}
- if (currentTab.type === SchemaDesignerTabType.TabForTable) {
+ if (currentTab.type === SchemaEditorTabType.TabForTable) {
const schemaTreeNodeKey = `s-${currentTab.schemaId}`;
if (!expandedKeysRef.value.includes(schemaTreeNodeKey)) {
expandedKeysRef.value.push(schemaTreeNodeKey);
@@ -520,7 +520,7 @@ const openTabForTable = (treeNode: TreeNode, tableId: string) => {
if (treeNode.type === "table") {
addTab({
id: generateUniqueTabId(),
- type: SchemaDesignerTabType.TabForTable,
+ type: SchemaEditorTabType.TabForTable,
schemaId: treeNode.schemaId,
tableId: tableId,
});
diff --git a/frontend/src/components/SchemaDesigner/Designer.vue b/frontend/src/components/SchemaEditorV1/Designer.vue
similarity index 73%
rename from frontend/src/components/SchemaDesigner/Designer.vue
rename to frontend/src/components/SchemaEditorV1/Designer.vue
index 67924987b5..89d168ca9d 100644
--- a/frontend/src/components/SchemaDesigner/Designer.vue
+++ b/frontend/src/components/SchemaEditorV1/Designer.vue
@@ -4,7 +4,7 @@
<TabsContainer />
<div :key="currentTab.id" class="w-full h-full relative overflow-y-auto">
<TableEditor
- v-if="currentTab.type === SchemaDesignerTabType.TabForTable"
+ v-if="currentTab.type === SchemaEditorTabType.TabForTable"
/>
</div>
</template>
@@ -16,8 +16,8 @@
import { computed } from "vue";
import TableEditor from "./Panels/TableEditor.vue";
import TabsContainer from "./TabsContainer.vue";
-import { SchemaDesignerTabType, useSchemaDesignerContext } from "./common";
+import { SchemaEditorTabType, useSchemaEditorContext } from "./common";
-const { getCurrentTab } = useSchemaDesignerContext();
+const { getCurrentTab } = useSchemaEditorContext();
const currentTab = computed(() => getCurrentTab());
</script>
diff --git a/frontend/src/components/SchemaDesigner/EmptyTips.vue b/frontend/src/components/SchemaEditorV1/EmptyTips.vue
similarity index 100%
rename from frontend/src/components/SchemaDesigner/EmptyTips.vue
rename to frontend/src/components/SchemaEditorV1/EmptyTips.vue
diff --git a/frontend/src/components/SchemaDesigner/Modals/ActionConfirmModal.vue b/frontend/src/components/SchemaEditorV1/Modals/ActionConfirmModal.vue
similarity index 100%
rename from frontend/src/components/SchemaDesigner/Modals/ActionConfirmModal.vue
rename to frontend/src/components/SchemaEditorV1/Modals/ActionConfirmModal.vue
diff --git a/frontend/src/components/SchemaDesigner/Modals/EditColumnForeignKeyModal.vue b/frontend/src/components/SchemaEditorV1/Modals/EditColumnForeignKeyModal.vue
similarity index 98%
rename from frontend/src/components/SchemaDesigner/Modals/EditColumnForeignKeyModal.vue
rename to frontend/src/components/SchemaEditorV1/Modals/EditColumnForeignKeyModal.vue
index f45c8efae9..ee5eafbd74 100644
--- a/frontend/src/components/SchemaDesigner/Modals/EditColumnForeignKeyModal.vue
+++ b/frontend/src/components/SchemaEditorV1/Modals/EditColumnForeignKeyModal.vue
@@ -77,7 +77,7 @@ import { computed, onMounted, reactive, watch } from "vue";
import { BBModal, BBSelect } from "@/bbkit";
import { Column, ForeignKey } from "@/types";
import { Engine } from "@/types/proto/v1/common";
-import { useSchemaDesignerContext } from "../common";
+import { useSchemaEditorContext } from "../common";
interface LocalState {
referencedSchemaId?: string;
@@ -104,7 +104,7 @@ const emit = defineEmits<{
(event: "close"): void;
}>();
-const { engine, editableSchemas } = useSchemaDesignerContext();
+const { engine, editableSchemas } = useSchemaEditorContext();
const state = reactive<LocalState>({
referencedSchemaId: props.schemaId,
});
diff --git a/frontend/src/components/SchemaDesigner/Modals/SchemaNameModal.vue b/frontend/src/components/SchemaEditorV1/Modals/SchemaNameModal.vue
similarity index 94%
rename from frontend/src/components/SchemaDesigner/Modals/SchemaNameModal.vue
rename to frontend/src/components/SchemaEditorV1/Modals/SchemaNameModal.vue
index 841f8790c4..cfee8bb33a 100644
--- a/frontend/src/components/SchemaDesigner/Modals/SchemaNameModal.vue
+++ b/frontend/src/components/SchemaEditorV1/Modals/SchemaNameModal.vue
@@ -30,7 +30,7 @@ import { reactive } from "vue";
import { useI18n } from "vue-i18n";
import { useNotificationStore } from "@/store";
import { SchemaMetadata } from "@/types/proto/store/database";
-import { useSchemaDesignerContext } from "../common";
+import { useSchemaEditorContext } from "../common";
const schemaNameFieldRegexp = /^\S+$/;
@@ -43,7 +43,7 @@ const emit = defineEmits<{
}>();
const { t } = useI18n();
-const { metadata } = useSchemaDesignerContext();
+const { metadata } = useSchemaEditorContext();
const notificationStore = useNotificationStore();
const state = reactive<LocalState>({
schemaName: "",
diff --git a/frontend/src/components/SchemaDesigner/Modals/TableNameModal.vue b/frontend/src/components/SchemaEditorV1/Modals/TableNameModal.vue
similarity index 95%
rename from frontend/src/components/SchemaDesigner/Modals/TableNameModal.vue
rename to frontend/src/components/SchemaEditorV1/Modals/TableNameModal.vue
index 468ecab32c..08276b02e5 100644
--- a/frontend/src/components/SchemaDesigner/Modals/TableNameModal.vue
+++ b/frontend/src/components/SchemaEditorV1/Modals/TableNameModal.vue
@@ -39,7 +39,7 @@ import {
} from "@/types";
import { ColumnMetadata, TableMetadata } from "@/types/proto/store/database";
import { Engine } from "@/types/proto/v1/common";
-import { SchemaDesignerTabType, useSchemaDesignerContext } from "../common";
+import { SchemaEditorTabType, useSchemaEditorContext } from "../common";
const tableNameFieldRegexp = /^\S+$/;
@@ -57,7 +57,7 @@ const emit = defineEmits<{
}>();
const { t } = useI18n();
-const { engine, editableSchemas, addTab } = useSchemaDesignerContext();
+const { engine, editableSchemas, addTab } = useSchemaEditorContext();
const notificationStore = useNotificationStore();
const state = reactive<LocalState>({
tableName: "",
@@ -122,7 +122,7 @@ const handleConfirmButtonClick = async () => {
schema.tableList.push(tableEdit);
addTab({
id: generateUniqueTabId(),
- type: SchemaDesignerTabType.TabForTable,
+ type: SchemaEditorTabType.TabForTable,
schemaId: props.schemaId,
tableId: tableEdit.id,
});
diff --git a/frontend/src/components/SchemaDesigner/Panels/TableEditor.vue b/frontend/src/components/SchemaEditorV1/Panels/TableEditor.vue
similarity index 99%
rename from frontend/src/components/SchemaDesigner/Panels/TableEditor.vue
rename to frontend/src/components/SchemaEditorV1/Panels/TableEditor.vue
index 216fefe184..f9fcf286a7 100644
--- a/frontend/src/components/SchemaDesigner/Panels/TableEditor.vue
+++ b/frontend/src/components/SchemaEditorV1/Panels/TableEditor.vue
@@ -303,11 +303,11 @@ import { getDataTypeSuggestionList } from "@/utils";
import FieldTemplates from "@/views/SchemaTemplate/FieldTemplates.vue";
import EditColumnForeignKeyModal from "../Modals/EditColumnForeignKeyModal.vue";
import {
- SchemaDesignerTabType,
+ SchemaEditorTabType,
TableTabContext,
- useSchemaDesignerContext,
+ useSchemaEditorContext,
} from "../common";
-import { isColumnChanged } from "../utils/column";
+import { isColumnChanged } from "../utils";
interface LocalState {
isFetchingDDL: boolean;
@@ -320,8 +320,7 @@ interface LocalState {
const { t } = useI18n();
const { readonly, engine, project, editableSchemas, getCurrentTab, addTab } =
- useSchemaDesignerContext();
-useSchemaDesignerContext();
+ useSchemaEditorContext();
const settingStore = useSettingV1Store();
const currentTab = computed(() => getCurrentTab() as TableTabContext);
const state = reactive<LocalState>({
@@ -611,7 +610,7 @@ const gotoForeignKeyReferencedTable = (column: Column) => {
addTab({
id: generateUniqueTabId(),
- type: SchemaDesignerTabType.TabForTable,
+ type: SchemaEditorTabType.TabForTable,
schemaId: referencedSchema.id,
tableId: referencedTable.id,
});
diff --git a/frontend/src/components/SchemaDesigner/TabsContainer.vue b/frontend/src/components/SchemaEditorV1/TabsContainer.vue
similarity index 89%
rename from frontend/src/components/SchemaDesigner/TabsContainer.vue
rename to frontend/src/components/SchemaEditorV1/TabsContainer.vue
index 50933aa413..c49aec45a9 100644
--- a/frontend/src/components/SchemaDesigner/TabsContainer.vue
+++ b/frontend/src/components/SchemaEditorV1/TabsContainer.vue
@@ -17,7 +17,7 @@
<div class="flex flex-row justify-start items-center mr-1">
<span class="mr-1">
<heroicons-outline:table-cells
- v-if="tab.type === SchemaDesignerTabType.TabForTable"
+ v-if="tab.type === SchemaEditorTabType.TabForTable"
class="rounded w-4 h-auto text-gray-400"
/>
</span>
@@ -48,13 +48,13 @@ import { NEllipsis } from "naive-ui";
import scrollIntoView from "scroll-into-view-if-needed";
import { computed, nextTick, ref, watch } from "vue";
import {
- useSchemaDesignerContext,
+ useSchemaEditorContext,
TabContext,
- SchemaDesignerTabType,
+ SchemaEditorTabType,
} from "./common";
-import { isTableChanged } from "./utils/table";
+import { isTableChanged } from "./utils";
-const { tabState, getCurrentTab, getTable } = useSchemaDesignerContext();
+const { tabState, getCurrentTab, getTable } = useSchemaEditorContext();
const tabsContainerRef = ref();
const tabList = computed(() => {
return Array.from(tabState.value.tabMap.values());
@@ -80,7 +80,7 @@ watch(
);
const getTabComputedClassList = (tab: TabContext) => {
- if (tab.type === SchemaDesignerTabType.TabForTable) {
+ if (tab.type === SchemaEditorTabType.TabForTable) {
const table = getTable(tab.schemaId, tab.tableId);
if (!table) {
return [];
@@ -100,7 +100,7 @@ const getTabComputedClassList = (tab: TabContext) => {
};
const getTabName = (tab: TabContext) => {
- if (tab.type === SchemaDesignerTabType.TabForTable) {
+ if (tab.type === SchemaEditorTabType.TabForTable) {
const table = getTable(tab.schemaId, tab.tableId);
return table.name || "Uknown tab";
} else {
diff --git a/frontend/src/components/SchemaDesigner/common/context.ts b/frontend/src/components/SchemaEditorV1/common/context.ts
similarity index 87%
rename from frontend/src/components/SchemaDesigner/common/context.ts
rename to frontend/src/components/SchemaEditorV1/common/context.ts
index ed01ada70f..c370a300ce 100644
--- a/frontend/src/components/SchemaDesigner/common/context.ts
+++ b/frontend/src/components/SchemaEditorV1/common/context.ts
@@ -1,22 +1,18 @@
import { head, isUndefined } from "lodash-es";
import { inject, InjectionKey, provide } from "vue";
-import {
- SchemaDesignerContext,
- SchemaDesignerTabType,
- TabContext,
-} from "./type";
+import { SchemaEditorContext, SchemaEditorTabType, TabContext } from "./type";
export const KEY = Symbol(
- "bb.schema-designer"
-) as InjectionKey<SchemaDesignerContext>;
+ "bb.schema-editor"
+) as InjectionKey<SchemaEditorContext>;
-export const useSchemaDesignerContext = () => {
+export const useSchemaEditorContext = () => {
return inject(KEY)!;
};
-export const provideSchemaDesignerContext = (
+export const provideSchemaEditorContext = (
context: Pick<
- SchemaDesignerContext,
+ SchemaEditorContext,
| "readonly"
| "engine"
| "baselineMetadata"
@@ -47,7 +43,7 @@ export const provideSchemaDesignerContext = (
}
if (
- item.type === SchemaDesignerTabType.TabForTable &&
+ item.type === SchemaEditorTabType.TabForTable &&
item.schemaId === tab.schemaId &&
item.tableId === tab.tableId
) {
@@ -89,7 +85,7 @@ export const provideSchemaDesignerContext = (
const tabList = Array.from(tabState.value.tabMap.values());
for (const tab of tabList) {
if (
- tab.type === SchemaDesignerTabType.TabForTable &&
+ tab.type === SchemaEditorTabType.TabForTable &&
tab.schemaId === schemaId &&
tab.tableId === tableId
) {
diff --git a/frontend/src/components/SchemaDesigner/common/index.ts b/frontend/src/components/SchemaEditorV1/common/index.ts
similarity index 100%
rename from frontend/src/components/SchemaDesigner/common/index.ts
rename to frontend/src/components/SchemaEditorV1/common/index.ts
diff --git a/frontend/src/components/SchemaDesigner/common/type.ts b/frontend/src/components/SchemaEditorV1/common/type.ts
similarity index 83%
rename from frontend/src/components/SchemaDesigner/common/type.ts
rename to frontend/src/components/SchemaEditorV1/common/type.ts
index b2d508327f..e4706663b3 100644
--- a/frontend/src/components/SchemaDesigner/common/type.ts
+++ b/frontend/src/components/SchemaEditorV1/common/type.ts
@@ -3,14 +3,14 @@ import { Schema, Table, ComposedProject } from "@/types";
import { Engine } from "@/types/proto/v1/common";
import { DatabaseMetadata } from "@/types/proto/v1/database_service";
-export enum SchemaDesignerTabType {
+export enum SchemaEditorTabType {
TabForTable = "table",
}
// Tab context for editing table.
export interface TableTabContext {
id: string;
- type: SchemaDesignerTabType.TabForTable;
+ type: SchemaEditorTabType.TabForTable;
schemaId: string;
tableId: string;
selectedSubtab?: string;
@@ -18,17 +18,17 @@ export interface TableTabContext {
export type TabContext = TableTabContext;
-export interface SchemaDesignerTabState {
+export interface SchemaEditorTabState {
tabMap: Map<string, TabContext>;
currentTabId?: string;
}
-export interface SchemaDesignerContext {
+export interface SchemaEditorContext {
readonly: Ref<boolean>;
baselineMetadata: Ref<DatabaseMetadata>;
engine: Ref<Engine>;
metadata: Ref<DatabaseMetadata>;
- tabState: Ref<SchemaDesignerTabState>;
+ tabState: Ref<SchemaEditorTabState>;
originalSchemas: Ref<Schema[]>;
editableSchemas: Ref<Schema[]>;
project: Ref<ComposedProject>;
diff --git a/frontend/src/components/SchemaDesigner/index.vue b/frontend/src/components/SchemaEditorV1/index.vue
similarity index 93%
rename from frontend/src/components/SchemaDesigner/index.vue
rename to frontend/src/components/SchemaEditorV1/index.vue
index ef70ec65bd..5a739c3197 100644
--- a/frontend/src/components/SchemaDesigner/index.vue
+++ b/frontend/src/components/SchemaEditorV1/index.vue
@@ -29,9 +29,8 @@ import { DatabaseMetadata } from "@/types/proto/v1/database_service";
import { SchemaDesign } from "@/types/proto/v1/schema_design_service";
import AsidePanel from "./AsidePanel.vue";
import Designer from "./Designer.vue";
-import { provideSchemaDesignerContext } from "./common";
-import { SchemaDesignerTabState } from "./common/type";
-import { rebuildEditableSchemas } from "./common/util";
+import { provideSchemaEditorContext, SchemaEditorTabState } from "./common";
+import { rebuildEditableSchemas } from "./utils";
const props = defineProps<{
readonly: boolean;
@@ -50,7 +49,7 @@ const project = ref<ComposedProject>(unknownProject());
const baselineMetadata = ref<DatabaseMetadata>(
DatabaseMetadata.fromPartial({})
);
-const tabState = ref<SchemaDesignerTabState>({
+const tabState = ref<SchemaEditorTabState>({
tabMap: new Map(),
});
@@ -72,7 +71,7 @@ const rebuildEditingState = () => {
};
};
-provideSchemaDesignerContext({
+provideSchemaEditorContext({
readonly: readonly,
baselineMetadata: baselineMetadata,
engine: engine,
diff --git a/frontend/src/components/SchemaDesigner/utils/column.ts b/frontend/src/components/SchemaEditorV1/utils/column.ts
similarity index 93%
rename from frontend/src/components/SchemaDesigner/utils/column.ts
rename to frontend/src/components/SchemaEditorV1/utils/column.ts
index 5683fb6f3e..dc3662aaa7 100644
--- a/frontend/src/components/SchemaDesigner/utils/column.ts
+++ b/frontend/src/components/SchemaEditorV1/utils/column.ts
@@ -1,12 +1,12 @@
import { isEqual, isUndefined } from "lodash-es";
-import { useSchemaDesignerContext } from "../common";
+import { useSchemaEditorContext } from "../common";
export const isColumnChanged = (
schemaId: string,
tableId: string,
columnId: string
): boolean => {
- const { originalSchemas, editableSchemas } = useSchemaDesignerContext();
+ const { originalSchemas, editableSchemas } = useSchemaEditorContext();
const originSchema = originalSchemas.value.find(
(schema) => schema.id === schemaId
);
diff --git a/frontend/src/components/SchemaEditorV1/utils/index.ts b/frontend/src/components/SchemaEditorV1/utils/index.ts
new file mode 100644
index 0000000000..a78d42ede9
--- /dev/null
+++ b/frontend/src/components/SchemaEditorV1/utils/index.ts
@@ -0,0 +1,4 @@
+export * from "./metadata";
+export * from "./schema";
+export * from "./table";
+export * from "./column";
diff --git a/frontend/src/components/SchemaEditorV1/utils/metadata.ts b/frontend/src/components/SchemaEditorV1/utils/metadata.ts
new file mode 100644
index 0000000000..19de58f75e
--- /dev/null
+++ b/frontend/src/components/SchemaEditorV1/utils/metadata.ts
@@ -0,0 +1,509 @@
+import { cloneDeep, isEqual, uniq } from "lodash-es";
+import {
+ Column,
+ ForeignKey,
+ Schema,
+ Table,
+ convertColumnMetadataToColumn,
+ convertSchemaMetadataToSchema,
+ convertTableMetadataToTable,
+} from "@/types";
+import {
+ ColumnMetadata,
+ DatabaseMetadata,
+ ForeignKeyMetadata,
+ IndexMetadata,
+ SchemaMetadata,
+ TableMetadata,
+} from "@/types/proto/v1/database_service";
+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));
+ continue;
+ } 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
+ );
+ }
+ }
+ }
+
+ // 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);
+ if (!schemaEdit) {
+ metadata.schemas = metadata.schemas.filter(
+ (item) => item.name !== schema.name
+ );
+ }
+ }
+
+ 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;
+ }
+
+ const foreignKeyList: ForeignKey[] = [];
+ for (const table of schema.tables) {
+ const editableTable = editableSchema.tableList.find(
+ (item) => item.name === table.name
+ );
+ if (!editableTable) {
+ continue;
+ }
+
+ 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);
+ }
+ }
+ editableSchema.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/SchemaDesigner/utils/schema.ts b/frontend/src/components/SchemaEditorV1/utils/schema.ts
similarity index 71%
rename from frontend/src/components/SchemaDesigner/utils/schema.ts
rename to frontend/src/components/SchemaEditorV1/utils/schema.ts
index 42e70e975e..6ad1ccedb7 100644
--- a/frontend/src/components/SchemaDesigner/utils/schema.ts
+++ b/frontend/src/components/SchemaEditorV1/utils/schema.ts
@@ -1,8 +1,8 @@
import { isEqual } from "lodash-es";
-import { useSchemaDesignerContext } from "../common";
+import { useSchemaEditorContext } from "../common";
export const isSchemaChanged = (schemaId: string): boolean => {
- const { originalSchemas, editableSchemas } = useSchemaDesignerContext();
+ const { originalSchemas, editableSchemas } = useSchemaEditorContext();
const originSchema = originalSchemas.value.find(
(schema) => schema.id === schemaId
);
diff --git a/frontend/src/components/SchemaDesigner/utils/table.ts b/frontend/src/components/SchemaEditorV1/utils/table.ts
similarity index 85%
rename from frontend/src/components/SchemaDesigner/utils/table.ts
rename to frontend/src/components/SchemaEditorV1/utils/table.ts
index d5b6b0d8e9..1afe93af3d 100644
--- a/frontend/src/components/SchemaDesigner/utils/table.ts
+++ b/frontend/src/components/SchemaEditorV1/utils/table.ts
@@ -1,8 +1,8 @@
import { isEqual } from "lodash-es";
-import { useSchemaDesignerContext } from "../common";
+import { useSchemaEditorContext } from "../common";
export const isTableChanged = (schemaId: string, tableId: string): boolean => {
- const { originalSchemas, editableSchemas } = useSchemaDesignerContext();
+ const { originalSchemas, editableSchemas } = useSchemaEditorContext();
const originSchema = originalSchemas.value.find(
(schema) => schema.id === schemaId
);