Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(sql-library): skip restricted folders during import

t8y29b05158c9a16
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<string[]> {
- const { readDir } = await import("@tauri-apps/plugin-fs");
- const { join } = await import("@tauri-apps/api/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;
+ const collectPaths = (entries: Awaited<ReturnType<typeof api.listSqlFilesInFolder>>): string[] => entries.flatMap((entry) => (entry.is_dir ? collectPaths(entry.children) : [entry.path]));
+
+ 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<Vec<SqlFile
.await
.map_err(|e| format!("Failed to scan folder: {e}"))?
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn collect_file_names(entries: &[SqlFileEntry], names: &mut Vec<String>) {
+ 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();
+ }
+}