fix(sql-files): distinguish files with duplicate names

This commit is contained in:
t8y2 committed 2026-07-11 18:32:17 +08:00
1 parent e4563f9659
commit 1dd4623919
13 files changed
+151 -12

No files matched your search

@@ -0,0 +1,16 @@
import { describe, expect, it } from "vitest";
import { externalSqlFileDisplayTitles, normalizeExternalSqlPath } from "@/lib/sql/sqlFileOpen";
describe("external SQL file paths", () => {
it("normalizes Windows separators for identity checks", () => {
expect(normalizeExternalSqlPath(" C:\\work\\demo.sql ")).toBe("C:/work/demo.sql");
});
it("uses the shortest unique parent path for duplicate filenames", () => {
expect(externalSqlFileDisplayTitles(["/work/demo/create.sql", "/work/learn/create.sql", "/work/query.sql"])).toEqual(["demo/create.sql", "learn/create.sql", "query.sql"]);
});
it("adds more parent segments when immediate parents also collide", () => {
expect(externalSqlFileDisplayTitles(["/one/sql/create.sql", "/two/sql/create.sql"])).toEqual(["one/sql/create.sql", "two/sql/create.sql"]);
});
});
+36 -2
View File
@@ -3,12 +3,46 @@ export function isSqlFilePath(path: string): boolean {
}
export function sqlFileTitleFromPath(path: string): string {
const trimmed = path.trim();
const normalized = trimmed.replace(/\\/g, "/");
const normalized = normalizeExternalSqlPath(path);
const name = normalized.split("/").filter(Boolean).pop();
return name || "script.sql";
}
export function normalizeExternalSqlPath(path: string): string {
const normalized = path
.trim()
.replace(/\\/g, "/")
.replace(/\/{2,}/g, "/");
return normalized.length > 1 ? normalized.replace(/\/$/, "") : normalized;
}
export function externalSqlFileDisplayTitles(paths: string[]): string[] {
const segments = paths.map((path) => normalizeExternalSqlPath(path).split("/").filter(Boolean));
const titles = segments.map((parts) => parts[parts.length - 1] || "script.sql");
const collisions = new Map<string, number[]>();
titles.forEach((title, index) => {
const indexes = collisions.get(title) ?? [];
indexes.push(index);
collisions.set(title, indexes);
});
for (const indexes of collisions.values()) {
if (indexes.length < 2) continue;
const maxDepth = Math.max(...indexes.map((index) => segments[index].length));
for (let depth = 2; depth <= maxDepth; depth++) {
const candidates = indexes.map((index) => segments[index].slice(-depth).join("/"));
if (new Set(candidates).size !== candidates.length) continue;
indexes.forEach((index, candidateIndex) => {
titles[index] = candidates[candidateIndex];
});
break;
}
}
return titles;
}
export function externalSqlFilePaths(paths: string[]): string[] {
return paths.filter(isSqlFilePath);
}
@@ -117,6 +117,9 @@ export function tabTooltipLines(tab: QueryTab, t: Translate): { label: string; v
if (tab.mode === "query" && queryTitle(tab)) {
lines.unshift({ label: t("tabs.tooltipTitle"), value: tab.title });
}
if (tab.mode === "query" && tab.externalSqlPath) {
lines.push({ label: t("tabs.tooltipFilePath"), value: tab.externalSqlPath });
}
if (tab.mode === "data" && tab.tableMeta?.tableName) {
lines.push({ label: t("tabs.tooltipTable"), value: tab.tableMeta.tableName });
}