Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: remove unused functions (#8752)
boojackb8f5a05684c0
diff --git a/frontend/src/components/Branch/BranchCreateView.vue b/frontend/src/components/Branch/BranchCreateView.vueindex ff05b93635..6cb4516463 100644--- a/frontend/src/components/Branch/BranchCreateView.vue+++ b/frontend/src/components/Branch/BranchCreateView.vue@@ -66,6 +66,7 @@ import { computed, onMounted, reactive, ref, watch } from "vue";import { useI18n } from "vue-i18n";import { useRoute, useRouter } from "vue-router";import SchemaEditorV1 from "@/components/SchemaEditorV1/index.vue";+import { mergeSchemaEditToMetadata } from "@/components/SchemaEditorV1/utils";import {pushNotification,useChangeHistoryStore,@@ -97,7 +98,7 @@ import {} from "@/types/proto/v1/sheet_service";import { extractChangeHistoryUID } from "@/utils";import BaselineSchemaSelector from "./BaselineSchemaSelector.vue";-import { mergeSchemaEditToMetadata, validateBranchName } from "./utils";+import { validateBranchName } from "./utils";interface BaselineSchema {// The uid of database.diff --git a/frontend/src/components/Branch/BranchDetailView.vue b/frontend/src/components/Branch/BranchDetailView.vueindex 135934053d..300217354b 100644--- a/frontend/src/components/Branch/BranchDetailView.vue+++ b/frontend/src/components/Branch/BranchDetailView.vue@@ -112,6 +112,10 @@ import { CSSProperties, computed, reactive, ref, watch } from "vue";import { useI18n } from "vue-i18n";import { useRouter } from "vue-router";import DatabaseInfo from "@/components/DatabaseInfo.vue";+import {+ mergeSchemaEditToMetadata,+ validateDatabaseMetadata,+} from "@/components/SchemaEditorV1/utils";import {pushNotification,useChangeHistoryStore,@@ -129,12 +133,7 @@ import { provideSQLCheckContext } from "../SQLCheck";import { getBaselineMetadataOfBranch } from "../SchemaEditorV1/utils/branch";import MergeBranchPanel from "./MergeBranchPanel.vue";import SchemaDesignEditor from "./SchemaDesignEditor.vue";-import {- generateForkedBranchName,- mergeSchemaEditToMetadata,- validateDatabaseMetadata,- validateBranchName,-} from "./utils";+import { generateForkedBranchName, validateBranchName } from "./utils";interface LocalState {schemaDesignTitle: string;diff --git a/frontend/src/components/Branch/SchemaDesignSQLCheckButton.vue b/frontend/src/components/Branch/SchemaDesignSQLCheckButton.vueindex 715c1890ec..76caec5e36 100644--- a/frontend/src/components/Branch/SchemaDesignSQLCheckButton.vue+++ b/frontend/src/components/Branch/SchemaDesignSQLCheckButton.vue@@ -24,12 +24,15 @@ import { cloneDeep, debounce } from "lodash-es";import { computed, ref, watch } from "vue";import { useI18n } from "vue-i18n";import { SQLCheckButton, SQLCheckSummary } from "@/components/SQLCheck";+import {+ mergeSchemaEditToMetadata,+ validateDatabaseMetadata,+} from "@/components/SchemaEditorV1/utils";import { schemaDesignServiceClient } from "@/grpcweb";import { useDatabaseV1Store, useSchemaEditorV1Store } from "@/store";import { DatabaseMetadata } from "@/types/proto/v1/database_service";import { SchemaDesign } from "@/types/proto/v1/schema_design_service";import { getBaselineMetadataOfBranch } from "../SchemaEditorV1/utils/branch";-import { mergeSchemaEditToMetadata, validateDatabaseMetadata } from "./utils";const props = defineProps<{schemaDesign: SchemaDesign;diff --git a/frontend/src/components/Branch/utils.ts b/frontend/src/components/Branch/utils.tsindex 857ed33a2d..881cb5b1b2 100644--- a/frontend/src/components/Branch/utils.ts+++ b/frontend/src/components/Branch/utils.ts@@ -1,526 +1,5 @@-import { cloneDeep, isEqual, uniq } from "lodash-es";import { useCurrentUserV1 } from "@/store";-import {- ColumnMetadata,- DatabaseMetadata,- ForeignKeyMetadata,- IndexMetadata,- SchemaMetadata,- 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 = (- 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.hasDefault = columnEdit.hasDefault;- column.defaultNull = columnEdit.defaultNull;- column.defaultString = columnEdit.defaultString;- column.defaultExpression = columnEdit.defaultExpression;- }- }- 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;- }-- 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);- }- }- }-- 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,- hasDefault: column.hasDefault,- defaultNull: column.defaultNull,- defaultString: column.defaultString,- defaultExpression: column.defaultExpression,- 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;-};--export const transformTableEditToMetadata = (- tableEdit: Table-): TableMetadata => {- const tableMetadata = TableMetadata.fromPartial({- name: tableEdit.name,- comment: tableEdit.comment,- columns: [],- indexes: [],- foreignKeys: [],- classification: tableEdit.classification,- });-- for (const column of tableEdit.columnList) {- tableMetadata.columns.push(- ColumnMetadata.fromPartial({- name: column.name,- type: column.type,- nullable: column.nullable,- hasDefault: column.hasDefault,- defaultNull: column.defaultNull,- defaultString: column.defaultString,- defaultExpression: column.defaultExpression,- 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,- hasDefault: columnEdit.hasDefault,- defaultNull: columnEdit.defaultNull,- defaultString: columnEdit.defaultString,- defaultExpression: columnEdit.defaultExpression,- 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.hasDefault = column.hasDefault;- editableColumn.defaultNull = column.defaultNull;- editableColumn.defaultString = column.defaultString;- editableColumn.defaultExpression = column.defaultExpression;- }-- for (const column of table.columns) {- const editableColumn = editableTable.columnList.find(- (item) => item.name === column.name- );- if (!editableColumn) {- const newColumn = convertColumnMetadataToColumn(column, "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, "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, "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);-};export const generateForkedBranchName = (branch: SchemaDesign): string => {const currentUser = useCurrentUserV1();diff --git a/frontend/src/components/SchemaEditorV1/utils/branch.ts b/frontend/src/components/SchemaEditorV1/utils/branch.tsindex b0145b8277..0de08835a8 100644--- a/frontend/src/components/SchemaEditorV1/utils/branch.ts+++ b/frontend/src/components/SchemaEditorV1/utils/branch.ts@@ -8,7 +8,7 @@ import {BranchSchema,convertSchemaMetadataList,} from "@/types/v1/schemaEditor";-import { rebuildEditableSchemas } from "./metadataV1";+import { rebuildEditableSchemas } from "./metadata";export const convertBranchToBranchSchema = (branch: SchemaDesigndiff --git a/frontend/src/components/SchemaEditorV1/utils/metadata.ts b/frontend/src/components/SchemaEditorV1/utils/metadata.tsindex e7b0d46563..d0972a9210 100644--- a/frontend/src/components/SchemaEditorV1/utils/metadata.ts+++ b/frontend/src/components/SchemaEditorV1/utils/metadata.ts@@ -267,7 +267,9 @@ const transformSchemaEditToMetadata = (schemaEdit: Schema): SchemaMetadata => {return schemaMetadata;};-const transformTableEditToMetadata = (tableEdit: Table): TableMetadata => {+export const transformTableEditToMetadata = (+ tableEdit: Table+): TableMetadata => {const tableMetadata = TableMetadata.fromPartial({name: tableEdit.name,columns: [],diff --git a/frontend/src/components/SchemaEditorV1/utils/metadataV1.ts b/frontend/src/components/SchemaEditorV1/utils/metadataV1.tsdeleted file mode 100644index 960d2c2856..0000000000--- a/frontend/src/components/SchemaEditorV1/utils/metadataV1.ts+++ /dev/null@@ -1,507 +0,0 @@-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: [],- comment: table.comment,- userComment: table.userComment,- classification: table.classification,- });-- 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: [],- comment: tableEdit.comment,- userComment: tableEdit.userComment,- classification: tableEdit.classification,- });-- 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, "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, "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, "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/SchemaTemplate/TableTemplateForm.vue b/frontend/src/components/SchemaTemplate/TableTemplateForm.vueindex fc7d6101a3..f5fbc0d9a1 100644--- a/frontend/src/components/SchemaTemplate/TableTemplateForm.vue+++ b/frontend/src/components/SchemaTemplate/TableTemplateForm.vue@@ -211,7 +211,7 @@<script lang="ts" setup>import { isEqual, cloneDeep } from "lodash-es";import { computed, nextTick, reactive } from "vue";-import { transformTableEditToMetadata } from "@/components/Branch/utils";+import { transformTableEditToMetadata } from "@/components/SchemaEditorV1/utils";import { Drawer, DrawerContent } from "@/components/v2";import { useSettingV1Store } from "@/store";import { Engine } from "@/types/proto/v1/common";