bd sync: 2025-11-24 12:25:34

This commit is contained in:
Steve Yegge
2025-11-24 12:25:35 -08:00
parent d66593f8a5
commit 0acd9d0a5d
5 changed files with 1707 additions and 6 deletions

View File

@@ -8,6 +8,61 @@ import (
"github.com/steveyegge/beads/internal/types"
)
// Transaction provides atomic multi-operation support within a single database transaction.
//
// The Transaction interface exposes a subset of Storage methods that execute within
// a single database transaction. This enables atomic workflows where multiple operations
// must either all succeed or all fail (e.g., creating issues with dependencies and labels).
//
// # Transaction Semantics
//
// - All operations within the transaction share the same database connection
// - Changes are not visible to other connections until commit
// - If any operation returns an error, the transaction is rolled back
// - If the callback function panics, the transaction is rolled back
// - On successful return from the callback, the transaction is committed
//
// # SQLite Specifics
//
// - Uses BEGIN IMMEDIATE mode to acquire write lock early
// - This prevents deadlocks when multiple operations compete for the same lock
// - IMMEDIATE mode serializes concurrent transactions properly
//
// # Example Usage
//
// err := store.RunInTransaction(ctx, func(tx storage.Transaction) error {
// // Create parent issue
// if err := tx.CreateIssue(ctx, parentIssue, actor); err != nil {
// return err // Triggers rollback
// }
// // Create child issue
// if err := tx.CreateIssue(ctx, childIssue, actor); err != nil {
// return err // Triggers rollback
// }
// // Add dependency between them
// if err := tx.AddDependency(ctx, dep, actor); err != nil {
// return err // Triggers rollback
// }
// return nil // Triggers commit
// })
type Transaction interface {
// Issue operations
CreateIssue(ctx context.Context, issue *types.Issue, actor string) error
CreateIssues(ctx context.Context, issues []*types.Issue, actor string) error
UpdateIssue(ctx context.Context, id string, updates map[string]interface{}, actor string) error
CloseIssue(ctx context.Context, id string, reason string, actor string) error
DeleteIssue(ctx context.Context, id string) error
GetIssue(ctx context.Context, id string) (*types.Issue, error) // For read-your-writes within transaction
// Dependency operations
AddDependency(ctx context.Context, dep *types.Dependency, actor string) error
RemoveDependency(ctx context.Context, issueID, dependsOnID string, actor string) error
// Label operations
AddLabel(ctx context.Context, issueID, label, actor string) error
RemoveLabel(ctx context.Context, issueID, label, actor string) error
}
// Storage defines the interface for issue storage backends
type Storage interface {
// Issues
@@ -89,6 +144,26 @@ type Storage interface {
RenameDependencyPrefix(ctx context.Context, oldPrefix, newPrefix string) error
RenameCounterPrefix(ctx context.Context, oldPrefix, newPrefix string) error
// Transactions
//
// RunInTransaction executes a function within a database transaction.
// The Transaction interface provides atomic multi-operation support.
//
// Transaction behavior:
// - If fn returns nil, the transaction is committed
// - If fn returns an error, the transaction is rolled back
// - If fn panics, the transaction is rolled back and the panic is re-raised
// - Uses BEGIN IMMEDIATE for SQLite to acquire write lock early
//
// Example:
// err := store.RunInTransaction(ctx, func(tx storage.Transaction) error {
// if err := tx.CreateIssue(ctx, issue, actor); err != nil {
// return err // Triggers rollback
// }
// return nil // Triggers commit
// })
RunInTransaction(ctx context.Context, fn func(tx Transaction) error) error
// Lifecycle
Close() error