db-pool 0.6.0

A thread-safe database pool for running database-tied integration tests in parallel
Documentation
fn main() {}

#[cfg(test)]
mod tests {
    #![allow(clippy::needless_return)]

    use db_pool::{
        r#async::{
            DatabasePool, DatabasePoolBuilderTrait, ReusableConnectionPool, SeaORMPostgresBackend,
        },
        PrivilegedPostgresConfig,
    };
    use dotenvy::dotenv;
    use sea_orm::{prelude::*, Set};
    use tokio::sync::OnceCell;
    use tokio_shared_rt::test;

    async fn get_connection_pool() -> ReusableConnectionPool<'static, SeaORMPostgresBackend> {
        static POOL: OnceCell<DatabasePool<SeaORMPostgresBackend>> = OnceCell::const_new();

        let db_pool = POOL
            .get_or_init(|| async {
                dotenv().ok();

                let config = PrivilegedPostgresConfig::from_env().unwrap();

                let backend = SeaORMPostgresBackend::new(
                    config,
                    |opts| {
                        opts.max_connections(10);
                    },
                    |opts| {
                        opts.max_connections(2);
                    },
                    move |conn| {
                        Box::pin(async move {
                            conn.execute_unprepared(
                                "CREATE TABLE book(id SERIAL PRIMARY KEY, title TEXT NOT NULL)",
                            )
                            .await
                            .unwrap();
                        })
                    },
                )
                .await
                .unwrap();

                backend.create_database_pool().await.unwrap()
            })
            .await;

        db_pool.pull_immutable().await
    }

    async fn test() {
        #[derive(Clone, Debug, DeriveEntityModel)]
        #[sea_orm(table_name = "book")]
        pub struct Model {
            #[sea_orm(primary_key)]
            id: i32,
            title: String,
        }

        #[derive(Debug, EnumIter, DeriveRelation)]
        pub enum Relation {}

        impl ActiveModelBehavior for ActiveModel {}

        let conn_pool = get_connection_pool().await;
        let conn = &**conn_pool;

        let book = ActiveModel {
            title: Set("Title".to_owned()),
            ..Default::default()
        };

        book.insert(conn).await.unwrap();

        let count = Entity::find().count(conn).await.unwrap();

        assert_eq!(count, 1);
    }

    #[test(shared)]
    async fn test1() {
        test().await;
    }

    #[test(shared)]
    async fn test2() {
        test().await;
    }
}