fix(saved-sql): scope SQL names and guard target persistence
This commit is contained in:
8 files changed
+715
-73
No files matched your search
@@ -0,0 +1,26 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { uniqueSavedSqlExportFileName } from "@/lib/savedSql/savedSqlExport";
|
||||
|
||||
describe("saved SQL export names", () => {
|
||||
it.each([
|
||||
["report:daily.sql", "report:daily.sql", "report_daily (2).sql"],
|
||||
["report_daily (2).sql", "report:daily.sql", "REPORT:DAILY.SQL"],
|
||||
["report/daily", "report\\daily", "report_daily.sql"],
|
||||
])("keeps all contents when sanitized names and generated suffixes collide: %j", (...names) => {
|
||||
const taken = new Set<string>();
|
||||
const output = new Map<string, string>();
|
||||
names.forEach((name, index) => {
|
||||
const path = uniqueSavedSqlExportFileName(name, taken);
|
||||
expect(path).not.toMatch(/[<>:"/\\|?*]/);
|
||||
output.set(path.toLocaleLowerCase(), `SELECT ${index};`);
|
||||
});
|
||||
expect([...output.values()]).toEqual(["SELECT 0;", "SELECT 1;", "SELECT 2;"]);
|
||||
});
|
||||
|
||||
it("preserves a safe filename and adds suffixes only for collisions", () => {
|
||||
const taken = new Set<string>();
|
||||
expect(uniqueSavedSqlExportFileName("Report.SQL", taken)).toBe("Report.SQL");
|
||||
expect(uniqueSavedSqlExportFileName("report", taken)).toBe("report (2).sql");
|
||||
expect(uniqueSavedSqlExportFileName("report", taken)).toBe("report (3).sql");
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
|
||||
import { ensureSqlExtension } from "@/lib/savedSql/savedSqlFileName";
|
||||
import { ensureSqlExtension, stripSqlExtension } from "@/lib/savedSql/savedSqlFileName";
|
||||
|
||||
export function savedSqlExportFileName(name: string): string {
|
||||
return (
|
||||
@@ -9,6 +9,19 @@ export function savedSqlExportFileName(name: string): string {
|
||||
);
|
||||
}
|
||||
|
||||
export function uniqueSavedSqlExportFileName(name: string, takenNames: Set<string>): string {
|
||||
const normalized = savedSqlExportFileName(name);
|
||||
const occupied = new Set([...takenNames].map((taken) => taken.toLocaleLowerCase()));
|
||||
const base = stripSqlExtension(normalized);
|
||||
let candidate = normalized;
|
||||
let counter = 2;
|
||||
while (occupied.has(candidate.toLocaleLowerCase())) {
|
||||
candidate = `${base} (${counter++}).sql`;
|
||||
}
|
||||
takenNames.add(candidate);
|
||||
return candidate;
|
||||
}
|
||||
|
||||
export async function exportSavedSqlFileContent(sql: string, fileName: string): Promise<"saved" | "cancelled"> {
|
||||
const defaultFileName = savedSqlExportFileName(fileName);
|
||||
if (isTauriRuntime()) {
|
||||
|
||||
Reference in new issue
Block a user