1#[macro_use]
3extern crate diesel;
4#[macro_use]
5extern crate serde;
6#[macro_use]
7extern crate diesel_migrations;
8#[macro_use]
9extern crate derivative;
10
11use std::sync::{Arc, Mutex, MutexGuard};
12
13use diesel::prelude::*;
14mod checkpoint;
18mod collection;
19mod core;
20
21pub(crate) mod schema;
22use gosh_core::*;
26use gut::prelude::*;
27
28embed_migrations!();
29
30#[derive(Clone, Derivative)]
31#[derivative(Debug)]
32pub struct DbConnection {
33 database_url: String,
34 #[derivative(Debug = "ignore")]
35 connection: Arc<Mutex<SqliteConnection>>,
36}
37
38impl DbConnection {
39 pub fn establish() -> Result<DbConnection> {
42 dotenvy::dotenv().ok();
44
45 let database_url =
46 std::env::var("GOSH_DATABASE_URL").with_context(|| format!("GOSH_DATABASE_URL var not set"))?;
47 debug!("Database: {}", database_url);
48
49 Self::connect(&database_url)
50 }
51
52 pub fn connect(database_url: &str) -> Result<DbConnection> {
54 let conn = SqliteConnection::establish(database_url)?;
56
57 conn.execute("PRAGMA synchronous = OFF")?;
67
68 let conn = Arc::new(Mutex::new(conn));
69
70 let db = DbConnection {
71 database_url: database_url.into(),
72 connection: conn.clone(),
73 };
74
75 db.migrate()?;
76
77 Ok(db)
78 }
79
80 pub fn database_url(&self) -> &str {
82 &self.database_url
83 }
84
85 pub(crate) fn get(&self) -> MutexGuard<'_, SqliteConnection> {
86 self.connection.lock().expect("cannot lock db connection!")
87 }
88
89 fn migrate(&self) -> Result<()> {
91 let conn = self.get();
92
93 embedded_migrations::run(&*conn)?;
95
96 Ok(())
97 }
98}
99pub mod prelude {
103 pub use crate::checkpoint::Checkpoint;
104 pub use crate::collection::Collection;
105}
106
107pub use crate::checkpoint::CheckpointDb;
108