Skip to content

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

chore: editor fix and improvement (#17811)

ecmadao45fc1286b7be
diff --git a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
index 5dc6ffed8e..160d19b8c4 100644
--- a/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
+++ b/frontend/src/components/AlterSchemaPrepForm/SchemaEditorModal.vue
@@ -480,7 +480,7 @@ const renderEmptyGeneratedDDLContent = (databases: ComposedDatabase[]) => {
const children = databases.map((database) => {
return (
<li>
- {t("schema-editor.nothing-changed-for-database", {
+ {t("schema-editor.failed-to-generate-ddl-for-database", {
database: database.databaseName,
})}
</li>
@@ -513,7 +513,7 @@ const confirmCreateIssueWithEmptyStatement = (
content: () => renderEmptyGeneratedDDLContent(databases),
style: "z-index: 100000",
negativeText: t("common.cancel"),
- positiveText: t("common.continue-anyway"),
+ positiveText: t("schema-editor.continue-with-empty-sql"),
closeOnEsc: false,
maskClosable: false,
onClose: () => {
diff --git a/frontend/src/components/SchemaEditorLite/Panels/PreviewPane.vue b/frontend/src/components/SchemaEditorLite/Panels/PreviewPane.vue
index 2fd8848784..2e511ae144 100644
--- a/frontend/src/components/SchemaEditorLite/Panels/PreviewPane.vue
+++ b/frontend/src/components/SchemaEditorLite/Panels/PreviewPane.vue
@@ -48,6 +48,7 @@ import {
useElementSize,
useLocalStorage,
useParentElement,
+ useDebounceFn,
} from "@vueuse/core";
import { ChevronDownIcon } from "lucide-vue-next";
import { computed, toRef, ref, watch } from "vue";
@@ -93,10 +94,7 @@ const status = ref<"pending" | "success" | "error">("success");
const data = ref<string>("");
const error = ref<Error | null>(null);
-// Track whether we've fetched at least once when expanded
-const hasFetchedOnce = ref(false);
-
-const fetchSchemaString = async () => {
+const fetchSchemaString = useDebounceFn(async () => {
if (!expanded.value) {
data.value = "";
status.value = "success";
@@ -123,42 +121,26 @@ const fetchSchemaString = async () => {
data.value = response.schemaString;
status.value = "success";
- hasFetchedOnce.value = true;
} catch (err) {
error.value = new Error(extractGrpcErrorMessage(err));
status.value = "error";
}
-};
+});
-// Only fetch when the panel is expanded for the first time
-// Subsequent fetches will only happen on manual trigger
watch(
- expanded,
- (newExpanded) => {
- if (newExpanded && !hasFetchedOnce.value && mocked.value) {
+ () => mocked.value,
+ () => {
+ if (expanded.value) {
fetchSchemaString();
}
},
- { immediate: true }
+ { deep: true, immediate: true }
);
-// Reset hasFetchedOnce when panel is collapsed
-watch(expanded, (newExpanded) => {
- if (!newExpanded) {
- hasFetchedOnce.value = false;
- data.value = "";
- }
-});
-
// Listen for refresh-preview events
useEmitteryEventListener(events, "refresh-preview", () => {
if (expanded.value && mocked.value) {
fetchSchemaString();
}
});
-
-// Expose method to trigger manual refresh
-defineExpose({
- refresh: fetchSchemaString,
-});
</script>
diff --git a/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/TableColumnEditor.vue b/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/TableColumnEditor.vue
index 153fdef72b..ab6591658a 100644
--- a/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/TableColumnEditor.vue
+++ b/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/TableColumnEditor.vue
@@ -50,16 +50,22 @@
<script lang="ts" setup>
import { create } from "@bufbuild/protobuf";
import { useElementSize } from "@vueuse/core";
-import { pick } from "lodash-es";
+import { pick, pull } from "lodash-es";
import type { DataTableColumn, DataTableInst } from "naive-ui";
import { NCheckbox, NDataTable } from "naive-ui";
import { computed, h, reactive, ref } from "vue";
import { useI18n } from "vue-i18n";
import ClassificationCell from "@/components/ColumnDataTable/ClassificationCell.vue";
import LabelEditorDrawer from "@/components/LabelEditorDrawer.vue";
+import {
+ removeColumnPrimaryKey,
+ upsertColumnPrimaryKey,
+ changeColumnNameInPrimaryKey,
+ removeColumnFromAllForeignKeys,
+} from "@/components/SchemaEditorLite";
import SemanticTypesDrawer from "@/components/SensitiveData/components/SemanticTypesDrawer.vue";
import { InlineInput } from "@/components/v2";
-import { useSettingV1Store, hasFeature } from "@/store";
+import { useSettingV1Store, hasFeature, pushNotification } from "@/store";
import type { ComposedDatabase } from "@/types";
import { Engine } from "@/types/proto-es/v1/common_pb";
import type { ColumnCatalog } from "@/types/proto-es/v1/database_catalog_service_pb";
@@ -73,6 +79,7 @@ import type {
} from "@/types/proto-es/v1/database_service_pb";
import { Setting_SettingName } from "@/types/proto-es/v1/setting_service_pb";
import { PlanFeature } from "@/types/proto-es/v1/subscription_service_pb";
+import { arraySwap } from "@/utils";
import { useSchemaEditorContext } from "../../context";
import type { EditStatus } from "../../types";
import type { DefaultValue } from "../../utils";
@@ -128,14 +135,6 @@ const props = withDefaults(
);
const emit = defineEmits<{
- (event: "drop", column: ColumnMetadata): void;
- (event: "restore", column: ColumnMetadata): void;
- (
- event: "reorder",
- column: ColumnMetadata,
- index: number,
- delta: -1 | 1
- ): void;
(
event: "foreign-key-edit",
column: ColumnMetadata,
@@ -146,11 +145,6 @@ const emit = defineEmits<{
column: ColumnMetadata,
fk: ForeignKeyMetadata
): void;
- (
- event: "primary-key-set",
- column: ColumnMetadata,
- isPrimaryKey: boolean
- ): void;
}>();
const state = reactive<LocalState>({
@@ -163,6 +157,7 @@ const {
showClassificationColumn: canShowClassificationColumn,
selectionEnabled,
markEditStatus,
+ removeEditStatus,
getColumnStatus,
getColumnCatalog,
removeColumnCatalog,
@@ -250,6 +245,63 @@ const primaryKey = computed(() => {
return props.table.indexes.find((idx) => idx.primary);
});
+const setColumnPrimaryKey = (column: ColumnMetadata, isPrimaryKey: boolean) => {
+ if (isPrimaryKey) {
+ column.nullable = false;
+ upsertColumnPrimaryKey(props.engine, props.table, column.name);
+ } else {
+ removeColumnPrimaryKey(props.table, column.name);
+ }
+ markColumnStatus(column, "updated");
+};
+
+const handleReorderColumn = (index: number, delta: -1 | 1) => {
+ const target = index + delta;
+ const { columns } = props.table;
+ if (target < 0) return;
+ if (target >= columns.length) return;
+ arraySwap(columns, index, target);
+};
+
+const handleDropColumn = (column: ColumnMetadata) => {
+ const { table } = props;
+ // Disallow to drop the last column.
+ const nonDroppedColumns = table.columns.filter((column) => {
+ return statusForColumn(column) !== "dropped";
+ });
+ if (nonDroppedColumns.length === 1) {
+ pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: t("schema-editor.message.cannot-drop-the-last-column"),
+ });
+ return;
+ }
+ const status = statusForColumn(column);
+ if (status === "created") {
+ pull(table.columns, column);
+ table.columns = table.columns.filter((col) => col !== column);
+
+ removeColumnPrimaryKey(table, column.name);
+ removeColumnFromAllForeignKeys(table, column.name);
+ removeColumnCatalog({
+ database: props.db.name,
+ schema: props.schema.name,
+ table: props.table.name,
+ column: column.name,
+ });
+ } else {
+ markColumnStatus(column, "dropped");
+ }
+};
+
+const handleRestoreColumn = (column: ColumnMetadata) => {
+ if (statusForColumn(column) === "created") {
+ return;
+ }
+ removeEditStatus(props.db, metadataForColumn(column), /* recursive */ false);
+};
+
const showClassification = computed(() => {
return (
props.showClassificationColumn === "ALWAYS" ||
@@ -317,7 +369,7 @@ const columns = computed(() => {
allowMoveUp: index > 0,
allowMoveDown: index < shownColumnList.value.length - 1,
disabled: props.disableChangeTable,
- onReorder: (delta: -1 | 1) => emit("reorder", column, index, delta),
+ onReorder: (delta: -1 | 1) => handleReorderColumn(index, delta),
});
},
},
@@ -328,6 +380,7 @@ const columns = computed(() => {
minWidth: 140,
className: "input-cell",
render: (column) => {
+ const isPk = isColumnPrimaryKey(column);
return h(InlineInput, {
value: column.name,
disabled: props.readonly || props.disableAlterColumn(column),
@@ -357,6 +410,10 @@ const columns = computed(() => {
});
const oldStatus = statusForColumn(column);
+ const oldName = column.name;
+ if (isPk) {
+ changeColumnNameInPrimaryKey(props.table, oldName, value);
+ }
column.name = value;
markColumnStatus(column, "updated", oldStatus);
},
@@ -529,7 +586,7 @@ const columns = computed(() => {
!props.allowChangePrimaryKeys ||
props.disableAlterColumn(column),
"onUpdate:checked": (checked: boolean) =>
- emit("primary-key-set", column, checked),
+ setColumnPrimaryKey(column, checked),
});
},
},
@@ -587,8 +644,8 @@ const columns = computed(() => {
return h(OperationCell, {
dropped: isDroppedColumn(column),
disabled: props.disableChangeTable,
- onDrop: () => emit("drop", column),
- onRestore: () => emit("restore", column),
+ onDrop: () => handleDropColumn(column),
+ onRestore: () => handleRestoreColumn(column),
});
},
},
diff --git a/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue b/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue
index 201d8b169f..21f52d2574 100644
--- a/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue
+++ b/frontend/src/components/SchemaEditorLite/Panels/TableEditor.vue
@@ -120,10 +120,6 @@
"
:disable-alter-column="disableAlterColumn"
:show-database-catalog-column="false"
- @drop="handleDropColumn"
- @restore="handleRestoreColumn"
- @reorder="handleReorderColumn"
- @primary-key-set="setColumnPrimaryKey"
@foreign-key-edit="handleEditColumnForeignKey"
@foreign-key-click="gotoForeignKeyReferencedTable"
/>
@@ -185,14 +181,12 @@
<script lang="ts" setup>
import { create } from "@bufbuild/protobuf";
-import { cloneDeep, head, pull } from "lodash-es";
+import { cloneDeep, head } from "lodash-es";
import { ArrowLeftIcon, PlusIcon } from "lucide-vue-next";
import { NButton } from "naive-ui";
import { computed, reactive, ref } from "vue";
-import { useI18n } from "vue-i18n";
import { IndexIcon, TablePartitionIcon } from "@/components/Icon";
import { Drawer, DrawerContent } from "@/components/v2";
-import { pushNotification } from "@/store/modules";
import type { ComposedDatabase } from "@/types";
import { Engine } from "@/types/proto-es/v1/common_pb";
import {
@@ -215,19 +209,10 @@ import {
} from "@/types/proto-es/v1/database_service_pb";
import type { ColumnMetadata as NewColumnMetadata } from "@/types/proto-es/v1/database_service_pb";
import type { SchemaTemplateSetting_FieldTemplate } from "@/types/proto-es/v1/setting_service_pb";
-import {
- arraySwap,
- instanceV1AllowsReorderColumns,
- randomString,
-} from "@/utils";
+import { instanceV1AllowsReorderColumns, randomString } from "@/utils";
import FieldTemplates from "@/views/SchemaTemplate/FieldTemplates.vue";
import EditColumnForeignKeyModal from "../Modals/EditColumnForeignKeyModal.vue";
import { useSchemaEditorContext } from "../context";
-import {
- removeColumnFromAllForeignKeys,
- removeColumnPrimaryKey,
- upsertColumnPrimaryKey,
-} from "../edit";
import {
engineSupportsEditIndexes,
engineSupportsEditTablePartitions,
@@ -261,19 +246,16 @@ interface LocalState {
showFeatureModal: boolean;
}
-const { t } = useI18n();
const {
readonly,
events,
options,
addTab,
markEditStatus,
- removeEditStatus,
getSchemaStatus,
getTableStatus,
getColumnStatus,
getDatabaseCatalog,
- removeColumnCatalog,
upsertColumnCatalog,
queuePendingScrollToColumn,
selectionEnabled,
@@ -393,16 +375,6 @@ const disableAlterColumn = (column: ColumnMetadata): boolean => {
);
};
-const setColumnPrimaryKey = (column: ColumnMetadata, isPrimaryKey: boolean) => {
- if (isPrimaryKey) {
- column.nullable = false;
- upsertColumnPrimaryKey(engine.value, props.table, column.name);
- } else {
- removeColumnPrimaryKey(props.table, column.name);
- }
- markColumnStatus(column, "updated");
-};
-
const handleAddColumn = () => {
const column = create(ColumnMetadataSchema, {});
/* eslint-disable-next-line vue/no-mutating-props */
@@ -556,63 +528,15 @@ const handleEditColumnForeignKey = (
state.showEditColumnForeignKeyModal = true;
};
-const handleDropColumn = (column: ColumnMetadata) => {
- const { table } = props;
- // Disallow to drop the last column.
- const nonDroppedColumns = table.columns.filter((column) => {
- return statusForColumn(column) !== "dropped";
- });
- if (nonDroppedColumns.length === 1) {
- pushNotification({
- module: "bytebase",
- style: "CRITICAL",
- title: t("schema-editor.message.cannot-drop-the-last-column"),
- });
- return;
- }
- const status = statusForColumn(column);
- if (status === "created") {
- pull(table.columns, column);
- table.columns = table.columns.filter((col) => col !== column);
-
- removeColumnPrimaryKey(table, column.name);
- removeColumnFromAllForeignKeys(table, column.name);
- removeColumnCatalog({
- database: props.db.name,
- schema: props.schema.name,
- table: props.table.name,
- column: column.name,
- });
- } else {
- markColumnStatus(column, "dropped");
- }
-};
-
-const handleRestoreColumn = (column: ColumnMetadata) => {
- if (statusForColumn(column) === "created") {
- return;
- }
- removeEditStatus(props.db, metadataForColumn(column), /* recursive */ false);
-};
-
-const handleReorderColumn = (
- column: ColumnMetadata,
- index: number,
- delta: -1 | 1
-) => {
- const target = index + delta;
- const { columns } = props.table;
- if (target < 0) return;
- if (target >= columns.length) return;
- arraySwap(columns, index, target);
-};
-
const mocked = computed(() => {
const { db, database, schema, table } = props;
const databaseCatalog = getDatabaseCatalog(db.name);
const mockedTable = cloneDeep(table);
mockedTable.columns = mockedTable.columns.filter((column) => {
+ if (!column.name) {
+ return false;
+ }
const status = getColumnStatus(db, { schema, table, column });
return status !== "dropped";
});
diff --git a/frontend/src/components/SchemaEditorLite/SchemaEditorLite.vue b/frontend/src/components/SchemaEditorLite/SchemaEditorLite.vue
index 44a99b608d..0d0798553c 100644
--- a/frontend/src/components/SchemaEditorLite/SchemaEditorLite.vue
+++ b/frontend/src/components/SchemaEditorLite/SchemaEditorLite.vue
@@ -158,7 +158,7 @@ const refreshPreview = () => {
defineExpose({
applyMetadataEdit,
refreshPreview,
- isDirty: computed(() => context.dirtyPaths.value.size > 0),
+ isDirty: context.isDirty,
});
</script>
diff --git a/frontend/src/components/SchemaEditorLite/context/edit.ts b/frontend/src/components/SchemaEditorLite/context/edit.ts
index bb2738333f..d391c4ca1e 100644
--- a/frontend/src/components/SchemaEditorLite/context/edit.ts
+++ b/frontend/src/components/SchemaEditorLite/context/edit.ts
@@ -99,6 +99,41 @@ export const useEditStatus = () => {
return "normal";
};
+ const replaceTableName = (
+ database: ComposedDatabase,
+ metadata: {
+ schema: SchemaMetadata;
+ table: TableMetadata;
+ },
+ newName: string
+ ) => {
+ const key = keyForResource(database, metadata);
+
+ const sections = key.split("/");
+ sections.pop();
+ sections.push(newName);
+ const newKey = sections.join("/");
+ if (key === newKey) {
+ return;
+ }
+
+ const pathMap = new Map<string /* old path */, string /* new path */>();
+ for (const path of dirtyPathsArray.value) {
+ if (!dirtyPaths.value.has(path)) {
+ continue;
+ }
+ if (path.startsWith(key)) {
+ pathMap.set(path, path.replace(key, newKey));
+ }
+ }
+
+ for (const [oldPath, newPath] of pathMap.entries()) {
+ const status = dirtyPaths.value.get(oldPath)!;
+ dirtyPaths.value.delete(oldPath);
+ dirtyPaths.value.set(newPath, status);
+ }
+ };
+
const getColumnStatus = (
database: ComposedDatabase,
metadata: {
@@ -181,8 +216,10 @@ export const useEditStatus = () => {
dirtyPaths.value.clear();
};
+ const isDirty = computed(() => dirtyPaths.value.size > 0);
+
return {
- dirtyPaths,
+ isDirty,
markEditStatus,
markEditStatusByKey,
getEditStatusByKey,
@@ -195,5 +232,6 @@ export const useEditStatus = () => {
getProcedureStatus,
getFunctionStatus,
getViewStatus,
+ replaceTableName,
};
};
diff --git a/frontend/src/components/SchemaEditorLite/edit.ts b/frontend/src/components/SchemaEditorLite/edit.ts
index 85d22407b1..281cbc15bb 100644
--- a/frontend/src/components/SchemaEditorLite/edit.ts
+++ b/frontend/src/components/SchemaEditorLite/edit.ts
@@ -55,6 +55,23 @@ export const removeColumnPrimaryKey = (
}
};
+export const changeColumnNameInPrimaryKey = (
+ table: TableMetadata,
+ oldName: string,
+ newName: string
+) => {
+ const pkIndex = table.indexes.findIndex((idx) => idx.primary);
+ if (pkIndex < 0) {
+ return;
+ }
+ const pk = table.indexes[pkIndex];
+ const columnIndex = pk.expressions.indexOf(oldName);
+ if (columnIndex < 0) {
+ return;
+ }
+ pk.expressions[columnIndex] = newName;
+};
+
export const upsertColumnFromForeignKey = (
fk: ForeignKeyMetadata,
columnName: string,
diff --git a/frontend/src/components/SchemaTemplate/TableTemplateForm.vue b/frontend/src/components/SchemaTemplate/TableTemplateForm.vue
index d14fcfa0c3..b723fc76ff 100644
--- a/frontend/src/components/SchemaTemplate/TableTemplateForm.vue
+++ b/frontend/src/components/SchemaTemplate/TableTemplateForm.vue
@@ -68,7 +68,7 @@
<RequiredStar />
</label>
<NInput
- :value="editing.tableMetadata.name"
+ :value="tableMetadata.name"
:disabled="readonly"
placeholder="table name"
@update:value="handleUpdateTableName"
@@ -81,7 +81,7 @@
{{ $t("schema-template.form.comment") }}
</label>
<NInput
- v-model:value="editing.tableMetadata.userComment"
+ v-model:value="tableMetadata.userComment"
type="textarea"
:autosize="{ minRows: 3, maxRows: 3 }"
:disabled="readonly"
@@ -116,17 +116,14 @@
:show-foreign-key="false"
:db="editing.db"
:database="editing.databaseMetadata"
- :schema="editing.schemaMetadata"
- :table="editing.tableMetadata"
+ :schema="schemaMetadata"
+ :table="tableMetadata"
:engine="editing.engine"
:allow-change-primary-keys="true"
:allow-reorder-columns="allowReorderColumns"
:max-body-height="640"
:show-database-catalog-column="true"
:show-classification-column="'ALWAYS'"
- @drop="handleDropColumn"
- @reorder="handleReorderColumn"
- @primary-key-set="setColumnPrimaryKey"
/>
</div>
</div>
@@ -178,19 +175,16 @@
<script lang="ts" setup>
import { create as createProto } from "@bufbuild/protobuf";
-import { cloneDeep, isEqual, pull } from "lodash-es";
+import { cloneDeep, isEqual } from "lodash-es";
import { PencilIcon, PlusIcon, XIcon } from "lucide-vue-next";
import type { SelectOption } from "naive-ui";
import { NButton, NInput } from "naive-ui";
-import { computed, reactive, ref, toRef } from "vue";
+import { computed, onMounted, reactive, ref, toRef } from "vue";
import { useI18n } from "vue-i18n";
import RequiredStar from "@/components/RequiredStar.vue";
import {
TableColumnEditor,
provideSchemaEditorContext,
- removeColumnFromAllForeignKeys,
- removeColumnPrimaryKey,
- upsertColumnPrimaryKey,
type EditStatus,
type EditTarget,
} from "@/components/SchemaEditorLite";
@@ -208,7 +202,6 @@ import {
ColumnMetadataSchema,
type ColumnMetadata,
} from "@/types/proto-es/v1/database_service_pb";
-import { type ColumnMetadata as OldColumnMetadata } from "@/types/proto-es/v1/database_service_pb";
import {
SchemaTemplateSettingSchema,
Setting_SettingName,
@@ -216,7 +209,7 @@ import {
type SchemaTemplateSetting_TableTemplate,
ValueSchema as SettingValueSchema,
} from "@/types/proto-es/v1/setting_service_pb";
-import { arraySwap, instanceV1AllowsReorderColumns } from "@/utils";
+import { instanceV1AllowsReorderColumns } from "@/utils";
import FieldTemplates from "@/views/SchemaTemplate/FieldTemplates.vue";
import ClassificationLevelBadge from "./ClassificationLevelBadge.vue";
import SelectClassificationDrawer from "./SelectClassificationDrawer.vue";
@@ -245,13 +238,21 @@ const editing = computed(() => {
return mockMetadataFromTableTemplate(props.template);
});
+const isCreate = computed(() => !props.template.table?.name);
+
+const schemaMetadata = computed(
+ () => editing.value.databaseMetadata.schemas[0]
+);
+
+const tableMetadata = computed(() => schemaMetadata.value.tables[0]);
+
const targets = computed(() => {
const target: EditTarget = {
database: editing.value.db,
metadata: editing.value.databaseMetadata,
- baselineMetadata: editing.value.databaseMetadata,
+ baselineMetadata: cloneDeep(editing.value.databaseMetadata),
catalog: editing.value.databaseCatalog,
- baselineCatalog: editing.value.databaseCatalog,
+ baselineCatalog: cloneDeep(editing.value.databaseCatalog),
};
return [target];
});
@@ -265,6 +266,28 @@ const context = provideSchemaEditorContext({
hidePreview: ref(false),
});
+onMounted(() => {
+ context.markEditStatus(
+ editing.value.db,
+ {
+ schema: schemaMetadata.value,
+ table: tableMetadata.value,
+ },
+ isCreate.value ? "created" : "normal"
+ );
+ for (const column of tableMetadata.value.columns) {
+ context.markEditStatus(
+ editing.value.db,
+ {
+ schema: schemaMetadata.value,
+ table: tableMetadata.value,
+ column,
+ },
+ isCreate.value ? "created" : "normal"
+ );
+ }
+});
+
const state = reactive<LocalState>({
showClassificationDrawer: false,
showFieldTemplateDrawer: false,
@@ -275,8 +298,8 @@ const settingStore = useSettingV1Store();
const tableCatalog = computed(() =>
context.getTableCatalog({
database: editing.value.databaseMetadata.name,
- schema: editing.value.schemaMetadata.name,
- table: editing.value.tableMetadata.name,
+ schema: schemaMetadata.value.name,
+ table: tableMetadata.value.name,
})
);
@@ -288,8 +311,8 @@ const tableClassificationId = computed({
context.upsertTableCatalog(
{
database: editing.value.databaseCatalog.name,
- schema: editing.value.schemaCatalog.name,
- table: editing.value.tableMetadata.name,
+ schema: schemaMetadata.value.name,
+ table: tableMetadata.value.name,
},
(catalog) => {
catalog.classification = id ?? "";
@@ -299,15 +322,11 @@ const tableClassificationId = computed({
});
const metadataForColumn = (column: ColumnMetadata) => {
- const {
- databaseMetadata: database,
- schemaMetadata: schema,
- tableMetadata: table,
- } = editing.value;
+ const { databaseMetadata: database } = editing.value;
return {
database: database,
- schema: schema,
- table: table,
+ schema: schemaMetadata.value,
+ table: tableMetadata.value,
column: column,
};
};
@@ -327,12 +346,28 @@ const allowReorderColumns = computed(() => {
return instanceV1AllowsReorderColumns(editing.value.engine);
});
+const validColumns = computed(() => {
+ return tableMetadata.value.columns.filter((col) => {
+ if (!col.name || !col.type) {
+ return false;
+ }
+ const status = context.getColumnStatus(
+ editing.value.db,
+ metadataForColumn(col)
+ );
+ return status !== "dropped";
+ });
+});
+
const submitDisabled = computed(() => {
- const { tableMetadata, category } = editing.value;
- if (!tableMetadata.name || tableMetadata.columns.length === 0) {
+ const { category } = editing.value;
+ if (!tableMetadata.value.name) {
+ return true;
+ }
+ if (tableMetadata.value.columns.some((col) => !col.name || !col.type)) {
return true;
}
- if (tableMetadata.columns.some((col) => !col.name || !col.type)) {
+ if (validColumns.value.length === 0) {
return true;
}
if (
@@ -349,6 +384,10 @@ const submitDisabled = computed(() => {
const onSubmit = async () => {
const template = rebuildTableTemplateFromMetadata({
...editing.value,
+ tableMetadata: {
+ ...tableMetadata.value,
+ columns: [...validColumns.value],
+ },
tableCatalog: tableCatalog.value
? tableCatalog.value
: createProto(TableCatalogSchema, {}),
@@ -396,57 +435,22 @@ const onSubmit = async () => {
};
const onColumnAdd = () => {
- const {
- db,
- databaseMetadata: database,
- schemaMetadata: schema,
- tableMetadata: table,
- } = editing.value;
+ const { db, databaseMetadata: database } = editing.value;
const column = createProto(ColumnMetadataSchema, {});
- table.columns.push(column);
+ tableMetadata.value.columns.push(column);
markColumnStatus(column, "created");
context.queuePendingScrollToColumn({
db,
metadata: {
database: database,
- schema: schema,
- table: table,
+ schema: schemaMetadata.value,
+ table: tableMetadata.value,
column: column,
},
});
};
-const handleDropColumn = (column: ColumnMetadata) => {
- const { tableMetadata } = editing.value;
- pull(tableMetadata.columns, column);
- tableMetadata.columns = tableMetadata.columns.filter((col) => col !== column);
-
- removeColumnPrimaryKey(tableMetadata, column.name);
- removeColumnFromAllForeignKeys(tableMetadata, column.name);
- // Convert back to update proto-es tableMetadata
- context.removeColumnCatalog({
- database: editing.value.databaseCatalog.name,
- schema: editing.value.schemaCatalog.name,
- table: editing.value.tableMetadata.name,
- column: column.name,
- });
-};
-
-const setColumnPrimaryKey = (column: ColumnMetadata, isPrimaryKey: boolean) => {
- if (isPrimaryKey) {
- column.nullable = false;
- upsertColumnPrimaryKey(
- editing.value.engine,
- editing.value.tableMetadata,
- column.name
- );
- } else {
- removeColumnPrimaryKey(editing.value.tableMetadata, column.name);
- }
- markColumnStatus(column, "updated");
-};
-
const handleApplyColumnTemplate = (
template: SchemaTemplateSetting_FieldTemplate
) => {
@@ -454,18 +458,18 @@ const handleApplyColumnTemplate = (
if (!template.column) {
return;
}
- const { db, tableMetadata, engine } = editing.value;
+ const { db, engine } = editing.value;
if (template.engine !== engine) {
return;
}
const column = cloneDeep(template.column);
- tableMetadata.columns.push(column);
+ tableMetadata.value.columns.push(column);
if (template.catalog) {
context.upsertColumnCatalog(
{
database: editing.value.databaseCatalog.name,
- schema: editing.value.schemaCatalog.name,
- table: editing.value.tableCatalog.name,
+ schema: schemaMetadata.value.name,
+ table: tableMetadata.value.name,
column: template.column.name,
},
(config) => {
@@ -480,24 +484,12 @@ const handleApplyColumnTemplate = (
});
};
-const handleReorderColumn = (
- column: OldColumnMetadata,
- index: number,
- delta: -1 | 1
-) => {
- const target = index + delta;
- const { columns } = editing.value.tableMetadata;
- if (target < 0) return;
- if (target >= columns.length) return;
- arraySwap(columns, index, target);
-};
-
const handleUpdateTableName = (name: string) => {
context.upsertTableCatalog(
{
database: editing.value.databaseCatalog.name,
- schema: editing.value.schemaCatalog.name,
- table: editing.value.tableMetadata.name,
+ schema: schemaMetadata.value.name,
+ table: tableMetadata.value.name,
},
(catalog) => {
catalog.name = name;
@@ -505,9 +497,17 @@ const handleUpdateTableName = (name: string) => {
);
context.removeTableCatalog({
database: editing.value.databaseCatalog.name,
- schema: editing.value.schemaCatalog.name,
- table: editing.value.tableMetadata.name,
+ schema: schemaMetadata.value.name,
+ table: tableMetadata.value.name,
});
- editing.value.tableMetadata.name = name;
+ context.replaceTableName(
+ editing.value.db,
+ {
+ schema: schemaMetadata.value,
+ table: tableMetadata.value,
+ },
+ name
+ );
+ tableMetadata.value.name = name;
};
</script>
diff --git a/frontend/src/components/SchemaTemplate/utils.ts b/frontend/src/components/SchemaTemplate/utils.ts
index 711226b8e7..3e37807ebe 100644
--- a/frontend/src/components/SchemaTemplate/utils.ts
+++ b/frontend/src/components/SchemaTemplate/utils.ts
@@ -103,47 +103,46 @@ export const mockMetadataFromTableTemplate = (
const db = {
...unknownDatabase(),
};
- const tableMetadata =
- cloneDeep(template.table) ?? create(TableMetadataSchema, {});
- const schemaMetadata = create(SchemaMetadataSchema, {
- name: "",
- tables: [tableMetadata],
- });
- const tableCatalog =
- cloneDeep(template.catalog) ??
- create(TableCatalogSchema, {
- name: "",
- kind: {
- case: "columns",
- value: create(TableCatalog_ColumnsSchema, {}),
- },
- });
const databaseMetadata = create(DatabaseMetadataSchema, {
name: db.name,
schemas: [
create(SchemaMetadataSchema, {
name: "",
- tables: [cloneDeep(template.table) ?? create(TableMetadataSchema, {})],
+ tables: [
+ cloneDeep(template.table) ??
+ create(TableMetadataSchema, {
+ name: "",
+ columns: [],
+ }),
+ ],
}),
],
});
- const schemaCatalog = create(SchemaCatalogSchema, {
- name: "",
- tables: [tableCatalog],
- });
+
const databaseCatalog = create(DatabaseCatalogSchema, {
name: db.name,
- schemas: [schemaCatalog],
+ schemas: [
+ create(SchemaCatalogSchema, {
+ name: "",
+ tables: [
+ cloneDeep(template.catalog) ??
+ create(TableCatalogSchema, {
+ name: "",
+ kind: {
+ case: "columns",
+ value: create(TableCatalog_ColumnsSchema, {}),
+ },
+ }),
+ ],
+ }),
+ ],
});
+
return reactive({
db,
databaseMetadata,
databaseCatalog,
- schemaCatalog,
- schemaMetadata,
- tableCatalog,
- tableMetadata,
id: template.id,
category: template.category,
engine: template.engine,
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 3cc5ed75eb..57fdf8dcbc 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -2146,7 +2146,8 @@
"sync-sql-from-schema-editor": "Sync SQL from Schema Editor",
"raw-sql": "Raw SQL",
"search-database-and-table": "Search database and table",
- "nothing-changed-for-database": "Nothing changed for database {database}",
+ "failed-to-generate-ddl-for-database": "Failed to generate the DDL for database {database}",
+ "continue-with-empty-sql": "Continue with empty SQL",
"actions": {
"create-schema": "Create schema",
"drop-schema": "Drop schema",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 5807bbbc38..3e665bb1af 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -2146,7 +2146,8 @@
"sync-sql-from-schema-editor": "Sincronizar SQL desde el Editor de esquema",
"raw-sql": "SQL sin procesar",
"search-database-and-table": "Buscar base de datos y tabla",
- "nothing-changed-for-database": "Nada cambió para la base de datos {database}",
+ "failed-to-generate-ddl-for-database": "Error al generar el DDL para la base de datos {database}",
+ "continue-with-empty-sql": "Continuar con SQL vacío",
"actions": {
"create-schema": "Crear esquema",
"drop-schema": "Eliminar esquema",
diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.json
index 02c9b4ee28..a21b0c76d8 100644
--- a/frontend/src/locales/ja-JP.json
+++ b/frontend/src/locales/ja-JP.json
@@ -2146,7 +2146,8 @@
"sync-sql-from-schema-editor": "スキーマエディタからSQLを同期する",
"raw-sql": "SQL文",
"search-database-and-table": "データベースとテーブルを検索する",
- "nothing-changed-for-database": "データベース {database} には何も変更されていません",
+ "failed-to-generate-ddl-for-database": "データベース {database} の DDL 生成に失敗しました",
+ "continue-with-empty-sql": "空のSQLで続行",
"actions": {
"create-schema": "新しいスキーマ",
"drop-schema": "スキーマの削除",
diff --git a/frontend/src/locales/vi-VN.json b/frontend/src/locales/vi-VN.json
index f3d9b7a3f1..f4614fd3d3 100644
--- a/frontend/src/locales/vi-VN.json
+++ b/frontend/src/locales/vi-VN.json
@@ -2146,7 +2146,8 @@
"sync-sql-from-schema-editor": "Đồng bộ SQL từ Trình soạn thảo lược đồ",
"raw-sql": "SQL thô",
"search-database-and-table": "Tìm kiếm cơ sở dữ liệu và bảng",
- "nothing-changed-for-database": "Không có gì thay đổi cho cơ sở dữ liệu {database}",
+ "failed-to-generate-ddl-for-database": "Không thể tạo DDL cho cơ sở dữ liệu {database}",
+ "continue-with-empty-sql": "Tiếp tục với SQL trống",
"actions": {
"create-schema": "Tạo lược đồ",
"drop-schema": "Xóa lược đồ",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index 270b78ea52..36781d7738 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -2146,7 +2146,8 @@
"sync-sql-from-schema-editor": "从 Schema 编辑器中同步 SQL",
"raw-sql": "SQL 语句",
"search-database-and-table": "搜索数据库和表",
- "nothing-changed-for-database": "数据库 {database} 未发生任何变化",
+ "failed-to-generate-ddl-for-database": "无法为数据库 {database} 生成 DDL",
+ "continue-with-empty-sql": "使用空 SQL 创建工单",
"actions": {
"create-schema": "新建 Schema",
"drop-schema": "删除 Schema",
diff --git a/frontend/src/views/SchemaTemplate/TableTemplates.vue b/frontend/src/views/SchemaTemplate/TableTemplates.vue
index 0dade109f2..7abd08670f 100644
--- a/frontend/src/views/SchemaTemplate/TableTemplates.vue
+++ b/frontend/src/views/SchemaTemplate/TableTemplates.vue
@@ -78,8 +78,6 @@ import { engineList } from "@/components/SchemaTemplate/utils";
import { Drawer, SearchBox } from "@/components/v2";
import { useSettingV1Store } from "@/store";
import { Engine } from "@/types/proto-es/v1/common_pb";
-import { TableCatalogSchema } from "@/types/proto-es/v1/database_catalog_service_pb";
-import { TableMetadataSchema } from "@/types/proto-es/v1/database_service_pb";
import type { SchemaTemplateSetting_TableTemplate } from "@/types/proto-es/v1/setting_service_pb";
import {
Setting_SettingName,
@@ -108,12 +106,6 @@ const initialTemplate = (): SchemaTemplateSetting_TableTemplate =>
id: uuidv1(),
engine: props.engine ?? Engine.MYSQL,
category: "",
- table: createProto(TableMetadataSchema, {
- name: "",
- userComment: "",
- columns: [],
- }),
- catalog: createProto(TableCatalogSchema, {}),
});
const state = reactive<LocalState>({