Converted to using rusqlite

This commit is contained in:
Mikayla Maki 2022-10-13 18:24:00 -07:00 committed by K Simmons
parent aa8fa4a6d5
commit dbea3cf20c
8 changed files with 91 additions and 172 deletions

View file

@ -1,16 +1,17 @@
mod kvp;
mod migrations;
use anyhow::Result;
use sqlx::sqlite::SqliteConnectOptions;
use sqlx::{Pool, Sqlite};
use migrations::MIGRATIONS;
use parking_lot::Mutex;
use rusqlite::Connection;
use std::path::Path;
use std::str::FromStr;
use std::sync::Arc;
pub use kvp::*;
pub struct Db {
pool: Pool<Sqlite>,
connecion: Connection,
in_memory: bool,
}
@ -21,35 +22,26 @@ pub struct Db {
impl Db {
/// Open or create a database at the given file path. Falls back to in memory database if the
/// database at the given path is corrupted
pub async fn open(path: &Path) -> Arc<Self> {
let options = SqliteConnectOptions::from_str(&format!(
"sqlite://{}",
path.to_string_lossy().to_string()
))
.expect("database path should always be well formed")
.create_if_missing(true);
pub fn open(path: &Path) -> Result<Arc<Mutex<Self>>> {
let conn = Connection::open(path)?;
Self::initialize(options, false)
.await
.unwrap_or(Self::open_in_memory().await)
Self::initialize(conn, false).or_else(|_| Self::open_in_memory())
}
/// Open a in memory database for testing and as a fallback.
pub async fn open_in_memory() -> Arc<Self> {
let options = SqliteConnectOptions::from_str(":memory:")
.expect("Should always be able to create in memory database options");
pub fn open_in_memory() -> Result<Arc<Mutex<Self>>> {
let conn = Connection::open_in_memory()?;
Self::initialize(options, true)
.await
.expect("Should always be able to create an in memory database")
Self::initialize(conn, true)
}
async fn initialize(options: SqliteConnectOptions, in_memory: bool) -> Result<Arc<Self>> {
let pool = Pool::<Sqlite>::connect_with(options).await?;
fn initialize(mut conn: Connection, in_memory: bool) -> Result<Arc<Mutex<Self>>> {
MIGRATIONS.to_latest(&mut conn)?;
sqlx::migrate!().run(&pool).await?;
Ok(Arc::new(Self { pool, in_memory }))
Ok(Arc::new(Mutex::new(Self {
connecion: conn,
in_memory,
})))
}
}
@ -61,7 +53,7 @@ mod tests {
#[gpui::test]
fn test_db() {
let dir = TempDir::new("db-test").unwrap();
let fake_db = Db::open_fake();
let fake_db = Db::open_in_memory().unwrap();
let real_db = Db::open(&dir.path().join("test.db")).unwrap();
for db in [&real_db, &fake_db] {