diff --git a/Cargo.toml b/Cargo.toml index 122a2c0..e1de720 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,6 @@ axum = "0.7" askama = "0.12" askama_axum = "0.4" tower-http = { version = "0.5", features = ["fs"] } -r2d2_postgres = "0.18" -deadpool-r2d2 = {version = "0.3", features = ["rt_tokio_1"]} -deadpool = "0.10" +deadpool-postgres = { version = "0.12", features = ["rt_tokio_1"] } +tokio-postgres = { version = "0.7", features = ["with-chrono-0_4"] } thiserror = "1.0" \ No newline at end of file diff --git a/src/db.rs b/src/db.rs index e802078..0d6b085 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,47 +1,31 @@ use { - deadpool, deadpool_r2d2::Runtime, thiserror::Error, + deadpool_postgres::{CreatePoolError, Pool, Runtime}, + thiserror::Error, + tokio_postgres::NoTls, }; -type PgManager = deadpool_r2d2::Manager< - r2d2_postgres::PostgresConnectionManager, ->; -type PgPool = deadpool_r2d2::Pool; - #[derive(Error, Debug)] pub enum Error { #[error("Could not initialize DB connection pool.")] - ConnectionPoolError(#[from] deadpool::managed::BuildError), - #[error("Error with Postgres database")] - PostrgesError(#[from] r2d2_postgres::postgres::Error) + ConnectionPoolError(#[from] CreatePoolError), } #[derive(Clone)] pub struct Database { - pg_pool: PgPool, + pg_pool: Pool, } impl Database { - pub fn create_pool(connection_url: &str, max_size: usize) -> Result { - let pg_config: r2d2_postgres::postgres::Config = connection_url.parse()?; - let r2d2_manager = r2d2_postgres::PostgresConnectionManager::new( - pg_config, - r2d2_postgres::postgres::NoTls, - ); - let manager = PgManager::new(r2d2_manager, Runtime::Tokio1); - let pg_pool = PgPool::builder(manager).max_size(max_size).build()?; + pub fn create_pool(connection_url: &str) -> Result { + let mut config = deadpool_postgres::Config::new(); + config.url = Some(connection_url.to_string()); + let pg_pool = config.create_pool(Some(Runtime::Tokio1), NoTls)?; Ok(Database { pg_pool }) } pub async fn log_num_users(&self) -> usize { - self.pg_pool - .get() - .await - .unwrap() - .interact(|conn| { - let results = conn.query("SELECT * FROM users;", &[]).unwrap(); - results.len() - }) - .await - .unwrap() + let client = self.pg_pool.get().await.unwrap(); + let results = client.query("SELECT * FROM users;", &[]).await.unwrap(); + results.len() } } diff --git a/src/main.rs b/src/main.rs index 074fa66..928765a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ fn main() { async fn localhub_main() -> Result<(), Error> { let config = get_config()?; - let db_pool = Database::create_pool(&config.database_url, 2)?; + let db_pool = Database::create_pool(&config.database_url)?; let app = app::routes() .with_state(db_pool)