Expand description
§Database Layer
This module provides database connectivity, schema management, and migration support for persisting Cloacina execution context and metadata.
§Components
connection: Database connection management and poolingschema: Diesel schema definitions
§Database Support
Supports both PostgreSQL and SQLite through compile-time feature flags:
- PostgreSQL: Full-featured with native UUID and timestamp types
- SQLite: File-based or in-memory with type conversions
The database layer handles:
- Context persistence and retrieval
- Task execution state tracking
- Automatic schema migrations
§Connection Pooling
The module uses r2d2 for connection pooling with the following default settings:
- Maximum pool size: 10 connections
- Connection timeout: 30 seconds
- Idle timeout: 10 minutes
These settings can be customized when creating a new pool.
§Error Handling
The module provides a custom Result type alias that standardizes error handling
across database operations. All database operations return this type, which wraps
diesel::result::Error for consistent error handling.
§Migration System
The migration system is built on top of Diesel’s migration framework and supports:
- Automatic migration detection and application
- Version tracking in the database
- Rollback support
- Transaction-safe migrations
Migrations are stored in the src/database/migrations directory and are embedded
into the binary at compile time.
§Usage
use cloacina::runner::DefaultRunner;
use cloacina::database::{DbPool, Result};
// Initialize executor (automatically runs migrations)
let executor = DefaultRunner::new("postgresql://user:pass@localhost/cloacina").await?;
// Manual database access example
let pool = DbPool::builder()
.max_size(15)
.build(ConnectionManager::new("postgresql://user:pass@localhost/cloacina"))?;
// Get a connection from the pool
let conn = pool.get()?;
// Run migrations manually if needed
run_migrations(&mut conn)?;§Public Types
- [
DbPool]: The connection pool type for managing database connections Result<T>: Type alias for database operation results
§Public Functions
- [
run_migrations]: Manually runs pending database migrations
Migrations are automatically applied when using DefaultRunner. For lower-level
database access, migrations can be run manually using run_migrations().
Re-exports§
pub use connection::AnyConnection;pub use connection::AnyPool;pub use connection::BackendType;pub use connection::Database;pub use admin::AdminError;pub use admin::DatabaseAdmin;pub use admin::TenantConfig;pub use admin::TenantCredentials;pub use universal_types::DbBinary;pub use universal_types::DbBool;pub use universal_types::DbTimestamp;pub use universal_types::DbUuid;pub use universal_types::UniversalBinary;pub use universal_types::UniversalBool;pub use universal_types::UniversalTimestamp;pub use universal_types::UniversalUuid;
Modules§
- admin
- Database administration module for multi-tenant operations
- connection
- Database connection management module supporting both PostgreSQL and SQLite.
- schema
- universal_
types - Universal type wrappers for cross-database compatibility
Constants§
- MIGRATIONS
- Embedded migrations for automatic schema management.
- POSTGRES_
MIGRATIONS - Embedded migrations for PostgreSQL.
- SQLITE_
MIGRATIONS - Embedded migrations for SQLite.
Functions§
- run_
migrations_ postgres - Runs pending PostgreSQL database migrations.
- run_
migrations_ sqlite - Runs pending SQLite database migrations.
Type Aliases§
- Result
- Type alias for database operation results.