fix: Initialize DB when it doesn't exist

This commit is contained in:
selfhoster selfhoster 2026-09-19 14:42:55 +02:00
commit c35bfaeaf8

View file

@ -47,6 +47,22 @@ pub struct FilesystemDatabase {
impl FilesystemDatabase { impl FilesystemDatabase {
pub async fn from_path(path: impl AsRef<Utf8Path>) -> Result<Database, BoxedError> { pub async fn from_path(path: impl AsRef<Utf8Path>) -> Result<Database, BoxedError> {
let path = path.as_ref(); let path = path.as_ref();
if !tokio::fs::try_exists(path)
.await
.map_err(|e| Box::new(FilesystemDatabaseError::ReadFileIO(path.to_path_buf(), e)))?
{
// The database doesn't exist yet, we create an empty one and try to write it
// to make sure the permissions are correct and the destination folder exists.
let mut db = Self {
inner: MemoryDatabase::default(),
path: path.to_path_buf(),
};
db.save().await?;
return Ok(Database::new(db));
}
let s = tokio::fs::read(path) let s = tokio::fs::read(path)
.await .await
.map_err(|e| Box::new(FilesystemDatabaseError::ReadFileIO(path.to_path_buf(), e)))?; .map_err(|e| Box::new(FilesystemDatabaseError::ReadFileIO(path.to_path_buf(), e)))?;