Skip to content

fix(userReports): prevent infinite loop in MV background job#6878

Merged
noliveleger merged 1 commit intorelease/2.026.07from
fix-mv-recreation
Mar 26, 2026
Merged

fix(userReports): prevent infinite loop in MV background job#6878
noliveleger merged 1 commit intorelease/2.026.07from
fix-mv-recreation

Conversation

@noliveleger
Copy link
Contributor

@noliveleger noliveleger commented Mar 26, 2026

💭 Notes

  • Root cause: when SKIP_HEAVY_MIGRATIONS=True, manage_user_reports_mv --create resets LongRunningMigration.status back to 'created', which re-triggers 0019_recreate_user_reports_mv indefinitely.
  • Fix: a new --force flag bypasses SKIP_HEAVY_MIGRATIONS and runs the migration immediately. The background job always passes --force.
  • This PR also reverts the CREATE INDEX CONCURRENTLY approach introduced in fix(userReports): prevent infinite loop in MV background job #6876: the view is freshly created at index time so there are no concurrent readers — CONCURRENTLY would have required autocommit mode for no benefit.
  • No behavior change when --force is not passed.

@noliveleger noliveleger requested a review from jnm as a code owner March 26, 2026 20:39
@noliveleger noliveleger self-assigned this Mar 26, 2026
@coderabbitai
Copy link

coderabbitai bot commented Mar 26, 2026

Important

Review skipped

Auto reviews are limited based on label configuration.

🏷️ Required labels (at least one) (1)
  • llm-ignore

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 41a86d55-e4aa-46e6-b2cf-bd708afec4b6

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps
Copy link

greptile-apps bot commented Mar 26, 2026

Greptile Summary

This PR fixes an infinite-loop bug in the 0019_recreate_user_reports_mv background migration job. When SKIP_HEAVY_MIGRATIONS=True, calling manage_user_reports_mv --create (without --force) was resetting LongRunningMigration.status back to 'created', which caused the scheduler to re-queue the job indefinitely. The fix ensures the background job always passes --force, bypassing the SKIP_HEAVY_MIGRATIONS guard and executing the migration immediately. The PR also reverts the CREATE INDEX CONCURRENTLY approach from #6876 — since the materialized view is freshly created at index time, there are no concurrent readers, and CONCURRENTLY would have required autocommit mode for no benefit.

Key changes:

  • 0019_recreate_user_reports_mv.py: Updated docstring to remove outdated reference to CONCURRENTLY
  • manage_user_reports_mv.py: Removed the --force-conditional CONCURRENTLY string replacement; CREATE_INDEXES_SQL is now used directly in both code paths, and the --force help text is updated accordingly

PR description concerns:

  • The PR title uses DEV-XXXX as a placeholder instead of a real ticket number, and the ### 🗒️ Checklist section has not been completed or deleted before merging (checklist items remain unchecked). The template requires these to be resolved before merging. The required ### 📣 Summary, ### 📖 Description, and ### 👷 Description for instance maintainers sections are also absent without being explicitly deleted.

Confidence Score: 4/5

The code fix is correct and minimal; safe to merge once the PR description and ticket reference are updated.

The logic change is small, well-targeted, and matches the stated root cause. The CONCURRENTLY revert is also sound (fresh view, no concurrent readers, transaction compatibility). No test coverage exists for this code path but the change is low-risk. The main outstanding item is the incomplete PR description (placeholder DEV-XXXX ticket, unchecked checklist, missing required template sections).

No files require special attention from a code perspective. The PR description needs cleanup before merge.

Important Files Changed

Filename Overview
kobo/apps/long_running_migrations/jobs/0019_recreate_user_reports_mv.py Docstring updated to remove outdated reference to CONCURRENTLY; functional code unchanged.
kobo/apps/user_reports/management/commands/manage_user_reports_mv.py Removed force-conditional CONCURRENTLY string replacement; CREATE_INDEXES_SQL now used directly. --force help text updated accordingly.

Sequence Diagram

sequenceDiagram
    participant Scheduler
    participant Job as 0019_recreate_user_reports_mv
    participant Command as manage_user_reports_mv
    participant DB as Database

    Note over Scheduler,DB: Before fix — SKIP_HEAVY_MIGRATIONS=True, no --force
    Scheduler->>Job: run()
    Job->>Command: call_command('manage_user_reports_mv', create=True)
    Command->>Command: SKIP_HEAVY_MIGRATIONS=True AND force=False → reschedule
    Command->>DB: UPDATE status='created'
    DB-->>Command: OK
    Note over Scheduler: Status reset to 'created' triggers re-queue
    Scheduler->>Job: run() again ♻️ (infinite loop)

    Note over Scheduler,DB: After fix — --force=True bypasses the guard
    Scheduler->>Job: run()
    Job->>Command: call_command('manage_user_reports_mv', create=True, force=True)
    Command->>Command: force=True → skip reschedule branch
    Command->>DB: CREATE MATERIALIZED VIEW
    DB-->>Command: OK
    Command->>DB: CREATE INDEXES (non-concurrent)
    DB-->>Command: OK
    Command-->>Job: success
    Job-->>Scheduler: done ✅
Loading

Reviews (2): Last reviewed commit: "refactor(user_reports): remove CONCURREN..." | Re-trigger Greptile

… as the view is newly created and has no concurrent readers
@noliveleger noliveleger changed the title fix(user_reports): prevent infinite loop in MV background job DEV-XXXX fix(userReports): prevent infinite loop in MV background job Mar 26, 2026
@noliveleger noliveleger merged commit f130c78 into release/2.026.07 Mar 26, 2026
21 checks passed
@noliveleger noliveleger deleted the fix-mv-recreation branch March 26, 2026 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant