2022-10-13 23:31:26 +00:00
|
|
|
mod kvp;
|
2022-10-14 01:24:00 +00:00
|
|
|
mod migrations;
|
2022-10-13 23:31:26 +00:00
|
|
|
|
2022-10-18 22:58:05 +00:00
|
|
|
use std::fs;
|
|
|
|
use std::path::{Path, PathBuf};
|
2022-10-18 18:43:18 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-06-03 00:28:28 +00:00
|
|
|
use anyhow::Result;
|
2022-10-18 18:43:18 +00:00
|
|
|
use log::error;
|
2022-10-14 01:24:00 +00:00
|
|
|
use parking_lot::Mutex;
|
|
|
|
use rusqlite::Connection;
|
2022-06-03 00:28:28 +00:00
|
|
|
|
2022-10-18 18:43:18 +00:00
|
|
|
use migrations::MIGRATIONS;
|
2022-06-03 00:28:28 +00:00
|
|
|
|
2022-10-18 22:58:05 +00:00
|
|
|
#[derive(Clone)]
|
2022-10-18 18:43:18 +00:00
|
|
|
pub enum Db {
|
2022-10-18 22:58:05 +00:00
|
|
|
Real(Arc<RealDb>),
|
2022-10-18 18:43:18 +00:00
|
|
|
Null,
|
2022-06-03 00:28:28 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 22:58:05 +00:00
|
|
|
pub struct RealDb {
|
|
|
|
connection: Mutex<Connection>,
|
|
|
|
path: Option<PathBuf>,
|
|
|
|
}
|
2022-10-13 22:43:42 +00:00
|
|
|
|
2022-06-03 00:28:28 +00:00
|
|
|
impl Db {
|
2022-10-18 22:58:05 +00:00
|
|
|
/// Open or create a database at the given directory path.
|
2022-10-28 18:50:26 +00:00
|
|
|
pub fn open(db_dir: &Path, channel: &'static str) -> Self {
|
2022-10-18 22:58:05 +00:00
|
|
|
// Use 0 for now. Will implement incrementing and clearing of old db files soon TM
|
2022-10-28 18:50:26 +00:00
|
|
|
let current_db_dir = db_dir.join(Path::new(&format!("0-{}", channel)));
|
2022-10-18 22:58:05 +00:00
|
|
|
fs::create_dir_all(¤t_db_dir)
|
|
|
|
.expect("Should be able to create the database directory");
|
|
|
|
let db_path = current_db_dir.join(Path::new("db.sqlite"));
|
|
|
|
|
|
|
|
Connection::open(db_path)
|
2022-10-18 18:43:18 +00:00
|
|
|
.map_err(Into::into)
|
2022-10-18 22:58:05 +00:00
|
|
|
.and_then(|connection| Self::initialize(connection))
|
|
|
|
.map(|connection| {
|
|
|
|
Db::Real(Arc::new(RealDb {
|
|
|
|
connection,
|
|
|
|
path: Some(db_dir.to_path_buf()),
|
|
|
|
}))
|
|
|
|
})
|
2022-10-18 18:43:18 +00:00
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
error!(
|
2022-10-18 22:58:05 +00:00
|
|
|
"Connecting to file backed db failed. Reverting to null db. {}",
|
2022-10-18 18:43:18 +00:00
|
|
|
e
|
|
|
|
);
|
2022-10-18 22:58:05 +00:00
|
|
|
Self::Null
|
2022-10-18 18:43:18 +00:00
|
|
|
})
|
2022-06-03 00:28:28 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 23:31:26 +00:00
|
|
|
/// Open a in memory database for testing and as a fallback.
|
2022-10-18 22:58:05 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
pub fn open_in_memory() -> Self {
|
2022-10-18 18:43:18 +00:00
|
|
|
Connection::open_in_memory()
|
|
|
|
.map_err(Into::into)
|
2022-10-18 22:58:05 +00:00
|
|
|
.and_then(|connection| Self::initialize(connection))
|
|
|
|
.map(|connection| {
|
|
|
|
Db::Real(Arc::new(RealDb {
|
|
|
|
connection,
|
|
|
|
path: None,
|
|
|
|
}))
|
|
|
|
})
|
2022-10-18 18:43:18 +00:00
|
|
|
.unwrap_or_else(|e| {
|
2022-10-18 22:58:05 +00:00
|
|
|
error!(
|
|
|
|
"Connecting to in memory db failed. Reverting to null db. {}",
|
|
|
|
e
|
|
|
|
);
|
|
|
|
Self::Null
|
2022-10-18 18:43:18 +00:00
|
|
|
})
|
2022-10-13 22:43:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 22:58:05 +00:00
|
|
|
fn initialize(mut conn: Connection) -> Result<Mutex<Connection>> {
|
2022-10-14 01:24:00 +00:00
|
|
|
MIGRATIONS.to_latest(&mut conn)?;
|
2022-10-13 22:43:42 +00:00
|
|
|
|
2022-10-18 22:58:05 +00:00
|
|
|
conn.pragma_update(None, "journal_mode", "WAL")?;
|
|
|
|
conn.pragma_update(None, "synchronous", "NORMAL")?;
|
|
|
|
conn.pragma_update(None, "foreign_keys", true)?;
|
|
|
|
conn.pragma_update(None, "case_sensitive_like", true)?;
|
|
|
|
|
|
|
|
Ok(Mutex::new(conn))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn persisting(&self) -> bool {
|
|
|
|
self.real().and_then(|db| db.path.as_ref()).is_some()
|
2022-06-03 00:28:28 +00:00
|
|
|
}
|
2022-10-18 18:43:18 +00:00
|
|
|
|
2022-10-18 22:58:05 +00:00
|
|
|
pub fn real(&self) -> Option<&RealDb> {
|
2022-10-18 18:43:18 +00:00
|
|
|
match self {
|
2022-10-18 22:58:05 +00:00
|
|
|
Db::Real(db) => Some(&db),
|
|
|
|
_ => None,
|
2022-10-18 18:43:18 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-03 00:28:28 +00:00
|
|
|
}
|
2022-10-18 22:58:05 +00:00
|
|
|
|
|
|
|
impl Drop for Db {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
match self {
|
|
|
|
Db::Real(real_db) => {
|
|
|
|
let lock = real_db.connection.lock();
|
|
|
|
|
|
|
|
let _ = lock.pragma_update(None, "analysis_limit", "500");
|
|
|
|
let _ = lock.pragma_update(None, "optimize", "");
|
|
|
|
}
|
|
|
|
Db::Null => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::migrations::MIGRATIONS;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_migrations() {
|
|
|
|
assert!(MIGRATIONS.validate().is_ok());
|
|
|
|
}
|
|
|
|
}
|