Do not create the wallet struct directly; instead, call new. (#707)

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.

Also follow documentation to make sure that failed `acquire` will not end up
dropping connections by setting  test_before_acquire to false

     However, if your workload is sensitive to dropped connections such as using an in-memory
     SQLite database with a pool size of 1, you can pretty easily ensure that a cancelled
     `acquire()` call will never drop connections by tweaking your [`PoolOptions`]:

     * Set [`test_before_acquire(false)`][PoolOptions::test_before_acquire]
     * Never set [`before_acquire`][PoolOptions::before_acquire] or
       [`after_connect`][PoolOptions::after_connect].
This commit is contained in:
C
2025-04-06 02:13:14 -04:00
committed by GitHub
parent d224cc57b5
commit 43ab1fdde1
9 changed files with 43 additions and 36 deletions

View File

@@ -81,29 +81,34 @@ impl MintSqliteDatabase {
/// Create new [`MintSqliteDatabase`]
#[cfg(not(feature = "sqlcipher"))]
pub async fn new<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
Ok(Self {
let db = Self {
pool: create_sqlite_pool(path.as_ref().to_str().ok_or(Error::InvalidDbPath)?).await?,
})
};
db.migrate().await?;
Ok(db)
}
/// Create new [`MintSqliteDatabase`]
#[cfg(feature = "sqlcipher")]
pub async fn new<P: AsRef<Path>>(path: P, password: String) -> Result<Self, Error> {
Ok(Self {
let db = Self {
pool: create_sqlite_pool(
path.as_ref().to_str().ok_or(Error::InvalidDbPath)?,
password,
)
.await?,
})
};
db.migrate().await?;
Ok(db)
}
/// Migrate [`MintSqliteDatabase`]
pub async fn migrate(&self) {
async fn migrate(&self) -> Result<(), Error> {
sqlx::migrate!("./src/mint/migrations")
.run(&self.pool)
.await
.expect("Could not run migrations");
.map_err(|_| Error::CouldNotInitialize)?;
Ok(())
}
}