@@ -250,6 +250,8 @@ impl Store<Postgres> {
250250 txid TEXT NOT NULL,
251251 whole_tx BYTEA,
252252 last_seen BIGINT,
253+ last_evicted BIGINT,
254+ first_seen BIGINT,
253255 PRIMARY KEY (wallet_name, txid)
254256 )"# ,
255257 r#"CREATE TABLE IF NOT EXISTS "bdk_wallet"."txout" (
@@ -283,14 +285,42 @@ impl Store<Postgres> {
283285 } ) ?;
284286 }
285287
286- // At the end of migration, insert the current version
287- // After all tables are created but before tx.commit()
288+ // Check current schema version and apply migrations if needed
289+ let current_version: Option < i32 > = sqlx:: query_scalar (
290+ r#"SELECT version FROM "bdk_wallet"."version" ORDER BY version DESC LIMIT 1"# ,
291+ )
292+ . fetch_optional ( & mut * tx)
293+ . await
294+ . map_err ( |e| BdkSqlxError :: QueryError {
295+ table : "select version" . to_string ( ) ,
296+ source : e,
297+ } ) ?;
298+
299+ match current_version {
300+ Some ( 1 ) => {
301+ // Migrate from v1 to v2: Add last_evicted and first_seen columns
302+ sqlx:: query (
303+ r#"ALTER TABLE "bdk_wallet"."tx"
304+ ADD COLUMN IF NOT EXISTS last_evicted BIGINT,
305+ ADD COLUMN IF NOT EXISTS first_seen BIGINT"# ,
306+ )
307+ . execute ( & mut * tx)
308+ . await
309+ . map_err ( |e| BdkSqlxError :: QueryError {
310+ table : "alter tx table" . to_string ( ) ,
311+ source : e,
312+ } ) ?;
313+ }
314+ _ => { } // Fresh install or already at v2
315+ }
316+
317+ // Insert or update to current version
288318 sqlx:: query (
289319 r#"INSERT INTO "bdk_wallet"."version" (version)
290320 VALUES ($1)
291321 ON CONFLICT (version) DO NOTHING"# ,
292322 )
293- . bind ( 1 ) // Current schema version
323+ . bind ( 2 ) // Current schema version is now 2
294324 . execute ( & mut * tx)
295325 . await
296326 . map_err ( |e| BdkSqlxError :: QueryError {
@@ -512,7 +542,7 @@ pub async fn tx_graph_changeset_from_postgres(
512542
513543 // Fetch transactions
514544 let rows = sqlx:: query (
515- r#"SELECT txid, whole_tx, last_seen FROM "bdk_wallet"."tx" WHERE wallet_name = $1"# ,
545+ r#"SELECT txid, whole_tx, last_seen, last_evicted, first_seen FROM "bdk_wallet"."tx" WHERE wallet_name = $1"# ,
516546 )
517547 . bind ( wallet_name)
518548 . fetch_all ( & mut * * db_tx)
@@ -527,6 +557,8 @@ pub async fn tx_graph_changeset_from_postgres(
527557 let txid = Txid :: from_str ( & txid) ?;
528558 let whole_tx: Option < Vec < u8 > > = row. get ( "whole_tx" ) ;
529559 let last_seen: Option < i64 > = row. get ( "last_seen" ) ;
560+ let _last_evicted: Option < i64 > = row. get ( "last_evicted" ) ;
561+ let _first_seen: Option < i64 > = row. get ( "first_seen" ) ;
530562
531563 if let Some ( tx_bytes) = whole_tx {
532564 if let Ok ( tx) = bitcoin:: Transaction :: consensus_decode ( & mut tx_bytes. as_slice ( ) ) {
@@ -536,6 +568,8 @@ pub async fn tx_graph_changeset_from_postgres(
536568 if let Some ( last_seen) = last_seen {
537569 changeset. last_seen . insert ( txid, last_seen as u64 ) ;
538570 }
571+ // Note: last_evicted and first_seen are fetched but not yet used in ChangeSet
572+ // These fields are stored for future use when bdk_chain supports them
539573 }
540574
541575 // Fetch txouts
@@ -602,6 +636,8 @@ pub async fn tx_graph_changeset_persist_to_postgres(
602636) -> Result < ( ) > {
603637 trace ! ( "tx graph changeset from postgres" ) ;
604638 for tx in & changeset. txs {
639+ // Note: last_evicted and first_seen columns are reserved for future use
640+ // when bdk_chain adds support for these fields in ChangeSet
605641 sqlx:: query (
606642 r#"INSERT INTO "bdk_wallet"."tx" (wallet_name, txid, whole_tx) VALUES ($1, $2, $3)
607643 ON CONFLICT (wallet_name, txid) DO UPDATE SET whole_tx = $3"# ,
0 commit comments