rvault/src/storage.rs

31 lines
881 B
Rust

use std::{fs::File, path::Path};
use log::*;
use sqlx::{sqlite::SqlitePoolOptions, Pool, Sqlite};
pub(crate) type DatabaseDriver = Pool<Sqlite>;
pub async fn create_pool(db_url: String) -> DatabaseDriver {
// Create SQLite database file if it does not exist
if db_url.starts_with("sqlite:") && db_url != ("sqlite::memory:") {
let path = db_url.replace("sqlite:", "");
if !Path::new(&path).exists() {
warn!("Sqlite database does not exist, creating file {}", path);
File::create(&path).expect("Failed to create database file");
}
}
let pool = SqlitePoolOptions::new()
.max_connections(5)
.test_before_acquire(true)
.connect(&db_url)
.await
.expect(&db_url);
sqlx::migrate!()
.run(&pool)
.await
.expect("Failed to apply migrations");
pool
}