refactor(desktop): organize lib modules by domain
This commit is contained in:
1 parent
90f0594e93
commit
7e7c4944e9
700 files changed
+1292
-1264
No files matched your search
@@ -0,0 +1,96 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { sortTablesByFkDependency, type TableWithFk } from "@/lib/table/tableDependencySort";
|
||||
|
||||
function fk(refTable: string) {
|
||||
return { name: `fk_to_${refTable}`, column: "id", ref_table: refTable, ref_column: "id" };
|
||||
}
|
||||
|
||||
function table(name: string, foreignKeys: ReturnType<typeof fk>[] = []): TableWithFk {
|
||||
return { name, foreignKeys };
|
||||
}
|
||||
|
||||
describe("sortTablesByFkDependency", () => {
|
||||
it("returns single table unchanged", () => {
|
||||
const input = [table("t1")];
|
||||
const result = sortTablesByFkDependency(input);
|
||||
expect(result.map((t) => t.name)).toEqual(["t1"]);
|
||||
});
|
||||
|
||||
it("returns empty array unchanged", () => {
|
||||
expect(sortTablesByFkDependency([])).toEqual([]);
|
||||
});
|
||||
|
||||
it("preserves original order when no FK dependencies exist", () => {
|
||||
const input = [table("t2"), table("t1")];
|
||||
const result = sortTablesByFkDependency(input);
|
||||
expect(result.map((t) => t.name)).toEqual(["t2", "t1"]);
|
||||
});
|
||||
|
||||
it("sorts referencing table before referenced table (simple dependency)", () => {
|
||||
// t2 references t1 → t2 should be dropped before t1
|
||||
const input = [table("t1"), table("t2", [fk("t1")])];
|
||||
const result = sortTablesByFkDependency(input);
|
||||
expect(result.map((t) => t.name)).toEqual(["t2", "t1"]);
|
||||
});
|
||||
|
||||
it("sorts referencing table before referenced table regardless of input order", () => {
|
||||
// Input order reversed from the above test
|
||||
const input = [table("t2", [fk("t1")]), table("t1")];
|
||||
const result = sortTablesByFkDependency(input);
|
||||
expect(result.map((t) => t.name)).toEqual(["t2", "t1"]);
|
||||
});
|
||||
|
||||
it("handles chain dependency (C→B→A)", () => {
|
||||
// t3 references t2, t2 references t1 → t3, t2, t1
|
||||
const input = [table("t1"), table("t2", [fk("t1")]), table("t3", [fk("t2")])];
|
||||
const result = sortTablesByFkDependency(input);
|
||||
expect(result.map((t) => t.name)).toEqual(["t3", "t2", "t1"]);
|
||||
});
|
||||
|
||||
it("handles multiple tables referencing the same table", () => {
|
||||
// t2 and t3 both reference t1 → t2,t3 before t1
|
||||
const input = [table("t1"), table("t2", [fk("t1")]), table("t3", [fk("t1")])];
|
||||
const result = sortTablesByFkDependency(input);
|
||||
const names = result.map((t) => t.name);
|
||||
// t1 must be last
|
||||
expect(names[names.length - 1]).toBe("t1");
|
||||
// t2 and t3 must come before t1
|
||||
expect(names.slice(0, 2)).toEqual(expect.arrayContaining(["t2", "t3"]));
|
||||
});
|
||||
|
||||
it("falls back to original order on cyclic dependency", () => {
|
||||
// t1 references t2 and t2 references t1
|
||||
const input = [table("t1", [fk("t2")]), table("t2", [fk("t1")])];
|
||||
const result = sortTablesByFkDependency(input);
|
||||
expect(result.map((t) => t.name)).toEqual(["t1", "t2"]);
|
||||
});
|
||||
|
||||
it("ignores FK references to tables not in the selected set", () => {
|
||||
// t2 references t3 (not selected), t1 has no FK → original order
|
||||
const input = [table("t1"), table("t2", [fk("t3")])];
|
||||
const result = sortTablesByFkDependency(input);
|
||||
expect(result.map((t) => t.name)).toEqual(["t1", "t2"]);
|
||||
});
|
||||
|
||||
it("handles table with multiple FKs where only some target selected tables", () => {
|
||||
// t3 references both t1 (selected) and t4 (not selected)
|
||||
const input = [table("t1"), table("t3", [fk("t1"), fk("t4")])];
|
||||
const result = sortTablesByFkDependency(input);
|
||||
expect(result.map((t) => t.name)).toEqual(["t3", "t1"]);
|
||||
});
|
||||
|
||||
it("handles complex DAG with multiple branches", () => {
|
||||
// t4 → t2 → t1
|
||||
// t3 → t1
|
||||
const input = [table("t1"), table("t2", [fk("t1")]), table("t3", [fk("t1")]), table("t4", [fk("t2")])];
|
||||
const result = sortTablesByFkDependency(input);
|
||||
const names = result.map((t) => t.name);
|
||||
// t1 must be last (everything references it directly or indirectly)
|
||||
expect(names[names.length - 1]).toBe("t1");
|
||||
// t4 must come before t2
|
||||
expect(names.indexOf("t4")).toBeLessThan(names.indexOf("t2"));
|
||||
// t2 and t3 must come before t1
|
||||
expect(names.indexOf("t2")).toBeLessThan(names.indexOf("t1"));
|
||||
expect(names.indexOf("t3")).toBeLessThan(names.indexOf("t1"));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,63 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { DBX_ROWID_COLUMN, canEditExistingTableRows, canUseKeylessRowPredicate, editablePrimaryKeys, editableRowIdentifierColumns, isClickHouseExistingRowReadonlyColumn, isTableDataEditable, supportsDataGridTransaction } from "@/lib/table/tableEditing";
|
||||
import type { ColumnInfo, IndexInfo } from "@/types/database";
|
||||
|
||||
function column(name: string, isPrimaryKey = false): ColumnInfo {
|
||||
return {
|
||||
name,
|
||||
data_type: "varchar",
|
||||
is_nullable: true,
|
||||
column_default: null,
|
||||
is_primary_key: isPrimaryKey,
|
||||
extra: null,
|
||||
};
|
||||
}
|
||||
|
||||
function index(columns: string[], isUnique = true, filter: string | null = null): IndexInfo {
|
||||
return {
|
||||
name: columns.join("_"),
|
||||
columns,
|
||||
is_unique: isUnique,
|
||||
is_primary: false,
|
||||
filter,
|
||||
};
|
||||
}
|
||||
|
||||
describe("tableEditing", () => {
|
||||
it("does not synthesize Oracle ROWID for views", () => {
|
||||
expect(editablePrimaryKeys("oracle", [column("ID"), column("NAME")], "VIEW")).toEqual([]);
|
||||
expect(editablePrimaryKeys("oracle", [column("ID"), column("NAME")], "TABLE")).toEqual([DBX_ROWID_COLUMN]);
|
||||
});
|
||||
|
||||
it("treats view data tabs as readonly", () => {
|
||||
expect(isTableDataEditable("oracle", [DBX_ROWID_COLUMN], "VIEW")).toBe(false);
|
||||
});
|
||||
|
||||
it("allows keyless row predicates only for databases that support them", () => {
|
||||
expect(canUseKeylessRowPredicate("postgres", [])).toBe(true);
|
||||
expect(canUseKeylessRowPredicate("mysql", [])).toBe(true);
|
||||
expect(canUseKeylessRowPredicate("jdbc", [])).toBe(false);
|
||||
expect(canUseKeylessRowPredicate("postgres", ["id"])).toBe(false);
|
||||
});
|
||||
|
||||
it("uses unique indexes as row identifiers when primary keys are absent", () => {
|
||||
expect(editableRowIdentifierColumns("postgres", [column("email"), column("name")], [index(["email", "name"]), index(["email"])])).toEqual(["email"]);
|
||||
expect(editableRowIdentifierColumns("postgres", [column("email"), column("name")], [index(["email"], true, "email IS NOT NULL")])).toEqual([]);
|
||||
expect(editableRowIdentifierColumns("postgres", [column("id", true), column("email")], [index(["email"])])).toEqual(["id"]);
|
||||
});
|
||||
|
||||
it("allows ClickHouse table editing when row identifiers are available", () => {
|
||||
expect(isTableDataEditable("clickhouse", ["id"], "BASE TABLE")).toBe(true);
|
||||
expect(canEditExistingTableRows("clickhouse", undefined, ["id"])).toBe(true);
|
||||
expect(supportsDataGridTransaction("clickhouse")).toBe(false);
|
||||
expect(isTableDataEditable("clickhouse", [], "BASE TABLE")).toBe(true);
|
||||
expect(canEditExistingTableRows("clickhouse", undefined, [])).toBe(false);
|
||||
});
|
||||
|
||||
it("treats ClickHouse row identifier cells as readonly on existing rows", () => {
|
||||
expect(isClickHouseExistingRowReadonlyColumn("clickhouse", "ID", ["id"])).toBe(true);
|
||||
expect(isClickHouseExistingRowReadonlyColumn("clickhouse", "name", ["id"])).toBe(false);
|
||||
expect(isClickHouseExistingRowReadonlyColumn("clickhouse", "event_date", ["id"], [{ ...column("event_date"), extra: "partition_key" }])).toBe(true);
|
||||
expect(isClickHouseExistingRowReadonlyColumn("postgres", "id", ["id"])).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,82 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { combineDataTypeForDatabase, createColumnDrafts, dataTypeLengthInputValue, isDataTypeLengthDisabled, isSqlServerIdentityCompatibleDataType, splitDataType } from "@/lib/table/tableStructureEditorState";
|
||||
|
||||
describe("tableStructureEditorState", () => {
|
||||
it("keeps mysql unsigned attributes in the editable base type", () => {
|
||||
expect(splitDataType("int(11) unsigned")).toEqual({ baseType: "int unsigned", params: "11" });
|
||||
expect(splitDataType("bigint(20) unsigned zerofill")).toEqual({
|
||||
baseType: "bigint unsigned zerofill",
|
||||
params: "20",
|
||||
});
|
||||
});
|
||||
|
||||
it("combines mysql unsigned type choices with the length field", () => {
|
||||
expect(combineDataTypeForDatabase("mysql", "int unsigned", "11")).toBe("int(11) unsigned");
|
||||
expect(combineDataTypeForDatabase("mysql", "bigint unsigned zerofill", "20")).toBe("bigint(20) unsigned zerofill");
|
||||
});
|
||||
|
||||
it("does not expose mysql enum or set values as editable length", () => {
|
||||
const dataType = "enum('purchase_in','sale_out','return_in','adjustment_out','transfer_in','transfer_out')";
|
||||
|
||||
expect(splitDataType(dataType)).toEqual({
|
||||
baseType: "enum",
|
||||
params: "'purchase_in','sale_out','return_in','adjustment_out','transfer_in','transfer_out'",
|
||||
});
|
||||
expect(isDataTypeLengthDisabled("mysql", "enum")).toBe(true);
|
||||
expect(isDataTypeLengthDisabled("mysql", "set")).toBe(true);
|
||||
expect(dataTypeLengthInputValue("mysql", dataType)).toBe("");
|
||||
expect(dataTypeLengthInputValue("mysql", "set('manual','auto')")).toBe("");
|
||||
});
|
||||
|
||||
it("strips SQL Server metadata parentheses from editable defaults", () => {
|
||||
const drafts = createColumnDrafts(
|
||||
[
|
||||
{
|
||||
name: "name",
|
||||
data_type: "nvarchar(100)",
|
||||
is_nullable: true,
|
||||
column_default: "('')",
|
||||
is_primary_key: false,
|
||||
extra: null,
|
||||
},
|
||||
{
|
||||
name: "active",
|
||||
data_type: "bit",
|
||||
is_nullable: false,
|
||||
column_default: "((1))",
|
||||
is_primary_key: false,
|
||||
extra: null,
|
||||
},
|
||||
{
|
||||
name: "created_at",
|
||||
data_type: "datetime2(7)",
|
||||
is_nullable: false,
|
||||
column_default: "((sysdatetime()))",
|
||||
is_primary_key: false,
|
||||
extra: null,
|
||||
},
|
||||
{
|
||||
name: "label",
|
||||
data_type: "nvarchar(100)",
|
||||
is_nullable: true,
|
||||
column_default: "('prefix (internal)')",
|
||||
is_primary_key: false,
|
||||
extra: null,
|
||||
},
|
||||
],
|
||||
"sqlserver",
|
||||
);
|
||||
|
||||
expect(drafts.map((draft) => draft.defaultValue)).toEqual(["''", "1", "sysdatetime()", "'prefix (internal)'"]);
|
||||
expect(drafts.map((draft) => draft.original?.column_default)).toEqual(["''", "1", "sysdatetime()", "'prefix (internal)'"]);
|
||||
});
|
||||
|
||||
it("limits SQL Server identity columns to supported data types", () => {
|
||||
expect(isSqlServerIdentityCompatibleDataType("int")).toBe(true);
|
||||
expect(isSqlServerIdentityCompatibleDataType("bigint")).toBe(true);
|
||||
expect(isSqlServerIdentityCompatibleDataType("numeric(18, 0)")).toBe(true);
|
||||
expect(isSqlServerIdentityCompatibleDataType("decimal(10)")).toBe(true);
|
||||
expect(isSqlServerIdentityCompatibleDataType("varchar(255)")).toBe(false);
|
||||
expect(isSqlServerIdentityCompatibleDataType("numeric(18, 2)")).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user