Rebase fix + Started writing the real SQL we're going to need

This commit is contained in:
Mikayla Maki 2022-10-24 16:55:32 -07:00
parent e5c6393f85
commit 500ecbf915
8 changed files with 109 additions and 81 deletions

View file

@ -11,7 +11,7 @@ use std::sync::Arc;
use anyhow::Result;
use log::error;
use parking_lot::Mutex;
use rusqlite::Connection;
use rusqlite::{backup, Connection};
use migrations::MIGRATIONS;
pub use workspace::*;
@ -54,27 +54,6 @@ impl Db {
})
}
/// Open a in memory database for testing and as a fallback.
#[cfg(any(test, feature = "test-support"))]
pub fn open_in_memory() -> Self {
Connection::open_in_memory()
.map_err(Into::into)
.and_then(|connection| Self::initialize(connection))
.map(|connection| {
Db::Real(Arc::new(RealDb {
connection,
path: None,
}))
})
.unwrap_or_else(|e| {
error!(
"Connecting to in memory db failed. Reverting to null db. {}",
e
);
Self::Null
})
}
fn initialize(mut conn: Connection) -> Result<Mutex<Connection>> {
MIGRATIONS.to_latest(&mut conn)?;
@ -96,6 +75,43 @@ impl Db {
_ => None,
}
}
/// Open a in memory database for testing and as a fallback.
pub fn open_in_memory() -> Self {
Connection::open_in_memory()
.map_err(Into::into)
.and_then(|connection| Self::initialize(connection))
.map(|connection| {
Db::Real(Arc::new(RealDb {
connection,
path: None,
}))
})
.unwrap_or_else(|e| {
error!(
"Connecting to in memory db failed. Reverting to null db. {}",
e
);
Self::Null
})
}
pub fn write_to<P: AsRef<Path>>(&self, dest: P) -> Result<()> {
self.real()
.map(|db| {
if db.path.is_some() {
panic!("DB already exists");
}
let lock = db.connection.lock();
let mut dst = Connection::open(dest)?;
let backup = backup::Backup::new(&lock, &mut dst)?;
backup.step(-1)?;
Ok(())
})
.unwrap_or(Ok(()))
}
}
impl Drop for Db {