Documentation
¶
Overview ¶
Example (BasicTransaction) ¶
Example_basicTransaction demonstrates basic transaction usage
package main
import (
"context"
"fmt"
"log"
"github.com/snowdreamtech/unirtm/internal/database"
"github.com/snowdreamtech/unirtm/internal/repository"
"github.com/snowdreamtech/unirtm/internal/transaction"
)
func main() {
// Open database
db, err := database.Open(context.Background(), database.Config{
Path: "/tmp/unirtm.db",
WALMode: true,
})
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Create transaction manager
tm := transaction.NewSQLiteTransactionManager(db.Conn())
// Begin transaction
ctx := context.Background()
tx, err := tm.Begin(ctx)
if err != nil {
log.Fatal(err)
}
// Ensure rollback on error
defer func() {
if err != nil {
tx.Rollback()
}
}()
// Create installation
installation := &repository.Installation{
Tool: "node",
Version: "20.0.0",
Backend: "github",
Provider: "node",
InstallPath: "/opt/unirtm/node/20.0.0",
Checksum: "abc123",
Metadata: "{}",
}
err = tx.InstallationRepo().Create(ctx, installation)
if err != nil {
log.Fatal(err)
}
// Commit transaction
err = tx.Commit()
if err != nil {
log.Fatal(err)
}
fmt.Println("Installation created successfully")
}
Output:
Example (ContextCancellation) ¶
Example_contextCancellation demonstrates handling context cancellation
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/snowdreamtech/unirtm/internal/database"
"github.com/snowdreamtech/unirtm/internal/repository"
"github.com/snowdreamtech/unirtm/internal/transaction"
)
func main() {
db, err := database.Open(context.Background(), database.Config{
Path: "/tmp/unirtm.db",
WALMode: true,
})
if err != nil {
log.Fatal(err)
}
defer db.Close()
tm := transaction.NewSQLiteTransactionManager(db.Conn())
// Create a context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
tx, err := tm.Begin(ctx)
if err != nil {
log.Fatal(err)
}
defer func() {
if err != nil {
tx.Rollback()
}
}()
// Perform operations with the context
installation := &repository.Installation{
Tool: "rust",
Version: "1.70.0",
Backend: "github",
Provider: "rust",
InstallPath: "/opt/unirtm/rust/1.70.0",
Checksum: "jkl012",
Metadata: "{}",
}
err = tx.InstallationRepo().Create(ctx, installation)
if err != nil {
log.Printf("Operation failed: %v", err)
return
}
// Commit before context timeout
err = tx.Commit()
if err != nil {
log.Fatal(err)
}
fmt.Println("Transaction completed before timeout")
}
Output:
Example (ErrorHandlingWithRollback) ¶
Example_errorHandlingWithRollback demonstrates automatic rollback on error
package main
import (
"context"
"fmt"
"log"
"github.com/snowdreamtech/unirtm/internal/database"
"github.com/snowdreamtech/unirtm/internal/repository"
"github.com/snowdreamtech/unirtm/internal/transaction"
)
func main() {
db, err := database.Open(context.Background(), database.Config{
Path: "/tmp/unirtm.db",
WALMode: true,
})
if err != nil {
log.Fatal(err)
}
defer db.Close()
tm := transaction.NewSQLiteTransactionManager(db.Conn())
ctx := context.Background()
// Function that performs operations in a transaction
performInstallation := func() error {
tx, err := tm.Begin(ctx)
if err != nil {
return fmt.Errorf("begin transaction: %w", err)
}
// Automatic rollback on any error
defer func() {
if err != nil {
if rbErr := tx.Rollback(); rbErr != nil {
log.Printf("rollback failed: %v", rbErr)
}
}
}()
// Create installation
installation := &repository.Installation{
Tool: "go",
Version: "1.21.0",
Backend: "github",
Provider: "go",
InstallPath: "/opt/unirtm/go/1.21.0",
Checksum: "ghi789",
Metadata: "{}",
}
err = tx.InstallationRepo().Create(ctx, installation)
if err != nil {
return fmt.Errorf("create installation: %w", err)
}
// Simulate an error condition
if installation.Version == "1.21.0" {
return fmt.Errorf("simulated error: version validation failed")
}
// This commit will never be reached due to the error above
return tx.Commit()
}
// Call the function
err = performInstallation()
if err != nil {
fmt.Printf("Installation failed (transaction rolled back): %v\n", err)
}
}
Output:
Example (MultiRepositoryTransaction) ¶
Example_multiRepositoryTransaction demonstrates atomic operations across multiple repositories
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/snowdreamtech/unirtm/internal/database"
"github.com/snowdreamtech/unirtm/internal/repository"
"github.com/snowdreamtech/unirtm/internal/transaction"
)
func main() {
db, err := database.Open(context.Background(), database.Config{
Path: "/tmp/unirtm.db",
WALMode: true,
})
if err != nil {
log.Fatal(err)
}
defer db.Close()
tm := transaction.NewSQLiteTransactionManager(db.Conn())
ctx := context.Background()
// Begin transaction
tx, err := tm.Begin(ctx)
if err != nil {
log.Fatal(err)
}
// Automatic rollback on error
defer func() {
if err != nil {
if rbErr := tx.Rollback(); rbErr != nil {
log.Printf("rollback failed: %v", rbErr)
}
}
}()
// 1. Create installation
installation := &repository.Installation{
Tool: "python",
Version: "3.11.0",
Backend: "github",
Provider: "python",
InstallPath: "/opt/unirtm/python/3.11.0",
Checksum: "def456",
Metadata: "{}",
}
err = tx.InstallationRepo().Create(ctx, installation)
if err != nil {
log.Fatal(err)
}
// 2. Log audit entry
auditEntry := &repository.AuditEntry{
Operation: "install",
Tool: installation.Tool,
Version: installation.Version,
Status: "success",
Duration: 5000,
Metadata: "{}",
}
err = tx.AuditRepo().Log(ctx, auditEntry)
if err != nil {
log.Fatal(err)
}
// 3. Update tool index
indexEntry := &repository.IndexEntry{
Tool: installation.Tool,
Description: "Python programming language",
Homepage: "https://python.org",
License: "PSF",
Backend: installation.Backend,
Metadata: "{}",
}
err = tx.IndexRepo().Upsert(ctx, indexEntry)
if err != nil {
log.Fatal(err)
}
// 4. Cache installation metadata
err = tx.CacheRepo().Set(ctx, "python:3.11.0:metadata", []byte("cached metadata"), 24*time.Hour)
if err != nil {
log.Fatal(err)
}
// Commit all operations atomically
err = tx.Commit()
if err != nil {
log.Fatal(err)
}
fmt.Println("Multi-repository transaction completed successfully")
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Transaction ¶
type Transaction interface {
// Commit commits the transaction
Commit() error
// Rollback rolls back the transaction
Rollback() error
// InstallationRepo returns the installation repository for this transaction
InstallationRepo() repository.InstallationRepository
// CacheRepo returns the cache repository for this transaction
CacheRepo() repository.CacheRepository
// AuditRepo returns the audit repository for this transaction
AuditRepo() repository.AuditRepository
// IndexRepo returns the index repository for this transaction
IndexRepo() repository.IndexRepository
}
Transaction represents an active database transaction with repository access Validates Requirements: 2.8 (Use transactions for all write operations), 3.3 (Support explicit commit operations)
type TransactionManager ¶
type TransactionManager interface {
// Begin starts a new transaction
Begin(ctx context.Context) (Transaction, error)
}
TransactionManager manages database transactions Validates Requirements: 2.8 (Use transactions for all write operations)
func NewSQLiteTransactionManager ¶
func NewSQLiteTransactionManager(db *sql.DB) TransactionManager
NewSQLiteTransactionManager creates a new SQLite transaction manager