feat(mysql): edit column charset and collation

This commit is contained in:
DengQingNian authored and GitHub committed 2026-07-10 15:13:57 +08:00
1 parent 0674433eea
commit e429c8945b
38 files changed
+524 -20

No files matched your search

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { combineDataTypeForDatabase, createColumnDrafts, dataTypeLengthInputValue, isDataTypeLengthDisabled, isSqlServerIdentityCompatibleDataType, splitDataType } from "@/lib/table/tableStructureEditorState";
import { combineDataTypeForDatabase, createColumnDrafts, dataTypeLengthInputValue, isDataTypeLengthDisabled, isMysqlCharacterDataType, isSqlServerIdentityCompatibleDataType, splitDataType } from "@/lib/table/tableStructureEditorState";
describe("tableStructureEditorState", () => {
it("keeps mysql unsigned attributes in the editable base type", () => {
@@ -87,4 +87,28 @@ describe("tableStructureEditorState", () => {
expect(isSqlServerIdentityCompatibleDataType("varchar(255)")).toBe(false);
expect(isSqlServerIdentityCompatibleDataType("numeric(18, 2)")).toBe(false);
});
it("identifies MySQL character data types that accept charset/collation", () => {
expect(isMysqlCharacterDataType("char(1)")).toBe(true);
expect(isMysqlCharacterDataType("varchar(255)")).toBe(true);
expect(isMysqlCharacterDataType("tinytext")).toBe(true);
expect(isMysqlCharacterDataType("text")).toBe(true);
expect(isMysqlCharacterDataType("mediumtext")).toBe(true);
expect(isMysqlCharacterDataType("longtext")).toBe(true);
expect(isMysqlCharacterDataType("enum('a','b')")).toBe(true);
expect(isMysqlCharacterDataType("set('x','y')")).toBe(true);
expect(isMysqlCharacterDataType("int")).toBe(false);
expect(isMysqlCharacterDataType("bigint(20) unsigned")).toBe(false);
expect(isMysqlCharacterDataType("decimal(10,2)")).toBe(false);
expect(isMysqlCharacterDataType("float")).toBe(false);
expect(isMysqlCharacterDataType("double")).toBe(false);
expect(isMysqlCharacterDataType("date")).toBe(false);
expect(isMysqlCharacterDataType("datetime")).toBe(false);
expect(isMysqlCharacterDataType("timestamp")).toBe(false);
expect(isMysqlCharacterDataType("json")).toBe(false);
expect(isMysqlCharacterDataType("binary(16)")).toBe(false);
expect(isMysqlCharacterDataType("varbinary(255)")).toBe(false);
expect(isMysqlCharacterDataType("blob")).toBe(false);
expect(isMysqlCharacterDataType("geometry")).toBe(false);
});
});
@@ -75,6 +75,8 @@ function templateColumn(createId: () => string, name: string, dataType: string,
defaultValue,
comment,
isPrimaryKey: false,
characterSet: "",
collation: "",
extra: {},
markedForDrop: false,
};
@@ -25,6 +25,8 @@ export interface EditableStructureColumn {
comment: string;
isPrimaryKey: boolean;
extra: ColumnExtra;
characterSet?: string;
collation?: string;
original?: ColumnInfo;
originalPosition?: number;
markedForDrop: boolean;
@@ -614,6 +614,8 @@ export function createColumnDrafts(columns: ColumnInfo[], databaseType?: Databas
defaultValue,
comment: column.comment ?? "",
isPrimaryKey: column.is_primary_key,
characterSet: column.character_set ?? "",
collation: column.collation ?? "",
extra: parseExtraToColumnExtra(column.extra, databaseType),
original: { ...column, column_default: column.column_default === null ? null : defaultValue },
originalPosition: index,
@@ -809,6 +811,14 @@ export function splitDataType(raw: string): { baseType: string; params: string }
return { baseType, params };
}
/** MySQL character/text types that accept `CHARACTER SET` and `COLLATE`. */
const MYSQL_CHARACTER_DATA_TYPES = new Set(["char", "varchar", "tinytext", "text", "mediumtext", "longtext", "enum", "set"]);
export function isMysqlCharacterDataType(dataType: string): boolean {
const { baseType } = splitDataType(dataType);
return MYSQL_CHARACTER_DATA_TYPES.has(baseType.trim().replace(/\s+/g, " ").toLowerCase());
}
export function isSqlServerIdentityCompatibleDataType(rawDataType: string): boolean {
const { baseType, params } = splitDataType(rawDataType);
const normalized = baseType.trim().replace(/\s+/g, " ").toLowerCase();