1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use log::{info, warn};
use crate::error::Error;
use sqlx::migrate::Migrator;
pub use sqlx::sqlite::{SqliteConnectOptions, SqlitePool};
pub use sqlx::Acquire;
use std::time::Duration;
static MIGRATOR: Migrator = sqlx::migrate!();
pub async fn get_file_pool(filename: &std::path::Path) -> Result<SqlitePool, Error> {
use sqlx::sqlite::{SqlitePoolOptions, SqliteSynchronous};
use sqlx::ConnectOptions;
let timeout = Duration::from_secs(30);
info!("Opening SQLite database file: {}", filename.display());
let mut conn = SqliteConnectOptions::new()
.filename(filename)
.synchronous(SqliteSynchronous::Normal)
.busy_timeout(timeout)
.shared_cache(false)
.pragma("temp_store", "memory");
conn.log_statements(log::LevelFilter::Trace);
let pool = SqlitePoolOptions::new()
.min_connections(5)
.connect_with(conn)
.await?;
Ok(pool)
}
pub async fn run_migrations(pool: &SqlitePool) -> Result<(), Error> {
warn!("Running migrations");
MIGRATOR.run(pool).await?;
Ok(())
}
pub struct SqlitePoolBuilder<'tempfile> {
path: Option<&'tempfile std::path::Path>,
migrate: bool,
}
impl<'tempfile> SqlitePoolBuilder<'tempfile> {
#[must_use]
pub fn new() -> Self {
Self {
path: None,
migrate: true,
}
}
#[must_use]
pub fn db_path(mut self, path: &'tempfile std::path::Path) -> Self {
self.path = Some(path);
self
}
#[must_use]
pub fn migrate(mut self, migrate: bool) -> Self {
self.migrate = migrate;
self
}
pub async fn build(self) -> Result<SqlitePool, Error> {
let db_path = self.path.expect("Must have a path");
log::debug!("Got db file {:?}", &db_path);
let pool = get_file_pool(db_path).await?;
if self.migrate {
run_migrations(&pool).await?;
}
Ok(pool)
}
}
impl<'tempfile> Default for SqlitePoolBuilder<'tempfile> {
fn default() -> Self {
Self::new()
}
}