From 9b05158c9a162f8ba0a46df719bdd97601d8e83b Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Sun, 2 Aug 2026 09:33:02 +0800 Subject: [PATCH] fix(sql-library): skip restricted folders during import --- .../src/components/layout/SqlLibraryPanel.vue | 16 +------- .../sqlLibraryImportEncoding.test.ts | 9 +++++ src-tauri/src/commands/list_sql_files.rs | 37 +++++++++++++++++++ 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/apps/desktop/src/components/layout/SqlLibraryPanel.vue b/apps/desktop/src/components/layout/SqlLibraryPanel.vue index 29471d3b5a..734f71e408 100644 --- a/apps/desktop/src/components/layout/SqlLibraryPanel.vue +++ b/apps/desktop/src/components/layout/SqlLibraryPanel.vue @@ -206,21 +206,9 @@ async function exportFolderContents(folder?: SavedSqlFolder) { } async function collectSqlFilesRecursively(dir: string): Promise { - const { readDir } = await import("@tauri-apps/plugin-fs"); - const { join } = await import("@tauri-apps/api/path"); + const collectPaths = (entries: Awaited>): string[] => entries.flatMap((entry) => (entry.is_dir ? collectPaths(entry.children) : [entry.path])); - const results: string[] = []; - for (const entry of await readDir(dir)) { - const fullPath = await join(dir, entry.name); - if (entry.isDirectory) { - results.push(...(await collectSqlFilesRecursively(fullPath))); - continue; - } - if (!entry.isFile) continue; - if (!fullPath.toLowerCase().endsWith(".sql")) continue; - results.push(fullPath); - } - return results; + return collectPaths(await api.listSqlFilesInFolder(dir)); } async function importDirectoryIntoLibrary(targetFolder?: SavedSqlFolder) { diff --git a/packages/app-tests/sqlLibraryImportEncoding.test.ts b/packages/app-tests/sqlLibraryImportEncoding.test.ts index ad52067c8a..57eb36b1fd 100644 --- a/packages/app-tests/sqlLibraryImportEncoding.test.ts +++ b/packages/app-tests/sqlLibraryImportEncoding.test.ts @@ -13,6 +13,15 @@ test("SQL library imports use the charset-aware external SQL reader", () => { assert.doesNotMatch(handler, /readTextFile/); }); +test("SQL library directory imports use the native pruned folder scanner", () => { + const start = source.indexOf("async function collectSqlFilesRecursively"); + const end = source.indexOf("async function importDirectoryIntoLibrary", start); + const scanner = start >= 0 && end > start ? source.slice(start, end) : ""; + + assert.match(scanner, /await api\.listSqlFilesInFolder\(dir\)/); + assert.doesNotMatch(scanner, /@tauri-apps\/plugin-fs|readDir/); +}); + test("SQL library file menu can open a file with the current tab target", () => { assert.match(source, /label: t\("sqlLibrary\.openInCurrentDatabase"\)/); assert.match(source, /action: \(\) => openFile\(target, "current"\)/); diff --git a/src-tauri/src/commands/list_sql_files.rs b/src-tauri/src/commands/list_sql_files.rs index 576ea5bc48..fa813adcf3 100644 --- a/src-tauri/src/commands/list_sql_files.rs +++ b/src-tauri/src/commands/list_sql_files.rs @@ -116,3 +116,40 @@ pub async fn list_sql_files_in_folder(folder_path: String) -> Result) { + for entry in entries { + if entry.is_dir { + collect_file_names(&entry.children, names); + } else { + names.push(entry.name.clone()); + } + } + } + + #[test] + fn scan_sql_files_skips_pruned_metadata_directories() { + let root = std::env::temp_dir().join(format!("dbx-sql-folder-scan-{}", uuid::Uuid::new_v4())); + let idea = root.join(".idea"); + let nested = root.join("queries"); + std::fs::create_dir_all(&idea).unwrap(); + std::fs::create_dir_all(&nested).unwrap(); + std::fs::write(root.join("root.sql"), "SELECT 1;").unwrap(); + std::fs::write(nested.join("nested.SQL"), "SELECT 2;").unwrap(); + std::fs::write(nested.join("notes.txt"), "ignored").unwrap(); + std::fs::write(idea.join("workspace.sql"), "SELECT 3;").unwrap(); + + let mut visited = HashSet::new(); + let entries = scan_sql_files(&root, 0, &mut visited); + let mut names = Vec::new(); + collect_file_names(&entries, &mut names); + names.sort(); + + assert_eq!(names, vec!["nested.SQL", "root.sql"]); + std::fs::remove_dir_all(root).unwrap(); + } +}