Centralize the definition of pool type

This commit is contained in:
Valentin Tolmer 2021-04-11 22:07:28 +02:00
parent 71045b08fe
commit a765d77b53
3 changed files with 12 additions and 10 deletions

View File

@ -4,8 +4,8 @@ use anyhow::{bail, Result};
use async_trait::async_trait; use async_trait::async_trait;
use futures_util::StreamExt; use futures_util::StreamExt;
use sea_query::{Expr, MysqlQueryBuilder, Query, SimpleExpr}; use sea_query::{Expr, MysqlQueryBuilder, Query, SimpleExpr};
use sqlx::any::AnyPool;
use sqlx::Row; use sqlx::Row;
use crate::domain::sql_tables::Pool;
#[cfg_attr(test, derive(PartialEq, Eq, Debug))] #[cfg_attr(test, derive(PartialEq, Eq, Debug))]
pub struct BindRequest { pub struct BindRequest {
@ -49,11 +49,11 @@ pub trait BackendHandler: Clone + Send {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SqlBackendHandler { pub struct SqlBackendHandler {
config: Configuration, config: Configuration,
sql_pool: AnyPool, sql_pool: Pool,
} }
impl SqlBackendHandler { impl SqlBackendHandler {
pub fn new(config: Configuration, sql_pool: AnyPool) -> Self { pub fn new(config: Configuration, sql_pool: Pool) -> Self {
SqlBackendHandler { config, sql_pool } SqlBackendHandler { config, sql_pool }
} }
} }
@ -157,7 +157,7 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_bind_admin() { async fn test_bind_admin() {
let sql_pool = sqlx::any::AnyPoolOptions::new() let sql_pool = PoolOptions::new()
.connect("sqlite::memory:") .connect("sqlite::memory:")
.await .await
.unwrap(); .unwrap();

View File

@ -1,5 +1,7 @@
use sea_query::*; use sea_query::*;
use sqlx::any::AnyPool;
pub type Pool = sqlx::any::AnyPool;
pub type PoolOptions = sqlx::any::AnyPoolOptions;
#[derive(Iden)] #[derive(Iden)]
pub enum Users { pub enum Users {
@ -30,7 +32,7 @@ pub enum Memberships {
GroupId, 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 // SQLite needs this pragma to be turned on. Other DB might not understand this, so ignore the
// error. // error.
let _ = sqlx::query("PRAGMA foreign_keys = ON").execute(pool).await; let _ = sqlx::query("PRAGMA foreign_keys = ON").execute(pool).await;
@ -126,7 +128,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn test_init_table() { async fn test_init_table() {
let sql_pool = sqlx::any::AnyPoolOptions::new() let sql_pool = PoolOptions::new()
.connect("sqlite::memory:") .connect("sqlite::memory:")
.await .await
.unwrap(); .unwrap();
@ -144,7 +146,7 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn test_already_init_table() { async fn test_already_init_table() {
let sql_pool = sqlx::any::AnyPoolOptions::new() let sql_pool = PoolOptions::new()
.connect("sqlite::memory:") .connect("sqlite::memory:")
.await .await
.unwrap(); .unwrap();

View File

@ -2,13 +2,13 @@ use crate::infra::configuration::Configuration;
use anyhow::Result; use anyhow::Result;
use futures_util::TryFutureExt; use futures_util::TryFutureExt;
use log::*; use log::*;
use sqlx::any::AnyPoolOptions; use crate::domain::sql_tables::PoolOptions;
mod domain; mod domain;
mod infra; mod infra;
async fn run_server(config: Configuration) -> Result<()> { async fn run_server(config: Configuration) -> Result<()> {
let sql_pool = AnyPoolOptions::new() let sql_pool = PoolOptions::new()
.max_connections(5) .max_connections(5)
.connect(&config.database_url) .connect(&config.database_url)
.await?; .await?;