Add auto-migration from sled to sqlite on startup

This commit is contained in:
rishflab 2021-09-23 11:18:39 +10:00
parent a738c9df8a
commit 0f7876c107
4 changed files with 93 additions and 24 deletions

View file

@ -22,10 +22,12 @@ impl SqliteDatabase {
{
let path_str = format!("sqlite:{}", path.as_ref().display());
let pool = SqlitePool::connect(&path_str).await?;
Ok(Self { pool })
let mut sqlite = Self { pool };
sqlite.run_migrations().await?;
Ok(sqlite)
}
pub async fn run_migrations(&mut self) -> anyhow::Result<()> {
async fn run_migrations(&mut self) -> anyhow::Result<()> {
sqlx::migrate!("./migrations").run(&self.pool).await?;
Ok(())
}
@ -374,9 +376,7 @@ mod tests {
// file has to exist in order to connect with sqlite
File::create(temp_db.clone()).unwrap();
let mut db = SqliteDatabase::open(temp_db).await?;
db.run_migrations().await.unwrap();
let db = SqliteDatabase::open(temp_db).await?;
Ok(db)
}