fix(sql-library): preserve folders during directory import
Closes #5877
This commit is contained in:
4 files changed
+101
-18
No files matched your search
@@ -0,0 +1,30 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { collectSavedSqlDirectoryImportFiles } from "@/lib/savedSql/savedSqlDirectoryImport";
|
||||
|
||||
describe("saved SQL directory import", () => {
|
||||
it("preserves nested directory paths without flattening file names", () => {
|
||||
const files = collectSavedSqlDirectoryImportFiles([
|
||||
{ name: "database", path: "/scripts/database", is_dir: true, children: [{ name: "daily.sql", path: "/scripts/database/daily.sql", is_dir: false, children: [] }] },
|
||||
{
|
||||
name: "incidents",
|
||||
path: "/scripts/incidents",
|
||||
is_dir: true,
|
||||
children: [
|
||||
{
|
||||
name: "postgres",
|
||||
path: "/scripts/incidents/postgres",
|
||||
is_dir: true,
|
||||
children: [{ name: "repair.sql", path: "/scripts/incidents/postgres/repair.sql", is_dir: false, children: [] }],
|
||||
},
|
||||
],
|
||||
},
|
||||
{ name: "root.sql", path: "/scripts/root.sql", is_dir: false, children: [] },
|
||||
]);
|
||||
|
||||
expect(files).toEqual([
|
||||
{ name: "daily.sql", path: "/scripts/database/daily.sql", folderNames: ["database"] },
|
||||
{ name: "repair.sql", path: "/scripts/incidents/postgres/repair.sql", folderNames: ["incidents", "postgres"] },
|
||||
{ name: "root.sql", path: "/scripts/root.sql", folderNames: [] },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,24 @@
|
||||
export interface SavedSqlDirectoryImportEntry {
|
||||
name: string;
|
||||
path: string;
|
||||
is_dir: boolean;
|
||||
children: SavedSqlDirectoryImportEntry[];
|
||||
}
|
||||
|
||||
export interface SavedSqlDirectoryImportFile {
|
||||
name: string;
|
||||
path: string;
|
||||
folderNames: string[];
|
||||
}
|
||||
|
||||
export function collectSavedSqlDirectoryImportFiles(entries: SavedSqlDirectoryImportEntry[], folderNames: string[] = []): SavedSqlDirectoryImportFile[] {
|
||||
const files: SavedSqlDirectoryImportFile[] = [];
|
||||
for (const entry of entries) {
|
||||
if (entry.is_dir) {
|
||||
files.push(...collectSavedSqlDirectoryImportFiles(entry.children, [...folderNames, entry.name]));
|
||||
} else {
|
||||
files.push({ name: entry.name, path: entry.path, folderNames });
|
||||
}
|
||||
}
|
||||
return files;
|
||||
}
|
||||
Reference in new issue
Block a user