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