p2panda_store/sqlite/
test_utils.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3use crate::sqlite::store::{
4    Pool, connection_pool, create_database, drop_database, run_pending_migrations,
5};
6
7pub fn db_test_url() -> String {
8    // Give each database a unique name.
9    let db_name = format!("dbmem{}", rand::random::<u32>());
10
11    // SQLite database stored in memory.
12    let url = format!("sqlite://{db_name}?mode=memory&cache=private");
13
14    url
15}
16
17pub async fn initialize_sqlite_db() -> Pool {
18    let url = db_test_url();
19
20    drop_database(&url).await.unwrap();
21    create_database(&url).await.unwrap();
22
23    let pool = connection_pool(&url, 1).await.unwrap();
24
25    if run_pending_migrations(&pool).await.is_err() {
26        pool.close().await;
27        panic!("Database migration failed");
28    }
29
30    pool
31}