use sqlx::{migrate, PgPool};

pub mod data;
mod deploy_config_mgr;
mod error;
mod redis_mgr;
mod sql_op_player_data;
pub use deploy_config_mgr::DbDeployConfigMgr;
pub use error::DbError;
pub use redis_mgr::RedisMgr;
pub use sql_op_player_data::SqlOpPlayerData;

pub async fn connect(connection_string: &str) -> Result<PgPool, DbError> {
    let pool = PgPool::connect(connection_string)
        .await
        .map_err(|_| DbError::ConnectionFailed)?;

    migrate!("./migrations")
        .run(&pool)
        .await
        .map_err(|err| DbError::from(err))?;

    Ok(pool)
}
