fix(gbase8s): make view source saves safer

This commit is contained in:
zipg authored and GitHub committed 2026-07-07 01:50:53 +08:00
1 parent 15b86ad6c9
commit 668919a7c9
25 files changed
+1343 -129

No files matched your search

@@ -0,0 +1,34 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import * as api from "@/lib/backend/api";
import { executeObjectSourceSave } from "@/lib/table/objectSourceEditor";
vi.mock("@/lib/backend/api", () => ({
executeInTransaction: vi.fn().mockResolvedValue({}),
executeQuery: vi.fn().mockResolvedValue({}),
executeScript: vi.fn().mockResolvedValue({}),
}));
beforeEach(() => {
vi.clearAllMocks();
});
describe("executeObjectSourceSave", () => {
it("runs multi-statement Informix source saves in a transaction", async () => {
await executeObjectSourceSave("conn-1", "stores", "informix", ["CREATE TEMP VIEW v AS SELECT 1", " ", "DROP VIEW v", "CREATE VIEW v AS SELECT 2"], "app");
expect(api.executeInTransaction).toHaveBeenCalledOnce();
expect(api.executeInTransaction).toHaveBeenCalledWith("conn-1", "stores", ["CREATE TEMP VIEW v AS SELECT 1", "DROP VIEW v", "CREATE VIEW v AS SELECT 2"], "app");
expect(api.executeQuery).not.toHaveBeenCalled();
expect(api.executeScript).not.toHaveBeenCalled();
});
it("keeps non-Informix source saves on the existing per-statement path", async () => {
await executeObjectSourceSave("conn-1", "app", "mysql", ["ALTER VIEW v AS SELECT 1", "", "ALTER VIEW v AS SELECT 2"], "public");
expect(api.executeInTransaction).not.toHaveBeenCalled();
expect(api.executeQuery).toHaveBeenCalledTimes(2);
expect(api.executeQuery).toHaveBeenNthCalledWith(1, "conn-1", "app", "ALTER VIEW v AS SELECT 1", "public");
expect(api.executeQuery).toHaveBeenNthCalledWith(2, "conn-1", "app", "ALTER VIEW v AS SELECT 2", "public");
expect(api.executeScript).not.toHaveBeenCalled();
});
});
@@ -44,3 +44,23 @@ export function buildEditableObjectSource(input: BuildEditableObjectSourceSqlInp
export function objectSourceSaveExecutionMode(_databaseType: DatabaseType): ObjectSourceSaveExecutionMode {
return "single";
}
export async function executeObjectSourceSave(connectionId: string, database: string, databaseType: DatabaseType, statements: string[], schema?: string): Promise<void> {
const nonEmptyStatements = statements.filter((sql) => sql.trim().length > 0);
if (nonEmptyStatements.length === 0) return;
if (databaseType === "informix" && nonEmptyStatements.length > 1) {
// Informix/GBase 8s view replacement is validate + drop/create; run it atomically
// so a failing final CREATE rolls back the original view instead of deleting it.
await api.executeInTransaction(connectionId, database, nonEmptyStatements, schema);
return;
}
for (const sql of nonEmptyStatements) {
if (objectSourceSaveExecutionMode(databaseType) === "single") {
await api.executeQuery(connectionId, database, sql, schema);
} else {
await api.executeScript(connectionId, database, sql, schema);
}
}
}