fix(query): enable editing multi-table JOIN result sets

This commit is contained in:
zipg authored and GitHub committed 2026-09-07 00:54:59 +08:00
1 parent 93a4b5803a
commit 14e1d4f25b
16 files changed
+345 -22

No files matched your search

@@ -627,6 +627,8 @@ describe("useDataGridEditor saveChanges reload", () => {
function createSaveTestEditor(
options: {
joinedWriteTargets?: import("@/types/database").QueryTab["queryWriteTargets"];
queryResult?: { columns: string[]; rows: CellValue[][] };
currentPage?: Ref<number>;
prepareFullReload?: () => void;
customSaveHandler?: { save: ReturnType<typeof vi.fn> };
@@ -644,6 +646,7 @@ describe("useDataGridEditor saveChanges reload", () => {
[2, "pending"],
],
});
if (options.queryResult) result.value = options.queryResult;
const editor = useDataGridEditor({
result: computed(() => result.value),
editable: computed(() => true),
@@ -659,6 +662,7 @@ describe("useDataGridEditor saveChanges reload", () => {
primaryKeys: ["id"],
})),
sourceColumns: computed(() => undefined),
joinedWriteTargets: computed(() => options.joinedWriteTargets),
onExecuteSql: computed(() => undefined),
customSaveHandler: computed(() => options.customSaveHandler),
manualTransactionSessionId: computed(() => options.manualTransactionSessionId),
@@ -694,6 +698,55 @@ describe("useDataGridEditor saveChanges reload", () => {
expect(emit).toHaveBeenCalledWith("reload", undefined, "", undefined, undefined, 100, 0);
});
it("previews and saves edits to both joined tables in a single transaction", async () => {
const refreshSavedRows = vi.fn();
const targets = [
{ tableMeta: { tableName: "users", primaryKeys: ["id"], columns: [] }, sourceColumns: ["id", "name", undefined, undefined] },
{ tableMeta: { tableName: "papers", primaryKeys: ["id"], columns: [] }, sourceColumns: [undefined, undefined, "id", "title"] },
];
mocks.prepareDataGridSave.mockImplementation(async (options) => ({ statements: ["update " + options.tableMeta.tableName], rollbackStatements: ["undo " + options.tableMeta.tableName] }));
mocks.executeInTransaction.mockResolvedValue({ affected_rows: 2 });
const { editor, emit } = createSaveTestEditor({ joinedWriteTargets: targets, queryResult: { columns: ["id", "name", "paper_id", "title"], rows: [[1, "old", 20, "old"]] }, refreshSavedRows });
editor.dirtyRows.value.set(
0,
new Map([
[1, "new name"],
[3, "new title"],
]),
);
expect(await editor.previewChanges()).toEqual(["update users", "update papers"]);
expect(mocks.executeInTransaction).not.toHaveBeenCalled();
await editor.saveChanges();
expect(mocks.executeInTransaction).toHaveBeenCalledWith("connection-1", "app", ["update users", "update papers"], undefined);
expect(mocks.executeBatch).not.toHaveBeenCalled();
expect(refreshSavedRows).not.toHaveBeenCalled();
expect(emit).toHaveBeenCalledWith("reload", undefined, "", undefined, undefined, 100, 0);
expect(mocks.prepareDataGridSave.mock.calls[0]![0].dirtyRows).toEqual([[0, [[1, "new name"]]]]);
expect(mocks.prepareDataGridSave.mock.calls[1]![0].dirtyRows).toEqual([[0, [[3, "new title"]]]]);
});
it("keeps both joined edits pending if the transaction fails", async () => {
const targets = [
{ tableMeta: { tableName: "users", primaryKeys: ["id"], columns: [] }, sourceColumns: ["id", "name", undefined, undefined] },
{ tableMeta: { tableName: "papers", primaryKeys: ["id"], columns: [] }, sourceColumns: [undefined, undefined, "id", "title"] },
];
mocks.prepareDataGridSave.mockImplementation(async (options) => ({ statements: ["update " + options.tableMeta.tableName], rollbackStatements: [] }));
mocks.executeInTransaction.mockRejectedValue(new Error("second update failed"));
const { editor, emit } = createSaveTestEditor({ joinedWriteTargets: targets, queryResult: { columns: ["id", "name", "paper_id", "title"], rows: [[1, "old", 20, "old"]] } });
editor.dirtyRows.value.set(
0,
new Map([
[1, "new name"],
[3, "new title"],
]),
);
await editor.saveChanges();
expect(editor.dirtyRows.value.get(0)?.size).toBe(2);
expect(editor.saveError.value).toContain("second update failed");
expect(mocks.executeBatch).not.toHaveBeenCalled();
expect(emit.mock.calls.some(([event]) => event === "reload")).toBe(false);
});
it("saves query-result edits through the active manual transaction session", async () => {
const statement = "UPDATE orders_test SET status='shipped' WHERE id=1";
const refreshSavedRows = vi.fn().mockResolvedValue(true);