Skip to content

doc: Add Diagnostic queries section#564

Draft
D1353L wants to merge 5 commits into
mainfrom
doc/diagnostic-queries-for-wallets-traceability-migration
Draft

doc: Add Diagnostic queries section#564
D1353L wants to merge 5 commits into
mainfrom
doc/diagnostic-queries-for-wallets-traceability-migration

Conversation

@D1353L
Copy link
Copy Markdown

@D1353L D1353L commented Mar 30, 2026

No description provided.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +183 to +184
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,
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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,

Copilot uses AI. Check for mistakes.
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
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
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
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
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

Copilot uses AI. Check for mistakes.
Comment on lines +307 to +313
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
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
@groyoh groyoh force-pushed the doc/wallet-traceability-migration branch from 22024e8 to 68ebce4 Compare April 1, 2026 19:56
@groyoh groyoh force-pushed the doc/diagnostic-queries-for-wallets-traceability-migration branch from f686059 to 22bbba9 Compare April 1, 2026 19:58
@D1353L D1353L marked this pull request as draft April 3, 2026 13:09
@groyoh groyoh force-pushed the doc/wallet-traceability-migration branch from 44399af to 3b1bc05 Compare April 7, 2026 08:50
Base automatically changed from doc/wallet-traceability-migration to main April 7, 2026 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants