Skip to content

Add org cleanup Rake tasks and shared Orgs::AssociationInspector#1259

Open
aaronskiba wants to merge 3 commits intointegrationfrom
aaron/delete-orphaned-orgs
Open

Add org cleanup Rake tasks and shared Orgs::AssociationInspector#1259
aaronskiba wants to merge 3 commits intointegrationfrom
aaron/delete-orphaned-orgs

Conversation

@aaronskiba
Copy link
Collaborator

@aaronskiba aaronskiba commented Jan 20, 2026

Changes proposed in this PR:

  1. Remove org deletion from CleanupUnmanagedOrgsWithUsersService

    • Org deletion is now handled by orgs:delete_orphan_orgs to reduce redundancy.
    • Added DRY_RUN support for safe preview of changes.
  2. New Rake tasks:

    • orgs:delete_junk_trackers
    • orgs:cleanup_junk_funders
      • Finds "junk funders" (orgs with only plans.funder associations and a "junk" name) and sets plan.funder_id = nil.
      • Supports DRY_RUN=true, outputs counts without mutating data.
    • orgs:delete_orphan_orgs
      • Deletes orphan orgs (no entries in any has_many, has_one, or HABTM associations).
      • Handles non-standard (funded_plans) and polymorphic (identifiers) associations.
      • Supports DRY_RUN=true, outputs orphan_orgs.csv and deleted_orphan_orgs.csv.
  3. Orgs::AssociationInspector

    • Shared service for orphan org queries.
    • Provides orphan_orgs(allow_funded_plans_association: false) to construct the base query.
    • Includes verify_expected_associations! to guard against DB schema changes.
    • Used by delete_junk_trackers and cleanup_junk_funders to prevent duplicate logic.

Execution notes / recommendations:

  • Take a full DB dump before running cleanup tasks for audit and rollback.
  • Recommended order:
    1. delete_junk_trackers
    2. cleanup_unmanaged_orgs_with_users
    3. cleanup_junk_funders
    4. delete_orphan_orgs (last)
  • DRY_RUN outputs can be compared against the DB dump to verify expected changes before mutation.
  • JUNK_FUNDER_NAMES (in app/services/orgs/cleanup_junk_funders_service.rb) contains the list of known "junk" funder names.
    • This list should be reviewed and verified before running cleanup tasks.

@github-actions
Copy link

1 Error
🚫 Please include a CHANGELOG entry. You can find it at CHANGELOG.md.
1 Warning
⚠️ There are code changes, but no corresponding tests. Please include tests if this PR introduces any modifications in behavior. \n
Ignore this warning if the PR ONLY contains translation.io synced updates.

Generated by 🚫 Danger

aaronskiba added a commit that referenced this pull request Jan 21, 2026
Adds `orgs:delete_junk_trackers` rake task and service to remove Tracker records with blank codes.

This cleans up junk data in the trackers table and unblocks deletion of otherwise orphaned orgs (see #1259).

Partly addresses #1260
Remove org deletion from `CleanupUnmanagedOrgsWithUsersService`.

Calling `orgs:delete_orphaned_orgs` after running this task can handle org deletion.

This removes redundancies between the tasks.
@aaronskiba aaronskiba force-pushed the aaron/delete-orphaned-orgs branch from e0c6a97 to 74872f2 Compare January 27, 2026 00:21
@aaronskiba aaronskiba changed the title Add orgs:delete_orphaned_orgs rake task Add org cleanup Rake tasks and shared Orgs::AssociationInspector Jan 27, 2026
@aaronskiba aaronskiba marked this pull request as ready for review January 27, 2026 00:32
@aaronskiba aaronskiba marked this pull request as draft January 27, 2026 16:09
@aaronskiba aaronskiba force-pushed the aaron/delete-orphaned-orgs branch from 74872f2 to ea23a35 Compare January 27, 2026 16:21
@aaronskiba aaronskiba marked this pull request as ready for review January 27, 2026 16:23
Adds several Rake tasks for cleaning up junk org and tracker data:

1. orgs:delete_junk_trackers
   - Deletes Tracker records with blank codes.
   - Partly addresses #1260.

2. orgs:cleanup_junk_funders
   - Finds "junk funders" (orgs with only `plans.funder` associations and a "junk" name) and sets `plan.funder_id = nil` for their plans.
   - Supports DRY_RUN=true, which outputs counts without mutating data.

3. orgs:delete_orphan_orgs
   - Deletes orphan orgs (no entries in any has_many, has_one, or HABTM associations).
   - Handles non-standard associations (`funded_plans` with `funder_id`) and polymorphic associations (`identifiers`).
   - Supports DRY_RUN=true, which outputs to-be-deleted orgs to `orphan_orgs.csv`.
   - Outputs deletion results per-org to `deleted_orphan_orgs.csv`.

Introduces `Orgs::AssociationInspector` (app/services/orgs/association_inspector.rb) to share orphan org query logic between `delete_junk_trackers` and `cleanup_junk_funders`. Provides:
- `orphan_orgs(allow_funded_plans_association: false)` to construct the base orphan orgs query.
- Future-proofing via `verify_expected_associations!` guard against DB schema or association changes.

**Notes / Recommendations for running tasks:**
- Take a full DB dump for audit and rollback purposes before running cleanup tasks.
- Recommended execution order:
  1. delete_junk_trackers
  2. cleanup_unmanaged_orgs_with_users
  3. cleanup_junk_funders
  4. delete_orphan_orgs (last)
- DRY_RUN outputs for the cleanup tasks can be compared against the DB dump to verify expected changes before performing mutations.
@aaronskiba aaronskiba force-pushed the aaron/delete-orphaned-orgs branch from ea23a35 to 38ba470 Compare January 27, 2026 16:27
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