Skip to main content

agentforge_db/
pool.rs

1use agentforge_core::AgentForgeError;
2use sqlx::postgres::PgPoolOptions;
3
4pub use sqlx::PgPool;
5
6/// Create a PostgreSQL connection pool.
7pub async fn create_pool(database_url: &str) -> Result<PgPool, AgentForgeError> {
8    PgPoolOptions::new()
9        .max_connections(20)
10        .min_connections(2)
11        .acquire_timeout(std::time::Duration::from_secs(5))
12        .connect(database_url)
13        .await
14        .map_err(|e| AgentForgeError::DatabaseError(format!("Failed to connect: {e}")))
15}
16
17/// Run all pending migrations from the migrations/ directory.
18pub async fn run_migrations(pool: &PgPool) -> Result<(), AgentForgeError> {
19    sqlx::migrate!("./migrations")
20        .run(pool)
21        .await
22        .map_err(|e| AgentForgeError::DatabaseError(format!("Migration failed: {e}")))
23}