Skip to content

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

chore: add computed class list to schema designer (#7206)

boojack8f806b43777b
diff --git a/frontend/src/components/SchemaDesigner/DatabaseTree.vue b/frontend/src/components/SchemaDesigner/DatabaseTree.vue
index 184ee99395..283cfadbc1 100644
--- a/frontend/src/components/SchemaDesigner/DatabaseTree.vue
+++ b/frontend/src/components/SchemaDesigner/DatabaseTree.vue
@@ -78,6 +78,7 @@ import { Engine } from "@/types/proto/v1/common";
import { useSchemaDesignerContext, SchemaDesignerTabType } from "./common";
import { generateUniqueTabId } from "@/store";
import { getHighlightHTMLByKeyWords, isDescendantOf } from "@/utils";
+import { isTableChanged } from "./utils/table";
import SchemaNameModal from "./Modals/SchemaNameModal.vue";
import TableNameModal from "./Modals/TableNameModal.vue";
@@ -125,6 +126,7 @@ const {
editableSchemas,
tabState,
addTab,
+ getTable,
getCurrentTab,
dropTable,
} = useSchemaDesignerContext();
@@ -235,12 +237,20 @@ const contextMenuOptions = computed(() => {
return [];
}
+ const isDropped = table.status === "dropped";
const options = [];
- options.push({
- key: "drop",
- label: t("schema-editor.actions.drop-table"),
- disabled: readonly.value,
- });
+ if (isDropped) {
+ options.push({
+ key: "restore",
+ label: t("schema-editor.actions.restore"),
+ });
+ } else {
+ options.push({
+ key: "drop",
+ label: t("schema-editor.actions.drop-table"),
+ disabled: readonly.value,
+ });
+ }
return options;
}
@@ -316,6 +326,8 @@ const renderLabel = ({ option: treeNode }: { option: TreeNode }) => {
additionalClassList.push("text-green-700");
} else if (table.status === "dropped") {
additionalClassList.push("text-red-700 line-through");
+ } else if (isTableChanged(treeNode.schemaId, treeNode.tableId)) {
+ additionalClassList.push("text-yellow-700");
}
}
}
@@ -426,6 +438,12 @@ const handleContextMenuDropdownSelect = async (key: string) => {
} else if (treeNode.type === "table") {
if (key === "drop") {
dropTable(treeNode.schemaId, treeNode.tableId);
+ } else if (key === "restore") {
+ const table = getTable(treeNode.schemaId, treeNode.tableId);
+ if (!table) {
+ return;
+ }
+ table.status = "normal";
}
}
contextMenu.showDropdown = false;
diff --git a/frontend/src/components/SchemaDesigner/Modals/TableNameModal.vue b/frontend/src/components/SchemaDesigner/Modals/TableNameModal.vue
index ade567a5bd..be4e48cb62 100644
--- a/frontend/src/components/SchemaDesigner/Modals/TableNameModal.vue
+++ b/frontend/src/components/SchemaDesigner/Modals/TableNameModal.vue
@@ -102,6 +102,7 @@ const handleConfirmButtonClick = async () => {
}
column.comment = "ID";
const columnEdit = convertColumnMetadataToColumn(column);
+ columnEdit.status = "created";
const tableEdit = convertTableMetadataToTable(table);
tableEdit.status = "created";
tableEdit.columnList.push(columnEdit);
diff --git a/frontend/src/components/SchemaDesigner/Panels/TableEditor.vue b/frontend/src/components/SchemaDesigner/Panels/TableEditor.vue
index f752716fb6..9773588f6c 100644
--- a/frontend/src/components/SchemaDesigner/Panels/TableEditor.vue
+++ b/frontend/src/components/SchemaDesigner/Panels/TableEditor.vue
@@ -20,7 +20,7 @@
<NInput
v-if="state.selectedSubtab === 'column-list'"
v-model:value="searchPattern"
- class="!w-48 mr-3"
+ class="!w-48"
:placeholder="$t('schema-editor.search-column')"
>
<template #prefix>
@@ -284,6 +284,7 @@ import {
TableTabContext,
useSchemaDesignerContext,
} from "../common";
+import { isColumnChanged } from "../utils/column";
type SubtabType = "column-list" | "raw-sql";
@@ -387,6 +388,14 @@ const getColumnItemComputedClassList = (column: Column) => {
return ["text-red-700", "cursor-not-allowed", "!bg-red-50", "opacity-70"];
} else if (column.status === "created") {
return ["text-green-700", "!bg-green-50"];
+ } else if (
+ isColumnChanged(
+ currentTab.value.schemaId,
+ currentTab.value.tableId,
+ column.id
+ )
+ ) {
+ return ["text-yellow-700", "!bg-yellow-50"];
}
return [];
};
diff --git a/frontend/src/components/SchemaDesigner/TabsContainer.vue b/frontend/src/components/SchemaDesigner/TabsContainer.vue
index 98fb61af81..3c2a5f3897 100644
--- a/frontend/src/components/SchemaDesigner/TabsContainer.vue
+++ b/frontend/src/components/SchemaDesigner/TabsContainer.vue
@@ -52,6 +52,7 @@ import {
TabContext,
SchemaDesignerTabType,
} from "./common";
+import { isTableChanged } from "./utils/table";
const { tabState, getCurrentTab, getTable } = useSchemaDesignerContext();
const tabsContainerRef = ref();
@@ -79,6 +80,22 @@ watch(
);
const getTabComputedClassList = (tab: TabContext) => {
+ if (tab.type === SchemaDesignerTabType.TabForTable) {
+ const table = getTable(tab.schemaId, tab.tableId);
+ if (!table) {
+ return [];
+ }
+
+ if (table.status === "dropped") {
+ return ["text-red-700", "line-through"];
+ }
+ if (table.status === "created") {
+ return ["text-green-700"];
+ }
+ if (isTableChanged(tab.schemaId, tab.tableId)) {
+ return ["text-yellow-700"];
+ }
+ }
return [];
};
diff --git a/frontend/src/components/SchemaDesigner/common/type.ts b/frontend/src/components/SchemaDesigner/common/type.ts
index 42ebd35e2d..27ca231897 100644
--- a/frontend/src/components/SchemaDesigner/common/type.ts
+++ b/frontend/src/components/SchemaDesigner/common/type.ts
@@ -29,8 +29,7 @@ export interface SchemaDesignerContext {
engine: Ref<Engine>;
metadata: Ref<DatabaseMetadata>;
tabState: Ref<SchemaDesignerTabState>;
-
- originalSchemas: Schema[];
+ originalSchemas: Ref<Schema[]>;
editableSchemas: Ref<Schema[]>;
// Tab related functions.
diff --git a/frontend/src/components/SchemaDesigner/index.vue b/frontend/src/components/SchemaDesigner/index.vue
index 06cf2110c0..632bde9935 100644
--- a/frontend/src/components/SchemaDesigner/index.vue
+++ b/frontend/src/components/SchemaDesigner/index.vue
@@ -35,6 +35,7 @@ const props = defineProps<{
const readonly = ref(props.readonly);
const engine = ref(props.engine);
const metadata = ref<DatabaseMetadata>(DatabaseMetadata.fromPartial({}));
+const originalSchemas = ref<Schema[]>([]);
const editableSchemas = ref<Schema[]>([]);
const baselineMetadata = ref<DatabaseMetadata>(
DatabaseMetadata.fromPartial({})
@@ -49,7 +50,7 @@ provideSchemaDesignerContext({
engine: engine,
metadata: metadata,
tabState: tabState,
- originalSchemas: cloneDeep(editableSchemas.value),
+ originalSchemas: originalSchemas,
editableSchemas: editableSchemas,
});
@@ -63,6 +64,7 @@ watch(
cloneDeep(props.schemaDesign?.schemaMetadata) ||
DatabaseMetadata.fromPartial({});
editableSchemas.value = convertSchemaMetadataList(metadata.value.schemas);
+ originalSchemas.value = cloneDeep(editableSchemas.value);
readonly.value = props.readonly;
engine.value = props.engine;
// NOTE: clear tab state in the following cases:
diff --git a/frontend/src/components/SchemaDesigner/utils/column.ts b/frontend/src/components/SchemaDesigner/utils/column.ts
index 2c42a8cbb3..5683fb6f3e 100644
--- a/frontend/src/components/SchemaDesigner/utils/column.ts
+++ b/frontend/src/components/SchemaDesigner/utils/column.ts
@@ -1,4 +1,51 @@
-// TODO(steven): Implement this function.
-export const isColumnChanged = (): boolean => {
- return false;
+import { isEqual, isUndefined } from "lodash-es";
+import { useSchemaDesignerContext } from "../common";
+
+export const isColumnChanged = (
+ schemaId: string,
+ tableId: string,
+ columnId: string
+): boolean => {
+ const { originalSchemas, editableSchemas } = useSchemaDesignerContext();
+ const originSchema = originalSchemas.value.find(
+ (schema) => schema.id === schemaId
+ );
+ const schema = editableSchemas.value.find((schema) => schema.id === schemaId);
+ const table = schema?.tableList.find((table) => table.id === tableId);
+ const originTable = originSchema?.tableList.find(
+ (table) => table.id === tableId
+ );
+ const column = table?.columnList.find((column) => column.id === columnId);
+ const originColumn = originTable?.columnList.find(
+ (column) => column.id === columnId
+ );
+
+ const isPrimaryKey = table?.primaryKey.columnIdList.includes(columnId);
+ const isPrimaryKeyOrigin =
+ originTable?.primaryKey.columnIdList.includes(columnId);
+
+ const originForeignKey = originSchema?.foreignKeyList.find(
+ (fk) => fk.tableId === table?.id && fk.columnIdList.includes(columnId)
+ );
+ const foreignKey = schema?.foreignKeyList.find(
+ (fk) => fk.tableId === table?.id && fk.columnIdList.includes(columnId)
+ );
+ const originIndex = originForeignKey?.columnIdList.findIndex(
+ (column) => column === columnId
+ );
+ const originForeignKeyColumn = isUndefined(originIndex)
+ ? undefined
+ : originForeignKey?.referencedColumnIdList[originIndex];
+ const index = foreignKey?.columnIdList.findIndex(
+ (column) => column === columnId
+ );
+ const foreignKeyColumn = isUndefined(index)
+ ? undefined
+ : foreignKey?.referencedColumnIdList[index];
+
+ return (
+ !isEqual(column, originColumn) ||
+ !isEqual(isPrimaryKey, isPrimaryKeyOrigin) ||
+ !isEqual(foreignKeyColumn, originForeignKeyColumn)
+ );
};
diff --git a/frontend/src/components/SchemaDesigner/utils/schema.ts b/frontend/src/components/SchemaDesigner/utils/schema.ts
index c9ba0c70d5..42e70e975e 100644
--- a/frontend/src/components/SchemaDesigner/utils/schema.ts
+++ b/frontend/src/components/SchemaDesigner/utils/schema.ts
@@ -1,4 +1,11 @@
-// TODO(steven): Implement this function.
-export const isSchemaChanged = (): boolean => {
- return false;
+import { isEqual } from "lodash-es";
+import { useSchemaDesignerContext } from "../common";
+
+export const isSchemaChanged = (schemaId: string): boolean => {
+ const { originalSchemas, editableSchemas } = useSchemaDesignerContext();
+ const originSchema = originalSchemas.value.find(
+ (schema) => schema.id === schemaId
+ );
+ const schema = editableSchemas.value.find((schema) => schema.id === schemaId);
+ return !isEqual(originSchema, schema);
};
diff --git a/frontend/src/components/SchemaDesigner/utils/table.ts b/frontend/src/components/SchemaDesigner/utils/table.ts
index 3a5f0405ff..d5b6b0d8e9 100644
--- a/frontend/src/components/SchemaDesigner/utils/table.ts
+++ b/frontend/src/components/SchemaDesigner/utils/table.ts
@@ -1,4 +1,23 @@
-// TODO(steven): Implement this function.
-export const isTableChanged = (): boolean => {
- return false;
+import { isEqual } from "lodash-es";
+import { useSchemaDesignerContext } from "../common";
+
+export const isTableChanged = (schemaId: string, tableId: string): boolean => {
+ const { originalSchemas, editableSchemas } = useSchemaDesignerContext();
+ const originSchema = originalSchemas.value.find(
+ (schema) => schema.id === schemaId
+ );
+ const schema = editableSchemas.value.find((schema) => schema.id === schemaId);
+ const originTable = originSchema?.tableList.find(
+ (table) => table.id === tableId
+ );
+ const table = schema?.tableList.find((table) => table.id === tableId);
+ const originForeignKeyList =
+ originSchema?.foreignKeyList.filter((fk) => fk.tableId === table?.id) || [];
+ const foreignKeyList =
+ schema?.foreignKeyList.filter((fk) => fk.tableId === table?.id) || [];
+
+ return (
+ !isEqual(originTable, table) ||
+ !isEqual(originForeignKeyList, foreignKeyList)
+ );
};