Fix FK constraint failures in AddComment and ApplyCompaction (bd-5arw)
Amp-Thread-ID: https://ampcode.com/threads/T-4358e6e4-28ea-4ed7-ba3f-3da39072e169 Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
@@ -273,7 +273,7 @@ func (s *SQLiteStorage) ApplyCompaction(ctx context.Context, issueID string, lev
|
|||||||
commitHashPtr = &commitHash
|
commitHashPtr = &commitHash
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := tx.ExecContext(ctx, `
|
res, err := tx.ExecContext(ctx, `
|
||||||
UPDATE issues
|
UPDATE issues
|
||||||
SET compaction_level = ?,
|
SET compaction_level = ?,
|
||||||
compacted_at = ?,
|
compacted_at = ?,
|
||||||
@@ -287,6 +287,14 @@ func (s *SQLiteStorage) ApplyCompaction(ctx context.Context, issueID string, lev
|
|||||||
return fmt.Errorf("failed to apply compaction metadata: %w", err)
|
return fmt.Errorf("failed to apply compaction metadata: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rows, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||||
|
}
|
||||||
|
if rows == 0 {
|
||||||
|
return fmt.Errorf("issue %s not found", issueID)
|
||||||
|
}
|
||||||
|
|
||||||
reductionPct := 0.0
|
reductionPct := 0.0
|
||||||
if originalSize > 0 {
|
if originalSize > 0 {
|
||||||
reductionPct = (1.0 - float64(compressedSize)/float64(originalSize)) * 100
|
reductionPct = (1.0 - float64(compressedSize)/float64(originalSize)) * 100
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package sqlite
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -369,3 +370,22 @@ func TestApplyCompaction(t *testing.T) {
|
|||||||
func timePtr(t time.Time) *time.Time {
|
func timePtr(t time.Time) *time.Time {
|
||||||
return &t
|
return &t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestApplyCompactionNotFound(t *testing.T) {
|
||||||
|
store, cleanup := setupTestDB(t)
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
nonExistentID := "bd-999"
|
||||||
|
|
||||||
|
err := store.ApplyCompaction(ctx, nonExistentID, 1, 100, 50, "abc123")
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("Expected error, got nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedError := "issue bd-999 not found"
|
||||||
|
if !strings.Contains(err.Error(), expectedError) {
|
||||||
|
t.Errorf("Expected error to contain %q, got %q", expectedError, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,24 @@ const limitClause = " LIMIT ?"
|
|||||||
// AddComment adds a comment to an issue
|
// AddComment adds a comment to an issue
|
||||||
func (s *SQLiteStorage) AddComment(ctx context.Context, issueID, actor, comment string) error {
|
func (s *SQLiteStorage) AddComment(ctx context.Context, issueID, actor, comment string) error {
|
||||||
return s.withTx(ctx, func(tx *sql.Tx) error {
|
return s.withTx(ctx, func(tx *sql.Tx) error {
|
||||||
_, err := tx.ExecContext(ctx, `
|
// Update issue updated_at timestamp first to verify issue exists
|
||||||
|
now := time.Now()
|
||||||
|
res, err := tx.ExecContext(ctx, `
|
||||||
|
UPDATE issues SET updated_at = ? WHERE id = ?
|
||||||
|
`, now, issueID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to update timestamp: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||||
|
}
|
||||||
|
if rows == 0 {
|
||||||
|
return fmt.Errorf("issue %s not found", issueID)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = tx.ExecContext(ctx, `
|
||||||
INSERT INTO events (issue_id, event_type, actor, comment)
|
INSERT INTO events (issue_id, event_type, actor, comment)
|
||||||
VALUES (?, ?, ?, ?)
|
VALUES (?, ?, ?, ?)
|
||||||
`, issueID, types.EventCommented, actor, comment)
|
`, issueID, types.EventCommented, actor, comment)
|
||||||
@@ -22,15 +39,6 @@ func (s *SQLiteStorage) AddComment(ctx context.Context, issueID, actor, comment
|
|||||||
return fmt.Errorf("failed to add comment: %w", err)
|
return fmt.Errorf("failed to add comment: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update issue updated_at timestamp
|
|
||||||
now := time.Now()
|
|
||||||
_, err = tx.ExecContext(ctx, `
|
|
||||||
UPDATE issues SET updated_at = ? WHERE id = ?
|
|
||||||
`, now, issueID)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to update timestamp: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mark issue as dirty for incremental export
|
// Mark issue as dirty for incremental export
|
||||||
_, err = tx.ExecContext(ctx, `
|
_, err = tx.ExecContext(ctx, `
|
||||||
INSERT INTO dirty_issues (issue_id, marked_at)
|
INSERT INTO dirty_issues (issue_id, marked_at)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package sqlite
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/steveyegge/beads/internal/types"
|
"github.com/steveyegge/beads/internal/types"
|
||||||
@@ -339,3 +340,21 @@ func TestEventTypesInHistory(t *testing.T) {
|
|||||||
t.Error("Expected EventClosed in history")
|
t.Error("Expected EventClosed in history")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAddCommentNotFound(t *testing.T) {
|
||||||
|
store, cleanup := setupTestDB(t)
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
nonExistentID := "bd-999"
|
||||||
|
|
||||||
|
err := store.AddComment(ctx, nonExistentID, "alice", "This should fail cleanly")
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("Expected error, got nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedError := "issue bd-999 not found"
|
||||||
|
if !strings.Contains(err.Error(), expectedError) {
|
||||||
|
t.Errorf("Expected error to contain %q, got %q", expectedError, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user