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: update schema editor pk/fk (#4018)
boojack92b8d4f73898
diff --git a/frontend/src/components/SchemaEditor/Modals/TableNameModal.vue b/frontend/src/components/SchemaEditor/Modals/TableNameModal.vueindex 426bc2e5a1..f6a9007c5a 100644--- a/frontend/src/components/SchemaEditor/Modals/TableNameModal.vue+++ b/frontend/src/components/SchemaEditor/Modals/TableNameModal.vue@@ -152,7 +152,7 @@ const handleConfirmButtonClick = async () => {// Update reference objects.const tab = editorStore.findTab(databaseId,- table.newName+ props.tableName) as TableTabContext;if (tab) {tab.tableName = table.newName;@@ -163,6 +163,11 @@ const handleConfirmButtonClick = async () => {if (primaryKey) {primaryKey.table = table.newName;}+ for (const fk of schema.foreignKeyList) {+ if (fk.table === props.tableName) {+ fk.table = table.newName;+ }+ }}dismissModal();}diff --git a/frontend/src/components/SchemaEditor/Panels/TableEditor.vue b/frontend/src/components/SchemaEditor/Panels/TableEditor.vueindex 4f083d79fa..9a47a08724 100644--- a/frontend/src/components/SchemaEditor/Panels/TableEditor.vue+++ b/frontend/src/components/SchemaEditor/Panels/TableEditor.vue@@ -148,7 +148,9 @@<BBCheckboxclass="ml-3":value="!column.nullable"- :disabled="disableAlterColumn(column)"+ :disabled="+ disableAlterColumn(column) || isColumnPrimaryKey(column)+ "@toggle="(value) => (column.nullable = !value)"/></div>@@ -260,7 +262,6 @@ import {Table,Schema,convertColumnMetadataToColumn,- PrimaryKey,ForeignKey,} from "@/types/schemaEditor/atomType";import { getDataTypeSuggestionList } from "@/utils";@@ -299,10 +300,11 @@ const schema = computed(() => {(schema) => schema.name === currentTab.schemaName) as Schema;});-const primaryKey = computed(() => {- return schema.value.primaryKeyList.find(- (pk) => pk.table === currentTab.tableName- ) as PrimaryKey;+const primaryKeyColumnList = computed(() => {+ return (+ schema.value.primaryKeyList.find((pk) => pk.table === currentTab.tableName)+ ?.columnList || []+ );});const foreignKeyList = computed(() => {return schema.value.foreignKeyList.filter(@@ -434,7 +436,7 @@ watch();const isColumnPrimaryKey = (column: Column): boolean => {- for (const columnRef of primaryKey.value.columnList) {+ for (const columnRef of primaryKeyColumnList.value) {if (columnRef.value === column) {return true;}@@ -484,12 +486,17 @@ const disableAlterColumn = (column: Column): boolean => {const setColumnPrimaryKey = (column: Column, isPrimaryKey: boolean) => {if (isPrimaryKey) {- primaryKey.value?.columnList.push(ref(column));+ column.nullable = false;+ primaryKeyColumnList.value.push(ref(column));} else {- const index = primaryKey.value?.columnList.findIndex(- (columnRef) => columnRef.value === column+ const primaryKey = schema.value.primaryKeyList.find(+ (pk) => pk.table === currentTab.tableName);- primaryKey.value?.columnList.splice(index, 1);+ if (primaryKey) {+ primaryKey.columnList = primaryKey.columnList.filter(+ (columnRef) => columnRef.value !== column+ );+ }}};@@ -524,9 +531,26 @@ const handleDropColumn = (column: Column) => {table.value.columnList = table.value.columnList.filter((item) => item !== column);- primaryKey.value.columnList = primaryKey.value.columnList.filter(- (item) => item.value !== column+ const primaryKey = schema.value.primaryKeyList.find(+ (pk) => pk.table === currentTab.tableName);+ if (primaryKey) {+ primaryKey.columnList = primaryKey.columnList.filter(+ (item) => item.value !== column+ );+ }+ const foreignKeyList = schema.value.foreignKeyList.filter(+ (fk) => fk.table === currentTab.tableName+ );+ for (const foreignKey of foreignKeyList) {+ const columnRefIndex = foreignKey.columnList.findIndex(+ (columnRef) => columnRef.value === column+ );+ if (columnRefIndex > -1) {+ foreignKey.columnList.splice(columnRefIndex, 1);+ foreignKey.referencedColumns.splice(columnRefIndex, 1);+ }+ }} else {column.status = "dropped";}@@ -555,31 +579,38 @@ const handleDiscardChanges = () => {table.value.oldName);+ const previousName = table.value.newName;table.value.newName = table.value.oldName;table.value.columnList = cloneDeep(originTable?.columnList ?? []);table.value.status = "normal";// TODO(steven): use reactive Ref.// Update reference objects.- currentTab.tableName = table.value.newName;- primaryKey.value.table = table.value.newName;- primaryKey.value.columnList = [];- const primaryKeyColumnNameList = (- originSchema?.primaryKeyList.find((pk) => pk.table === table.value.oldName)- ?.columnList || []- ).map((columnRef) => columnRef.value.oldName);- for (const column of table.value.columnList) {- if (primaryKeyColumnNameList.includes(column.oldName)) {- primaryKey.value.columnList.push(ref(column));+ currentTab.tableName = table.value.oldName;+ const primaryKey = schema.value.primaryKeyList.find(+ (pk) => pk.table === previousName+ );+ if (primaryKey) {+ const originPrimaryKeyColumnNameList = (+ originSchema?.primaryKeyList.find(+ (pk) => pk.table === table.value.oldName+ )?.columnList || []+ ).map((columnRef) => columnRef.value.oldName);+ primaryKey.table = table.value.oldName;+ primaryKey.columnList = [];+ for (const column of table.value.columnList) {+ if (originPrimaryKeyColumnNameList.includes(column.oldName)) {+ primaryKey.columnList.push(ref(column));+ }}}schema.value.foreignKeyList = schema.value.foreignKeyList.filter(- (fk) => fk.table !== table.value.newName+ (fk) => fk.table !== table.value.oldName);const originForeignKeyList =originSchema?.foreignKeyList.filter(- (fk) => fk.table === table.value.newName+ (fk) => fk.table === table.value.oldName) || [];for (const originForeignKey of originForeignKeyList) {const tempForeignKey: ForeignKey = {diff --git a/frontend/src/components/SchemaEditor/utils/table.ts b/frontend/src/components/SchemaEditor/utils/table.tsindex fcb2696114..ba960b2269 100644--- a/frontend/src/components/SchemaEditor/utils/table.ts+++ b/frontend/src/components/SchemaEditor/utils/table.ts@@ -57,14 +57,14 @@ export const isEqualForeignKey = () => {return isEqual({- ...originForeignKey,columnList: originForeignKey?.columnList.map((columnRef) => columnRef.value),+ referencedColumns: originForeignKey?.referencedColumns,},{- ...foreignKey,columnList: foreignKey?.columnList.map((columnRef) => columnRef.value),+ referencedColumns: foreignKey?.referencedColumns,});};diff --git a/frontend/src/utils/schemaEditor/diffSchema.ts b/frontend/src/utils/schemaEditor/diffSchema.tsindex 4731f47b88..52001fabe3 100644--- a/frontend/src/utils/schemaEditor/diffSchema.ts+++ b/frontend/src/utils/schemaEditor/diffSchema.ts@@ -62,7 +62,7 @@ export const diffSchema = (originSchema: Schema, schema: Schema) => {.map((columnRef) => columnRef.value).sort();const originForeignKeyList = originSchema.foreignKeyList.filter(- (fk) => fk.table === table.newName+ (fk) => fk.table === table.oldName);const foreignKeyList = schema.foreignKeyList.filter((fk) => fk.table === table.newName@@ -157,21 +157,34 @@ const composeForeignKeyWithAlterTableContext = (foreignKeyList: ForeignKey[],alterTableContext: AlterTableContext) => {- for (let i = 0; i < foreignKeyList.length; i++) {- const originForeignKey = originForeignKeyList[i];- const foreignKey = foreignKeyList[i];+ for (const foreignKey of foreignKeyList) {+ const originForeignKey =+ originForeignKeyList[foreignKeyList.findIndex((fk) => fk === foreignKey)];if (!isEqualForeignKey(originForeignKey, foreignKey)) {if (!isUndefined(originForeignKey)) {alterTableContext.dropForeignKeyList.push(originForeignKey.name);}- if (foreignKey.columnList.length > 0) {+ if (+ foreignKey.columnList.length > 0 &&+ foreignKey.columnList.length === foreignKey.referencedColumns.length+ ) {+ const columnList: string[] = [];+ const referencedColumnList: string[] = [];+ for (const column of foreignKey.columnList) {+ if (column.value.status !== "dropped") {+ columnList.push(column.value.newName);+ referencedColumnList.push(+ foreignKey.referencedColumns[+ foreignKey.columnList.findIndex((item) => item === column)+ ]+ );+ }+ }alterTableContext.addForeignKeyList.push({- columnList: foreignKey.columnList.map(- (columnRef) => columnRef.value.newName- ),referencedTable: foreignKey.referencedTable,- referencedColumnList: foreignKey.referencedColumns,+ columnList,+ referencedColumnList,});}}diff --git a/plugin/parser/edit/mysql/deparse.go b/plugin/parser/edit/mysql/deparse.goindex ac583dda49..ff78e61b85 100644--- a/plugin/parser/edit/mysql/deparse.go+++ b/plugin/parser/edit/mysql/deparse.go@@ -31,22 +31,22 @@ func (*SchemaEditor) DeparseDatabaseEdit(databaseEdit *api.DatabaseEdit) (stringstmtList = append(stmtList, stmt)}- for _, alterTableContext := range databaseEdit.AlterTableList {- alterTableStmt, err := transformAlterTableContext(alterTableContext)+ for _, renameTableContext := range databaseEdit.RenameTableList {+ renameTableStmt := transformRenameTableContext(renameTableContext)+ stmt, err := restoreASTNode(renameTableStmt)if err != nil {return "", err}- stmt, err := restoreASTNode(alterTableStmt)+ stmtList = append(stmtList, stmt)+ }+ for _, alterTableContext := range databaseEdit.AlterTableList {+ alterTableStmt, err := transformAlterTableContext(alterTableContext)if err != nil {return "", err}- stmtList = append(stmtList, stmt)- }- for _, renameTableContext := range databaseEdit.RenameTableList {- renameTableStmt := transformRenameTableContext(renameTableContext)- stmt, err := restoreASTNode(renameTableStmt)+ stmt, err := restoreASTNode(alterTableStmt)if err != nil {return "", err}diff --git a/plugin/parser/edit/mysql/deparse_test.go b/plugin/parser/edit/mysql/deparse_test.goindex e681040bf3..f349b8d4d9 100644--- a/plugin/parser/edit/mysql/deparse_test.go+++ b/plugin/parser/edit/mysql/deparse_test.go@@ -291,7 +291,7 @@ func TestDeparseAlterAndRenameTable(t *testing.T) {},},},- want: "ALTER TABLE `t1` ADD COLUMN (`id` INT NOT NULL, `id_card` INT NOT NULL);\n\nRENAME TABLE `t1` TO `t2`;\n",+ want: "RENAME TABLE `t1` TO `t2`;\n\nALTER TABLE `t1` ADD COLUMN (`id` INT NOT NULL, `id_card` INT NOT NULL);\n",},}