From 074cef83583f2d7ca84cc86a33f70d43478d966c Mon Sep 17 00:00:00 2001 From: hb <34821912+chenhbb@users.noreply.github.com> Date: Tue, 30 Jun 2026 02:45:21 +0800 Subject: [PATCH] fix(duckdb): replace empty placeholder database files --- crates/dbx-core/src/connection.rs | 1 + crates/dbx-core/src/db/duckdb_driver.rs | 28 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/crates/dbx-core/src/connection.rs b/crates/dbx-core/src/connection.rs index b4584d8c49..bdc5f458e6 100644 --- a/crates/dbx-core/src/connection.rs +++ b/crates/dbx-core/src/connection.rs @@ -3377,6 +3377,7 @@ mod tests { async fn duckdb_client_session_reuses_base_pool_to_avoid_file_locks() { let (state, dir) = test_app_state().await; let db_path = dir.join("session.duckdb"); + duckdb::Connection::open(&db_path).unwrap(); let mut config = mysql_config(None); config.id = "duckdb-conn".to_string(); config.name = "DuckDB".to_string(); diff --git a/crates/dbx-core/src/db/duckdb_driver.rs b/crates/dbx-core/src/db/duckdb_driver.rs index d64ee97519..ad7cf93942 100644 --- a/crates/dbx-core/src/db/duckdb_driver.rs +++ b/crates/dbx-core/src/db/duckdb_driver.rs @@ -1,5 +1,6 @@ #![cfg(feature = "duckdb-bundled")] +use std::fs; use std::path::Path; use std::sync::Arc; use std::sync::Mutex; @@ -16,6 +17,7 @@ pub fn connect_path(path: &str) -> Result>, String let is_memory = is_memory_database_path(path); if !is_memory { validate_duckdb_path(path)?; + remove_empty_placeholder_file(path)?; } let connection = if is_memory { duckdb::Connection::open_in_memory() } else { duckdb::Connection::open(path) } @@ -50,6 +52,19 @@ fn validate_duckdb_path(path: &str) -> Result<(), String> { Ok(()) } +fn remove_empty_placeholder_file(path: &str) -> Result<(), String> { + let path_obj = Path::new(path); + if is_network_path(path) || !path_obj.exists() { + return Ok(()); + } + + let metadata = fs::metadata(path_obj).map_err(|e| format!("Failed to read database file metadata: {e}"))?; + if metadata.is_file() && metadata.len() == 0 { + fs::remove_file(path_obj).map_err(|e| format!("Failed to replace empty DuckDB placeholder file: {e}"))?; + } + Ok(()) +} + fn is_network_path(path: &str) -> bool { path.starts_with("\\\\") || path.starts_with("//") || path.contains("wsl.localhost") || path.contains("wsl$") } @@ -94,4 +109,17 @@ mod tests { assert_eq!(value, 42); } + + #[test] + fn connect_path_replaces_empty_placeholder_file() { + let path = std::env::temp_dir().join(format!("dbx-duckdb-empty-{}.duckdb", uuid::Uuid::new_v4())); + std::fs::write(&path, "").expect("write empty placeholder"); + assert_eq!(std::fs::metadata(&path).expect("placeholder metadata").len(), 0); + + let con = connect_path(path.to_string_lossy().as_ref()).expect("connect empty placeholder DuckDB file"); + close_connection(con); + + assert!(std::fs::metadata(&path).expect("database metadata").len() > 0); + let _ = std::fs::remove_file(path); + } }