@@ -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
307194impl Store < Postgres > {
308195 #[ tracing:: instrument( skip_all) ]
0 commit comments