Skip to content

Add gc_inspect command to diagnose garbage collection status#2

Merged
SupraSummus merged 1 commit into
mainfrom
claude/vibrant-allen-r5lo5l
Jun 16, 2026
Merged

Add gc_inspect command to diagnose garbage collection status#2
SupraSummus merged 1 commit into
mainfrom
claude/vibrant-allen-r5lo5l

Conversation

@SupraSummus

Copy link
Copy Markdown
Member

Summary

This PR adds a new gc_inspect management command that allows users to diagnose why a specific object is or isn't being garbage collected, without actually deleting anything. It also refactors the config loading logic into a reusable function.

Key Changes

  • New gc_inspect command (django_gc/management/commands/gc_inspect.py): A read-only diagnostic tool that reports the garbage collection status of a single object by model label and primary key. Reports one of four statuses: not found, excluded by filter, referenced by other objects, or collectable.

  • New InspectResult dataclass: Encapsulates the result of inspecting an object, including status, message, and list of referencing objects.

  • New inspect_object() function: Core logic that mirrors the decisions made by process_batch() (filter evaluation and foreign key reference checking) without deleting anything. This allows explaining why a specific row is or isn't collected.

  • Refactored get_combined_config() function: Extracted the config merging and collision detection logic from the gc command's handle() method into a standalone function for reuse by gc_inspect.

  • Updated README: Added documentation for the new gc_inspect command with usage examples and explanation of the four possible statuses.

  • Comprehensive test coverage: Added tests for all inspect_object() scenarios (not found, collectable, referenced, excluded by filter, ignored fields) and the new gc_inspect command.

Implementation Details

  • The inspect_object() function reuses the existing find_fk_fields() utility to identify foreign key references, ensuring consistency with the main garbage collection logic.
  • The command validates that the model is configured for garbage collection before attempting inspection.
  • References are formatted as app_label.ModelName.field_name (pk=...) for clarity.
  • The implementation properly handles the ignored_referencing_fields configuration option.

https://claude.ai/code/session_016DhEphjmyfNcAX38vVbjfk

@SupraSummus
SupraSummus force-pushed the claude/vibrant-allen-r5lo5l branch 2 times, most recently from d473b6c to d30042c Compare June 16, 2026 10:27
Adds a read-only gc_inspect management command that explains why a
single object is or is not garbage collected, without deleting
anything:

    python manage.py gc_inspect myapp.Attachment 42

It reports one of: object not found, excluded by filter (not currently
a candidate), referenced by N other objects (each blocking foreign key
listed), or unreferenced and collectable. The logic mirrors the gc
command's deletion decisions (filter, then foreign-key references,
honoring ignored_referencing_fields) so the reported status matches
what an actual run would do.

Shared helpers used by both commands (get_combined_config,
find_fk_fields) are factored into django_gc.core; each command keeps
its own private logic.

https://claude.ai/code/session_016DhEphjmyfNcAX38vVbjfk
@SupraSummus
SupraSummus force-pushed the claude/vibrant-allen-r5lo5l branch from d30042c to 695a68f Compare June 16, 2026 10:28
@SupraSummus
SupraSummus merged commit d9dfa58 into main Jun 16, 2026
3 checks passed
@SupraSummus
SupraSummus deleted the claude/vibrant-allen-r5lo5l branch June 16, 2026 10:29
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.

1 participant