Prevent decremeting stars of project with zero star count

What does this MR do and why?

This MR prevents 500 errors caused by attempts to decrement project star counts that are already at zero, violating the projects_star_count_positive database constraint.

Query Plans

user.starred_projects.where('star_count > 0').update_counters(star_count: -1)

Before

After

References

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Screenshots or screen recordings

N/A

How to set up and validate locally

We have no clear replication step as its unclear how did the star count of the projects became out of sync with the users' starred projects. Although we can simulate this situation by doing the following:

  1. Create a new account and sign in.
  2. Star a project.
  3. In the rails console, run Project.find(...).update!(star_count: 0).
  4. Sign in as admin and go to http://localhost:3000/admin/users.
  5. Block/ban/deactivate the user from step (1).

Before: Raises PG::CheckViolation: ERROR: new row for relation "projects" violates check constraint "projects_star_count_positive"

After: User is blocked/banned successfully.

Edited by Shane Maglangit

Merge request reports

Loading