Skip to content

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

fix: diff checks in editing schema (#8869)

boojack86662c7b1900
diff --git a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
index da23e7b426..9f8f9dea4e 100644
--- a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
@@ -163,6 +163,7 @@ import { allowGhostMigrationV1 } from "@/utils";
import MonacoEditor from "../MonacoEditor";
import { provideSQLCheckContext } from "../SQLCheck";
import {
+ initialSchemaConfigToMetadata,
mergeSchemaEditToMetadata,
validateDatabaseMetadata,
} from "../SchemaEditorV1/utils";
@@ -324,7 +325,13 @@ const getChangedDatabaseMetadatas = () => {
databaseSchema.schemaList,
cloneDeep(metadata)
);
- if (isEqual(metadata, mergedMetadata)) {
+ // Initial an empty schema config to origin metadata to prevent unexpected diff.
+ initialSchemaConfigToMetadata(metadata);
+ if (
+ // If there is no schema change, we don't need to create an issue.
+ databaseSchema.schemaList.length === 0 ||
+ isEqual(metadata, mergedMetadata)
+ ) {
continue;
}
diff --git a/frontend/src/components/SchemaEditorV1/utils/metadata.ts b/frontend/src/components/SchemaEditorV1/utils/metadata.ts
index 4808515ecd..2b3829783c 100644
--- a/frontend/src/components/SchemaEditorV1/utils/metadata.ts
+++ b/frontend/src/components/SchemaEditorV1/utils/metadata.ts
@@ -107,6 +107,8 @@ export const mergeSchemaEditToMetadata = (
}
}
if (tableEdit.primaryKey.columnIdList.length > 0) {
+ const hasPrimaryKey =
+ table.indexes.find((index) => index.primary) !== undefined;
const primaryIndex =
table.indexes.find((index) => index.primary) ||
IndexMetadata.fromPartial({
@@ -123,9 +125,8 @@ export const mergeSchemaEditToMetadata = (
primaryIndex.expressions.push(column.name);
}
}
- table.indexes = table.indexes.filter((index) => !index.primary);
primaryIndex.expressions = uniq(primaryIndex.expressions);
- if (primaryIndex.expressions.length > 0) {
+ if (!hasPrimaryKey) {
table.indexes.push(primaryIndex);
}
} else {
@@ -257,6 +258,24 @@ export const mergeSchemaEditToMetadata = (
return metadata;
};
+export const initialSchemaConfigToMetadata = (metadata: DatabaseMetadata) => {
+ metadata.schemaConfigs = metadata.schemas.map((schema) =>
+ SchemaConfig.fromPartial({
+ name: schema.name,
+ tableConfigs: schema.tables.map((table) =>
+ TableConfig.fromPartial({
+ name: table.name,
+ columnConfigs: table.columns.map((column) =>
+ ColumnConfig.fromPartial({
+ name: column.name,
+ })
+ ),
+ })
+ ),
+ })
+ );
+};
+
const transformSchemaEditToMetadata = (schemaEdit: Schema): SchemaMetadata => {
const schemaMetadata = SchemaMetadata.fromPartial({
name: schemaEdit.name,
@@ -423,9 +442,7 @@ export const rebuildEditableSchemas = (
}
// Rebuild primary key from primary index.
- const primaryIndex = table.indexes.find(
- (index) => index.primary === true
- );
+ const primaryIndex = table.indexes.find((index) => index.primary);
if (primaryIndex) {
editableTable.primaryKey.name = primaryIndex.name;
for (const columnName of primaryIndex.expressions) {