feat(structure): insert column after selected
This commit is contained in:
1 parent
b6581d3dc9
commit
3b54fa56df
4 files changed
+108
-19
No files matched your search
@@ -18,6 +18,7 @@ import {
|
||||
mysqlEnumDataType,
|
||||
parseExtraToColumnExtra,
|
||||
rehydrateColumnDraftsFromMetadata,
|
||||
resolveInsertColumnIndex,
|
||||
restoreDamengLengthUnitsAfterSave,
|
||||
splitDataType,
|
||||
} from "@/lib/table/tableStructureEditorState";
|
||||
@@ -248,6 +249,19 @@ describe("tableStructureEditorState", () => {
|
||||
expect(hasExistingColumnTypeChange([column])).toBe(false);
|
||||
});
|
||||
|
||||
it("inserts new columns after the selected row or appends when none is selected", () => {
|
||||
const columns = [{ id: "a" }, { id: "b" }, { id: "c" }];
|
||||
|
||||
expect(resolveInsertColumnIndex(columns, null)).toBe(3);
|
||||
expect(resolveInsertColumnIndex(columns, undefined)).toBe(3);
|
||||
expect(resolveInsertColumnIndex(columns, "a")).toBe(1);
|
||||
expect(resolveInsertColumnIndex(columns, "b")).toBe(2);
|
||||
expect(resolveInsertColumnIndex(columns, "c")).toBe(3);
|
||||
expect(resolveInsertColumnIndex(columns, "missing")).toBe(3);
|
||||
expect(resolveInsertColumnIndex([], "a")).toBe(0);
|
||||
expect(resolveInsertColumnIndex([{ id: "a", markedForDrop: true }, { id: "b" }], "a")).toBe(2);
|
||||
});
|
||||
|
||||
it("strips SQL Server metadata parentheses from editable defaults", () => {
|
||||
const drafts = createColumnDrafts(
|
||||
[
|
||||
|
||||
@@ -1066,6 +1066,14 @@ export function defaultNewColumnDataType(dbType: DatabaseType | undefined, dataT
|
||||
return dbType === "sqlite" ? "text" : "varchar(255)";
|
||||
}
|
||||
|
||||
/** Index at which to insert a new column (after the selected row, or append when none). */
|
||||
export function resolveInsertColumnIndex(columns: readonly { id: string; markedForDrop?: boolean }[], selectedColumnId: string | null | undefined): number {
|
||||
if (!selectedColumnId) return columns.length;
|
||||
// Dropped rows are not valid insertion anchors.
|
||||
const index = columns.findIndex((column) => column.id === selectedColumnId && !column.markedForDrop);
|
||||
return index >= 0 ? index + 1 : columns.length;
|
||||
}
|
||||
|
||||
function isMysqlDeprecatedDefaultParameterType(baseType: string): boolean {
|
||||
const typeName = baseType.split(/\s+/)[0];
|
||||
return ["tinyint", "smallint", "mediumint", "int", "integer", "bigint", "float", "double", "real"].includes(typeName ?? "");
|
||||
|
||||
Reference in new issue
Block a user