zed/crates/db/src/db.rs

123 lines
3.2 KiB
Rust
Raw Normal View History

mod items;
2022-10-13 23:31:26 +00:00
mod kvp;
2022-10-14 01:24:00 +00:00
mod migrations;
2022-10-20 22:07:58 +00:00
mod pane;
mod workspace;
2022-10-13 23:31:26 +00:00
use std::fs;
use std::path::{Path, PathBuf};
2022-10-18 18:43:18 +00:00
use std::sync::Arc;
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-10-18 18:43:18 +00:00
use migrations::MIGRATIONS;
#[derive(Clone)]
2022-10-18 18:43:18 +00:00
pub enum Db {
Real(Arc<RealDb>),
2022-10-18 18:43:18 +00:00
Null,
}
pub struct RealDb {
connection: Mutex<Connection>,
path: Option<PathBuf>,
}
2022-10-13 22:43:42 +00:00
impl Db {
/// 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 {
// 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)));
fs::create_dir_all(&current_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)
.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!(
"Connecting to file backed db failed. Reverting to null db. {}",
2022-10-18 18:43:18 +00:00
e
);
Self::Null
2022-10-18 18:43:18 +00:00
})
}
2022-10-13 23:31:26 +00:00
/// Open a in memory database for testing and as a fallback.
#[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)
.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| {
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
}
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
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-10-18 18:43:18 +00:00
pub fn real(&self) -> Option<&RealDb> {
2022-10-18 18:43:18 +00:00
match self {
Db::Real(db) => Some(&db),
_ => None,
2022-10-18 18:43:18 +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());
}
}