use diesel::{Connection, PgConnection};
use testcontainers::{
Container, GenericImage, ImageExt,
core::{IntoContainerPort, WaitFor},
runners::SyncRunner,
};
use crate::{containers, exit_slot::ExitSlot};
pub type Slot = ExitSlot<Container<GenericImage>>;
pub fn connect(slot: &'static Slot, tag: &str) -> PgConnection {
let port = slot.with(
|| {
containers::sweep_abandoned();
GenericImage::new("postgres", tag)
.with_exposed_port(5432.tcp())
.with_wait_for(WaitFor::message_on_stderr(
"database system is ready to accept connections",
))
.with_env_var("POSTGRES_PASSWORD", "postgres")
.with_env_var("POSTGRES_USER", "postgres")
.with_env_var("POSTGRES_DB", "postgres")
.with_labels(containers::labels("postgres"))
.start()
.unwrap_or_else(|e| panic!("start postgres {tag}: {e}"))
},
|container| {
container
.get_host_port_ipv4(5432)
.expect("get postgres port")
},
);
let url = format!("host=127.0.0.1 port={port} user=postgres password=postgres dbname=postgres");
PgConnection::establish(&url).unwrap_or_else(|e| panic!("connect to postgres {tag}: {e}"))
}