Skip to content

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

chore: update column default value in field template and column table (#8747)

ecmadao7d06ce763ae0
diff --git a/frontend/src/components/ColumnTable.vue b/frontend/src/components/ColumnTable.vue
index 1358536f5d..8d9308004d 100644
--- a/frontend/src/components/ColumnTable.vue
+++ b/frontend/src/components/ColumnTable.vue
@@ -83,7 +83,7 @@
{{ column.type }}
</BBTableCell>
<BBTableCell class="bb-grid-cell">
- {{ column.default }}
+ {{ getColumnDefaultValuePlaceholder(column) }}
</BBTableCell>
<BBTableCell class="bb-grid-cell">
{{ column.nullable }}
@@ -168,6 +168,7 @@ import { cloneDeep } from "lodash-es";
import { computed, PropType, reactive } from "vue";
import { useI18n } from "vue-i18n";
import { BBTableColumn } from "@/bbkit/types";
+import { getColumnDefaultValuePlaceholder } from "@/components/SchemaEditorV1/utils/columnDefaultValue";
import {
useCurrentUserV1,
useDBSchemaV1Store,
diff --git a/frontend/src/components/SchemaEditorV1/Panels/TableColumnEditor.vue b/frontend/src/components/SchemaEditorV1/Panels/TableColumnEditor.vue
index 68db5f3945..8b899e328d 100644
--- a/frontend/src/components/SchemaEditorV1/Panels/TableColumnEditor.vue
+++ b/frontend/src/components/SchemaEditorV1/Panels/TableColumnEditor.vue
@@ -106,7 +106,7 @@
<NDropdown
trigger="click"
:disabled="readonly || disableAlterColumn(column)"
- :options="getColumnDefaultValueOptions(column)"
+ :options="getColumnDefaultValueOptions(engine, column.type)"
@select="(key: string) => handleColumnDefaultFieldChange(column, key)"
>
<button class="absolute right-5">
@@ -241,9 +241,10 @@ import { Table, Column, ForeignKey } from "@/types/v1/schemaEditor";
import { getDataTypeSuggestionList } from "@/utils";
import ColumnDefaultValueExpressionModal from "../Modals/ColumnDefaultValueExpressionModal.vue";
import {
- DefaultValueOption,
- getColumnTypeDefaultValueOptions,
+ getColumnDefaultDisplayString,
+ getColumnDefaultValuePlaceholder,
getDefaultValueByKey,
+ getColumnDefaultValueOptions,
} from "../utils/columnDefaultValue";
interface LocalState {
@@ -341,40 +342,6 @@ 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) {
diff --git a/frontend/src/components/SchemaEditorV1/utils/columnDefaultValue.ts b/frontend/src/components/SchemaEditorV1/utils/columnDefaultValue.ts
index 7de202e124..a215b9003a 100644
--- a/frontend/src/components/SchemaEditorV1/utils/columnDefaultValue.ts
+++ b/frontend/src/components/SchemaEditorV1/utils/columnDefaultValue.ts
@@ -1,4 +1,6 @@
+import { useI18n } from "vue-i18n";
import { Engine } from "@/types/proto/v1/common";
+import { ColumnDefaultValue } from "@/types/v1/schemaEditor";
interface DefaultValue {
hasDefault: boolean;
@@ -110,3 +112,41 @@ export const getDefaultValueByKey = (key: string) => {
];
return options.find((option) => option.key === key)?.value;
};
+
+export const getColumnDefaultDisplayString = (column: ColumnDefaultValue) => {
+ if (!column.hasDefault || column.defaultNull) {
+ return undefined;
+ }
+ return column.defaultString || column.defaultExpression || "";
+};
+
+export const getColumnDefaultValuePlaceholder = (
+ column: ColumnDefaultValue
+): 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 "";
+};
+
+export const getColumnDefaultValueOptions = (
+ engine: Engine,
+ columnType: string
+): DefaultValueOption[] => {
+ const { t } = useI18n();
+ return getColumnTypeDefaultValueOptions(engine, columnType).map((option) => {
+ return {
+ ...option,
+ label: t(`schema-editor.default.${option.key}`),
+ };
+ });
+};
diff --git a/frontend/src/components/SchemaTemplate/FieldTemplateForm.vue b/frontend/src/components/SchemaTemplate/FieldTemplateForm.vue
index 188a043c2e..f0486d7497 100644
--- a/frontend/src/components/SchemaTemplate/FieldTemplateForm.vue
+++ b/frontend/src/components/SchemaTemplate/FieldTemplateForm.vue
@@ -169,15 +169,28 @@
<label for="default-value" class="textlabel">
{{ $t("schema-template.form.default-value") }}
</label>
- <input
- v-model="state.column!.default"
- required
- name="default-value"
- type="text"
- class="textfield mt-1 w-full"
- :placeholder="getDefaultValue(state.column)"
- :disabled="!allowEdit"
- />
+ <div class="flex flex-row items-center relative">
+ <input
+ class="textfield mt-1 w-full"
+ type="text"
+ :value="getColumnDefaultDisplayString(state.column!)"
+ :disabled="!allowEdit"
+ :placeholder="getColumnDefaultValuePlaceholder(state.column!)"
+ @change="(e) => handleColumnDefaultInputChange(e)"
+ />
+ <NDropdown
+ trigger="click"
+ :disabled="!allowEdit"
+ :options="getColumnDefaultValueOptions(state.engine, state.column!.type)"
+ @select="(key: string) => handleColumnDefaultFieldChange(key)"
+ >
+ <button class="absolute right-5">
+ <heroicons-solid:chevron-up-down
+ class="w-4 h-auto text-gray-400"
+ />
+ </button>
+ </NDropdown>
+ </div>
</div>
<!-- nullable -->
@@ -248,11 +261,25 @@
@dismiss="state.showClassificationDrawer = false"
@select="onClassificationSelect"
/>
+
+ <ColumnDefaultValueExpressionModal
+ v-if="state.showColumnDefaultValueExpressionModal"
+ :expression="state.column!.defaultExpression"
+ @close="state.showColumnDefaultValueExpressionModal = false"
+ @update:expression="handleSelectedColumnDefaultValueExpressionChange"
+ />
</template>
<script lang="ts" setup>
import { isEqual, cloneDeep } from "lodash-es";
+import { NDropdown } from "naive-ui";
import { computed, reactive, watch } from "vue";
+import {
+ getColumnDefaultDisplayString,
+ getColumnDefaultValuePlaceholder,
+ getDefaultValueByKey,
+ getColumnDefaultValueOptions,
+} from "@/components/SchemaEditorV1/utils/columnDefaultValue";
import { DrawerContent } from "@/components/v2";
import { useSettingV1Store } from "@/store";
import { Engine } from "@/types/proto/v1/common";
@@ -268,12 +295,7 @@ import {
convertKVListToLabels,
convertLabelsToKVList,
} from "@/utils";
-import {
- engineList,
- getDefaultValue,
- caregoryList,
- classificationConfig,
-} from "./utils";
+import { engineList, caregoryList, classificationConfig } from "./utils";
const props = defineProps<{
create: boolean;
@@ -285,6 +307,7 @@ const emit = defineEmits(["dismiss"]);
interface LocalState extends SchemaTemplateSetting_FieldTemplate {
showClassificationDrawer: boolean;
+ showColumnDefaultValueExpressionModal: boolean;
kvList: { key: string; value: string }[];
}
@@ -294,9 +317,11 @@ const state = reactive<LocalState>({
category: props.template.category,
column: Object.assign({}, props.template.column),
showClassificationDrawer: false,
+ showColumnDefaultValueExpressionModal: false,
config: Object.assign({}, props.template.config),
kvList: [],
});
+
const settingStore = useSettingV1Store();
const allowEdit = computed(() => {
return (
@@ -420,4 +445,52 @@ const onClassificationSelect = (id: string) => {
state.column.classification = id;
state.showClassificationDrawer = false;
};
+
+const handleColumnDefaultInputChange = (event: Event) => {
+ const value = (event.target as HTMLInputElement).value;
+ if (!state.column) {
+ return;
+ }
+ state.column.hasDefault = true;
+ state.column.defaultNull = undefined;
+ if (state.column.defaultString !== undefined) {
+ state.column.defaultString = value;
+ return;
+ }
+ // By default, user input is treated as expression.
+ state.column.defaultExpression = value;
+};
+
+const handleColumnDefaultFieldChange = (key: string) => {
+ if (key === "expression") {
+ state.showColumnDefaultValueExpressionModal = true;
+ return;
+ }
+
+ const defaultValue = getDefaultValueByKey(key);
+ if (!defaultValue || !state.column) {
+ return;
+ }
+
+ state.column.hasDefault = defaultValue.hasDefault;
+ state.column.defaultNull = defaultValue.defaultNull;
+ state.column.defaultString = defaultValue.defaultString;
+ state.column.defaultExpression = defaultValue.defaultExpression;
+ if (state.column.hasDefault && state.column.defaultNull) {
+ state.column.nullable = true;
+ }
+};
+
+const handleSelectedColumnDefaultValueExpressionChange = (
+ expression: string
+) => {
+ if (!state.column) {
+ return;
+ }
+ state.column.hasDefault = true;
+ state.column.defaultNull = undefined;
+ state.column.defaultString = undefined;
+ state.column.defaultExpression = expression;
+ state.showColumnDefaultValueExpressionModal = false;
+};
</script>
diff --git a/frontend/src/components/SchemaTemplate/FieldTemplateTable.vue b/frontend/src/components/SchemaTemplate/FieldTemplateTable.vue
index d5a539b04f..38a0784477 100644
--- a/frontend/src/components/SchemaTemplate/FieldTemplateTable.vue
+++ b/frontend/src/components/SchemaTemplate/FieldTemplateTable.vue
@@ -24,7 +24,7 @@
{{ item.column?.type }}
</div>
<div class="bb-grid-cell">
- {{ getDefaultValue(item.column) }}
+ {{ getColumnDefaultValuePlaceholder(item.column!) }}
</div>
<div class="bb-grid-cell">
{{ item.column?.comment }}
@@ -64,13 +64,14 @@ import { NPopconfirm } from "naive-ui";
import { computed } from "vue";
import { useI18n } from "vue-i18n";
import { BBGrid, BBGridColumn } from "@/bbkit";
+import { getColumnDefaultValuePlaceholder } from "@/components/SchemaEditorV1/utils/columnDefaultValue";
import { useSettingV1Store } from "@/store";
import { Engine } from "@/types/proto/v1/common";
import {
SchemaTemplateSetting,
SchemaTemplateSetting_FieldTemplate,
} from "@/types/proto/v1/setting_service";
-import { getDefaultValue, classificationConfig } from "./utils";
+import { classificationConfig } from "./utils";
const props = defineProps<{
engine?: Engine;
diff --git a/frontend/src/components/SchemaTemplate/utils.ts b/frontend/src/components/SchemaTemplate/utils.ts
index b797ab4105..8ce24838ce 100644
--- a/frontend/src/components/SchemaTemplate/utils.ts
+++ b/frontend/src/components/SchemaTemplate/utils.ts
@@ -1,20 +1,9 @@
import { computed } from "vue";
import { useSettingV1Store } from "@/store";
import { Engine } from "@/types/proto/v1/common";
-import { ColumnMetadata } from "@/types/proto/v1/database_service";
export const engineList = [Engine.MYSQL, Engine.POSTGRES];
-export const getDefaultValue = (column: ColumnMetadata | undefined) => {
- if (column?.default) {
- return column.default;
- }
- if (column?.nullable) {
- return "NULL";
- }
- return "EMPTY";
-};
-
export const caregoryList = computed(() => {
const settingStore = useSettingV1Store();
const setting = settingStore.getSettingByName("bb.workspace.schema-template");
diff --git a/frontend/src/types/v1/schemaEditor/atomType.ts b/frontend/src/types/v1/schemaEditor/atomType.ts
index daf4b68d27..3e2868d272 100644
--- a/frontend/src/types/v1/schemaEditor/atomType.ts
+++ b/frontend/src/types/v1/schemaEditor/atomType.ts
@@ -8,17 +8,19 @@ import {
type Status = "normal" | "created" | "dropped";
-export interface Column {
+export interface ColumnDefaultValue {
+ hasDefault: boolean;
+ defaultNull?: boolean;
+ defaultString?: string;
+ defaultExpression?: string;
+}
+export interface Column extends ColumnDefaultValue {
id: string;
name: string;
type: string;
nullable: boolean;
comment: string;
userComment: string;
- hasDefault: boolean;
- defaultNull?: boolean;
- defaultString?: string;
- defaultExpression?: string;
status: Status;
classification?: string;
}