[PR #1154] [MERGED] fix: MySQL constraint violations returning HTTP 500 instead of 400 for task bucket duplicates #9047

Closed
opened 2026-04-23 08:39:08 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/go-vikunja/vikunja/pull/1154
Author: @Copilot
Created: 7/21/2025
Status: Merged
Merged: 7/22/2025
Merged by: @kolaente

Base: mainHead: copilot/fix-1153


📝 Commits (8)

  • c6e05fd Initial plan
  • a4a0db7 feat: handle MySQL constraint violations for task bucket duplicates
  • 33500c5 enhance: add multi-database constraint violation detection
  • 9aaded0 test: improve TaskBucket constraint violation test with proper database integration
  • 7d28e09 test: update TaskBucket constraint violation test to use upsert logic
  • 4efc950 fix lint
  • 7927989 fix: make the test actually good
  • f94fe47 test does not make sense

📊 Changes

4 files changed (+215 additions, -0 deletions)

View changed files

📝 pkg/db/helpers.go (+49 -0)
📝 pkg/db/helpers_test.go (+130 -0)
📝 pkg/models/error.go (+28 -0)
📝 pkg/models/kanban_task_bucket.go (+8 -0)

📄 Description

Problem

When attempting to create duplicate task bucket entries, MySQL unique constraint violations were returning HTTP 500 Internal Server Error instead of a proper HTTP 400 Bad Request response. This occurred when the unique constraint UQE_task_buckets_task_project_view on the combination of task_id and project_view_id was violated.

Original Error:

Error 1062 (23000): Duplicate entry '424-557' for key 'UQE_task_buckets_task_project_view'

Solution

Added proper database constraint violation detection and error handling across all supported database engines (MySQL, PostgreSQL, SQLite):

Changes Made

  1. New Application Error Type (pkg/models/error.go):

    type ErrTaskAlreadyExistsInBucket struct {
        TaskID        int64
        ProjectViewID int64
    }
    
    • Returns HTTP 400 Bad Request with error code 10006
    • User-friendly message: "This task already exists in a bucket for this project view."
  2. Universal Database Constraint Detection (pkg/db/helpers.go):

    • IsUniqueConstraintError() - detects constraint violations across all database types
    • IsMySQLDuplicateEntryError() - maintains backward compatibility
    • Handles error patterns for:
      • MySQL: Error 1062 (23000): Duplicate entry ... for key ...
      • PostgreSQL: duplicate key value violates unique constraint "..."
      • SQLite: UNIQUE constraint failed: table.column
  3. Enhanced Error Handling (pkg/models/kanban_task_bucket.go):

    if db.IsUniqueConstraintError(err, "UQE_task_buckets_task_project_view") {
        return ErrTaskAlreadyExistsInBucket{
            TaskID:        b.TaskID,
            ProjectViewID: b.ProjectViewID,
        }
    }
    

Test Coverage

Added comprehensive tests for all database engines:

  • MySQL Error 1062 detection
  • PostgreSQL constraint violation detection
  • SQLite unique constraint failure detection
  • Case-insensitive error matching
  • Proper HTTP status code and error message validation

Before/After

Before:

  • HTTP 500 Internal Server Error
  • Generic error message sent to Sentry
  • Poor user experience

After:

  • HTTP 400 Bad Request
  • Clear error message: "This task already exists in a bucket for this project view."
  • Proper error code 10006 for API consumers
  • Other database errors remain unchanged (no breaking changes)

Fixes #1153.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/go-vikunja/vikunja/pull/1154 **Author:** [@Copilot](https://github.com/apps/copilot-swe-agent) **Created:** 7/21/2025 **Status:** ✅ Merged **Merged:** 7/22/2025 **Merged by:** [@kolaente](https://github.com/kolaente) **Base:** `main` ← **Head:** `copilot/fix-1153` --- ### 📝 Commits (8) - [`c6e05fd`](https://github.com/go-vikunja/vikunja/commit/c6e05fd79db60dde410396db263b37b768eac189) Initial plan - [`a4a0db7`](https://github.com/go-vikunja/vikunja/commit/a4a0db7c290f8646a041f5d098c6b5bf4bfc589b) feat: handle MySQL constraint violations for task bucket duplicates - [`33500c5`](https://github.com/go-vikunja/vikunja/commit/33500c52aea01bfb06c79c2dfd2e8cb241842e91) enhance: add multi-database constraint violation detection - [`9aaded0`](https://github.com/go-vikunja/vikunja/commit/9aaded06650466610a0796f2ddeb288b37f7d305) test: improve TaskBucket constraint violation test with proper database integration - [`7d28e09`](https://github.com/go-vikunja/vikunja/commit/7d28e09ad5d9683bf4941fd7f70677d78fd9ad76) test: update TaskBucket constraint violation test to use upsert logic - [`4efc950`](https://github.com/go-vikunja/vikunja/commit/4efc950a4cd56851d5f9fbb087c070d12fb89f13) fix lint - [`7927989`](https://github.com/go-vikunja/vikunja/commit/792798965dc8354dbb64787f419b0cc66af28ebf) fix: make the test actually good - [`f94fe47`](https://github.com/go-vikunja/vikunja/commit/f94fe47f4c4e48851bcef5995881b945e2c8bbbc) test does not make sense ### 📊 Changes **4 files changed** (+215 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `pkg/db/helpers.go` (+49 -0) 📝 `pkg/db/helpers_test.go` (+130 -0) 📝 `pkg/models/error.go` (+28 -0) 📝 `pkg/models/kanban_task_bucket.go` (+8 -0) </details> ### 📄 Description ## Problem When attempting to create duplicate task bucket entries, MySQL unique constraint violations were returning HTTP 500 Internal Server Error instead of a proper HTTP 400 Bad Request response. This occurred when the unique constraint `UQE_task_buckets_task_project_view` on the combination of `task_id` and `project_view_id` was violated. **Original Error:** ``` Error 1062 (23000): Duplicate entry '424-557' for key 'UQE_task_buckets_task_project_view' ``` ## Solution Added proper database constraint violation detection and error handling across all supported database engines (MySQL, PostgreSQL, SQLite): ### Changes Made 1. **New Application Error Type** (`pkg/models/error.go`): ```go type ErrTaskAlreadyExistsInBucket struct { TaskID int64 ProjectViewID int64 } ``` - Returns HTTP 400 Bad Request with error code `10006` - User-friendly message: "This task already exists in a bucket for this project view." 2. **Universal Database Constraint Detection** (`pkg/db/helpers.go`): - `IsUniqueConstraintError()` - detects constraint violations across all database types - `IsMySQLDuplicateEntryError()` - maintains backward compatibility - Handles error patterns for: - **MySQL**: `Error 1062 (23000): Duplicate entry ... for key ...` - **PostgreSQL**: `duplicate key value violates unique constraint "..."` - **SQLite**: `UNIQUE constraint failed: table.column` 3. **Enhanced Error Handling** (`pkg/models/kanban_task_bucket.go`): ```go if db.IsUniqueConstraintError(err, "UQE_task_buckets_task_project_view") { return ErrTaskAlreadyExistsInBucket{ TaskID: b.TaskID, ProjectViewID: b.ProjectViewID, } } ``` ### Test Coverage Added comprehensive tests for all database engines: - MySQL Error 1062 detection - PostgreSQL constraint violation detection - SQLite unique constraint failure detection - Case-insensitive error matching - Proper HTTP status code and error message validation ### Before/After **Before:** - HTTP 500 Internal Server Error - Generic error message sent to Sentry - Poor user experience **After:** - HTTP 400 Bad Request - Clear error message: "This task already exists in a bucket for this project view." - Proper error code `10006` for API consumers - Other database errors remain unchanged (no breaking changes) Fixes #1153. <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to start the survey. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-23 08:39:08 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/vikunja#9047