From a765d77b536558558df0f49299379c8792eaed3c Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Sun, 11 Apr 2021 22:07:28 +0200 Subject: [PATCH] Centralize the definition of pool type --- src/domain/handler.rs | 8 ++++---- src/domain/sql_tables.rs | 10 ++++++---- src/main.rs | 4 ++-- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/domain/handler.rs b/src/domain/handler.rs index d3614d7..5bb0bbc 100644 --- a/src/domain/handler.rs +++ b/src/domain/handler.rs @@ -4,8 +4,8 @@ use anyhow::{bail, Result}; use async_trait::async_trait; use futures_util::StreamExt; use sea_query::{Expr, MysqlQueryBuilder, Query, SimpleExpr}; -use sqlx::any::AnyPool; use sqlx::Row; +use crate::domain::sql_tables::Pool; #[cfg_attr(test, derive(PartialEq, Eq, Debug))] pub struct BindRequest { @@ -49,11 +49,11 @@ pub trait BackendHandler: Clone + Send { #[derive(Debug, Clone)] pub struct SqlBackendHandler { config: Configuration, - sql_pool: AnyPool, + sql_pool: Pool, } impl SqlBackendHandler { - pub fn new(config: Configuration, sql_pool: AnyPool) -> Self { + pub fn new(config: Configuration, sql_pool: Pool) -> Self { SqlBackendHandler { config, sql_pool } } } @@ -157,7 +157,7 @@ mod tests { #[tokio::test] async fn test_bind_admin() { - let sql_pool = sqlx::any::AnyPoolOptions::new() + let sql_pool = PoolOptions::new() .connect("sqlite::memory:") .await .unwrap(); diff --git a/src/domain/sql_tables.rs b/src/domain/sql_tables.rs index f5b0226..e1db5c1 100644 --- a/src/domain/sql_tables.rs +++ b/src/domain/sql_tables.rs @@ -1,5 +1,7 @@ use sea_query::*; -use sqlx::any::AnyPool; + +pub type Pool = sqlx::any::AnyPool; +pub type PoolOptions = sqlx::any::AnyPoolOptions; #[derive(Iden)] pub enum Users { @@ -30,7 +32,7 @@ pub enum Memberships { GroupId, } -pub async fn init_table(pool: &AnyPool) -> sqlx::Result<()> { +pub async fn init_table(pool: &Pool) -> sqlx::Result<()> { // SQLite needs this pragma to be turned on. Other DB might not understand this, so ignore the // error. let _ = sqlx::query("PRAGMA foreign_keys = ON").execute(pool).await; @@ -126,7 +128,7 @@ mod tests { #[actix_rt::test] async fn test_init_table() { - let sql_pool = sqlx::any::AnyPoolOptions::new() + let sql_pool = PoolOptions::new() .connect("sqlite::memory:") .await .unwrap(); @@ -144,7 +146,7 @@ mod tests { #[actix_rt::test] async fn test_already_init_table() { - let sql_pool = sqlx::any::AnyPoolOptions::new() + let sql_pool = PoolOptions::new() .connect("sqlite::memory:") .await .unwrap(); diff --git a/src/main.rs b/src/main.rs index 64f26a6..8d7a66d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,13 +2,13 @@ use crate::infra::configuration::Configuration; use anyhow::Result; use futures_util::TryFutureExt; use log::*; -use sqlx::any::AnyPoolOptions; +use crate::domain::sql_tables::PoolOptions; mod domain; mod infra; async fn run_server(config: Configuration) -> Result<()> { - let sql_pool = AnyPoolOptions::new() + let sql_pool = PoolOptions::new() .max_connections(5) .connect(&config.database_url) .await?;