diff --git a/src/repo.rs b/src/repo.rs index 51553f4..4c5d923 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -72,6 +72,9 @@ pub(crate) enum RepoError { #[error("Error in sled")] SledError(#[from] crate::repo::sled::SledError), + #[error("Error in postgres")] + PostgresError(#[from] crate::repo::postgres::PostgresError), + #[error("Upload was already claimed")] AlreadyClaimed, diff --git a/src/repo/postgres.rs b/src/repo/postgres.rs index f4e71da..c66303c 100644 --- a/src/repo/postgres.rs +++ b/src/repo/postgres.rs @@ -1,20 +1,17 @@ -mod embedded { - use refinery::embed_migrations; - - embed_migrations!("./src/repo/postgres/migrations"); -} +mod embedded; mod schema; +use diesel::prelude::*; use diesel_async::{ pooled_connection::{ - deadpool::{BuildError, Pool}, + deadpool::{BuildError, Pool, PoolError}, AsyncDieselConnectionManager, }, - AsyncPgConnection, + AsyncPgConnection, RunQueryDsl, }; use url::Url; -use super::{BaseRepo, HashRepo}; +use super::{BaseRepo, HashRepo, RepoError}; #[derive(Clone)] pub(crate) struct PostgresRepo { @@ -34,7 +31,13 @@ pub(crate) enum ConnectPostgresError { } #[derive(Debug, thiserror::Error)] -enum PostgresError {} +pub(crate) enum PostgresError { + #[error("Error in db pool")] + Pool(#[source] PoolError), + + #[error("Error in database")] + Diesel(#[source] diesel::result::Error), +} impl PostgresRepo { pub(crate) async fn connect(postgres_url: Url) -> Result { @@ -64,14 +67,22 @@ impl PostgresRepo { impl BaseRepo for PostgresRepo {} -/* -#[async_trait::async_trait] +#[async_trait::async_trait(?Send)] impl HashRepo for PostgresRepo { async fn size(&self) -> Result { - let conn = self.pool.get().await.map_err(PostgresError::from)?; + use schema::hashes::dsl::*; + + let mut conn = self.pool.get().await.map_err(PostgresError::Pool)?; + + let count = hashes + .count() + .get_result::(&mut conn) + .await + .map_err(PostgresError::Diesel)?; + + Ok(count.try_into().expect("non-negative count")) } } -*/ impl std::fmt::Debug for PostgresRepo { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { diff --git a/src/repo/postgres/embedded.rs b/src/repo/postgres/embedded.rs new file mode 100644 index 0000000..482a885 --- /dev/null +++ b/src/repo/postgres/embedded.rs @@ -0,0 +1,3 @@ +use refinery::embed_migrations; + +embed_migrations!("./src/repo/postgres/migrations"); diff --git a/src/repo/postgres/migrations/V0002__create_variants.rs b/src/repo/postgres/migrations/V0002__create_variants.rs index 8377ddc..c49c62c 100644 --- a/src/repo/postgres/migrations/V0002__create_variants.rs +++ b/src/repo/postgres/migrations/V0002__create_variants.rs @@ -24,7 +24,5 @@ pub(crate) fn migration() -> String { ); }); - let s = m.make::().to_string(); - println!("{s}"); - s + m.make::().to_string() }