fix(editor): protect external SQL files from conflicting changes
This commit is contained in:
29 files changed
+1125
-65
No files matched your search
@@ -9,7 +9,13 @@ vi.mock("@tauri-apps/api/core", () => ({
|
||||
invoke: mocks.invoke,
|
||||
}));
|
||||
|
||||
import { readExternalSqlFile } from "@/lib/backend/tauri";
|
||||
import { inspectExternalSqlFile, readExternalSqlFile, readExternalSqlFileSnapshot, writeExternalSqlFile } from "@/lib/backend/tauri";
|
||||
|
||||
const version = {
|
||||
sizeBytes: 9,
|
||||
modifiedNs: "123000000",
|
||||
contentHash: "abc123",
|
||||
};
|
||||
|
||||
describe("external SQL file API", () => {
|
||||
beforeEach(() => {
|
||||
@@ -17,12 +23,49 @@ describe("external SQL file API", () => {
|
||||
});
|
||||
|
||||
it("returns editor content from the structured backend response", async () => {
|
||||
mocks.invoke.mockResolvedValue({ kind: "content", content: "select 1;" });
|
||||
mocks.invoke.mockResolvedValue({ kind: "content", content: "select 1;", version });
|
||||
|
||||
await expect(readExternalSqlFile("/tmp/demo.sql")).resolves.toBe("select 1;");
|
||||
expect(mocks.invoke).toHaveBeenCalledWith("read_external_sql_file", { path: "/tmp/demo.sql" });
|
||||
});
|
||||
|
||||
it("returns the disk version with an editor snapshot", async () => {
|
||||
mocks.invoke.mockResolvedValue({ kind: "content", content: "select 1;", version });
|
||||
|
||||
await expect(readExternalSqlFileSnapshot("/tmp/demo.sql")).resolves.toEqual({ content: "select 1;", version });
|
||||
});
|
||||
|
||||
it("inspects metadata without reading editor content", async () => {
|
||||
mocks.invoke.mockResolvedValue({ kind: "present", sizeBytes: 9, modifiedNs: "123000000" });
|
||||
|
||||
await expect(inspectExternalSqlFile("/tmp/demo.sql")).resolves.toEqual({ kind: "present", sizeBytes: 9, modifiedNs: "123000000" });
|
||||
expect(mocks.invoke).toHaveBeenCalledWith("inspect_external_sql_file", { path: "/tmp/demo.sql" });
|
||||
});
|
||||
|
||||
it("passes the expected version to checked writes", async () => {
|
||||
mocks.invoke.mockResolvedValue({ kind: "written", version });
|
||||
|
||||
await expect(writeExternalSqlFile("/tmp/demo.sql", "select 2;", { expectedContentHash: "abc123" })).resolves.toEqual({ kind: "written", version });
|
||||
expect(mocks.invoke).toHaveBeenCalledWith("write_external_sql_file", {
|
||||
path: "/tmp/demo.sql",
|
||||
content: "select 2;",
|
||||
expectedContentHash: "abc123",
|
||||
expectedMissing: false,
|
||||
});
|
||||
});
|
||||
|
||||
it("passes an expected-missing precondition to recreate writes", async () => {
|
||||
mocks.invoke.mockResolvedValue({ kind: "written", version });
|
||||
|
||||
await expect(writeExternalSqlFile("/tmp/demo.sql", "select 2;", { expectedMissing: true })).resolves.toEqual({ kind: "written", version });
|
||||
expect(mocks.invoke).toHaveBeenCalledWith("write_external_sql_file", {
|
||||
path: "/tmp/demo.sql",
|
||||
content: "select 2;",
|
||||
expectedContentHash: null,
|
||||
expectedMissing: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("maps oversized responses to a typed frontend error", async () => {
|
||||
mocks.invoke.mockResolvedValue({ kind: "tooLarge", sizeBytes: 50 * 1024 ** 3, maxSizeBytes: 64 * 1024 ** 2 });
|
||||
|
||||
|
||||
@@ -333,6 +333,8 @@ export const pendingOpenSqlFiles = forward("pendingOpenSqlFiles");
|
||||
export const pendingOpenDbFiles = forward("pendingOpenDbFiles");
|
||||
export const pendingOpenConnectionLinks = forward("pendingOpenConnectionLinks");
|
||||
export const readExternalSqlFile = forward("readExternalSqlFile");
|
||||
export const readExternalSqlFileSnapshot = forward("readExternalSqlFileSnapshot");
|
||||
export const inspectExternalSqlFile = forward("inspectExternalSqlFile");
|
||||
export const writeExternalSqlFile = forward("writeExternalSqlFile");
|
||||
export const saveExternalSqlFile = forward("saveExternalSqlFile");
|
||||
export const listSqlFilesInFolder = forward("listSqlFilesInFolder");
|
||||
|
||||
@@ -1864,11 +1864,19 @@ export async function readExternalSqlFile(_path: string): Promise<string> {
|
||||
throw new Error("Opening external SQL file paths is only available in the desktop app");
|
||||
}
|
||||
|
||||
export async function writeExternalSqlFile(_path: string, _content: string): Promise<void> {
|
||||
export async function readExternalSqlFileSnapshot(_path: string): Promise<import("@/lib/backend/tauri").ExternalSqlFileSnapshot> {
|
||||
throw new Error("Opening external SQL file paths is only available in the desktop app");
|
||||
}
|
||||
|
||||
export async function inspectExternalSqlFile(_path: string): Promise<import("@/lib/backend/tauri").ExternalSqlFileStatus> {
|
||||
throw new Error("Inspecting external SQL file paths is only available in the desktop app");
|
||||
}
|
||||
|
||||
export async function writeExternalSqlFile(_path: string, _content: string, _options: { expectedContentHash?: string; expectedMissing?: boolean } = {}): Promise<import("@/lib/backend/tauri").ExternalSqlFileWriteResult> {
|
||||
throw new Error("Saving external SQL file paths is only available in the desktop app");
|
||||
}
|
||||
|
||||
export async function saveExternalSqlFile(_defaultFileName: string, _content: string): Promise<string | null> {
|
||||
export async function saveExternalSqlFile(_defaultFileName: string, _content: string): Promise<{ path: string; version: import("@/types/database").ExternalSqlFileVersion } | null> {
|
||||
throw new Error("Saving SQL files locally is only available in the desktop app");
|
||||
}
|
||||
|
||||
|
||||
@@ -56,6 +56,7 @@ import type {
|
||||
SshConfigHostEntry,
|
||||
TunnelProfile,
|
||||
TransactionLog,
|
||||
ExternalSqlFileVersion,
|
||||
} from "@/types/database";
|
||||
import { isTauriCommandUnavailable, normalizeConnectionTestResult } from "@/lib/connection/connectionDatabaseInfo";
|
||||
import type { CollectionInfo } from "@/types/database";
|
||||
@@ -754,19 +755,41 @@ export async function pendingOpenConnectionLinks(): Promise<string[]> {
|
||||
return invoke("pending_open_connection_links");
|
||||
}
|
||||
|
||||
export async function readExternalSqlFile(path: string): Promise<string> {
|
||||
const result = await invoke<{ kind: "content"; content: string } | { kind: "tooLarge"; sizeBytes: number; maxSizeBytes: number }>("read_external_sql_file", { path });
|
||||
export interface ExternalSqlFileSnapshot {
|
||||
content: string;
|
||||
version: ExternalSqlFileVersion;
|
||||
}
|
||||
|
||||
export type ExternalSqlFileStatus = { kind: "present"; sizeBytes: number; modifiedNs: string } | { kind: "missing" };
|
||||
|
||||
export type ExternalSqlFileWriteResult = { kind: "written"; version: ExternalSqlFileVersion } | { kind: "conflict"; currentVersion: ExternalSqlFileVersion } | { kind: "missing" };
|
||||
|
||||
export async function readExternalSqlFileSnapshot(path: string): Promise<ExternalSqlFileSnapshot> {
|
||||
const result = await invoke<{ kind: "content"; content: string; version: ExternalSqlFileVersion } | { kind: "tooLarge"; sizeBytes: number; maxSizeBytes: number }>("read_external_sql_file", { path });
|
||||
if (result.kind === "tooLarge") {
|
||||
throw new ExternalSqlFileTooLargeError(result.sizeBytes, result.maxSizeBytes);
|
||||
}
|
||||
return result.content;
|
||||
return { content: result.content, version: result.version };
|
||||
}
|
||||
|
||||
export async function writeExternalSqlFile(path: string, content: string): Promise<void> {
|
||||
return invoke("write_external_sql_file", { path, content });
|
||||
export async function readExternalSqlFile(path: string): Promise<string> {
|
||||
return (await readExternalSqlFileSnapshot(path)).content;
|
||||
}
|
||||
|
||||
export async function saveExternalSqlFile(defaultFileName: string, content: string): Promise<string | null> {
|
||||
export async function inspectExternalSqlFile(path: string): Promise<ExternalSqlFileStatus> {
|
||||
return invoke("inspect_external_sql_file", { path });
|
||||
}
|
||||
|
||||
export async function writeExternalSqlFile(path: string, content: string, options: { expectedContentHash?: string; expectedMissing?: boolean } = {}): Promise<ExternalSqlFileWriteResult> {
|
||||
return invoke("write_external_sql_file", {
|
||||
path,
|
||||
content,
|
||||
expectedContentHash: options.expectedContentHash ?? null,
|
||||
expectedMissing: options.expectedMissing ?? false,
|
||||
});
|
||||
}
|
||||
|
||||
export async function saveExternalSqlFile(defaultFileName: string, content: string): Promise<{ path: string; version: ExternalSqlFileVersion } | null> {
|
||||
return invoke("save_external_sql_file", { defaultFileName, content });
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user