Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat(frontend): renovate default value UI for SchemaEditor (#12158)
Liu Jifec9fa36ae08
diff --git a/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/TableColumnEditor.vue b/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/TableColumnEditor.vueindex 6455847f9e..66fe8bf9f1 100644--- a/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/TableColumnEditor.vue+++ b/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/TableColumnEditor.vue@@ -81,7 +81,6 @@ import ColumnDefaultValueExpressionModal from "../../Modals/ColumnDefaultValueEximport { useSchemaEditorContext } from "../../context";import type { EditStatus } from "../../types";import type { DefaultValueOption } from "../../utils";-import { getDefaultValueByKey, isTextOfColumnType } from "../../utils";import { markUUID } from "../common";import {DataTypeCell,@@ -90,9 +89,9 @@ import {ReorderCell,SelectionCell,SemanticTypeCell,+ DefaultValueCell,+ LabelsCell,} from "./components";-import DefaultValueCell from "./components/DefaultValueCell.vue";-import LabelsCell from "./components/LabelsCell.vue";interface LocalState {pendingUpdateColumn?: ColumnMetadata;@@ -419,11 +418,9 @@ const columns = computed(() => {return h(DefaultValueCell, {column,disabled: props.readonly || props.disableAlterColumn(column),- schemaTemplateColumnTypes: schemaTemplateColumnTypes.value,engine: props.engine,- onInput: (value: string) => handleColumnDefaultInput(column, value),- onSelect: (option: DefaultValueOption) =>- handleColumnDefaultSelect(column, option.key),+ onInput: (value) => handleColumnDefaultInput(column, value),+ onSelect: (option) => handleColumnDefaultSelect(column, option),});},},@@ -606,43 +603,58 @@ const schemaTemplateColumnTypes = computed(() => {});const handleColumnDefaultInput = (column: ColumnMetadata, value: string) => {- column.hasDefault = true;- column.defaultNull = undefined;- // If column is text type or has default string, we will treat user's input as string.- if (- isTextOfColumnType(props.engine, column.type) ||- column.defaultString !== undefined- ) {+ if (!column.hasDefault) return;+ if (column.defaultNull) return;++ if (typeof column.defaultString === "string") {column.defaultString = value;- column.defaultExpression = undefined;- markColumnStatus(column, "updated");- return;+ } else if (typeof column.defaultExpression === "string") {+ column.defaultExpression = value;}- // Otherwise we will treat user's input as expression.- column.defaultExpression = value;markColumnStatus(column, "updated");};-const handleColumnDefaultSelect = (column: ColumnMetadata, key: string) => {- if (key === "expression") {- editColumnDefaultValueExpressionContext.value = column;+const handleColumnDefaultSelect = (+ column: ColumnMetadata,+ option: DefaultValueOption+) => {+ const defaults = option.value;+ if (!defaults.hasDefault) {+ Object.assign(column, defaults);markColumnStatus(column, "updated");return;}-- const defaultValue = getDefaultValueByKey(key);- if (!defaultValue) {+ if (defaults.defaultNull) {+ Object.assign(column, defaults);+ markColumnStatus(column, "updated");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;+ if (typeof defaults.defaultExpression === "string") {+ Object.assign(column, {+ ...defaults,+ // copy current editing expr / string to column.defaultExpression+ defaultExpression:+ column.defaultExpression ??+ column.defaultString ??+ defaults.defaultExpression ??+ "",+ });+ markColumnStatus(column, "updated");+ return;+ }+ if (typeof defaults.defaultString === "string") {+ Object.assign(column, {+ ...defaults,+ // copy current editing string to column.defaultString+ defaultString:+ column.defaultString ??+ column.defaultExpression ??+ defaults.defaultString ??+ "",+ });+ markColumnStatus(column, "updated");+ return;}- markColumnStatus(column, "updated");};const handleSelectColumnDefaultValueExpression = (expression: string) => {diff --git a/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/components/DefaultValueCell.vue b/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/components/DefaultValueCell.vueindex 8e74e51241..d001a7fbf4 100644--- a/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/components/DefaultValueCell.vue+++ b/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/components/DefaultValueCell.vue@@ -1,44 +1,106 @@<template>- <DropdownInput- :value="value"+ <NDropdown+ trigger="click"+ placement="bottom-start"+ :value="dropdownValue":options="options"- :placeholder="placeholder":consistent-menu-width="false"- :style="style"- suffix-style="right: 3px"+ :disabled="disabled"class="bb-schema-editor--column-default-value-select"+ style="max-height: 20rem; overflow-y: auto; overflow-x: hidden"@focus="focused = true"@blur="focused = false"- @update:value="handleUpdateValue"- />+ @select="handleSelect"+ >+ <NInput+ ref="inputRef"+ :value="inputValue"+ :placeholder="placeholder"+ :disabled="inputDisabled"+ :style="inputStyle"+ @update:value="handleInput"+ >+ <template #suffix>+ <!-- use the same icon and style with NSelect -->+ <NElement+ tag="button"+ class="absolute top-1/2 right-[3px] -translate-y-1/2"+ :class="[+ disabled+ ? 'text-[var(--placeholder-color-disabled)] cursor-not-allowed'+ : 'text-[var(--placeholder-color)] hover:text-[var(--primary-color-hover)] cursor-pointer',+ ]"+ >+ <svg+ viewBox="0 0 16 16"+ fill="none"+ xmlns="http://www.w3.org/2000/svg"+ class="w-4 h-4"+ >+ <path+ d="M3.14645 5.64645C3.34171 5.45118 3.65829 5.45118 3.85355 5.64645L8 9.79289L12.1464 5.64645C12.3417 5.45118 12.6583 5.45118 12.8536 5.64645C13.0488 5.84171 13.0488 6.15829 12.8536 6.35355L8.35355 10.8536C8.15829 11.0488 7.84171 11.0488 7.64645 10.8536L3.14645 6.35355C2.95118 6.15829 2.95118 5.84171 3.14645 5.64645Z"+ fill="currentColor"+ ></path>+ </svg>+ </NElement>+ </template>+ </NInput>+ </NDropdown></template><script lang="ts" setup>-import type { SelectOption } from "naive-ui";+import {+ type InputInst,+ type SelectOption,+ NDropdown,+ NElement,+ NInput,+} from "naive-ui";import type { CSSProperties } from "vue";import { computed, ref } from "vue";+import { useI18n } from "vue-i18n";import type { DefaultValueOption } from "@/components/SchemaEditorLite/utils";import {+ DEFAULT_EXPRESSION_OPTION,+ DEFAULT_NULL_OPTION,+ DEFAULT_STRING_OPTION,+ NO_DEFAULT_OPTION,getColumnDefaultDisplayString,- getColumnDefaultValueOptions,getColumnDefaultValuePlaceholder,- getDefaultValueByKey,} from "@/components/SchemaEditorLite/utils";-import { DropdownInput } from "@/components/v2";import type { Engine } from "@/types/proto/v1/common";import type { ColumnMetadata } from "@/types/proto/v1/database_service";+type DefaultValueSelectOption = SelectOption & {+ value: string;+ defaults: DefaultValueOption;+};+const props = defineProps<{column: ColumnMetadata;engine: Engine;+ disabled?: boolean;}>();const emit = defineEmits<{(event: "input", value: string): void;(event: "select", option: DefaultValueOption): void;}>();++const { t } = useI18n();const focused = ref(false);+const inputRef = ref<InputInst>();++const dropdownValue = computed(() => {+ const { hasDefault, defaultNull, defaultString, defaultExpression } =+ props.column;+ if (!hasDefault) return "no-default";+ if (defaultNull) return "null";+ if (typeof defaultString === "string") return "string";+ if (typeof defaultExpression === "string") return "expression";+ return null;+});-const value = computed(() => {+const inputValue = computed(() => {return getColumnDefaultDisplayString(props.column) ?? null;});@@ -46,28 +108,66 @@ const placeholder = computed(() => {return getColumnDefaultValuePlaceholder(props.column);});-const options = computed(() => {- return getColumnDefaultValueOptions(- props.engine,- props.column.type- ).map<SelectOption>((opt) => ({- value: opt.key,- label: opt.label as string,- defaultValue: opt.value,- }));+const options = computed((): DefaultValueSelectOption[] => {+ return [+ {+ key: NO_DEFAULT_OPTION.key,+ value: NO_DEFAULT_OPTION.key,+ label: t("schema-editor.default.no-default"),+ defaults: NO_DEFAULT_OPTION,+ },+ {+ key: DEFAULT_NULL_OPTION.key,+ value: DEFAULT_NULL_OPTION.key,+ label: t("schema-editor.default.null"),+ defaults: DEFAULT_NULL_OPTION,+ },+ {+ key: DEFAULT_STRING_OPTION.key,+ value: DEFAULT_STRING_OPTION.key,+ label: t("schema-editor.default.value"),+ defaults: DEFAULT_STRING_OPTION,+ },+ {+ key: DEFAULT_EXPRESSION_OPTION.key,+ value: DEFAULT_EXPRESSION_OPTION.key,+ label: t("schema-editor.default.expression"),+ defaults: DEFAULT_EXPRESSION_OPTION,+ },+ ];});-const handleUpdateValue = (key: string) => {- const value = getDefaultValueByKey(key);- if (value) {- emit("select", { key, value });+const inputDisabled = computed(() => {+ if (props.disabled) return true;+ if (dropdownValue.value === "no-default" || dropdownValue.value === "null")+ return true;+ return false;+});++const handleSelect = (value: string) => {+ const option = options.value.find((opt) => opt.value === value);+ if (!option) {return;}- emit("input", key);+ const { defaults } = option;+ emit("select", defaults);++ if (+ typeof defaults.value.defaultExpression === "string" ||+ typeof defaults.value.defaultString === "string"+ ) {+ requestAnimationFrame(() => {+ inputRef.value?.focus();+ });+ }+};++const handleInput = (value: string) => {+ emit("input", value);};-const style = computed(() => {+const inputStyle = computed(() => {const style: CSSProperties = {"--n-padding-left": "6px","--n-padding-right": "16px",diff --git a/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/components/index.ts b/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/components/index.tsindex 76148bac83..63276dbcfb 100644--- a/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/components/index.ts+++ b/frontend/src/components/SchemaEditorLite/Panels/TableColumnEditor/components/index.ts@@ -1,6 +1,7 @@import DataTypeCell from "./DataTypeCell.vue";import DefaultValueCell from "./DefaultValueCell.vue";import ForeignKeyCell from "./ForeignKeyCell.vue";+import LabelsCell from "./LabelsCell.vue";import OperationCell from "./OperationCell.vue";import ReorderCell from "./ReorderCell.vue";import SelectionCell from "./SelectionCell.vue";@@ -14,4 +15,5 @@ export {ForeignKeyCell,OperationCell,SemanticTypeCell,+ LabelsCell,};diff --git a/frontend/src/components/SchemaEditorLite/utils/columnDefaultValue.ts b/frontend/src/components/SchemaEditorLite/utils/columnDefaultValue.tsindex 28a50c7c34..ae4a5d2407 100644--- a/frontend/src/components/SchemaEditorLite/utils/columnDefaultValue.ts+++ b/frontend/src/components/SchemaEditorLite/utils/columnDefaultValue.ts@@ -1,28 +1,40 @@import type { DropdownOption } from "naive-ui";import { t } from "@/plugins/i18n";import { Engine } from "@/types/proto/v1/common";+import type { ColumnMetadata } from "@/types/proto/v1/database_service";import type { ColumnDefaultValue } from "@/types/v1/schemaEditor";-interface DefaultValue {- hasDefault: boolean;- defaultNull?: boolean;- defaultString?: string;- defaultExpression?: string;-}+type DefaultValue = Pick<+ ColumnMetadata,+ "hasDefault" | "defaultNull" | "defaultString" | "defaultExpression"+>;export interface DefaultValueOption {key: string;value: DefaultValue;}-const NO_DEFAULT_OPTION: DefaultValueOption = {+export const NO_DEFAULT_OPTION: DefaultValueOption = {key: "no-default",value: {hasDefault: false,+ defaultNull: undefined,+ defaultString: undefined,+ defaultExpression: undefined,+ },+};++export const DEFAULT_NULL_OPTION: DefaultValueOption = {+ key: "null",+ value: {+ hasDefault: true,+ defaultNull: true,+ defaultString: undefined,+ defaultExpression: undefined,},};-const EMPTY_STRING_OPTION: DefaultValueOption = {+export const EMPTY_STRING_OPTION: DefaultValueOption = {key: "empty-string",value: {hasDefault: true,@@ -30,10 +42,22 @@ const EMPTY_STRING_OPTION: DefaultValueOption = {},};-const EXPRESSION_OPTION: DefaultValueOption = {+export const DEFAULT_STRING_OPTION: DefaultValueOption = {+ key: "string",+ value: {+ hasDefault: true,+ defaultNull: undefined,+ defaultString: "",+ defaultExpression: undefined,+ },+};++export const DEFAULT_EXPRESSION_OPTION: DefaultValueOption = {key: "expression",value: {hasDefault: true,+ defaultNull: undefined,+ defaultString: undefined,defaultExpression: "",},};@@ -85,7 +109,11 @@ export const getColumnTypeDefaultValueOptions = (type.startsWith("VARCHAR") ||type.startsWith("CHAR")) {- return [NO_DEFAULT_OPTION, EMPTY_STRING_OPTION, EXPRESSION_OPTION];+ return [+ NO_DEFAULT_OPTION,+ EMPTY_STRING_OPTION,+ DEFAULT_EXPRESSION_OPTION,+ ];} else if (type === "INTEGER" ||type === "INT" ||@@ -94,7 +122,7 @@ export const getColumnTypeDefaultValueOptions = (type === "MEDIUMINT" ||type === "BIGINT") {- return [NO_DEFAULT_OPTION, INT_ZERO_OPTION, EXPRESSION_OPTION];+ return [NO_DEFAULT_OPTION, INT_ZERO_OPTION, DEFAULT_EXPRESSION_OPTION];} else if (type === "FLOAT" || type === "DOUBLE") {return [NO_DEFAULT_OPTION, INT_ZERO_OPTION];} else if (type === "BOOL" || type === "BOOLEAN") {@@ -106,7 +134,11 @@ export const getColumnTypeDefaultValueOptions = (type.startsWith("VARCHAR") ||type.startsWith("CHAR")) {- return [NO_DEFAULT_OPTION, EMPTY_STRING_OPTION, EXPRESSION_OPTION];+ return [+ NO_DEFAULT_OPTION,+ EMPTY_STRING_OPTION,+ DEFAULT_EXPRESSION_OPTION,+ ];} else if (type === "SMALLINT" ||type === "INTEGER" ||@@ -118,21 +150,21 @@ export const getColumnTypeDefaultValueOptions = (type === "INT4" ||type === "INT8") {- return [NO_DEFAULT_OPTION, INT_ZERO_OPTION, EXPRESSION_OPTION];+ return [NO_DEFAULT_OPTION, INT_ZERO_OPTION, DEFAULT_EXPRESSION_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];+ return [NO_DEFAULT_OPTION, EMPTY_STRING_OPTION, DEFAULT_EXPRESSION_OPTION];};export const getDefaultValueByKey = (key: string) => {const options = [NO_DEFAULT_OPTION,EMPTY_STRING_OPTION,- EXPRESSION_OPTION,+ DEFAULT_EXPRESSION_OPTION,INT_ZERO_OPTION,BOOLEAN_TRUE_OPTION,BOOLEAN_FALSE_OPTION,diff --git a/frontend/src/locales/en-US.json b/frontend/src/locales/en-US.jsonindex 1e06772be9..1eb83c33fe 100644--- a/frontend/src/locales/en-US.json+++ b/frontend/src/locales/en-US.json@@ -2386,7 +2386,10 @@"expression": "Expression","zero": "0","true": "True",- "false": "False"+ "false": "False",+ "null": "NULL",+ "string": "String",+ "value": "Value"},"nothing-changed-for-database": "Nothing changed for database {database}","message": {diff --git a/frontend/src/locales/es-ES.json b/frontend/src/locales/es-ES.jsonindex dfcec508c3..fa25bacc6c 100644--- a/frontend/src/locales/es-ES.json+++ b/frontend/src/locales/es-ES.json@@ -2386,7 +2386,10 @@"expression": "Expresión","zero": "0","true": "True",- "false": "False"+ "false": "False",+ "null": "NULL",+ "string": "Cadena",+ "value": "Valor"},"nothing-changed-for-database": "Nada cambió para la base de datos {database}","message": {@@ -3537,4 +3540,4 @@"self": "Última actualización"}}-}\ No newline at end of file+}diff --git a/frontend/src/locales/ja-JP.json b/frontend/src/locales/ja-JP.jsonindex 159c8471f9..38872fc595 100644--- a/frontend/src/locales/ja-JP.json+++ b/frontend/src/locales/ja-JP.json@@ -2383,10 +2383,13 @@"default": {"no-default": "デフォルト値なし","empty-string": "空の文字列",- "expression": "表現",+ "expression": "式","zero": "0","true": "真実",- "false": "間違い"+ "false": "間違い",+ "null": "NULL",+ "string": "文字列",+ "value": "値"},"nothing-changed-for-database": "データベース {database} 変更なし","message": {@@ -3537,4 +3540,4 @@"self": "最後の更新"}}-}\ No newline at end of file+}diff --git a/frontend/src/locales/zh-CN.json b/frontend/src/locales/zh-CN.jsonindex 425ccff403..02faf01b63 100644--- a/frontend/src/locales/zh-CN.json+++ b/frontend/src/locales/zh-CN.json@@ -2386,7 +2386,10 @@"expression": "表达式","zero": "0","true": "True",- "false": "False"+ "false": "False",+ "null": "NULL",+ "string": "字符串",+ "value": "值"},"nothing-changed-for-database": "数据库 {database} 无任何改动","message": {@@ -3537,4 +3540,4 @@"self": "最后更新"}}-}\ No newline at end of file+}