20 lines
745 B
Rust
20 lines
745 B
Rust
use crate::config::AppConfig;
|
|
use sqlx::postgres::{PgPool, PgPoolOptions};
|
|
use std::time::Duration;
|
|
|
|
/// 初始化数据库连接池
|
|
pub async fn init_pool(config: &AppConfig) -> Result<PgPool, sqlx::Error> {
|
|
PgPoolOptions::new()
|
|
.max_connections(config.db_max_connections)
|
|
.min_connections(config.db_min_connections)
|
|
.acquire_timeout(Duration::from_secs(3)) // 获取连接超时时间
|
|
.connect(&config.database_url)
|
|
.await
|
|
}
|
|
|
|
// (可选) 可以在应用启动时自动运行迁移
|
|
// pub async fn run_migrations(pool: &PgPool) -> Result<(), sqlx::migrate::MigrateError> {
|
|
// // 这要求你在项目根目录有 `migrations/` 文件夹
|
|
// sqlx::migrate!("./migrations").run(pool).await
|
|
// }
|