feat(structure): add copy-all-column-names to table designer
This commit is contained in:
11 files changed
+91
-1
No files matched your search
@@ -26,6 +26,8 @@ import {
|
||||
resolveInsertColumnIndex,
|
||||
restoreCharacterLengthUnitsAfterSave,
|
||||
splitDataType,
|
||||
structureColumnCommentsForCopy,
|
||||
structureColumnNamesForCopy,
|
||||
tableStructureIdentifierComparisonKey,
|
||||
} from "@/lib/table/tableStructureEditorState";
|
||||
|
||||
@@ -703,3 +705,42 @@ describe("tableStructureEditorState", () => {
|
||||
expect(isMysqlCharacterDataType("geometry")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("structureColumnNamesForCopy", () => {
|
||||
const column = (name: string, markedForDrop = false) => ({ name, markedForDrop });
|
||||
|
||||
it("keeps the visible field order", () => {
|
||||
expect(structureColumnNamesForCopy([column("id"), column("name"), column("note")])).toEqual(["id", "name", "note"]);
|
||||
});
|
||||
|
||||
it("drops fields marked for drop and blank names", () => {
|
||||
expect(structureColumnNamesForCopy([column("id"), column("drop_me", true), column(" "), column(" name ")])).toEqual(["id", "name"]);
|
||||
});
|
||||
|
||||
it("returns nothing for an empty table", () => {
|
||||
expect(structureColumnNamesForCopy([])).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("structureColumnCommentsForCopy", () => {
|
||||
it("maps trimmed names to trimmed comments", () => {
|
||||
const comments = structureColumnCommentsForCopy([
|
||||
{ name: "id", comment: " 主键 ", markedForDrop: false },
|
||||
{ name: "name", comment: "名称", markedForDrop: false },
|
||||
]);
|
||||
expect([...comments]).toEqual([
|
||||
["id", "主键"],
|
||||
["name", "名称"],
|
||||
]);
|
||||
});
|
||||
|
||||
it("skips dropped fields, blank comments and blank names", () => {
|
||||
const comments = structureColumnCommentsForCopy([
|
||||
{ name: "gone", comment: "已删除", markedForDrop: true },
|
||||
{ name: "empty", comment: " ", markedForDrop: false },
|
||||
{ name: "blank-name", comment: null, markedForDrop: false },
|
||||
{ name: " ", comment: "没有字段名", markedForDrop: false },
|
||||
]);
|
||||
expect(comments.size).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,26 @@
|
||||
import type { ColumnInfo, DatabaseConnectionInfo, DatabaseType, ForeignKeyInfo, IndexInfo, TriggerInfo } from "@/types/database.ts";
|
||||
import type { ColumnExtra, EditableStructureColumn, EditableStructureForeignKey, EditableStructureIndex, EditableStructureTrigger } from "@/lib/table/tableStructureEditorSql.ts";
|
||||
|
||||
/**
|
||||
* Column names offered by the structure editor's "copy all column names" action.
|
||||
* Fields marked for drop disappear on save, so they are not offered.
|
||||
*/
|
||||
export function structureColumnNamesForCopy(columns: readonly Pick<EditableStructureColumn, "name" | "markedForDrop">[]): string[] {
|
||||
return columns.filter((column) => !column.markedForDrop && column.name.trim()).map((column) => column.name.trim());
|
||||
}
|
||||
|
||||
/** Comment lookup for the same action, keyed by the trimmed column name. */
|
||||
export function structureColumnCommentsForCopy(columns: readonly Pick<EditableStructureColumn, "name" | "comment" | "markedForDrop">[]): Map<string, string> {
|
||||
const comments = new Map<string, string>();
|
||||
for (const column of columns) {
|
||||
if (column.markedForDrop) continue;
|
||||
const name = column.name.trim();
|
||||
const comment = column.comment?.trim();
|
||||
if (name && comment) comments.set(name, comment);
|
||||
}
|
||||
return comments;
|
||||
}
|
||||
|
||||
export function hasExistingColumnTypeChange(columns: readonly EditableStructureColumn[]): boolean {
|
||||
return columns.some((column) => !!column.original && !column.markedForDrop && column.dataType !== column.original.data_type);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user