diff --git a/zcash_client_sqlite/src/wallet/db.rs b/zcash_client_sqlite/src/wallet/db.rs index 78283a3e0..804246b4f 100644 --- a/zcash_client_sqlite/src/wallet/db.rs +++ b/zcash_client_sqlite/src/wallet/db.rs @@ -884,6 +884,7 @@ GROUP BY notes.account_id, notes.txid"; /// that controls the output. pub(super) const VIEW_TX_OUTPUTS: &str = " CREATE VIEW v_tx_outputs AS +WITH unioned AS ( -- select all outputs received by the wallet SELECT transactions.txid AS txid, ro.pool AS output_pool, @@ -902,7 +903,7 @@ LEFT JOIN sent_notes ON sent_notes.id = ro.sent_note_id -- join on the accounts table to obtain account UUIDs LEFT JOIN accounts from_account ON from_account.id = sent_notes.from_account_id LEFT JOIN accounts to_account ON to_account.id = ro.account_id -UNION +UNION ALL -- select all outputs sent from the wallet to external recipients SELECT transactions.txid AS txid, sent_notes.output_pool AS output_pool, @@ -919,8 +920,11 @@ JOIN transactions LEFT JOIN v_received_outputs ro ON ro.sent_note_id = sent_notes.id -- join on the accounts table to obtain account UUIDs LEFT JOIN accounts from_account ON from_account.id = sent_notes.from_account_id --- exclude any sent notes for which a row exists in the v_received_outputs view -WHERE ro.account_id IS NULL"; +) +-- merge duplicate rows while retaining maximum information +SELECT txid, output_pool, output_index, max(from_account_uuid), max(to_account_uuid), max(to_address), max(value), max(is_change), max(memo) +FROM unioned +GROUP BY txid, output_pool, output_index"; pub(super) fn view_sapling_shard_scan_ranges(params: &P) -> String { format!( diff --git a/zcash_client_sqlite/src/wallet/init/migrations/add_account_uuids.rs b/zcash_client_sqlite/src/wallet/init/migrations/add_account_uuids.rs index 1c7e8f5c5..3a1743232 100644 --- a/zcash_client_sqlite/src/wallet/init/migrations/add_account_uuids.rs +++ b/zcash_client_sqlite/src/wallet/init/migrations/add_account_uuids.rs @@ -273,6 +273,7 @@ impl RusqliteMigration for Migration { -- Replace accounts.id with accounts.uuid in v_tx_outputs. DROP VIEW v_tx_outputs; CREATE VIEW v_tx_outputs AS + WITH unioned AS ( -- select all outputs received by the wallet SELECT transactions.txid AS txid, ro.pool AS output_pool, @@ -291,7 +292,7 @@ impl RusqliteMigration for Migration { -- join on the accounts table to obtain account UUIDs LEFT JOIN accounts from_account ON from_account.id = sent_notes.from_account_id LEFT JOIN accounts to_account ON to_account.id = ro.account_id - UNION + UNION ALL -- select all outputs sent from the wallet to external recipients SELECT transactions.txid AS txid, sent_notes.output_pool AS output_pool, @@ -308,8 +309,11 @@ impl RusqliteMigration for Migration { LEFT JOIN v_received_outputs ro ON ro.sent_note_id = sent_notes.id -- join on the accounts table to obtain account UUIDs LEFT JOIN accounts from_account ON from_account.id = sent_notes.from_account_id - -- exclude any sent notes for which a row exists in the v_received_outputs view - WHERE ro.account_id IS NULL", + ) + -- merge duplicate rows while retaining maximum information + SELECT txid, output_pool, output_index, max(from_account_uuid), max(to_account_uuid), max(to_address), max(value), max(is_change), max(memo) + FROM unioned + GROUP BY txid, output_pool, output_index", )?; Ok(())