Skip to content

Commit

Permalink
refactor: do migrate only for new store
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Oct 19, 2024
1 parent 9997ce7 commit 39650cb
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 49 deletions.
4 changes: 2 additions & 2 deletions examples/bdk_sqlx_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async fn main() -> anyhow::Result<()> {
&secp,
)?;
let mut store =
bdk_sqlx::Store::<Postgres>::new_with_url(url.clone(), wallet_name).await?;
bdk_sqlx::Store::<Postgres>::new_with_url(url.clone(), wallet_name, true).await?;

let mut wallet = match Wallet::load().load_wallet_async(&mut store).await? {
Some(wallet) => wallet,
Expand Down Expand Up @@ -85,7 +85,7 @@ async fn main() -> anyhow::Result<()> {
bdk_wallet::wallet_name_from_descriptor(VAULT_DESC, Some(CHANGE_DESC), NETWORK, &secp)?;

let mut store =
bdk_sqlx::Store::<Postgres>::new_with_url(url.clone(), wallet_name).await?;
bdk_sqlx::Store::<Postgres>::new_with_url(url.clone(), wallet_name, true).await?;

let mut wallet = match Wallet::load().load_wallet_async(&mut store).await? {
Some(wallet) => wallet,
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ pub enum BdkSqlxError {
pub struct Store<DB: Database> {
pub(crate) pool: Pool<DB>,
wallet_name: String,
migration: bool,
}

type FutureResult<'a, T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'a>>;
32 changes: 11 additions & 21 deletions src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl AsyncWalletPersister for Store<Postgres> {
Self: 'a,
{
info!("initialize store");
Box::pin(store.migrate_and_read())
Box::pin(store.read())
}

#[tracing::instrument]
Expand All @@ -59,44 +59,34 @@ impl Store<Postgres> {
pub async fn new(
pool: Pool<Postgres>,
wallet_name: String,
migration: bool,
migrate: bool,
) -> Result<Self, BdkSqlxError> {
info!("new store");
Ok(Self {
pool,
wallet_name,
migration,
})
info!("new postgres store");
if migrate {
info!("migrate");
migrate!("./migrations/postgres").run(&pool).await?;
}
Ok(Self { pool, wallet_name })
}

/// Construct a new [`Store`] without an existing pg connection.
#[tracing::instrument]
pub async fn new_with_url(
url: String,
wallet_name: String,
migrate: bool,
) -> Result<Store<Postgres>, BdkSqlxError> {
info!("new store with url");
let pool = PgPool::connect(url.as_str()).await?;
Ok(Self {
pool,
wallet_name,
migration: true,
})
Self::new(pool, wallet_name, migrate).await
}
}

impl Store<Postgres> {
#[tracing::instrument]
pub(crate) async fn migrate_and_read(&self) -> Result<ChangeSet, BdkSqlxError> {
info!("migrate and read");
if self.migration {
migrate!("./migrations/postgres").run(&self.pool).await?;
}

pub(crate) async fn read(&self) -> Result<ChangeSet, BdkSqlxError> {
let mut tx = self.pool.begin().await?;

let mut changeset = ChangeSet::default();

let sql =
"SELECT n.name as network,
k_int.descriptor as internal_descriptor, k_int.last_revealed as internal_last_revealed,
Expand Down
35 changes: 13 additions & 22 deletions src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl AsyncWalletPersister for Store<Sqlite> {
Self: 'a,
{
info!("initialize store");
Box::pin(store.migrate_and_read())
Box::pin(store.read())
}

#[tracing::instrument]
Expand All @@ -52,22 +52,22 @@ impl AsyncWalletPersister for Store<Sqlite> {
}

impl Store<Sqlite> {
/// Construct a new [`Store`] with an existing sqlite connection.
/// Construct a new [`Store`] with an existing sqlite connection pool.
#[tracing::instrument]
pub async fn new(
pool: Pool<Sqlite>,
wallet_name: String,
migration: bool,
migrate: bool,
) -> Result<Self, BdkSqlxError> {
info!("new store");
Ok(Self {
pool,
wallet_name,
migration,
})
info!("new sqlite store");
if migrate {
info!("migrate");
migrate!("./migrations/postgres").run(&pool).await?;
}
Ok(Self { pool, wallet_name })
}

/// Construct a new [`Store`] without an existing sqlite connection.
/// Construct a new [`Store`] without an existing sqlite connection pool.
///
/// The SQLite DB URL should look like "sqlite://bdk_wallet.sqlite?mode=rwc".
///
Expand All @@ -77,6 +77,7 @@ impl Store<Sqlite> {
pub async fn new_with_url(
url: Option<String>,
wallet_name: String,
migrate: bool,
) -> Result<Store<Sqlite>, BdkSqlxError> {
info!("new store with url");
let pool = if let Some(url) = url {
Expand All @@ -91,26 +92,16 @@ impl Store<Sqlite> {
.connect(":memory:")
.await?
};
Ok(Self {
pool,
wallet_name,
migration: true,
})
Self::new(pool, wallet_name, migrate).await
}
}

impl Store<Sqlite> {
#[tracing::instrument]
pub(crate) async fn migrate_and_read(&self) -> Result<ChangeSet, BdkSqlxError> {
pub(crate) async fn read(&self) -> Result<ChangeSet, BdkSqlxError> {
info!("migrate and read");
if self.migration {
migrate!("./migrations/sqlite").run(&self.pool).await?;
}

let mut tx = self.pool.begin().await?;

let mut changeset = ChangeSet::default();

let sql =
"SELECT n.name as network,
k_int.descriptor as internal_descriptor, k_int.last_revealed as internal_last_revealed,
Expand Down
7 changes: 4 additions & 3 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ impl AsyncWalletPersister for TestStore {
{
info!("initialize test store");
match store {
TestStore::Postgres(store) => Box::pin(store.migrate_and_read()),
TestStore::Sqlite(store) => Box::pin(store.migrate_and_read()),
TestStore::Postgres(store) => Box::pin(store.read()),
TestStore::Sqlite(store) => Box::pin(store.read()),
}
}

Expand Down Expand Up @@ -151,7 +151,8 @@ async fn create_test_stores(wallet_name: String) -> anyhow::Result<Vec<TestStore
let pool = Pool::<Postgres>::connect(&url.clone()).await?;
// Drop all before creating new store for testing
pool.drop_all().await?;
let postgres_store = Store::<Postgres>::new_with_url(url.clone(), wallet_name.clone()).await?;
let postgres_store =
Store::<Postgres>::new_with_url(url.clone(), wallet_name.clone(), true).await?;
stores.push(TestStore::Postgres(postgres_store));

// Setup sqlite in-memory database
Expand Down

0 comments on commit 39650cb

Please sign in to comment.