fix(structure): trim trailing spaces from edited column names

This commit is contained in:
zipg authored and GitHub committed 2026-09-29 21:04:41 +08:00
1 parent ef05a6a19b
commit bb5ea45470
4 files changed
+427 -4

No files matched your search

@@ -13,6 +13,7 @@ import {
dataTypeLengthUnitValue,
DATA_TYPE_OPTIONS,
defaultNewColumnDataType,
draftColumnNameForSql,
getDataTypeLengthUnitOptions,
getDefaultLengthForType,
hasExistingColumnTypeChange,
@@ -1039,3 +1040,25 @@ describe("structureColumnCommentsForCopy", () => {
expect(comments.size).toBe(0);
});
});
describe("draftColumnNameForSql", () => {
// MySQL rejects identifiers that end with a space (ERROR 1166), so a name the
// user pasted with a trailing space must not reach the DDL builder as typed.
it("drops the trailing whitespace of a name the user typed", () => {
expect(draftColumnNameForSql("device_app_face_status ", "app_auth_status")).toBe("device_app_face_status");
expect(draftColumnNameForSql("display_name\t", "name")).toBe("display_name");
expect(draftColumnNameForSql("new_column\n ")).toBe("new_column");
});
// #9654: leading spaces are legal inside a backtick-quoted identifier, so the
// user's spelling is kept for a new column and for a metadata name alike.
it("keeps leading spaces of a name the user typed", () => {
expect(draftColumnNameForSql(" content1")).toBe(" content1");
});
it("keeps an untouched metadata name byte-exact so whitespace is never read as a rename", () => {
expect(draftColumnNameForSql(" content1", " content1")).toBe(" content1");
expect(draftColumnNameForSql("content1 ", "content1 ")).toBe("content1 ");
expect(draftColumnNameForSql("plain", "plain")).toBe("plain");
});
});
@@ -52,6 +52,21 @@ export function structureColumnCommentsForCopy(columns: readonly Pick<EditableSt
return comments;
}
/**
* Column name handed to the DDL builder for a draft column.
*
* MySQL rejects identifiers that end with a space (ERROR 1166 "Incorrect column
* name"), so a pasted name carrying a stray trailing space produced an
* unexecutable `ALTER TABLE ... CHANGE COLUMN ...` statement that the editor
* still previewed as executable. Only the trailing whitespace is dropped:
* leading spaces are legal in a backtick-quoted identifier and are kept, both
* for a name the user typed (`#9654`) and for a metadata name, which is passed
* through byte-exact so that an unrelated edit never turns into a bogus rename.
*/
export function draftColumnNameForSql(name: string, originalName?: string | null): string {
return originalName === name ? name : name.trimEnd();
}
export function hasExistingColumnTypeChange(columns: readonly EditableStructureColumn[]): boolean {
return columns.some((column) => !!column.original && !column.markedForDrop && column.dataType !== column.original.data_type);
}