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: tweak index name for mysql&tidb (#13629)
boojacke3905d3fdfc7
diff --git a/frontend/src/components/SchemaEditorLite/Aside/Tree.vue b/frontend/src/components/SchemaEditorLite/Aside/Tree.vueindex 43fc835240..8d0f118041 100644--- a/frontend/src/components/SchemaEditorLite/Aside/Tree.vue+++ b/frontend/src/components/SchemaEditorLite/Aside/Tree.vue@@ -128,7 +128,11 @@ import type {TableMetadata,ViewMetadata,} from "@/types/proto/v1/database_service";-import { getHighlightHTMLByKeyWords, isDescendantOf } from "@/utils";+import {+ getFixedPrimaryKey,+ getHighlightHTMLByKeyWords,+ isDescendantOf,+} from "@/utils";import FunctionNameModal from "../Modals/FunctionNameModal.vue";import ProcedureNameModal from "../Modals/ProcedureNameModal.vue";import SchemaNameModal from "../Modals/SchemaNameModal.vue";@@ -609,6 +613,7 @@ const renderSuffix = ({ option }: { option: TreeOption }) => {const handleDuplicateTable = (treeNode: TreeNodeForTable) => {const { db } = treeNode;+ const engine = db.instanceResource.engine;const { schema, table } = treeNode.metadata;const flattenTableList = treeNode.metadata.database.schemas.flatMap((schema) => schema.tables@@ -654,7 +659,15 @@ const handleDuplicateTable = (treeNode: TreeNodeForTable) => {newTable.name = getDuplicateName(targetName);// As index names should be unique, we need to generate new names for them.for (const index of newTable.indexes) {- index.name = `${index.name}_${MD5(`${newTable.name}_${Date.now()}`).toString().slice(0, 6)}`;+ let name = `${index.name}_${MD5(`${newTable.name}_${Date.now()}`).toString().slice(0, 6)}`;+ if (index.primary) {+ const fixedName = getFixedPrimaryKey(engine);+ // If the primary key name is fixed, use it instead.+ if (fixedName) {+ name = fixedName;+ }+ }+ index.name = name;}schema.tables.push(newTable);markEditStatus(diff --git a/frontend/src/components/SchemaEditorLite/Modals/TableNameModal.vue b/frontend/src/components/SchemaEditorLite/Modals/TableNameModal.vueindex 548fa8ad36..8ef02026a8 100644--- a/frontend/src/components/SchemaEditorLite/Modals/TableNameModal.vue+++ b/frontend/src/components/SchemaEditorLite/Modals/TableNameModal.vue@@ -122,7 +122,7 @@ const handleConfirmButtonClick = async () => {column.type = engine === Engine.POSTGRES ? "integer" : "int";column.comment = "";table.columns.push(column);- upsertColumnPrimaryKey(table, column.name);+ upsertColumnPrimaryKey(engine, table, column.name);markEditStatus(props.database,{diff --git a/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue b/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vueindex f42b41f2c3..ff59eee4e2 100644--- a/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue+++ b/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue@@ -376,7 +376,7 @@ const disableAlterColumn = (column: ColumnMetadata): boolean => {const setColumnPrimaryKey = (column: ColumnMetadata, isPrimaryKey: boolean) => {if (isPrimaryKey) {column.nullable = false;- upsertColumnPrimaryKey(props.table, column.name);+ upsertColumnPrimaryKey(engine.value, props.table, column.name);} else {removeColumnPrimaryKey(props.table, column.name);}diff --git a/frontend/src/components/SchemaEditorLite/edit.ts b/frontend/src/components/SchemaEditorLite/edit.tsindex c5d674b7b1..f1a93e56cc 100644--- a/frontend/src/components/SchemaEditorLite/edit.ts+++ b/frontend/src/components/SchemaEditorLite/edit.ts@@ -1,23 +1,29 @@import { MD5 } from "crypto-js";import { pull, pullAt } from "lodash-es";+import { Engine } from "@/types/proto/v1/common";import type {ForeignKeyMetadata,TableMetadata,} from "@/types/proto/v1/database_service";import { IndexMetadata } from "@/types/proto/v1/database_service";-import { upsertArray } from "@/utils";+import { getFixedPrimaryKey, upsertArray } from "@/utils";export const upsertColumnPrimaryKey = (+ engine: Engine,table: TableMetadata,columnName: string) => {const pkIndex = table.indexes.findIndex((idx) => idx.primary);if (pkIndex < 0) {- // Generate a unique pk name.- // Format: table_pk_{md5(table_pk_timestamp).slice(0, 6)}, e.g. test_pk_d4402d- const nameParts: string[] = [table.name, "pk"];- const rawName = nameParts.join("_").toLowerCase();- const name = `${rawName}_${MD5(`${rawName}_${Date.now()}`).toString().slice(0, 6)}`;+ let name = getFixedPrimaryKey(engine);+ // If no fixed primary key, generate a unique name.+ if (!name) {+ // For Postgres, constraint name must be unique within the schema.+ // Format: table_pk_{md5(table_pk_timestamp).slice(0, 6)}, e.g. test_pk_d4402d+ const nameParts: string[] = [table.name, "pk"];+ const rawName = nameParts.join("_").toLowerCase();+ name = `${rawName}_${MD5(`${rawName}_${Date.now()}`).toString().slice(0, 6)}`;+ }table.indexes.push(IndexMetadata.fromPartial({name,diff --git a/frontend/src/components/SchemaTemplate/TableTemplateForm.vue b/frontend/src/components/SchemaTemplate/TableTemplateForm.vueindex e7622bfe63..1b39ee738b 100644--- a/frontend/src/components/SchemaTemplate/TableTemplateForm.vue+++ b/frontend/src/components/SchemaTemplate/TableTemplateForm.vue@@ -445,7 +445,11 @@ const handleDropColumn = (column: ColumnMetadata) => {const setColumnPrimaryKey = (column: ColumnMetadata, isPrimaryKey: boolean) => {if (isPrimaryKey) {column.nullable = false;- upsertColumnPrimaryKey(editing.value.table, column.name);+ upsertColumnPrimaryKey(+ editing.value.engine,+ editing.value.table,+ column.name+ );} else {removeColumnPrimaryKey(editing.value.table, column.name);}diff --git a/frontend/src/utils/v1/instance.ts b/frontend/src/utils/v1/instance.tsindex 0bf499de21..717be4cb92 100644--- a/frontend/src/utils/v1/instance.ts+++ b/frontend/src/utils/v1/instance.ts@@ -369,3 +369,11 @@ export const isValidSpannerHost = (host: string) => {/^projects\/(?<PROJECT_ID>(?:[a-z]|[-.:]|[0-9])+)\/instances\/(?<INSTANCE_ID>(?:[a-z]|[-]|[0-9])+)$/;return RE.test(host);};++export const getFixedPrimaryKey = (engine: Engine) => {+ // For MySQL and TiDB, the name of a primary key is always PRIMARY.+ if ([Engine.MYSQL, Engine.TIDB].includes(engine)) {+ return "PRIMARY";+ }+ return undefined;+};