fix: Initialize DB when it doesn't exist
This commit is contained in:
parent
ccbd97b836
commit
c35bfaeaf8
1 changed files with 16 additions and 0 deletions
|
|
@ -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)))?;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue