fix(saved-sql): auto-number duplicate saved SQL file names
This commit is contained in:
18 files changed
+266
-50
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { ensureSqlExtension, stripSqlExtension } from "@/lib/savedSql/savedSqlFileName";
|
||||
import { ensureSqlExtension, nextAvailableSqlName, stripSqlExtension } from "@/lib/savedSql/savedSqlFileName";
|
||||
|
||||
describe("savedSqlFileName", () => {
|
||||
it("appends .sql when missing", () => {
|
||||
@@ -18,3 +18,34 @@ describe("savedSqlFileName", () => {
|
||||
expect(stripSqlExtension("report.SQL")).toBe("report");
|
||||
});
|
||||
});
|
||||
|
||||
describe("nextAvailableSqlName", () => {
|
||||
it("keeps a free name unchanged", () => {
|
||||
expect(nextAvailableSqlName("query_4", new Set())).toBe("query_4.sql");
|
||||
expect(nextAvailableSqlName("query_4.sql", new Set(["other.sql"]))).toBe("query_4.sql");
|
||||
});
|
||||
|
||||
it("continues an underscored counter series instead of nesting a new suffix", () => {
|
||||
expect(nextAvailableSqlName("query_3.sql", new Set(["query_3.sql"]))).toBe("query_4.sql");
|
||||
expect(nextAvailableSqlName("query_4.sql", new Set(["query_3.sql", "query_4.sql", "query_5.sql"]))).toBe("query_6.sql");
|
||||
});
|
||||
|
||||
it("continues a parenthesized counter series", () => {
|
||||
expect(nextAvailableSqlName("report (2).sql", new Set(["report (2).sql"]))).toBe("report (3).sql");
|
||||
expect(nextAvailableSqlName("report(2).sql", new Set(["report(2).sql"]))).toBe("report(3).sql");
|
||||
});
|
||||
|
||||
it("starts a parenthesized series for names without a counter", () => {
|
||||
expect(nextAvailableSqlName("report.sql", new Set(["report.sql"]))).toBe("report (2).sql");
|
||||
expect(nextAvailableSqlName("report.sql", new Set(["report.sql", "REPORT (2).SQL"]))).toBe("report (3).sql");
|
||||
});
|
||||
|
||||
it("compares names case-insensitively and implies the .sql extension", () => {
|
||||
expect(nextAvailableSqlName("Query_7", new Set(["query_7.SQL"]))).toBe("Query_8.sql");
|
||||
expect(nextAvailableSqlName("query_7.sql", new Set(["query_7"]))).toBe("query_8.sql");
|
||||
});
|
||||
|
||||
it("skips over taken names inside the series", () => {
|
||||
expect(nextAvailableSqlName("query_1.sql", new Set(["query_1.sql", "query_2.sql", "query_4.sql"]))).toBe("query_3.sql");
|
||||
});
|
||||
});
|
||||
@@ -7,3 +7,47 @@ export function ensureSqlExtension(name: string): string {
|
||||
export function stripSqlExtension(name: string): string {
|
||||
return name.replace(/\.sql$/i, "");
|
||||
}
|
||||
|
||||
interface SqlNameSeries {
|
||||
index: number;
|
||||
format: (index: number) => string;
|
||||
}
|
||||
|
||||
/**
|
||||
* The de-duplication suffix family a name already belongs to.
|
||||
*
|
||||
* A name that carries its own trailing counter keeps that formatting so the
|
||||
* next free name stays in the same series — `query_3.sql` continues as
|
||||
* `query_4.sql` and `report (2).sql` as `report (3).sql`, never the nested
|
||||
* `query_4 (2).sql` / `report (2) (2).sql` that stacking a fresh suffix would
|
||||
* produce. Names without a counter start a parenthesized series at 2.
|
||||
*/
|
||||
function sqlNameSeries(base: string): SqlNameSeries | undefined {
|
||||
const parenthesized = /^(.*?)([ _]?)\((\d+)\)$/.exec(base);
|
||||
if (parenthesized?.[1]) return { index: Number(parenthesized[3]), format: (index) => `${parenthesized[1]}${parenthesized[2]}(${index}).sql` };
|
||||
const underscored = /^(.*?)_(\d+)$/.exec(base);
|
||||
if (underscored?.[1]) return { index: Number(underscored[2]), format: (index) => `${underscored[1]}_${index}.sql` };
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves `baseName` to the first name that is free in `takenNames`.
|
||||
*
|
||||
* `takenNames` must come from the same scope the caller will persist into —
|
||||
* the saved-SQL library keys names by connection, catalog, database and folder,
|
||||
* so a caller that counts a narrower set can still hand back a colliding name.
|
||||
* Matching is case-insensitive and `.sql` is implied, mirroring the library's
|
||||
* own uniqueness rule.
|
||||
*/
|
||||
export function nextAvailableSqlName(baseName: string, takenNames: ReadonlySet<string>): string {
|
||||
const normalized = ensureSqlExtension(baseName);
|
||||
const normalizedTakenNames = new Set([...takenNames].map((name) => ensureSqlExtension(name).toLocaleLowerCase()));
|
||||
if (!normalizedTakenNames.has(normalized.toLocaleLowerCase())) return normalized;
|
||||
|
||||
const base = stripSqlExtension(normalized);
|
||||
const series = sqlNameSeries(base);
|
||||
const format = series?.format ?? ((index: number) => `${base} (${index}).sql`);
|
||||
let index = (series?.index ?? 1) + 1;
|
||||
while (normalizedTakenNames.has(format(index).toLocaleLowerCase())) index++;
|
||||
return format(index);
|
||||
}
|
||||
Reference in new issue
Block a user