Skip to content

Commit 1bb34e1

Browse files
committed
refactor: embed migrations, move bin to examples crate, cleanup dependencies
1 parent 01f38c0 commit 1bb34e1

File tree

9 files changed

+48
-40
lines changed

9 files changed

+48
-40
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/target
1+
target
22
Cargo.lock
33
*.sqlite
44
.env

Cargo.toml

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,20 @@
11
[package]
2-
name = "bdk-sqlx"
2+
name = "bdk_sqlx"
33
version = "0.1.0"
44
edition = "2021"
55

6-
[lib]
7-
name = "bdk_sqlx"
8-
path = "src/lib.rs"
9-
10-
[[bin]]
11-
name = "async_wallet_bdk_sqlx"
12-
path = "src/main.rs"
13-
146
[dependencies]
7+
bdk_wallet = { version = "1.0.0-beta.5" }
8+
serde = { version = "1.0.208", features = ["derive"] }
9+
serde_json = "1.0.125"
1510
sqlx = { version = "0.8.1", default-features = false, features = ["runtime-tokio", "tls-rustls-ring","derive", "postgres", "sqlite", "json", "chrono", "uuid", "sqlx-macros", "migrate"] }
1611
thiserror = "1"
1712
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] }
18-
serde = { version = "1.0.208", features = ["derive"] }
19-
serde_json = "1.0.125"
20-
better-panic = "0.3.0"
21-
rustls = "0.23.12"
2213
tracing = "0.1.40"
2314
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde_json", "json"] }
24-
anyhow = "1.0.86"
25-
rand = "0.8.5"
26-
uuid = "1.10.0"
27-
assert_matches = "1.5.0"
28-
pg-embed = { version = "0.7.1", features = ["default"] }
2915

30-
31-
bdk_wallet = { git = "https://github.com/bitcoindevkit/bdk", tag = "v1.0.0-beta.2", features = ["std"], default-features = false }
32-
bdk_chain = { git = "https://github.com/bitcoindevkit/bdk", tag = "v1.0.0-beta.2" }
33-
bdk_electrum = { git = "https://github.com/bitcoindevkit/bdk", tag = "v1.0.0-beta.2" }
34-
bdk_testenv = { git = "https://github.com/bitcoindevkit/bdk", tag = "v1.0.0-beta.2" }
16+
[dev-dependencies]
17+
assert_matches = "1.5.0"
18+
bdk_electrum = { version = "0.19.0"}
19+
bdk_testenv = { version = "0.10.0" }
20+
better-panic = "0.3.0"

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# bdk-sqlx
22

3+
## Status
4+
5+
This crate is still **EXPERIMENTAL** do not use with mainnet wallets.
36

47
## Testing
58

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "bdk_sqlx_postgres"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
anyhow = "1.0.86"
8+
bdk_electrum = { version = "0.19.0"}
9+
bdk_sqlx = { path = "../.." }
10+
bdk_wallet = "1.0.0-beta.5"
11+
better-panic = "0.3.0"
12+
rustls = "0.23.12"
13+
tokio = "1.40.0"
14+
tracing = "0.1.40"
15+
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde_json", "json"] }

src/main.rs renamed to examples/bdk_sqlx_postgres/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ use std::collections::HashSet;
33
use std::io::Write;
44

55
use bdk_electrum::{electrum_client, BdkElectrumClient};
6+
use bdk_sqlx::sqlx::Postgres;
67
use bdk_sqlx::Store;
78
use bdk_wallet::bitcoin::secp256k1::Secp256k1;
89
use bdk_wallet::bitcoin::Network;
910
use bdk_wallet::{KeychainKind, PersistedWallet, Wallet};
1011
use better_panic::Settings;
1112
use rustls::crypto::ring::default_provider;
12-
use sqlx::Postgres;
1313
use tracing_subscriber::layer::SubscriberExt;
1414
use tracing_subscriber::util::SubscriberInitExt;
1515
use tracing_subscriber::EnvFilter;
@@ -35,6 +35,7 @@ async fn main() -> anyhow::Result<()> {
3535
default_provider()
3636
.install_default()
3737
.expect("Failed to install rustls default crypto provider");
38+
3839
Settings::debug()
3940
.most_recent_first(false)
4041
.lineno_suffix(true)
@@ -120,7 +121,7 @@ async fn main() -> anyhow::Result<()> {
120121
fn electrum(wallet: &mut PersistedWallet<Store<Postgres>>) -> anyhow::Result<()> {
121122
let client = BdkElectrumClient::new(electrum_client::Client::new(ELECTRUM_URL)?);
122123

123-
// Populate the electrum client's transaction cache so it doesn't redownload transaction we
124+
// Populate the electrum client's transaction cache so it doesn't re-download transaction we
124125
// already have.
125126
client.populate_tx_cache(wallet.tx_graph().full_txs().map(|tx_node| tx_node.tx));
126127

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use bdk_chain::miniscript;
1515
use bdk_wallet::bitcoin;
1616
use bdk_wallet::chain as bdk_chain;
1717

18+
pub use sqlx;
1819
use sqlx::Database;
1920
use sqlx::Pool;
2021

@@ -33,6 +34,9 @@ pub enum BdkSqlxError {
3334
/// sqlx error
3435
#[error("sqlx error: {0}")]
3536
Sqlx(#[from] sqlx::Error),
37+
/// migrate error
38+
#[error("migrate error: {0}")]
39+
Migrate(#[from] sqlx::migrate::MigrateError),
3640
}
3741

3842
/// Manages a pool of database connections.

src/postgres.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use bdk_wallet::descriptor::{Descriptor, DescriptorPublicKey, ExtendedDescriptor
2020
use bdk_wallet::KeychainKind::{External, Internal};
2121
use bdk_wallet::{AsyncWalletPersister, ChangeSet, KeychainKind};
2222
use serde_json::json;
23-
use sqlx::migrate::Migrator;
2423
use sqlx::postgres::PgRow;
24+
use sqlx::sqlx_macros::migrate;
2525
use sqlx::{
2626
postgres::{PgPool, Postgres},
2727
FromRow, Pool, Row, Transaction,
@@ -96,10 +96,7 @@ impl Store<Postgres> {
9696
pub(crate) async fn migrate_and_read(&self) -> Result<ChangeSet, BdkSqlxError> {
9797
info!("migrate and read");
9898
if self.migration {
99-
let migrator = Migrator::new(std::path::Path::new("./migrations/postgres/"))
100-
.await
101-
.unwrap();
102-
migrator.run(&self.pool).await.unwrap();
99+
migrate!("migrations/postgres").run(&self.pool).await?;
103100
}
104101

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

src/sqlite.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ use bdk_wallet::KeychainKind::{External, Internal};
2121
use bdk_wallet::{AsyncWalletPersister, ChangeSet, KeychainKind};
2222
use serde_json::json;
2323
use sqlx::sqlite::SqliteRow;
24-
use sqlx::{
25-
migrate::Migrator,
26-
sqlite::{SqlitePool, SqlitePoolOptions},
27-
};
24+
use sqlx::sqlite::{SqlitePool, SqlitePoolOptions};
25+
use sqlx::sqlx_macros::migrate;
2826
use sqlx::{sqlite::Sqlite, FromRow, Pool, Row, Transaction};
2927
use tracing::info;
3028

@@ -112,10 +110,7 @@ impl Store<Sqlite> {
112110
pub(crate) async fn migrate_and_read(&self) -> Result<ChangeSet, BdkSqlxError> {
113111
info!("migrate and read");
114112
if self.migration {
115-
let migrator = Migrator::new(std::path::Path::new("./migrations/sqlite/"))
116-
.await
117-
.unwrap();
118-
migrator.run(&self.pool).await.unwrap();
113+
migrate!("./migrations/sqlite/").run(&self.pool).await?;
119114
}
120115

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

src/test.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ use bdk_chain::BlockId;
1111
use bdk_electrum::electrum_client::Client;
1212
use bdk_electrum::{electrum_client, BdkElectrumClient};
1313
use bdk_testenv::bitcoincore_rpc::RpcApi;
14-
use bdk_testenv::TestEnv;
14+
use bdk_testenv::{anyhow, TestEnv};
15+
use bdk_wallet::bitcoin::constants::ChainHash;
16+
use bdk_wallet::bitcoin::hashes::Hash;
17+
use bdk_wallet::bitcoin::secp256k1::Secp256k1;
18+
use bdk_wallet::bitcoin::Network::Signet;
19+
use bdk_wallet::bitcoin::{BlockHash, Network};
20+
use bdk_wallet::chain::BlockId;
21+
use bdk_wallet::miniscript::{Descriptor, DescriptorPublicKey};
1522
use bdk_wallet::{
1623
descriptor,
1724
descriptor::ExtendedDescriptor,

0 commit comments

Comments
 (0)