pub mod config;
pub mod error;
pub mod models;
pub mod repo;
pub mod seeds;
pub mod strategy_hash;

use config::DatabaseConfig;
use error::DalError;
use sqlx::PgPool;
use sqlx::postgres::PgPoolOptions;
use tracing::info;

/// Connect to PostgreSQL and run migrations.
pub async fn connect(config: &DatabaseConfig) -> Result<PgPool, DalError> {
    info!(
        max_connections = config.max_connections,
        "connecting to PostgreSQL"
    );

    let pool = PgPoolOptions::new()
        .max_connections(config.max_connections)
        .connect(&config.url)
        .await?;

    info!("running migrations");
    sqlx::migrate!("./migrations").run(&pool).await?;

    info!("database ready");
    Ok(pool)
}
