1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Copyright 2025-2026 Colliery Software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//! # 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 pooling
//! - [`schema`]: 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
//!
//! ```rust,ignore
//! 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)?;
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
//! ## 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()`.
use ;
// Re-export connection types from the connection module
pub use ;
// Legacy type aliases - only available when exactly one backend is enabled
pub use ;
pub use ;
// Re-export admin types for tenant management (PostgreSQL only)
pub use ;
/// Type alias for database operation results.
///
/// Standardizes error handling across database operations.
pub type Result<T> = Result;
// Re-export universal types for convenience
pub use ;
/// Embedded migrations for PostgreSQL.
pub const POSTGRES_MIGRATIONS: EmbeddedMigrations =
embed_migrations!;
/// Embedded migrations for SQLite.
pub const SQLITE_MIGRATIONS: EmbeddedMigrations =
embed_migrations!;
/// Embedded migrations for automatic schema management.
///
/// Contains all SQL migration files for setting up the database schema.
/// Note: With dual-backend support, use POSTGRES_MIGRATIONS or SQLITE_MIGRATIONS
/// directly based on your backend. This alias defaults to PostgreSQL.
pub const MIGRATIONS: EmbeddedMigrations = POSTGRES_MIGRATIONS;
/// Embedded migrations alias (defaults to SQLite when postgres not enabled)
pub const MIGRATIONS: EmbeddedMigrations = SQLITE_MIGRATIONS;
/// Runs pending database migrations.
///
/// This function applies any pending migrations to bring the database
/// schema up to date with the current version.
///
/// Note: This function is only available when exactly one database backend
/// is enabled (either postgres or sqlite, but not both). For dual-backend
/// builds, use `run_migrations_postgres` or `run_migrations_sqlite` instead.
///
/// # Arguments
///
/// * `conn` - Mutable reference to a database connection (PostgreSQL or SQLite)
///
/// # Returns
///
/// * `Ok(())` - If migrations complete successfully
/// * `Err(_)` - If migration fails
///
/// # Examples
///
/// ```rust,ignore
/// use cloacina::database::{run_migrations, DbConnection};
///
/// # fn example(mut conn: DbConnection) -> Result<(), diesel::result::Error> {
/// run_migrations(&mut conn)?;
/// # Ok(())
/// # }
/// ```
/// Runs pending PostgreSQL database migrations.
///
/// This function applies any pending migrations to bring the PostgreSQL database
/// schema up to date with the current version. Use this function in dual-backend
/// builds where `run_migrations` is not available.
///
/// # Arguments
///
/// * `conn` - Mutable reference to a PostgreSQL database connection
///
/// # Returns
///
/// * `Ok(())` - If migrations complete successfully
/// * `Err(_)` - If migration fails
/// Runs pending SQLite database migrations.
///
/// This function applies any pending migrations to bring the SQLite database
/// schema up to date with the current version. Use this function in dual-backend
/// builds where `run_migrations` is not available.
///
/// # Arguments
///
/// * `conn` - Mutable reference to a SQLite database connection
///
/// # Returns
///
/// * `Ok(())` - If migrations complete successfully
/// * `Err(_)` - If migration fails