1pub mod kvp;
2
3// Re-export indoc and sqlez so clients only need to include us
4pub use indoc::indoc;
5pub use lazy_static;
6pub use sqlez;
7
8use std::fs::{create_dir_all, remove_dir_all};
9use std::path::Path;
10
11#[cfg(any(test, feature = "test-support"))]
12use anyhow::Result;
13#[cfg(any(test, feature = "test-support"))]
14use sqlez::connection::Connection;
15use sqlez::domain::{Domain, Migrator};
16use sqlez::thread_safe_connection::ThreadSafeConnection;
17use util::channel::{ReleaseChannel, RELEASE_CHANNEL, RELEASE_CHANNEL_NAME};
18use util::paths::DB_DIR;
19
20const INITIALIZE_QUERY: &'static str = indoc! {"
21 PRAGMA journal_mode=WAL;
22 PRAGMA synchronous=NORMAL;
23 PRAGMA busy_timeout=1;
24 PRAGMA foreign_keys=TRUE;
25 PRAGMA case_sensitive_like=TRUE;
26"};
27
28/// Open or create a database at the given directory path.
29pub fn open_file_db<M: Migrator>() -> ThreadSafeConnection<M> {
30 // Use 0 for now. Will implement incrementing and clearing of old db files soon TM
31 let current_db_dir = (*DB_DIR).join(Path::new(&format!("0-{}", *RELEASE_CHANNEL_NAME)));
32
33 if *RELEASE_CHANNEL == ReleaseChannel::Dev && std::env::var("WIPE_DB").is_ok() {
34 remove_dir_all(¤t_db_dir).ok();
35 }
36
37 create_dir_all(¤t_db_dir).expect("Should be able to create the database directory");
38 let db_path = current_db_dir.join(Path::new("db.sqlite"));
39
40 ThreadSafeConnection::new(Some(db_path.to_string_lossy().as_ref()), true)
41 .with_initialize_query(INITIALIZE_QUERY)
42}
43
44pub fn open_memory_db<M: Migrator>(db_name: Option<&str>) -> ThreadSafeConnection<M> {
45 ThreadSafeConnection::new(db_name, false).with_initialize_query(INITIALIZE_QUERY)
46}
47
48#[cfg(any(test, feature = "test-support"))]
49pub fn write_db_to<D: Domain, P: AsRef<Path>>(
50 conn: &ThreadSafeConnection<D>,
51 dest: P,
52) -> Result<()> {
53 let destination = Connection::open_file(dest.as_ref().to_string_lossy().as_ref());
54 conn.backup_main(&destination)
55}
56
57/// Implements a basic DB wrapper for a given domain
58#[macro_export]
59macro_rules! connection {
60 ($id:ident: $t:ident<$d:ty>) => {
61 pub struct $t(::db::sqlez::thread_safe_connection::ThreadSafeConnection<$d>);
62
63 impl ::std::ops::Deref for $t {
64 type Target = ::db::sqlez::thread_safe_connection::ThreadSafeConnection<$d>;
65
66 fn deref(&self) -> &Self::Target {
67 &self.0
68 }
69 }
70
71 ::db::lazy_static::lazy_static! {
72 pub static ref $id: $t = $t(if cfg!(any(test, feature = "test-support")) {
73 ::db::open_memory_db(None)
74 } else {
75 ::db::open_file_db()
76 });
77 }
78 };
79}