fix(duckdb): replace empty placeholder database files

This commit is contained in:
hb authored and GitHub committed 2026-06-30 02:45:21 +08:00
1 parent 285a1c555a
commit 074cef8358
2 files changed
+29

No files matched your search

+1
View File
@@ -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();
+28
View File
@@ -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<Arc<Mutex<duckdb::Connection>>, 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);
}
}