feat(structure): improve type colors and index editing

This commit is contained in:
weekhy authored and GitHub committed 2026-10-04 00:04:49 +08:00
1 parent da1094df93
commit fa7dde7c03
33 files changed
+1631 -132

No files matched your search

@@ -38,6 +38,8 @@ export interface EditableStructureIndex {
name: string;
columns: string[];
nameEdited?: boolean;
/** First selected field, retained when a composite index's members change. */
autoNameColumn?: string;
isUnique: boolean;
isPrimary: boolean;
filter: string;
@@ -1257,6 +1257,35 @@ export function toColumnNames(columns: string[]): string {
const AUTO_INDEX_NAME_MAX_LENGTH = 63;
export type StructureIndexKind = "primary" | "unique" | "index" | "fulltext" | "spatial";
type IndexNamingOptions = Partial<Pick<EditableStructureIndex, "isPrimary" | "isUnique" | "indexType">> & { maxLength?: number };
export function structureIndexKind(index: IndexNamingOptions): StructureIndexKind {
if (index.isPrimary) return "primary";
if (index.indexType?.trim().toUpperCase() === "FULLTEXT") return "fulltext";
if (index.indexType?.trim().toUpperCase() === "SPATIAL") return "spatial";
return index.isUnique ? "unique" : "index";
}
export type SpecialIndexColumnIssue = "specialIndexUnique" | "fulltextIndexColumns" | "spatialIndexColumn" | "spatialIndexNullable";
export function specialIndexColumnIssue(dialect: string, indexType: string, columns: readonly Pick<EditableStructureColumn, "dataType" | "isNullable">[], isUnique = false): SpecialIndexColumnIssue | null {
const type = indexType.trim().toUpperCase();
const fulltext = dialect === "mysql" && type === "FULLTEXT";
const spatial = (dialect === "mysql" || dialect === "sqlserver") && type === "SPATIAL";
if (!fulltext && !spatial) return null;
if (isUnique) return "specialIndexUnique";
if (!columns.length) return null;
const types = columns.map((column) => splitDataType(column.dataType).baseType.toLowerCase());
if (fulltext && types.some((value) => !/^(?:char|varchar|tinytext|text|mediumtext|longtext)$/.test(value))) return "fulltextIndexColumns";
if (spatial) {
const validType = dialect === "mysql" ? /^(?:geometry|point|linestring|polygon|multipoint|multilinestring|multipolygon|geometrycollection|geomcollection)$/ : /^(?:geometry|geography)$/;
if (columns.length !== 1 || !validType.test(types[0])) return "spatialIndexColumn";
if (dialect === "mysql" && columns[0].isNullable) return "spatialIndexNullable";
}
return null;
}
function normalizeIndexNamePart(value: string): string {
const trimmed = value.trim();
const unquoted = (trimmed.startsWith("[") && trimmed.endsWith("]")) || (trimmed.startsWith("`") && trimmed.endsWith("`")) || (trimmed.startsWith('"') && trimmed.endsWith('"')) ? trimmed.slice(1, -1) : trimmed;
@@ -1275,6 +1304,7 @@ function truncateIndexName(value: string, maxLength = AUTO_INDEX_NAME_MAX_LENGTH
return `${value.slice(0, maxLength - suffix.length).replace(/_+$/g, "")}${suffix}`;
}
// Keep the original API and table-qualified naming for existing callers and dialects.
export function generateIndexName(tableName: string, columns: string[], maxLength = AUTO_INDEX_NAME_MAX_LENGTH): string {
const parts = [tableName, ...columns].map(normalizeIndexNamePart).filter(Boolean);
if (parts.length === 0) return "";
@@ -1297,6 +1327,53 @@ export function generateUniqueIndexName(tableName: string, columns: string[], ex
return base;
}
function indexNameHash(value: string): string {
// Keep long names deterministic and distinguish columns beyond the cutoff.
let hash = 2166136261;
for (let i = 0; i < value.length; i++) hash = Math.imul(hash ^ value.charCodeAt(i), 16777619);
return (hash >>> 0).toString(16).padStart(8, "0");
}
function truncateShortIndexName(value: string, maxLength = AUTO_INDEX_NAME_MAX_LENGTH): string {
if (value.length <= maxLength) return value;
const suffix = `_${indexNameHash(value)}`;
if (maxLength <= suffix.length) return value.slice(0, maxLength);
return `${value.slice(0, maxLength - suffix.length).replace(/_+$/g, "")}${suffix}`;
}
export function generateShortIndexName(columnName: string, options: IndexNamingOptions = {}): string {
if (options.isPrimary) return "PRIMARY";
if (!columnName.trim()) return "";
// MySQL permits BMP Unicode identifiers, but not supplementary characters.
const normalized = columnName
.trim()
.replace(/[^\p{L}\p{N}]+/gu, "_")
.replace(/[\uD800-\uDFFF]/g, "_")
.replace(/_+/g, "_")
.replace(/^_+|_+$/g, "")
.toLowerCase();
const name = normalized || `column_${indexNameHash(columnName)}`;
const prefix = { primary: "pk", unique: "uk", index: "idx", fulltext: "ft", spatial: "sp" }[structureIndexKind(options)];
return truncateShortIndexName(`${prefix}_${name}`, options.maxLength);
}
export function generateUniqueShortIndexName(columnName: string, existingNames: Iterable<string>, options: IndexNamingOptions = {}): string {
const maxLength = options.maxLength ?? AUTO_INDEX_NAME_MAX_LENGTH;
const base = generateShortIndexName(columnName, options);
if (!base) return "";
if (options.isPrimary) return base;
const taken = new Set([...existingNames].map((name) => name.trim().toLowerCase()).filter(Boolean));
if (!taken.has(base.toLowerCase())) return base;
for (let counter = 2; ; counter++) {
const suffix = `_${counter}`;
const stem = generateShortIndexName(columnName, { ...options, maxLength: Math.max(1, maxLength - suffix.length) });
const candidate = `${stem}${suffix}`;
if (!taken.has(candidate.toLowerCase())) return candidate;
}
}
export function splitDataType(raw: string): { baseType: string; params: string } {
const trimmed = raw.trim();
const parenIdx = trimmed.indexOf("(");
@@ -14,7 +14,7 @@ export interface TableStructureRefreshScope {
export function visibleTableStructureRefreshScope(activeTab: TableInfoTab): TableStructureRefreshScope {
switch (activeTab) {
case "columns":
return { columns: true, indexes: false, foreignKeys: false, constraints: false, triggers: false, partitions: false, tableComment: true };
return { columns: true, indexes: true, foreignKeys: false, constraints: false, triggers: false, partitions: false, tableComment: true };
case "indexes":
return { columns: true, indexes: true, foreignKeys: false, constraints: false, triggers: false, partitions: false, tableComment: true };
case "foreignKeys":