Skip to content

Commit 44a76a0

Browse files
committed
Do not create the wallet struct directly; instead, call new.
The bug comes with the SQLx-sqlite pool bug, where several connections are created by default, but the `new` function takes care of that, fixing that bug by making a single instance of the database. If constructed directly, the pool would create several connections to the database, which in most instances is fine, but with SQLite :memory: each connection is entirely independent.
1 parent d224cc5 commit 44a76a0

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

crates/cdk-sqlite/src/common.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,13 @@ pub async fn create_sqlite_pool(
3131
.connect_with(db_options)
3232
.await?;
3333

34+
if path.contains(":memory:") {
35+
// Ensure that the pool has the minimum number of connections open
36+
// This makes sure the pool initializes with exactly one connection.
37+
// This is necessary because `min_connections` does not guarantee
38+
// an immediate connection unless it is actively used or explicitly initialized.
39+
pool.acquire().await?;
40+
}
41+
3442
Ok(pool)
3543
}

crates/cdk-sqlite/src/wallet/memory.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ use super::WalletSqliteDatabase;
66

77
/// Creates a new in-memory [`WalletSqliteDatabase`] instance
88
pub async fn empty() -> Result<WalletSqliteDatabase, Error> {
9-
let db = WalletSqliteDatabase {
10-
pool: sqlx::sqlite::SqlitePool::connect(":memory:")
11-
.await
12-
.map_err(|e| Error::Database(Box::new(e)))?,
13-
};
9+
#[cfg(not(feature = "sqlcipher"))]
10+
let db = WalletSqliteDatabase::new(":memory:").await?;
11+
#[cfg(feature = "sqlcipher")]
12+
let db = WalletSqliteDatabase::new(":memory:", "memory".to_owned()).await?;
1413
db.migrate().await;
1514
Ok(db)
1615
}

0 commit comments

Comments
 (0)