Skip to content

Commit 340653b

Browse files
committed
Make sure default SQLite database parent dir is created.
Signed-off-by: Ryan Levick <[email protected]>
1 parent c132a03 commit 340653b

File tree

2 files changed

+30
-15
lines changed
  • crates

2 files changed

+30
-15
lines changed

crates/factor-sqlite/src/runtime_config/spin.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use spin_factors::{
1010
anyhow::{self, Context as _},
1111
runtime_config::toml::GetTomlValue,
1212
};
13+
use spin_sqlite_inproc::InProcDatabaseLocation;
1314
use spin_world::v2::sqlite as v2;
1415
use tokio::sync::OnceCell;
1516

@@ -108,10 +109,7 @@ impl DefaultLabelResolver for RuntimeConfigResolver {
108109
.as_deref()
109110
.map(|p| p.join(DEFAULT_SQLITE_DB_FILENAME));
110111
let factory = move || {
111-
let location = match &path {
112-
Some(path) => spin_sqlite_inproc::InProcDatabaseLocation::Path(path.clone()),
113-
None => spin_sqlite_inproc::InProcDatabaseLocation::InMemory,
114-
};
112+
let location = InProcDatabaseLocation::from_path(path.clone())?;
115113
let connection = spin_sqlite_inproc::InProcConnection::new(location)?;
116114
Ok(Box::new(connection) as _)
117115
};
@@ -202,17 +200,11 @@ impl LocalDatabase {
202200
///
203201
/// `base_dir` is the base directory path from which `path` is resolved if it is a relative path.
204202
fn connection_creator(self, base_dir: &Path) -> anyhow::Result<impl ConnectionCreator> {
205-
let location = match self.path {
206-
Some(path) => {
207-
let path = resolve_relative_path(&path, base_dir);
208-
// Create the store's parent directory if necessary
209-
// unwrapping the parent is fine, because `resolve_relative_path`` will always return a path with a parent
210-
std::fs::create_dir_all(path.parent().unwrap())
211-
.context("Failed to create sqlite database directory")?;
212-
spin_sqlite_inproc::InProcDatabaseLocation::Path(path)
213-
}
214-
None => spin_sqlite_inproc::InProcDatabaseLocation::InMemory,
215-
};
203+
let path = self
204+
.path
205+
.as_ref()
206+
.map(|p| resolve_relative_path(p, base_dir));
207+
let location = InProcDatabaseLocation::from_path(path)?;
216208
let factory = move || {
217209
let connection = spin_sqlite_inproc::InProcConnection::new(location.clone())?;
218210
Ok(Box::new(connection) as _)

crates/sqlite-inproc/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,29 @@ pub enum InProcDatabaseLocation {
1616
Path(PathBuf),
1717
}
1818

19+
impl InProcDatabaseLocation {
20+
/// Convert an optional path to a database location.
21+
///
22+
/// Ensures that the parent directory of the database exists.
23+
pub fn from_path(path: Option<PathBuf>) -> anyhow::Result<Self> {
24+
match path {
25+
Some(path) => {
26+
// Create the store's parent directory if necessary
27+
if let Some(parent) = path.parent() {
28+
std::fs::create_dir_all(parent).with_context(|| {
29+
format!(
30+
"failed to create sqlite database directory '{}'",
31+
parent.display()
32+
)
33+
})?;
34+
}
35+
Ok(Self::Path(path))
36+
}
37+
None => Ok(Self::InMemory),
38+
}
39+
}
40+
}
41+
1942
/// A connection to a sqlite database
2043
pub struct InProcConnection {
2144
location: InProcDatabaseLocation,

0 commit comments

Comments
 (0)