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
// Author: D.S. Ljungmark <spider@skuggor.se>, Modio AB
// SPDX-License-Identifier: AGPL-3.0-or-later
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;

// Embed our database migrations
static MIGRATOR: Migrator = sqlx::migrate!();

async fn get_file_pool(filename: &std::path::Path) -> Result<SqlitePool, Error> {
    use sqlx::sqlite::{SqlitePoolOptions, SqliteSynchronous};
    use sqlx::ConnectOptions;

    // Maybe we need to use JournalMode WAL to avoid locked tables,
    // but we would prefer not to on the firmware.
    //    use sqlx::sqlite::SqliteJournalMode;
    //    and conn.journal_mode(SqliteJournalMode::WAL)
    let timeout = Duration::from_secs(30);
    info!("Opening SQLite database file: {}", filename.display());
    let conn = SqliteConnectOptions::new()
        .filename(filename)
        .synchronous(SqliteSynchronous::Normal)
        .busy_timeout(timeout)
        .shared_cache(false)
        .pragma("temp_store", "memory")
        .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> {
    MIGRATOR.run(pool).await?;
    Ok(())
}

/// Helper around building a pool
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,
        }
    }

    /// Set the `db_path`
    /// It is expected that it is a reference to a path that should exist on disk,
    /// and be visible by it's filename.
    ///
    /// The caller is expected to ensure that the file exists.
    #[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");
        let pool = get_file_pool(db_path).await?;

        if self.migrate {
            warn!("Running migrations on {:?}", &db_path);
            run_migrations(&pool).await?;
        }
        Ok(pool)
    }
}

impl<'tempfile> Default for SqlitePoolBuilder<'tempfile> {
    fn default() -> Self {
        Self::new()
    }
}