feat(structure): auto-switch column length default based on data type

When changing a column's data type in the table structure editor, the
length parameter now updates to a sensible default for the new type
(e.g. int→11, tinyint→4) instead of preserving the old value.
This commit is contained in:
t8y2 2026-05-28 21:26:10 +08:00
parent c943bf4179
commit 9aae48d089
2 changed files with 37 additions and 1 deletions

View File

@ -42,6 +42,7 @@ import {
createColumnDrafts,
createIndexDrafts,
DATA_TYPE_OPTIONS,
getDefaultLengthForType,
splitDataType,
toColumnNames,
} from "@/lib/tableStructureEditorState";
@ -574,7 +575,7 @@ watch(
(column.dataType = combineDataTypeForDatabase(
databaseType,
v,
splitDataType(column.dataType).params,
getDefaultLengthForType(databaseType, v),
))
"
/>

View File

@ -230,6 +230,36 @@ export const DATA_TYPE_OPTIONS: Record<string, string[]> = {
],
};
export const DEFAULT_TYPE_LENGTHS: Record<string, string> = {
tinyint: "4",
smallint: "6",
mediumint: "9",
int: "11",
integer: "11",
int4: "11",
bigint: "20",
int8: "20",
float: "10,2",
real: "10,2",
"double precision": "10,2",
double: "10,2",
decimal: "10,0",
numeric: "10,0",
number: "10,0",
char: "1",
character: "1",
varchar: "255",
"character varying": "255",
varchar2: "255",
nvarchar2: "255",
nvarchar: "255",
nchar: "1",
varbinary: "255",
binary: "1",
bit: "1",
year: "4",
};
export function createColumnDrafts(columns: ColumnInfo[]): EditableStructureColumn[] {
return columns.map((column, index) => ({
id: `existing:${column.name}`,
@ -335,6 +365,11 @@ function isValidTemporalPrecision(dbType: DatabaseType | undefined, params: stri
return Number.isInteger(value) && value >= 0 && value <= max && String(value) === params;
}
export function getDefaultLengthForType(_dbType: DatabaseType | undefined, baseType: string): string {
const key = baseType.trim().toLowerCase();
return DEFAULT_TYPE_LENGTHS[key] ?? "";
}
export function buildStructureTargetLabel(
connectionName: string | undefined,
database: string | undefined,