|
| 1 | +package sqlite |
| 2 | + |
| 3 | +// TODO(dolt-parity): These DecisionPoint methods need corresponding implementations |
| 4 | +// in the dolt storage backend when it's added. The interface is defined in |
| 5 | +// storage/storage.go and must be implemented for all backends. |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "database/sql" |
| 10 | + "fmt" |
| 11 | + |
| 12 | + "github.com/steveyegge/beads/internal/types" |
| 13 | +) |
| 14 | + |
| 15 | +// CreateDecisionPoint creates a new decision point for an issue. |
| 16 | +func (s *SQLiteStorage) CreateDecisionPoint(ctx context.Context, dp *types.DecisionPoint) error { |
| 17 | + // Verify issue exists |
| 18 | + var exists bool |
| 19 | + err := s.db.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM issues WHERE id = ?)`, dp.IssueID).Scan(&exists) |
| 20 | + if err != nil { |
| 21 | + return fmt.Errorf("failed to check issue existence: %w", err) |
| 22 | + } |
| 23 | + if !exists { |
| 24 | + return fmt.Errorf("issue %s not found", dp.IssueID) |
| 25 | + } |
| 26 | + |
| 27 | + // Convert empty strings to NULL for optional FK fields |
| 28 | + var priorID interface{} |
| 29 | + if dp.PriorID != "" { |
| 30 | + priorID = dp.PriorID |
| 31 | + } |
| 32 | + |
| 33 | + // Insert decision point |
| 34 | + _, err = s.db.ExecContext(ctx, ` |
| 35 | + INSERT INTO decision_points ( |
| 36 | + issue_id, prompt, options, default_option, selected_option, |
| 37 | + response_text, responded_at, responded_by, iteration, max_iterations, |
| 38 | + prior_id, guidance, requested_by, created_at |
| 39 | + ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP) |
| 40 | + `, dp.IssueID, dp.Prompt, dp.Options, dp.DefaultOption, dp.SelectedOption, |
| 41 | + dp.ResponseText, dp.RespondedAt, dp.RespondedBy, dp.Iteration, dp.MaxIterations, |
| 42 | + priorID, dp.Guidance, dp.RequestedBy) |
| 43 | + if err != nil { |
| 44 | + return fmt.Errorf("failed to insert decision point: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +// GetDecisionPoint retrieves the decision point for an issue. |
| 51 | +func (s *SQLiteStorage) GetDecisionPoint(ctx context.Context, issueID string) (*types.DecisionPoint, error) { |
| 52 | + // Hold read lock during database operations to prevent reconnect() from |
| 53 | + // closing the connection mid-query (GH#607 race condition fix) |
| 54 | + s.reconnectMu.RLock() |
| 55 | + defer s.reconnectMu.RUnlock() |
| 56 | + |
| 57 | + dp := &types.DecisionPoint{} |
| 58 | + err := s.db.QueryRowContext(ctx, ` |
| 59 | + SELECT issue_id, prompt, options, |
| 60 | + COALESCE(default_option, ''), COALESCE(selected_option, ''), |
| 61 | + COALESCE(response_text, ''), responded_at, COALESCE(responded_by, ''), |
| 62 | + iteration, max_iterations, |
| 63 | + COALESCE(prior_id, ''), COALESCE(guidance, ''), COALESCE(requested_by, ''), created_at |
| 64 | + FROM decision_points |
| 65 | + WHERE issue_id = ? |
| 66 | + `, issueID).Scan( |
| 67 | + &dp.IssueID, &dp.Prompt, &dp.Options, |
| 68 | + &dp.DefaultOption, &dp.SelectedOption, |
| 69 | + &dp.ResponseText, &dp.RespondedAt, &dp.RespondedBy, |
| 70 | + &dp.Iteration, &dp.MaxIterations, |
| 71 | + &dp.PriorID, &dp.Guidance, &dp.RequestedBy, &dp.CreatedAt, |
| 72 | + ) |
| 73 | + if err == sql.ErrNoRows { |
| 74 | + return nil, nil |
| 75 | + } |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf("failed to query decision point: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + return dp, nil |
| 81 | +} |
| 82 | + |
| 83 | +// UpdateDecisionPoint updates an existing decision point. |
| 84 | +func (s *SQLiteStorage) UpdateDecisionPoint(ctx context.Context, dp *types.DecisionPoint) error { |
| 85 | + // Convert empty strings to NULL for optional FK fields |
| 86 | + var priorID interface{} |
| 87 | + if dp.PriorID != "" { |
| 88 | + priorID = dp.PriorID |
| 89 | + } |
| 90 | + |
| 91 | + result, err := s.db.ExecContext(ctx, ` |
| 92 | + UPDATE decision_points SET |
| 93 | + prompt = ?, |
| 94 | + options = ?, |
| 95 | + default_option = ?, |
| 96 | + selected_option = ?, |
| 97 | + response_text = ?, |
| 98 | + responded_at = ?, |
| 99 | + responded_by = ?, |
| 100 | + iteration = ?, |
| 101 | + max_iterations = ?, |
| 102 | + prior_id = ?, |
| 103 | + guidance = ? |
| 104 | + WHERE issue_id = ? |
| 105 | + `, dp.Prompt, dp.Options, dp.DefaultOption, dp.SelectedOption, |
| 106 | + dp.ResponseText, dp.RespondedAt, dp.RespondedBy, |
| 107 | + dp.Iteration, dp.MaxIterations, priorID, dp.Guidance, dp.IssueID) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("failed to update decision point: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + rowsAffected, err := result.RowsAffected() |
| 113 | + if err != nil { |
| 114 | + return fmt.Errorf("failed to get rows affected: %w", err) |
| 115 | + } |
| 116 | + if rowsAffected == 0 { |
| 117 | + return fmt.Errorf("decision point not found for issue %s", dp.IssueID) |
| 118 | + } |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +// ListPendingDecisions returns all decision points that haven't been responded to. |
| 124 | +func (s *SQLiteStorage) ListPendingDecisions(ctx context.Context) ([]*types.DecisionPoint, error) { |
| 125 | + // Hold read lock during database operations to prevent reconnect() from |
| 126 | + // closing the connection mid-query (GH#607 race condition fix) |
| 127 | + s.reconnectMu.RLock() |
| 128 | + defer s.reconnectMu.RUnlock() |
| 129 | + |
| 130 | + rows, err := s.db.QueryContext(ctx, ` |
| 131 | + SELECT issue_id, prompt, options, |
| 132 | + COALESCE(default_option, ''), COALESCE(selected_option, ''), |
| 133 | + COALESCE(response_text, ''), responded_at, COALESCE(responded_by, ''), |
| 134 | + iteration, max_iterations, |
| 135 | + COALESCE(prior_id, ''), COALESCE(guidance, ''), COALESCE(requested_by, ''), created_at |
| 136 | + FROM decision_points |
| 137 | + WHERE responded_at IS NULL |
| 138 | + ORDER BY created_at ASC |
| 139 | + `) |
| 140 | + if err != nil { |
| 141 | + return nil, fmt.Errorf("failed to query pending decisions: %w", err) |
| 142 | + } |
| 143 | + defer func() { _ = rows.Close() }() |
| 144 | + |
| 145 | + var results []*types.DecisionPoint |
| 146 | + for rows.Next() { |
| 147 | + dp := &types.DecisionPoint{} |
| 148 | + err := rows.Scan( |
| 149 | + &dp.IssueID, &dp.Prompt, &dp.Options, |
| 150 | + &dp.DefaultOption, &dp.SelectedOption, |
| 151 | + &dp.ResponseText, &dp.RespondedAt, &dp.RespondedBy, |
| 152 | + &dp.Iteration, &dp.MaxIterations, |
| 153 | + &dp.PriorID, &dp.Guidance, &dp.RequestedBy, &dp.CreatedAt, |
| 154 | + ) |
| 155 | + if err != nil { |
| 156 | + return nil, fmt.Errorf("failed to scan decision point: %w", err) |
| 157 | + } |
| 158 | + results = append(results, dp) |
| 159 | + } |
| 160 | + |
| 161 | + if err := rows.Err(); err != nil { |
| 162 | + return nil, fmt.Errorf("error iterating decision points: %w", err) |
| 163 | + } |
| 164 | + |
| 165 | + return results, nil |
| 166 | +} |
0 commit comments