Skip to content

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

chore(schema-editor): update column default value field (#8742)

boojack867d1fd61ee7
diff --git a/frontend/src/components/Branch/utils.ts b/frontend/src/components/Branch/utils.ts
index 932b6d06f6..857ed33a2d 100644
--- a/frontend/src/components/Branch/utils.ts
+++ b/frontend/src/components/Branch/utils.ts
@@ -92,7 +92,10 @@ export const mergeSchemaEditToMetadata = (
column.comment = columnEdit.comment;
column.userComment = columnEdit.userComment;
column.classification = columnEdit.classification ?? "";
- column.default = columnEdit.default;
+ column.hasDefault = columnEdit.hasDefault;
+ column.defaultNull = columnEdit.defaultNull;
+ column.defaultString = columnEdit.defaultString;
+ column.defaultExpression = columnEdit.defaultExpression;
}
}
for (const column of table.columns) {
@@ -229,7 +232,10 @@ const transformSchemaEditToMetadata = (schemaEdit: Schema): SchemaMetadata => {
name: column.name,
type: column.type,
nullable: column.nullable,
- default: column.default,
+ hasDefault: column.hasDefault,
+ defaultNull: column.defaultNull,
+ defaultString: column.defaultString,
+ defaultExpression: column.defaultExpression,
comment: column.comment,
userComment: column.userComment,
classification: column.classification,
@@ -280,7 +286,10 @@ export const transformTableEditToMetadata = (
name: column.name,
type: column.type,
nullable: column.nullable,
- default: column.default,
+ hasDefault: column.hasDefault,
+ defaultNull: column.defaultNull,
+ defaultString: column.defaultString,
+ defaultExpression: column.defaultExpression,
comment: column.comment,
userComment: column.userComment,
classification: column.classification,
@@ -315,7 +324,10 @@ const transformColumnEditToMetadata = (columnEdit: Column): ColumnMetadata => {
name: columnEdit.name,
type: columnEdit.type,
nullable: columnEdit.nullable,
- default: columnEdit.default,
+ hasDefault: columnEdit.hasDefault,
+ defaultNull: columnEdit.defaultNull,
+ defaultString: columnEdit.defaultString,
+ defaultExpression: columnEdit.defaultExpression,
comment: columnEdit.comment,
userComment: columnEdit.userComment,
classification: columnEdit.classification,
@@ -363,7 +375,10 @@ export const rebuildEditableSchemas = (
editableColumn.comment = column.comment;
editableColumn.userComment = column.userComment;
editableColumn.classification = column.classification;
- editableColumn.default = column.default;
+ editableColumn.hasDefault = column.hasDefault;
+ editableColumn.defaultNull = column.defaultNull;
+ editableColumn.defaultString = column.defaultString;
+ editableColumn.defaultExpression = column.defaultExpression;
}
for (const column of table.columns) {
diff --git a/frontend/src/components/SchemaEditorV1/Modals/ColumnDefaultValueExpressionModal.vue b/frontend/src/components/SchemaEditorV1/Modals/ColumnDefaultValueExpressionModal.vue
new file mode 100644
index 0000000000..f4dbd8bc56
--- /dev/null
+++ b/frontend/src/components/SchemaEditorV1/Modals/ColumnDefaultValueExpressionModal.vue
@@ -0,0 +1,68 @@
+<template>
+ <BBModal
+ :title="$t('schema-editor.default.expression')"
+ class="shadow-inner outline outline-gray-200"
+ @close="dismissModal"
+ >
+ <div class="w-72">
+ <BBTextField
+ class="my-2 w-full"
+ :value="state.expression"
+ @input="handleExpressionChange"
+ />
+ </div>
+ <div class="w-full flex items-center justify-end mt-2 space-x-3 pr-1 pb-1">
+ <button type="button" class="btn-normal" @click="dismissModal">
+ {{ $t("common.cancel") }}
+ </button>
+ <button class="btn-primary" @click="handleConfirmButtonClick">
+ {{ $t("common.save") }}
+ </button>
+ </div>
+ </BBModal>
+</template>
+
+<script lang="ts" setup>
+import { reactive } from "vue";
+import { useNotificationStore } from "@/store";
+
+interface LocalState {
+ expression: string;
+}
+
+const props = defineProps<{
+ expression?: string;
+}>();
+
+const emit = defineEmits<{
+ (event: "close"): void;
+ (event: "update:expression", value: string): void;
+}>();
+
+const notificationStore = useNotificationStore();
+const state = reactive<LocalState>({
+ expression: props.expression || "",
+});
+
+const handleExpressionChange = (event: Event) => {
+ state.expression = (event.target as HTMLInputElement).value;
+};
+
+const handleConfirmButtonClick = async () => {
+ if (!state.expression) {
+ notificationStore.pushNotification({
+ module: "bytebase",
+ style: "CRITICAL",
+ title: "Expression cannot be empty",
+ });
+ return;
+ }
+
+ emit("update:expression", state.expression);
+ dismissModal();
+};
+
+const dismissModal = () => {
+ emit("close");
+};
+</script>
diff --git a/frontend/src/components/SchemaEditorV1/Panels/TableColumnEditor.vue b/frontend/src/components/SchemaEditorV1/Panels/TableColumnEditor.vue
index 1bd818863f..68db5f3945 100644
--- a/frontend/src/components/SchemaEditorV1/Panels/TableColumnEditor.vue
+++ b/frontend/src/components/SchemaEditorV1/Panels/TableColumnEditor.vue
@@ -96,21 +96,18 @@
:class="getColumnClassList(column, row)"
>
<input
- v-model="column.default"
- :disabled="readonly || disableAlterColumn(column)"
- :placeholder="
- column.default === undefined || !column.nullable
- ? 'EMPTY'
- : 'NULL'
- "
class="column-field-input !pr-8"
type="text"
+ :value="getColumnDefaultDisplayString(column)"
+ :disabled="readonly || disableAlterColumn(column)"
+ :placeholder="getColumnDefaultValuePlaceholder(column)"
+ @change="(e) => handleColumnDefaultInputChange(e, column)"
/>
<NDropdown
trigger="click"
:disabled="readonly || disableAlterColumn(column)"
- :options="dataDefaultOptions"
- @select="(defaultString:string) => handleColumnDefaultFieldChange(column, defaultString)"
+ :options="getColumnDefaultValueOptions(column)"
+ @select="(key: string) => handleColumnDefaultFieldChange(column, key)"
>
<button class="absolute right-5">
<heroicons-solid:chevron-up-down
@@ -216,6 +213,13 @@
</template>
</BBGrid>
+ <ColumnDefaultValueExpressionModal
+ v-if="editColumnDefaultValueExpressionContext"
+ :expression="editColumnDefaultValueExpressionContext.defaultExpression"
+ @close="editColumnDefaultValueExpressionContext = undefined"
+ @update:expression="handleSelectedColumnDefaultValueExpressionChange"
+ />
+
<SelectClassificationDrawer
v-if="classificationConfig"
:show="state.pendingUpdateColumn !== undefined"
@@ -228,13 +232,19 @@
<script lang="ts" setup>
import { flatten } from "lodash-es";
import { NDropdown } from "naive-ui";
-import { computed, reactive, watchEffect } from "vue";
+import { computed, reactive, ref } from "vue";
import { useI18n } from "vue-i18n";
import { BBCheckbox } from "@/bbkit";
import { useSettingV1Store } from "@/store/modules";
import { Engine } from "@/types/proto/v1/common";
import { Table, Column, ForeignKey } from "@/types/v1/schemaEditor";
import { getDataTypeSuggestionList } from "@/utils";
+import ColumnDefaultValueExpressionModal from "../Modals/ColumnDefaultValueExpressionModal.vue";
+import {
+ DefaultValueOption,
+ getColumnTypeDefaultValueOptions,
+ getDefaultValueByKey,
+} from "../utils/columnDefaultValue";
interface LocalState {
pendingUpdateColumn?: Column;
@@ -279,17 +289,7 @@ const state = reactive<LocalState>({});
const { t } = useI18n();
const settingStore = useSettingV1Store();
-
-const dataDefaultOptions = [
- {
- label: "NULL",
- key: "NULL",
- },
- {
- label: "EMPTY",
- key: "EMPTY",
- },
-];
+const editColumnDefaultValueExpressionContext = ref<Column>();
const classificationConfig = computed(() => {
if (!props.classificationConfigId) {
@@ -341,6 +341,40 @@ const columnHeaderList = computed(() => {
].filter((header) => !header.hide);
});
+const getColumnDefaultDisplayString = (column: Column) => {
+ if (!column.hasDefault || column.defaultNull) {
+ return undefined;
+ }
+ return column.defaultString || column.defaultExpression || "";
+};
+
+const getColumnDefaultValuePlaceholder = (column: Column): string => {
+ if (!column.hasDefault) {
+ return "No default";
+ }
+ if (column.defaultNull) {
+ return "Null";
+ }
+ if (column.defaultString !== undefined) {
+ return column.defaultString || "Empty string";
+ }
+ if (column.defaultExpression !== undefined) {
+ return column.defaultExpression || "Empty expression";
+ }
+ return "";
+};
+
+const getColumnDefaultValueOptions = (column: Column): DefaultValueOption[] => {
+ return getColumnTypeDefaultValueOptions(props.engine, column.type).map(
+ (option) => {
+ return {
+ ...option,
+ label: t(`schema-editor.default.${option.key}`),
+ };
+ }
+ );
+};
+
const getColumnClassList = (column: Column, index: number): string[] => {
const classList = props.getColumnItemComputedClassList(column);
if (index % 2 === 1) {
@@ -400,15 +434,49 @@ const isDroppedColumn = (column: Column): boolean => {
return column.status === "dropped";
};
-const handleColumnDefaultFieldChange = (
- column: Column,
- defaultString: string
+const handleColumnDefaultInputChange = (event: Event, column: Column) => {
+ const value = (event.target as HTMLInputElement).value;
+ column.hasDefault = true;
+ column.defaultNull = undefined;
+ if (column.defaultString !== undefined) {
+ column.defaultString = value;
+ return;
+ }
+ // By default, user input is treated as expression.
+ column.defaultExpression = value;
+};
+
+const handleColumnDefaultFieldChange = (column: Column, key: string) => {
+ if (key === "expression") {
+ editColumnDefaultValueExpressionContext.value = column;
+ return;
+ }
+
+ const defaultValue = getDefaultValueByKey(key);
+ if (!defaultValue) {
+ return;
+ }
+
+ column.hasDefault = defaultValue.hasDefault;
+ column.defaultNull = defaultValue.defaultNull;
+ column.defaultString = defaultValue.defaultString;
+ column.defaultExpression = defaultValue.defaultExpression;
+ if (column.hasDefault && column.defaultNull) {
+ column.nullable = true;
+ }
+};
+
+const handleSelectedColumnDefaultValueExpressionChange = (
+ expression: string
) => {
- if (defaultString === "NULL" && column.nullable) {
- column.default = "NULL";
- } else if (defaultString === "EMPTY") {
- column.default = undefined;
+ const column = editColumnDefaultValueExpressionContext.value;
+ if (!column) {
+ return;
}
+ column.hasDefault = true;
+ column.defaultNull = undefined;
+ column.defaultString = undefined;
+ column.defaultExpression = expression;
};
const onClassificationSelect = (classificationId: string) => {
@@ -418,16 +486,6 @@ const onClassificationSelect = (classificationId: string) => {
state.pendingUpdateColumn.classification = classificationId;
state.pendingUpdateColumn = undefined;
};
-
-watchEffect(() => {
- shownColumnList.value.forEach((column) => {
- if (column.default === "NULL") {
- column.default = column.nullable ? "NULL" : undefined;
- } else if (column.default === "") {
- column.default = undefined;
- }
- });
-});
</script>
<style scoped>
diff --git a/frontend/src/components/SchemaEditorV1/utils/columnDefaultValue.ts b/frontend/src/components/SchemaEditorV1/utils/columnDefaultValue.ts
new file mode 100644
index 0000000000..7de202e124
--- /dev/null
+++ b/frontend/src/components/SchemaEditorV1/utils/columnDefaultValue.ts
@@ -0,0 +1,112 @@
+import { Engine } from "@/types/proto/v1/common";
+
+interface DefaultValue {
+ hasDefault: boolean;
+ defaultNull?: boolean;
+ defaultString?: string;
+ defaultExpression?: string;
+}
+
+export interface DefaultValueOption {
+ key: string;
+ value: DefaultValue;
+}
+
+const NO_DEFAULT_OPTION: DefaultValueOption = {
+ key: "no-default",
+ value: {
+ hasDefault: false,
+ },
+};
+
+const EMPTY_STRING_OPTION: DefaultValueOption = {
+ key: "empty-string",
+ value: {
+ hasDefault: true,
+ defaultString: "",
+ },
+};
+
+const EXPRESSION_OPTION: DefaultValueOption = {
+ key: "expression",
+ value: {
+ hasDefault: true,
+ defaultExpression: "",
+ },
+};
+
+const INT_ZERO_OPTION: DefaultValueOption = {
+ key: "zero",
+ value: {
+ hasDefault: true,
+ defaultExpression: "0",
+ },
+};
+
+const BOOLEAN_TRUE_OPTION: DefaultValueOption = {
+ key: "true",
+ value: {
+ hasDefault: true,
+ defaultExpression: "true",
+ },
+};
+
+const BOOLEAN_FALSE_OPTION: DefaultValueOption = {
+ key: "false",
+ value: {
+ hasDefault: true,
+ defaultExpression: "false",
+ },
+};
+
+export const getColumnTypeDefaultValueOptions = (
+ engine: Engine,
+ columnType: string
+): DefaultValueOption[] => {
+ const type = columnType.toUpperCase();
+ if (engine === Engine.MYSQL || engine === Engine.TIDB) {
+ if (
+ type === "TEXT" ||
+ type.startsWith("VARCHAR") ||
+ type.startsWith("CHAR")
+ ) {
+ return [NO_DEFAULT_OPTION, EMPTY_STRING_OPTION, EXPRESSION_OPTION];
+ } else if (
+ type === "INT" ||
+ type === "INTEGER" ||
+ type === "FLOAT" ||
+ type === "DOUBLE"
+ ) {
+ return [NO_DEFAULT_OPTION, INT_ZERO_OPTION];
+ } else if (type === "BOOL" || type === "BOOLEAN") {
+ return [NO_DEFAULT_OPTION, BOOLEAN_TRUE_OPTION, BOOLEAN_FALSE_OPTION];
+ }
+ } else if (engine === Engine.POSTGRES) {
+ if (
+ type === "TEXT" ||
+ type.startsWith("VARCHAR") ||
+ type.startsWith("CHAR")
+ ) {
+ return [NO_DEFAULT_OPTION, EMPTY_STRING_OPTION, EXPRESSION_OPTION];
+ } else if (type === "INTEGER" || type === "BIGINT" || type === "SERIAL") {
+ return [NO_DEFAULT_OPTION, INT_ZERO_OPTION];
+ } else if (type === "BOOLEAN") {
+ return [NO_DEFAULT_OPTION, BOOLEAN_TRUE_OPTION, BOOLEAN_FALSE_OPTION];
+ }
+ }
+
+ // Default options.
+ return [NO_DEFAULT_OPTION, EMPTY_STRING_OPTION, EXPRESSION_OPTION];
+};
+
+export const getDefaultValueByKey = (key: string) => {
+ const options = [
+ NO_DEFAULT_OPTION,
+ EMPTY_STRING_OPTION,
+ EXPRESSION_OPTION,
+ INT_ZERO_OPTION,
+ BOOLEAN_TRUE_OPTION,
+ BOOLEAN_FALSE_OPTION,
+ ];
+ return options.find((option) => option.key === key)?.value;
+};
diff --git a/frontend/src/components/SchemaEditorV1/utils/metadata.ts b/frontend/src/components/SchemaEditorV1/utils/metadata.ts
index 789f5423bd..e7b0d46563 100644
--- a/frontend/src/components/SchemaEditorV1/utils/metadata.ts
+++ b/frontend/src/components/SchemaEditorV1/utils/metadata.ts
@@ -90,7 +90,10 @@ export const mergeSchemaEditToMetadata = (
column.comment = columnEdit.comment;
column.userComment = columnEdit.userComment;
column.classification = columnEdit.classification ?? "";
- column.default = columnEdit.default;
+ column.hasDefault = columnEdit.hasDefault;
+ column.defaultNull = columnEdit.defaultNull;
+ column.defaultString = columnEdit.defaultString;
+ column.defaultExpression = columnEdit.defaultExpression;
}
}
for (const column of table.columns) {
@@ -228,7 +231,10 @@ const transformSchemaEditToMetadata = (schemaEdit: Schema): SchemaMetadata => {
name: column.name,
type: column.type,
nullable: column.nullable,
- default: column.default,
+ hasDefault: column.hasDefault,
+ defaultNull: column.defaultNull,
+ defaultString: column.defaultString,
+ defaultExpression: column.defaultExpression,
comment: column.comment,
userComment: column.userComment,
classification: column.classification,
@@ -275,7 +281,10 @@ const transformTableEditToMetadata = (tableEdit: Table): TableMetadata => {
name: column.name,
type: column.type,
nullable: column.nullable,
- default: column.default,
+ hasDefault: column.hasDefault,
+ defaultNull: column.defaultNull,
+ defaultString: column.defaultString,
+ defaultExpression: column.defaultExpression,
comment: column.comment,
userComment: column.userComment,
classification: column.classification,
@@ -310,7 +319,10 @@ const transformColumnEditToMetadata = (columnEdit: Column): ColumnMetadata => {
name: columnEdit.name,
type: columnEdit.type,
nullable: columnEdit.nullable,
- default: columnEdit.default,
+ hasDefault: columnEdit.hasDefault,
+ defaultNull: columnEdit.defaultNull,
+ defaultString: columnEdit.defaultString,
+ defaultExpression: columnEdit.defaultExpression,
comment: columnEdit.comment,
userComment: columnEdit.userComment,
classification: columnEdit.classification,
@@ -358,7 +370,10 @@ export const rebuildEditableSchemas = (
editableColumn.comment = column.comment;
editableColumn.userComment = column.userComment;
editableColumn.classification = column.classification;
- editableColumn.default = column.default;
+ editableColumn.hasDefault = column.hasDefault;
+ editableColumn.defaultNull = column.defaultNull;
+ editableColumn.defaultString = column.defaultString;
+ editableColumn.defaultExpression = column.defaultExpression;
}
for (const column of table.columns) {
diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.json
index 896c1bda4d..a7d9b1af6b 100644
--- a/frontend/src/locales/en-US.json
+++ b/frontend/src/locales/en-US.json
@@ -2119,6 +2119,14 @@
"foreign-key": "Foreign Key",
"classification": "Classification"
},
+ "default": {
+ "no-default": "No default",
+ "empty-string": "Empty string",
+ "expression": "Expression",
+ "zero": "0",
+ "true": "True",
+ "false": "False"
+ },
"nothing-changed": "Nothing changed",
"message": {
"invalid-schema-name": "Invalid schema name",
diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.json
index 19655ac81b..a750dd9cab 100644
--- a/frontend/src/locales/es-ES.json
+++ b/frontend/src/locales/es-ES.json
@@ -2115,6 +2115,14 @@
"foreign-key": "Clave foránea",
"classification": "Clasificación"
},
+ "default": {
+ "no-default": "Sin valor predeterminado",
+ "empty-string": "Cadena vacía",
+ "expression": "Expresión",
+ "zero": "0",
+ "true": "True",
+ "false": "False"
+ },
"nothing-changed": "Nada ha cambiado",
"message": {
"invalid-schema-name": "Nombre de esquema inválido",
diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.json
index c310e15ec6..602f8bea65 100644
--- a/frontend/src/locales/zh-CN.json
+++ b/frontend/src/locales/zh-CN.json
@@ -2115,6 +2115,14 @@
"foreign-key": "外键",
"classification": "分类分级"
},
+ "default": {
+ "no-default": "无默认值",
+ "empty-string": "空字符串",
+ "expression": "表达式",
+ "zero": "0",
+ "true": "True",
+ "false": "False"
+ },
"nothing-changed": "无任何改动",
"message": {
"invalid-schema-name": "无效的 Schema 名称",
diff --git a/frontend/src/types/v1/schemaEditor/atomType.ts b/frontend/src/types/v1/schemaEditor/atomType.ts
index 90e057f119..daf4b68d27 100644
--- a/frontend/src/types/v1/schemaEditor/atomType.ts
+++ b/frontend/src/types/v1/schemaEditor/atomType.ts
@@ -15,7 +15,10 @@ export interface Column {
nullable: boolean;
comment: string;
userComment: string;
- default?: string;
+ hasDefault: boolean;
+ defaultNull?: boolean;
+ defaultString?: string;
+ defaultExpression?: string;
status: Status;
classification?: string;
}
@@ -70,7 +73,10 @@ export const convertColumnMetadataToColumn = (
nullable: columnMetadata.nullable,
comment: columnMetadata.comment,
userComment: columnMetadata.userComment,
- default: columnMetadata.default,
+ hasDefault: columnMetadata.hasDefault,
+ defaultNull: columnMetadata.defaultNull,
+ defaultString: columnMetadata.defaultString,
+ defaultExpression: columnMetadata.defaultExpression,
classification: columnMetadata.classification,
status,
};