doc: Add Diagnostic queries section#564
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new “Diagnostic queries” section to the vX.Y.Z migration guide so self-hosted users can validate wallet traceability data integrity after running the backfill.
Changes:
- Introduces an accordion-based set of SQL diagnostics for balance drift, over/under-consumption, and negative values.
- Documents when and why to run these checks after migration.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| SUM(CASE WHEN wt.transaction_type = 0 THEN ROUND(wt.amount * 100) ELSE 0 END) AS inbound_cents, | ||
| SUM(CASE WHEN wt.transaction_type = 1 THEN ROUND(wt.amount * 100) ELSE 0 END) AS outbound_cents, |
There was a problem hiding this comment.
The queries compute cents using ROUND(wt.amount * 100), but earlier in this guide the validation messages reference amount_cents on wallet_transactions (including cases where it can be non-integer). If the column is actually amount_cents (as implied), these queries will fail on self-hosted DBs. Consider switching to wt.amount_cents (optionally rounding/casting if historical data stored decimals) for consistency with the rest of the guide.
| SUM(CASE WHEN wt.transaction_type = 0 THEN ROUND(wt.amount * 100) ELSE 0 END) AS inbound_cents, | |
| SUM(CASE WHEN wt.transaction_type = 1 THEN ROUND(wt.amount * 100) ELSE 0 END) AS outbound_cents, | |
| SUM(CASE WHEN wt.transaction_type = 0 THEN wt.amount_cents ELSE 0 END) AS inbound_cents, | |
| SUM(CASE WHEN wt.transaction_type = 1 THEN wt.amount_cents ELSE 0 END) AS outbound_cents, |
| wt.wallet_id, | ||
| SUM(wtc.consumed_amount_cents) AS total_consumed_cents | ||
| FROM wallet_transaction_consumptions wtc | ||
| JOIN wallet_transactions wt ON wt.id = wtc.inbound_wallet_transaction_id |
There was a problem hiding this comment.
In the overconsumption query, inbound_totals filters wallet_transactions to status = 1, but consumption_by_inbound does not filter the joined inbound transactions by status (or type). This makes the comparison asymmetric and can produce false positives/negatives if there are consumptions linked to non-settled (or unexpected type) inbound transactions. Apply the same status/type constraints in consumption_by_inbound, or clarify why they intentionally differ.
| JOIN wallet_transactions wt ON wt.id = wtc.inbound_wallet_transaction_id | |
| JOIN wallet_transactions wt ON wt.id = wtc.inbound_wallet_transaction_id | |
| WHERE wt.transaction_type = 0 AND wt.status = 1 |
| wt.wallet_id, | ||
| SUM(wtc.consumed_amount_cents) AS total_consumed_cents | ||
| FROM wallet_transaction_consumptions wtc | ||
| JOIN wallet_transactions wt ON wt.id = wtc.outbound_wallet_transaction_id |
There was a problem hiding this comment.
In the underconsumption query, outbound_totals filters wallet_transactions to status = 1, but consumption_by_outbound does not filter the joined outbound transactions by status (or type). This can skew the comparison if consumptions exist for non-settled/out-of-scope outbound transactions. Align the filters between the two CTEs (or document why they differ).
| JOIN wallet_transactions wt ON wt.id = wtc.outbound_wallet_transaction_id | |
| JOIN wallet_transactions wt | |
| ON wt.id = wtc.outbound_wallet_transaction_id | |
| AND wt.transaction_type = 1 | |
| AND wt.status = 1 |
| JOIN wallet_transactions wt ON wt.id = wtc.inbound_wallet_transaction_id | ||
| GROUP BY wt.wallet_id | ||
| ), | ||
| cons_outbound AS ( | ||
| SELECT wt.wallet_id, SUM(wtc.consumed_amount_cents) AS total_consumed_cents | ||
| FROM wallet_transaction_consumptions wtc | ||
| JOIN wallet_transactions wt ON wt.id = wtc.outbound_wallet_transaction_id |
There was a problem hiding this comment.
The negative-values query aggregates transactions with wt.status = 1, but the consumption aggregates (cons_inbound / cons_outbound) don't apply any status filter on the joined wallet_transactions. For consistency (and to avoid flagging values that come from non-settled transactions), apply the same status constraint to the consumption CTE joins or explicitly state that consumptions are always settled-only.
| JOIN wallet_transactions wt ON wt.id = wtc.inbound_wallet_transaction_id | |
| GROUP BY wt.wallet_id | |
| ), | |
| cons_outbound AS ( | |
| SELECT wt.wallet_id, SUM(wtc.consumed_amount_cents) AS total_consumed_cents | |
| FROM wallet_transaction_consumptions wtc | |
| JOIN wallet_transactions wt ON wt.id = wtc.outbound_wallet_transaction_id | |
| JOIN wallet_transactions wt ON wt.id = wtc.inbound_wallet_transaction_id AND wt.status = 1 | |
| GROUP BY wt.wallet_id | |
| ), | |
| cons_outbound AS ( | |
| SELECT wt.wallet_id, SUM(wtc.consumed_amount_cents) AS total_consumed_cents | |
| FROM wallet_transaction_consumptions wtc | |
| JOIN wallet_transactions wt ON wt.id = wtc.outbound_wallet_transaction_id AND wt.status = 1 |
22024e8 to
68ebce4
Compare
f686059 to
22bbba9
Compare
44399af to
3b1bc05
Compare
No description provided.