Skip to content

feat: delete channel logs after history retention period#4586

Merged
stuartc merged 2 commits intomainfrom
4504-delete-channel-logs-after-retention-period
Apr 1, 2026
Merged

feat: delete channel logs after history retention period#4586
stuartc merged 2 commits intomainfrom
4504-delete-channel-logs-after-retention-period

Conversation

@stuartc
Copy link
Copy Markdown
Member

@stuartc stuartc commented Mar 30, 2026

Description

This PR adds channel request log cleanup to the existing project data retention system, and applies the "run is king" audit integrity pattern to channel tables.

Retention cleanup:

  • Channel requests older than the project's history_retention_period are now batch-deleted by the same Oban cron job that cleans up workflow run history
  • Orphaned channel snapshots (no longer referenced by any request, not the current version) are cleaned up after request deletion
  • Uses the same batched CTE deletion pattern as delete_workorders_history for DB safety

Audit integrity (FK constraint change):

  • channel_requests.channel_id changed from ON DELETE CASCADE to ON DELETE RESTRICT, matching the cascade refactor in Cascade delete relationships around runs/dataclips (run is king) #4538/b4f8dceea0
  • delete_channel/2 now explicitly deletes requests before the channel
  • Project deletion path (handle_delete_project) cleans up channel requests before cascading

Closes #4504

Validation steps

  1. Run mix ecto.migrate — migration applies cleanly
  2. mix test test/lightning/channels_test.exs — 56 tests pass (7 new)
  3. mix test test/lightning/projects_test.exs — 116 tests pass (3 new)
  4. Full suite: 3 pre-existing failures (unrelated workflow controller ordering + worker version)

Additional notes for the reviewer

  1. The migration uses NOT VALID + VALIDATE CONSTRAINT for zero-downtime deployment (same pattern as Cascade delete relationships around runs/dataclips (run is king) #4538)
  2. channel_snapshots.channel_id intentionally stays DELETE_ALL — snapshots are config records; their audit value is through requests referencing them
  3. No UI changes — this is purely backend plumbing

AI Usage

  • I have used Claude Code

Pre-submission checklist

  • I have performed an AI review of my code
  • I have implemented and tested all related authorization policies.
  • I have updated the changelog.
  • I have ticked a box in "AI usage" in this PR

@github-project-automation github-project-automation bot moved this to New Issues in Core Mar 30, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 30, 2026

Codecov Report

❌ Patch coverage is 92.30769% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.51%. Comparing base (3fe81d8) to head (63237ce).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
lib/lightning/channels.ex 91.42% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4586      +/-   ##
==========================================
+ Coverage   89.49%   89.51%   +0.01%     
==========================================
  Files         441      441              
  Lines       21249    21288      +39     
==========================================
+ Hits        19017    19055      +38     
- Misses       2232     2233       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@stuartc stuartc requested a review from midigofrank March 30, 2026 14:25
Copy link
Copy Markdown
Collaborator

@midigofrank midigofrank left a comment

Choose a reason for hiding this comment

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

Nicely done

stuartc added 2 commits April 1, 2026 07:26
Channel request logs are now cleaned up by the existing project data
retention system, using the same history_retention_period setting that
controls workflow run history.

What this adds:
- Batched deletion of expired channel_requests (by started_at) in the
  per-project Oban retention job, with orphaned channel_snapshot cleanup
- RESTRICT FK on channel_requests.channel_id (matching the "run is king"
  audit integrity pattern from #4538) — deleting a channel can no longer
  silently destroy its request history
- Explicit request cleanup in delete_channel/2 and project deletion path

Migration: channel_requests.channel_id CASCADE → RESTRICT (reversible,
uses NOT VALID + VALIDATE CONSTRAINT for zero-downtime).
Consolidate three copies of the CTE batch deletion pattern into a shared
batch_delete_requests/1 private function. delete_channel/2 now uses the
batched path (instead of unbatched Repo.delete_all) to handle
high-volume channels safely.

Also fixes doc terminology (ON DELETE DELETE_ALL -> on_delete: :delete_all),
uses Config.activity_cleanup_chunk_size() consistently, and moves logging
out of delete_unused_channel_snapshots into its caller.
@stuartc stuartc force-pushed the 4504-delete-channel-logs-after-retention-period branch from 1386673 to 63237ce Compare April 1, 2026 05:26
@stuartc stuartc merged commit b7e74a1 into main Apr 1, 2026
8 checks passed
@stuartc stuartc deleted the 4504-delete-channel-logs-after-retention-period branch April 1, 2026 06:16
@github-project-automation github-project-automation bot moved this from New Issues to Done in Core Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Delete Logs After History Retention Period

2 participants