2022-10-27 20:58:54 +00:00
|
|
|
pub mod kvp;
|
2022-10-13 23:31:26 +00:00
|
|
|
|
2022-11-17 00:35:56 +00:00
|
|
|
use std::fs::create_dir_all;
|
2022-11-01 20:15:58 +00:00
|
|
|
use std::path::Path;
|
2022-10-18 18:43:18 +00:00
|
|
|
|
2022-11-14 21:18:44 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-06-03 00:28:28 +00:00
|
|
|
use anyhow::Result;
|
2022-11-01 20:15:58 +00:00
|
|
|
use indoc::indoc;
|
2022-11-14 21:18:44 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-11-01 20:15:58 +00:00
|
|
|
use sqlez::connection::Connection;
|
2022-11-17 00:35:56 +00:00
|
|
|
use sqlez::domain::{Domain, Migrator};
|
2022-11-01 20:15:58 +00:00
|
|
|
use sqlez::thread_safe_connection::ThreadSafeConnection;
|
2022-11-17 00:35:56 +00:00
|
|
|
use util::channel::RELEASE_CHANNEL_NAME;
|
|
|
|
use util::paths::DB_DIR;
|
2022-11-04 20:22:35 +00:00
|
|
|
|
2022-11-10 23:29:29 +00:00
|
|
|
const INITIALIZE_QUERY: &'static str = indoc! {"
|
|
|
|
PRAGMA journal_mode=WAL;
|
|
|
|
PRAGMA synchronous=NORMAL;
|
2022-11-17 00:35:56 +00:00
|
|
|
PRAGMA busy_timeout=1;
|
2022-11-10 23:29:29 +00:00
|
|
|
PRAGMA foreign_keys=TRUE;
|
|
|
|
PRAGMA case_sensitive_like=TRUE;
|
|
|
|
"};
|
|
|
|
|
2022-11-14 21:18:44 +00:00
|
|
|
/// Open or create a database at the given directory path.
|
2022-11-17 00:35:56 +00:00
|
|
|
pub fn open_file_db<M: Migrator>() -> ThreadSafeConnection<M> {
|
2022-11-14 21:18:44 +00:00
|
|
|
// Use 0 for now. Will implement incrementing and clearing of old db files soon TM
|
2022-11-17 00:35:56 +00:00
|
|
|
let current_db_dir = (*DB_DIR).join(Path::new(&format!("0-{}", *RELEASE_CHANNEL_NAME)));
|
|
|
|
|
|
|
|
// if *RELEASE_CHANNEL == ReleaseChannel::Dev {
|
|
|
|
// remove_dir_all(¤t_db_dir).ok();
|
|
|
|
// }
|
|
|
|
|
|
|
|
create_dir_all(¤t_db_dir).expect("Should be able to create the database directory");
|
2022-11-14 21:18:44 +00:00
|
|
|
let db_path = current_db_dir.join(Path::new("db.sqlite"));
|
|
|
|
|
|
|
|
ThreadSafeConnection::new(db_path.to_string_lossy().as_ref(), true)
|
|
|
|
.with_initialize_query(INITIALIZE_QUERY)
|
2022-10-18 22:58:05 +00:00
|
|
|
}
|
2022-10-13 22:43:42 +00:00
|
|
|
|
2022-11-14 21:18:44 +00:00
|
|
|
pub fn open_memory_db<D: Domain>(db_name: &str) -> ThreadSafeConnection<D> {
|
|
|
|
ThreadSafeConnection::new(db_name, false).with_initialize_query(INITIALIZE_QUERY)
|
|
|
|
}
|
2022-11-04 20:22:35 +00:00
|
|
|
|
2022-11-14 21:18:44 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
pub fn write_db_to<D: Domain, P: AsRef<Path>>(
|
|
|
|
conn: &ThreadSafeConnection<D>,
|
|
|
|
dest: P,
|
|
|
|
) -> Result<()> {
|
|
|
|
let destination = Connection::open_file(dest.as_ref().to_string_lossy().as_ref());
|
|
|
|
conn.backup_main(&destination)
|
2022-11-04 20:22:35 +00:00
|
|
|
}
|
2022-11-17 00:35:56 +00:00
|
|
|
|
|
|
|
/// Implements a basic DB wrapper for a given domain
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! connection {
|
|
|
|
($id:ident: $t:ident<$d:ty>) => {
|
|
|
|
pub struct $t(::sqlez::thread_safe_connection::ThreadSafeConnection<$d>);
|
|
|
|
|
|
|
|
impl ::std::ops::Deref for $t {
|
|
|
|
type Target = ::sqlez::thread_safe_connection::ThreadSafeConnection<$d>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
pub static ref $id: $t = $t(::db::open_file_db());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|