Skip to content

Commit 4131982

Browse files
fixup/postgres/sqlite: removed migration
1 parent 8168dba commit 4131982

7 files changed

Lines changed: 4 additions & 131 deletions

File tree

.github/workflows/rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ jobs:
183183
- name: Install cargo-audit
184184
run: cargo install cargo-audit
185185
- name: Run cargo-audit
186-
run: cargo audit
186+
run: cargo audit --ignore RUSTSEC-2023-0071
187187

188188
unused-dependencies:
189189
name: Check Unused Dependencies

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2021"
77
bdk_wallet = { version = "1.2.0", features = ["test-utils"] }
88
serde = { version = "1.0.208", features = ["derive"] }
99
serde_json = "1.0.125"
10-
sqlx = { version = "0.8.5", default-features = false, features = ["runtime-tokio", "tls-rustls-ring", "derive", "postgres", "sqlite", "json", "chrono", "uuid", "migrate"] }
10+
sqlx = { version = "0.8.5", default-features = false, features = ["runtime-tokio", "tls-rustls-ring", "derive", "postgres", "sqlite", "json", "chrono", "uuid"] }
1111
thiserror = "2"
1212
tokio = { version = "1.44.0", features = ["macros", "rt-multi-thread"] }
1313
tracing = "0.1.40"

examples/bdk_sqlx_postgres.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ async fn main() -> anyhow::Result<()> {
5959

6060
let mut store = PgStoreBuilder::new(wallet_name.clone())
6161
.network(NETWORK)
62-
.migrate(true)
6362
.build_with_url(&url)
6463
.await?;
6564

@@ -90,7 +89,6 @@ async fn main() -> anyhow::Result<()> {
9089

9190
let mut store = PgStoreBuilder::new(wallet_name.clone())
9291
.network(NETWORK)
93-
.migrate(true)
9492
.build_with_url(&url)
9593
.await?;
9694

src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ pub enum BdkSqlxError {
3333
/// sqlx error
3434
#[error("sqlx error: {0}")]
3535
Sqlx(#[from] sqlx::Error),
36-
/// migrate error
37-
#[error("migrate error: {0}")]
38-
Migrate(#[from] sqlx::migrate::MigrateError),
3936
/// Network confusion
4037
#[error("Invalid Network expected {expected}, got {got}")]
4138
InvalidNetwork {
@@ -88,7 +85,6 @@ pub struct Store<DB: Database> {
8885
pub struct PgStoreBuilder {
8986
wallet_name: String,
9087
pool: Option<PgPool>,
91-
migrate: bool,
9288
network: Option<Network>,
9389
}
9490

src/postgres.rs

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ impl PgStoreBuilder {
120120
Self {
121121
wallet_name,
122122
pool: None,
123-
migrate: false,
124123
network: None,
125124
}
126125
}
@@ -134,15 +133,6 @@ impl PgStoreBuilder {
134133
self
135134
}
136135

137-
/// Sets whether database migrations should be run during [`Store`] initialization.
138-
///
139-
/// When set to true, the necessary database schema and tables will be created
140-
/// if they don't already exist.
141-
pub fn migrate(mut self, migrate: bool) -> Self {
142-
self.migrate = migrate;
143-
self
144-
}
145-
146136
/// Sets the Bitcoin network for the [`Store`].
147137
///
148138
/// The network is required to build a valid [`Store`]. If not provided,
@@ -174,9 +164,6 @@ impl PgStoreBuilder {
174164
pool,
175165
wallet_name: self.wallet_name,
176166
};
177-
if self.migrate {
178-
store.migrate().await?;
179-
}
180167

181168
initialize_network(network)?;
182169

@@ -203,106 +190,6 @@ impl PgStoreBuilder {
203190
}
204191
}
205192

206-
impl Store<Postgres> {
207-
/// Runs Migrations for a [`Store`] without an existing pg connection.
208-
#[tracing::instrument(skip_all)]
209-
pub async fn migrate(&self) -> Result<()> {
210-
trace!("migrating bdk sqlx");
211-
212-
let mut tx = self.pool.begin().await?;
213-
214-
// Create the schema first
215-
let create_schema_query = r#"CREATE SCHEMA IF NOT EXISTS "bdk_wallet""#;
216-
sqlx::query(create_schema_query)
217-
.execute(&mut *tx)
218-
.await
219-
.map_err(|e| BdkSqlxError::QueryError {
220-
table: "create schema bdk_wallet".to_string(),
221-
source: e,
222-
})?;
223-
224-
// Create the tables one by one
225-
let queries = [
226-
r#"CREATE TABLE IF NOT EXISTS "bdk_wallet"."version" (
227-
version INTEGER PRIMARY KEY
228-
)"#,
229-
r#"CREATE TABLE IF NOT EXISTS "bdk_wallet"."network" (
230-
wallet_name TEXT PRIMARY KEY,
231-
name TEXT NOT NULL
232-
)"#,
233-
r#"CREATE TABLE IF NOT EXISTS "bdk_wallet"."keychain" (
234-
wallet_name TEXT NOT NULL,
235-
keychainkind TEXT NOT NULL,
236-
descriptor TEXT NOT NULL,
237-
descriptor_id BYTEA NOT NULL,
238-
last_revealed INTEGER DEFAULT 0,
239-
PRIMARY KEY (wallet_name, keychainkind)
240-
)"#,
241-
r#"CREATE TABLE IF NOT EXISTS "bdk_wallet"."block" (
242-
wallet_name TEXT NOT NULL,
243-
hash TEXT NOT NULL,
244-
height INTEGER NOT NULL,
245-
PRIMARY KEY (wallet_name, hash)
246-
)"#,
247-
r#"CREATE INDEX IF NOT EXISTS idx_block_height ON "bdk_wallet"."block" (height)"#,
248-
r#"CREATE TABLE IF NOT EXISTS "bdk_wallet"."tx" (
249-
wallet_name TEXT NOT NULL,
250-
txid TEXT NOT NULL,
251-
whole_tx BYTEA,
252-
last_seen BIGINT,
253-
PRIMARY KEY (wallet_name, txid)
254-
)"#,
255-
r#"CREATE TABLE IF NOT EXISTS "bdk_wallet"."txout" (
256-
wallet_name TEXT NOT NULL,
257-
txid TEXT NOT NULL,
258-
vout INTEGER NOT NULL,
259-
value BIGINT NOT NULL,
260-
script BYTEA NOT NULL,
261-
PRIMARY KEY (wallet_name, txid, vout)
262-
)"#,
263-
r#"CREATE TABLE IF NOT EXISTS "bdk_wallet"."anchor_tx" (
264-
wallet_name TEXT NOT NULL,
265-
block_hash TEXT NOT NULL,
266-
anchor JSONB NOT NULL,
267-
txid TEXT NOT NULL,
268-
PRIMARY KEY (wallet_name, block_hash, txid),
269-
FOREIGN KEY (wallet_name, block_hash) REFERENCES "bdk_wallet"."block"(wallet_name, hash),
270-
FOREIGN KEY (wallet_name, txid) REFERENCES "bdk_wallet"."tx"(wallet_name, txid)
271-
)"#,
272-
r#"CREATE INDEX IF NOT EXISTS idx_anchor_tx_txid ON "bdk_wallet"."anchor_tx" (txid)"#,
273-
];
274-
275-
// Execute each query separately
276-
for query in &queries {
277-
sqlx::query(query)
278-
.execute(&mut *tx)
279-
.await
280-
.map_err(|e| BdkSqlxError::QueryError {
281-
table: query.to_string(),
282-
source: e,
283-
})?;
284-
}
285-
286-
// At the end of migration, insert the current version
287-
// After all tables are created but before tx.commit()
288-
sqlx::query(
289-
r#"INSERT INTO "bdk_wallet"."version" (version)
290-
VALUES ($1)
291-
ON CONFLICT (version) DO NOTHING"#,
292-
)
293-
.bind(1) // Current schema version
294-
.execute(&mut *tx)
295-
.await
296-
.map_err(|e| BdkSqlxError::QueryError {
297-
table: "insert version".to_string(),
298-
source: e,
299-
})?;
300-
301-
tx.commit().await?;
302-
303-
Ok(())
304-
}
305-
}
306193

307194
impl Store<Postgres> {
308195
#[tracing::instrument(skip_all)]

src/sqlite.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use bdk_wallet::{AsyncWalletPersister, ChangeSet, KeychainKind};
2222
use serde_json::json;
2323
use sqlx::sqlite::SqliteRow;
2424
use sqlx::sqlite::{SqlitePool, SqlitePoolOptions};
25-
use sqlx::sqlx_macros::migrate;
2625
use sqlx::{sqlite::Sqlite, FromRow, Pool, Row, Transaction};
2726
use tracing::info;
2827

@@ -57,13 +56,8 @@ impl Store<Sqlite> {
5756
pub async fn new(
5857
pool: Pool<Sqlite>,
5958
wallet_name: String,
60-
migrate: bool,
6159
) -> Result<Self, BdkSqlxError> {
6260
info!("new sqlite store");
63-
if migrate {
64-
info!("migrate");
65-
migrate!("./migrations/sqlite").run(&pool).await?;
66-
}
6761
Ok(Self { pool, wallet_name })
6862
}
6963

@@ -77,7 +71,6 @@ impl Store<Sqlite> {
7771
pub async fn new_with_url(
7872
url: Option<String>,
7973
wallet_name: String,
80-
migrate: bool,
8174
) -> Result<Store<Sqlite>, BdkSqlxError> {
8275
info!("new store with url");
8376
let pool = if let Some(url) = url {
@@ -92,7 +85,7 @@ impl Store<Sqlite> {
9285
.connect(":memory:")
9386
.await?
9487
};
95-
Self::new(pool, wallet_name, migrate).await
88+
Self::new(pool, wallet_name).await
9689
}
9790
}
9891

src/test.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,13 @@ async fn create_test_stores(wallet_name: String) -> anyhow::Result<Vec<TestStore
177177
pool.drop_all().await?;
178178
let postgres_store = PgStoreBuilder::new(wallet_name.clone())
179179
.network(NETWORK)
180-
.migrate(true)
181180
.build_with_url(&url)
182181
.await?;
183182
stores.push(TestStore::Postgres(postgres_store));
184183

185184
// Setup sqlite in-memory database
186185
let pool = SqlitePool::connect(":memory:").await?;
187-
let sqlite_store = Store::<Sqlite>::new(pool.clone(), wallet_name.clone(), true).await?;
186+
let sqlite_store = Store::<Sqlite>::new(pool.clone(), wallet_name.clone()).await?;
188187
stores.push(TestStore::Sqlite(sqlite_store));
189188

190189
Ok(stores)

0 commit comments

Comments
 (0)