Skip to main content

cdk_sqlite/mint/
mod.rs

1//! SQLite Mint
2
3use cdk_sql_common::mint::SQLMintAuthDatabase;
4use cdk_sql_common::SQLMintDatabase;
5
6use crate::common::SqliteConnectionManager;
7
8pub mod memory;
9
10/// Mint SQLite implementation with rusqlite
11pub type MintSqliteDatabase = SQLMintDatabase<SqliteConnectionManager>;
12
13/// Mint Auth database with rusqlite
14pub type MintSqliteAuthDatabase = SQLMintAuthDatabase<SqliteConnectionManager>;
15
16#[cfg(test)]
17mod test {
18    use std::fs::remove_file;
19    use std::time::Duration;
20
21    use cdk_common::mint_db_test;
22    use cdk_sql_common::pool::Pool;
23    use cdk_sql_common::stmt::query;
24
25    use super::*;
26    use crate::common::Config;
27
28    async fn provide_db(_test_name: String) -> MintSqliteDatabase {
29        memory::empty().await.unwrap()
30    }
31
32    mint_db_test!(provide_db);
33
34    #[tokio::test]
35    async fn bug_opening_relative_path() {
36        let config: Config = "test.db".into();
37
38        let pool = Pool::<SqliteConnectionManager>::new(config);
39        let db = pool.get().await;
40        assert!(db.is_ok());
41        let _ = remove_file("test.db");
42    }
43
44    #[tokio::test]
45    async fn exhausted_in_memory_pool_times_out() {
46        let config: Config = ":memory:".into();
47        let pool = Pool::<SqliteConnectionManager>::new(config);
48
49        let _conn = pool.get().await.expect("valid connection");
50        let result = pool.get_timeout(Duration::from_millis(10)).await;
51
52        assert!(matches!(result, Err(cdk_sql_common::pool::Error::Timeout)));
53    }
54
55    #[tokio::test]
56    async fn open_legacy_and_migrate() {
57        let file = format!(
58            "{}/db.sqlite",
59            std::env::temp_dir().to_str().unwrap_or_default()
60        );
61
62        {
63            let _ = remove_file(&file);
64            #[cfg(not(feature = "sqlcipher"))]
65            let config: Config = file.as_str().into();
66            #[cfg(feature = "sqlcipher")]
67            let config: Config = (file.as_str(), "test").into();
68
69            let pool = Pool::<SqliteConnectionManager>::new(config);
70
71            let conn = pool.get().await.expect("valid connection");
72
73            query(include_str!("../../tests/legacy-sqlx.sql"))
74                .expect("query")
75                .execute(&*conn)
76                .await
77                .expect("create former db failed");
78        }
79
80        #[cfg(not(feature = "sqlcipher"))]
81        let conn = MintSqliteDatabase::new(file.as_str()).await;
82
83        #[cfg(feature = "sqlcipher")]
84        let conn = MintSqliteDatabase::new((file.as_str(), "test")).await;
85
86        assert!(conn.is_ok(), "Failed with {:?}", conn.unwrap_err());
87
88        let _ = remove_file(&file);
89    }
90}