refactor(db): open_store no longer creates the directory

handlers::db_path already creates the DATA_DIR before it opens
$DATA_DIR/task_queue.db, and every other caller (tests) passes a path whose
parent exists, so the guard plus its one-caller rusqlite_error mapper were
duplicated work. The doc now says which caller owns the directory.
This commit is contained in:
2026-09-21 17:42:56 +08:00
parent 65c9aa6c5c
commit f7179d65ee
+3 -10
View File
@@ -135,13 +135,10 @@ pub fn open_db(path: &str) -> rusqlite::Result<Connection> {
/// Opens the shared DB file, runs the merged schema for all three tables and
/// returns a pool for it. One call per process in production (the stores
/// share the returned pool); tests call it per tempdir.
/// share the returned pool); tests call it per tempdir. The file's directory
/// must exist already — [`crate::handlers::db_path`] is what creates it, and
/// it is the only caller that takes a path it did not get from a tempdir.
pub fn open_store(path: &str) -> rusqlite::Result<Arc<DbPool>> {
if let Some(parent) = std::path::Path::new(path).parent()
&& !parent.as_os_str().is_empty()
{
std::fs::create_dir_all(parent).map_err(rusqlite_error)?;
}
let conn = open_db(path)?;
schema_init(&conn)?;
migrate(&conn)?;
@@ -182,10 +179,6 @@ fn migrate(conn: &Connection) -> rusqlite::Result<()> {
Ok(())
}
fn rusqlite_error(e: std::io::Error) -> rusqlite::Error {
rusqlite::Error::ToSqlConversionFailure(Box::new(e))
}
/// Creates the `tasks`, `chat_state` and `link_cache` tables (idempotent).
/// The three stores used to own their own schema; keeping it in one place
/// means one initialization for the whole database file.