-
Hey, how can i build a database pool that doesn't commit any operations performed on it? In regular pub type DbConn = PgConnection;
pub type DbPool = r2d2::Pool<ConnectionManager<DbConn>>;
pub fn initialize_test_pool(database_url: &str) -> Result<DbPool, Box<dyn Error>> {
let manager = r2d2::ConnectionManager::<DbConn>::new(database_url);
let pool = r2d2::Pool::builder()
.test_on_check_out(true)
.max_size(1)
.connection_customizer(Box::new(TestCustomizer))
.build(manager)?;
Ok(pool)
} I tried using a pub type DbConn = AsyncPgConnection;
pub type DbPool = Pool<diesel_async::AsyncPgConnection>;
fn establish_test_connection(
url: &str,
) -> BoxFuture<ConnectionResult<DbConn>> {
let fut = async {
DbConn::establish(url)
.await
.map_err(|e| ConnectionError::BadConnection(e.to_string()))
};
fut.boxed()
}
pub async fn init_test_database(url: &str) -> std::io::Result<DbPool> {
let conn = establish_connection(url).await?;
run_migrations(conn).await?;
let mut config = ManagerConfig::default();
config.custom_setup = Box::new(establish_test_connection);
let db_config = AsyncDieselConnectionManager::<DbConn>::new_with_config(url, config);
let pool = Pool::builder(db_config).build().map_err(|e| {
error!("Failed to create database connection pool: {}", e);
std::io::ErrorKind::Other
})?;
Ok(pool)
} However i'm lost how to wrap the connection into a test transaction. Does someone have a solution for this kind of problem? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You need to do just call The That written I'm open to accept a PR that adds a similar construct to the pools provided by diesel-async to make this more discoverable. |
Beta Was this translation helpful? Give feedback.
You need to do just call
AsyncConnection::begin_test_transaction
in yourestablish_test_connection
The
TestCustomizer
does nothing else internally.That written I'm open to accept a PR that adds a similar construct to the pools provided by diesel-async to make this more discoverable.