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
- Cold: https://console.postgres.ai/gitlab/gitlab-production-main/sessions/36907/commands/113270
- After: https://console.postgres.ai/gitlab/gitlab-production-main/sessions/36907/commands/113271
After
- Cold: https://console.postgres.ai/gitlab/gitlab-production-main/sessions/36907/commands/113278
- Warm: https://console.postgres.ai/gitlab/gitlab-production-main/sessions/36907/commands/113277
References
- Closes #487435 (closed)
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:
- Create a new account and sign in.
- Star a project.
- In the rails console, run
Project.find(...).update!(star_count: 0). - Sign in as admin and go to http://localhost:3000/admin/users.
- 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.