Implement first query

This commit is contained in:
asonix 2023-09-02 13:52:15 -05:00
parent 5167fe331e
commit 8eb4cda256
4 changed files with 31 additions and 16 deletions

View File

@ -72,6 +72,9 @@ pub(crate) enum RepoError {
#[error("Error in sled")] #[error("Error in sled")]
SledError(#[from] crate::repo::sled::SledError), SledError(#[from] crate::repo::sled::SledError),
#[error("Error in postgres")]
PostgresError(#[from] crate::repo::postgres::PostgresError),
#[error("Upload was already claimed")] #[error("Upload was already claimed")]
AlreadyClaimed, AlreadyClaimed,

View File

@ -1,20 +1,17 @@
mod embedded { mod embedded;
use refinery::embed_migrations;
embed_migrations!("./src/repo/postgres/migrations");
}
mod schema; mod schema;
use diesel::prelude::*;
use diesel_async::{ use diesel_async::{
pooled_connection::{ pooled_connection::{
deadpool::{BuildError, Pool}, deadpool::{BuildError, Pool, PoolError},
AsyncDieselConnectionManager, AsyncDieselConnectionManager,
}, },
AsyncPgConnection, AsyncPgConnection, RunQueryDsl,
}; };
use url::Url; use url::Url;
use super::{BaseRepo, HashRepo}; use super::{BaseRepo, HashRepo, RepoError};
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct PostgresRepo { pub(crate) struct PostgresRepo {
@ -34,7 +31,13 @@ pub(crate) enum ConnectPostgresError {
} }
#[derive(Debug, thiserror::Error)] #[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 { impl PostgresRepo {
pub(crate) async fn connect(postgres_url: Url) -> Result<Self, ConnectPostgresError> { pub(crate) async fn connect(postgres_url: Url) -> Result<Self, ConnectPostgresError> {
@ -64,14 +67,22 @@ impl PostgresRepo {
impl BaseRepo for PostgresRepo {} impl BaseRepo for PostgresRepo {}
/* #[async_trait::async_trait(?Send)]
#[async_trait::async_trait]
impl HashRepo for PostgresRepo { impl HashRepo for PostgresRepo {
async fn size(&self) -> Result<u64, RepoError> { async fn size(&self) -> Result<u64, RepoError> {
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::<i64>(&mut conn)
.await
.map_err(PostgresError::Diesel)?;
Ok(count.try_into().expect("non-negative count"))
} }
} }
*/
impl std::fmt::Debug for PostgresRepo { impl std::fmt::Debug for PostgresRepo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {

View File

@ -0,0 +1,3 @@
use refinery::embed_migrations;
embed_migrations!("./src/repo/postgres/migrations");

View File

@ -24,7 +24,5 @@ pub(crate) fn migration() -> String {
); );
}); });
let s = m.make::<Pg>().to_string(); m.make::<Pg>().to_string()
println!("{s}");
s
} }