Fix bd-pq5k: merge conflicts now prefer closed>open and deletion>modification

CHANGES:
1. Merge logic (internal/merge/merge.go):
   - Added mergeStatus() enforcing closed ALWAYS wins over open
   - Fixed closed_at handling: only set when status='closed'
   - Changed deletion handling: deletion ALWAYS wins over modification

2. Deletion tracking (cmd/bd/snapshot_manager.go):
   - Updated ComputeAcceptedDeletions to accept all merge deletions
   - Removed "unchanged locally" check (deletion wins regardless)

3. FK constraint helper (internal/storage/sqlite/util.go):
   - Added IsForeignKeyConstraintError() for bd-koab
   - Detects FK violations for graceful import handling

TESTS UPDATED:
- TestMergeStatus: comprehensive status merge tests
- TestIsForeignKeyConstraintError: FK constraint detection
- bd-pq5k test: validates no invalid state (status=open with closed_at)
- Deletion tests: reflect new deletion-wins behavior
- All tests pass ✓

This ensures issues never get stuck in invalid states and prevents
the insane situation where issues never die!

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-11-23 21:42:43 -08:00
parent b428254f89
commit d4f9a05bb2
8 changed files with 926 additions and 55 deletions

View File

@@ -51,4 +51,15 @@ func IsUniqueConstraintError(err error) bool {
return strings.Contains(err.Error(), "UNIQUE constraint failed")
}
// IsForeignKeyConstraintError checks if an error is a FOREIGN KEY constraint violation
// This can occur when importing issues that reference deleted issues (e.g., after merge)
func IsForeignKeyConstraintError(err error) bool {
if err == nil {
return false
}
errStr := err.Error()
return strings.Contains(errStr, "FOREIGN KEY constraint failed") ||
strings.Contains(errStr, "foreign key constraint failed")
}

View File

@@ -50,6 +50,54 @@ func TestIsUniqueConstraintError(t *testing.T) {
}
}
func TestIsForeignKeyConstraintError(t *testing.T) {
tests := []struct {
name string
err error
expected bool
}{
{
name: "nil error",
err: nil,
expected: false,
},
{
name: "FOREIGN KEY constraint error (uppercase)",
err: errors.New("FOREIGN KEY constraint failed"),
expected: true,
},
{
name: "foreign key constraint error (lowercase)",
err: errors.New("foreign key constraint failed"),
expected: true,
},
{
name: "FOREIGN KEY with details",
err: errors.New("FOREIGN KEY constraint failed: dependencies.depends_on_id"),
expected: true,
},
{
name: "UNIQUE constraint error",
err: errors.New("UNIQUE constraint failed: issues.id"),
expected: false,
},
{
name: "other error",
err: errors.New("some other database error"),
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := IsForeignKeyConstraintError(tt.err)
if result != tt.expected {
t.Errorf("IsForeignKeyConstraintError(%v) = %v, want %v", tt.err, result, tt.expected)
}
})
}
}
func TestExecInTransaction(t *testing.T) {
ctx := context.Background()
store := newTestStore(t, t.TempDir()+"/test.db")