Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: schema editor preview for new table (#20736)
ecmadao4a34f5c056a5
diff --git a/frontend/src/react/components/SchemaEditorLite/core/refreshEditStatus.test.ts b/frontend/src/react/components/SchemaEditorLite/core/refreshEditStatus.test.tsnew file mode 100644index 0000000000..4cc05667a9--- /dev/null+++ b/frontend/src/react/components/SchemaEditorLite/core/refreshEditStatus.test.ts@@ -0,0 +1,53 @@+import { create } from "@bufbuild/protobuf";+import { describe, expect, test, vi } from "vitest";+import type { Database } from "@/types/proto-es/v1/database_service_pb";+import {+ ColumnMetadataSchema,+ DatabaseMetadataSchema,+ SchemaMetadataSchema,+ TableMetadataSchema,+} from "@/types/proto-es/v1/database_service_pb";+import type { EditStatusContext } from "../types";+import { refreshTableEditStatus } from "./refreshEditStatus";++describe("refreshTableEditStatus", () => {+ test("preserves created table status while notifying metadata changes", () => {+ const db = { name: "instances/test/databases/db" } as Database;+ const baselineMetadata = create(DatabaseMetadataSchema, {+ name: "db",+ schemas: [+ create(SchemaMetadataSchema, {+ name: "public",+ tables: [],+ }),+ ],+ });+ const column = create(ColumnMetadataSchema, {+ name: "id",+ type: "bigint",+ });+ const table = create(TableMetadataSchema, {+ name: "users",+ columns: [column],+ });+ const schema = create(SchemaMetadataSchema, {+ name: "public",+ tables: [table],+ });+ const editStatus = {+ getEditStatusByKey: vi.fn(() => "created"),+ markEditStatus: vi.fn(),+ removeEditStatus: vi.fn(),+ getColumnStatus: vi.fn(() => "created"),+ } as unknown as EditStatusContext;++ refreshTableEditStatus(editStatus, db, baselineMetadata, schema, table);++ expect(editStatus.markEditStatus).toHaveBeenCalledWith(+ db,+ { schema, table },+ "created"+ );+ expect(editStatus.removeEditStatus).not.toHaveBeenCalled();+ });+});diff --git a/frontend/src/react/components/SchemaEditorLite/core/refreshEditStatus.ts b/frontend/src/react/components/SchemaEditorLite/core/refreshEditStatus.tsindex 81f9f29089..9d2c5c9a7c 100644--- a/frontend/src/react/components/SchemaEditorLite/core/refreshEditStatus.ts+++ b/frontend/src/react/components/SchemaEditorLite/core/refreshEditStatus.ts@@ -92,66 +92,69 @@ export function refreshTableEditStatus(const ownStatus = editStatus.getEditStatusByKey(keyForResource(db, { schema, table }));- if (ownStatus !== "created" && ownStatus !== "dropped") {- const baselineTable = baselineMetadata.schemas- .find((s) => s.name === schema.name)- ?.tables.find((t) => t.name === table.name);+ if (ownStatus === "created" || ownStatus === "dropped") {+ editStatus.markEditStatus(db, { schema, table }, ownStatus);+ return;+ }- // The table's own status reflects its scalar fields plus indexes, foreign- // keys, and partitions. Index/FK/partition edits are all tracked on the- // table key (see TableEditor's add handlers), so partitions must be- // compared here too — otherwise a reverted column/comment edit could clear- // the only marker for an added partition. Column edits surface separately- // via the per-column statuses below.- const ownChanged =- !baselineTable ||- !isEqual(- pick(table, ComparableTableFields),- pick(baselineTable, ComparableTableFields)- ) ||- !isEqualForeignKeys(baselineTable.foreignKeys, table.foreignKeys) ||- !isEqualIndexes(baselineTable.indexes, table.indexes) ||- !isEqualPartitions(baselineTable.partitions, table.partitions);- if (ownChanged) {- editStatus.markEditStatus(db, { schema, table }, "updated");- } else {- // Non-recursive: clear only the table's own key so the per-column- // statuses (set below) survive.- editStatus.removeEditStatus(db, { schema, table }, false);- }+ const baselineTable = baselineMetadata.schemas+ .find((s) => s.name === schema.name)+ ?.tables.find((t) => t.name === table.name);- // Edit-status keys are column-name based, so two columns sharing a name- // (an in-progress rename collision) map to the same key. Treat any such- // duplicate as changed and never clear its key — otherwise the unchanged- // twin could remove the dirty marker the renamed twin just set, leaving- // the editor wrongly clean while the metadata actually differs.- const nameCounts = new Map<string, number>();- for (const column of table.columns) {- nameCounts.set(column.name, (nameCounts.get(column.name) ?? 0) + 1);- }+ // The table's own status reflects its scalar fields plus indexes, foreign+ // keys, and partitions. Index/FK/partition edits are all tracked on the+ // table key (see TableEditor's add handlers), so partitions must be+ // compared here too — otherwise a reverted column/comment edit could clear+ // the only marker for an added partition. Column edits surface separately+ // via the per-column statuses below.+ const ownChanged =+ !baselineTable ||+ !isEqual(+ pick(table, ComparableTableFields),+ pick(baselineTable, ComparableTableFields)+ ) ||+ !isEqualForeignKeys(baselineTable.foreignKeys, table.foreignKeys) ||+ !isEqualIndexes(baselineTable.indexes, table.indexes) ||+ !isEqualPartitions(baselineTable.partitions, table.partitions);+ if (ownChanged) {+ editStatus.markEditStatus(db, { schema, table }, "updated");+ } else {+ // Non-recursive: clear only the table's own key so the per-column+ // statuses (set below) survive.+ editStatus.removeEditStatus(db, { schema, table }, false);+ }- for (const column of table.columns) {- const columnStatus = editStatus.getColumnStatus(db, {- schema,- table,- column,- });- if (columnStatus === "created" || columnStatus === "dropped") continue;- const baselineColumn = baselineTable?.columns.find(- (c) => c.name === column.name+ // Edit-status keys are column-name based, so two columns sharing a name+ // (an in-progress rename collision) map to the same key. Treat any such+ // duplicate as changed and never clear its key — otherwise the unchanged+ // twin could remove the dirty marker the renamed twin just set, leaving+ // the editor wrongly clean while the metadata actually differs.+ const nameCounts = new Map<string, number>();+ for (const column of table.columns) {+ nameCounts.set(column.name, (nameCounts.get(column.name) ?? 0) + 1);+ }++ for (const column of table.columns) {+ const columnStatus = editStatus.getColumnStatus(db, {+ schema,+ table,+ column,+ });+ if (columnStatus === "created" || columnStatus === "dropped") continue;+ const baselineColumn = baselineTable?.columns.find(+ (c) => c.name === column.name+ );+ const changed =+ (nameCounts.get(column.name) ?? 0) > 1 ||+ !baselineColumn ||+ !isEqual(+ pick(column, ComparableColumnFields),+ pick(baselineColumn, ComparableColumnFields));- const changed =- (nameCounts.get(column.name) ?? 0) > 1 ||- !baselineColumn ||- !isEqual(- pick(column, ComparableColumnFields),- pick(baselineColumn, ComparableColumnFields)- );- if (changed) {- editStatus.markEditStatus(db, { schema, table, column }, "updated");- } else {- editStatus.removeEditStatus(db, { schema, table, column }, false);- }+ if (changed) {+ editStatus.markEditStatus(db, { schema, table, column }, "updated");+ } else {+ editStatus.removeEditStatus(db, { schema, table, column }, false);}}}