Issue #3608700 by codeitwisely, marcus_johansson: Purge usage data on user deletion, reassign on cancel

Closes https://www.drupal.org/project/ai_metering/issues/3608700

What changed

  • hook_ENTITY_TYPE_delete() for user: deletes the uid's rows from ai_metering_usage, ai_metering_rollup and ai_metering_quota (the rollup table landed in #3608701, after this issue was filed, so it is covered too).
  • hook_user_cancel(): with the 'delete account, keep content' method, usage and rollup rows are reassigned to the anonymous user so team totals and historical dashboards keep adding up, while the personal linkage is removed. Rollup grains that collide with an existing anonymous row are summed, not duplicated. Quota rows are deleted (personal by nature). Blocking methods touch nothing.
  • Three kernel tests: full purge on delete, reassign with grain merge on cancel, no-op on block.

Testing steps

  1. Create a user, run a few AI operations with it.
  2. Delete the account (delete account and content): its rows are gone from all three tables.
  3. Cancel another account with 'delete account, keep content': usage moves to Anonymous, quota rows are removed.

Verified live: a throwaway account ran 5 mixed operations (3 cloud, 2 local, all with latency recorded), then account deletion purged usage, rollup and quota rows. Suite green (139 tests, 551 assertions), PHPCS and PHPStan clean.

AI assistance

Implemented with AI assistance (Claude Code), verified end to end on a live site.

Merge request reports

Loading