Repository: diesel-rs/diesel
File: diesel/src/r2d2.rs (Rust)

Code before:
//!     // Refer to the `r2d2` documentation for more methods to use
//!     // when building a connection pool
//!     Pool::builder()
//!         .max_size(pool_size)
//!         .test_on_check_out(true)
//!         .build(manager)
//!         .expect("Could not build connection pool")
//! }
//!
//! pub fn create_user(conn: &mut PgConnection, user_name: &str) -> Result<usize, Error> {
//!     use schema::users::dsl::*;
//!
//!     diesel::insert_into(users)
//!         .values(name.eq(user_name))
//!         .execute(conn)
//! }
//!
//! pub fn setup_user_table(conn: &mut PgConnection) {
//!     diesel::sql_query("DROP TABLE IF EXISTS users CASCADE").execute(conn).unwrap();
//!     diesel::sql_query("CREATE TABLE users (
//!         id SERIAL PRIMARY KEY,
//!         name VARCHAR NOT NULL
//!     )")
//!         .execute(conn)
//!         .unwrap();
//! }
//!
//! pub fn delete_user_table(conn: &mut PgConnection) {
//!     diesel::sql_query("DROP TABLE IF EXISTS users CASCADE").execute(conn).unwrap();
//! }
//!
//! fn main() {
//!     let pool_size = 1;
//!     let pool = get_connection_pool(pool_size);
//!     setup_user_table(&mut pool.get().unwrap());
//!
//!     let mut threads = vec![];
//!     let max_users_to_create = 1;
//!
//!     for i in 0..max_users_to_create {
//!         let pool = pool.clone();
//!         threads.push(thread::spawn({
//!             move || {
//!                 let conn = &mut pool.get().unwrap();
//!                 let name = format!("Person {}", i);
//!                 create_user(conn, &name).unwrap();
//!             }
//!         }))
//!     }
//!
//!     for handle in threads {

Review comment (suggestion):
Request to reviewers: whether or not I should try and emulate [this logic](https://github.com/diesel-rs/diesel/blob/c6dcd6ba116b86758febf5594c03bfed13aadb3c/diesel/src/doctest_setup.rs#L16) -- i.e. that nothing is ever actually committed.

On one hand, I believe that makes a lot of sense; on the other, since we're using a connection pool (even in a contrived example here where there's only 1 connection), I'm not sure how best to do this -- and furthermore, it's only really a problem if tests run in parallel which I don't think they do but I'm not sure if we want to make that assumption (assuming the Rust ecosystem/some crate one day adds support for running tests in parallel)

Another thing I could do is just run raw SQL queries with `diesel::sql_query` and create and entirely new table `users_table_for_r2d2_module_level_doctest` or something

Code after:
//! # impl CustomizeConnection<DbConnection, R2D2Error> for SetupUserTableCustomizer
//! # {
//! #     fn on_acquire(&self, conn: &mut DbConnection) -> Result<(), R2D2Error> {
//! #         setup_database(conn);
//! #         Ok(())
//! #     }
//! # }
//!
//! pub fn get_connection_pool() -> Pool<ConnectionManager<DbConnection>> {
//!     let url = database_url_for_env();
//!     let manager = ConnectionManager::<DbConnection>::new(url);
//!     // Refer to the `r2d2` documentation for more methods to use
//!     // when building a connection pool
//!     Pool::builder()
//! #         .max_size(1)
//!         .test_on_check_out(true)
//! #         .connection_customizer(Box::new(SetupUserTableCustomizer))
//!         .build(manager)
//!         .expect("Could not build connection pool")
//! }
//!
//! pub fn create_user(conn: &mut DbConnection, user_name: &str) -> Result<usize, Error> {
//!     use schema::users::dsl::*;
//!
//!     diesel::insert_into(users)
//!         .values(name.eq(user_name))
//!         .execute(conn)
//! }
//!
//! fn main() {
//!     let pool = get_connection_pool();
//!     let mut threads = vec![];
//!     let max_users_to_create = 1;
//!
//!     for i in 0..max_users_to_create {
//!         let pool = pool.clone();
//!         threads.push(thread::spawn({
//!             move || {
//!                 let conn = &mut pool.get().unwrap();
//!                 let name = format!("Person {}", i);
//!                 create_user(conn, &name).unwrap();
//!             }
//!         }))
//!     }
//!
//!     for handle in threads {
//!         handle.join().unwrap();
//!     }
//! }
//! ```
//!