+async fn build_db_pool_settings_opt(
+ settings: Option<&Settings>,
+) -> Result<ActualDbPool, LemmyError> {
+ let db_url = get_database_url(settings);
+ let pool_size = settings.map(|s| s.database.pool_size).unwrap_or(5);
+ // We only support TLS with sslmode=require currently
+ let tls_enabled = db_url.contains("sslmode=require");
+ let manager = if tls_enabled {
+ // diesel-async does not support any TLS connections out of the box, so we need to manually
+ // provide a setup function which handles creating the connection
+ AsyncDieselConnectionManager::<AsyncPgConnection>::new_with_setup(&db_url, establish_connection)
+ } else {
+ AsyncDieselConnectionManager::<AsyncPgConnection>::new(&db_url)
+ };
+ let pool = Pool::builder(manager)
+ .max_size(pool_size)
+ .wait_timeout(POOL_TIMEOUT)
+ .create_timeout(POOL_TIMEOUT)
+ .recycle_timeout(POOL_TIMEOUT)
+ .runtime(Runtime::Tokio1)
+ .build()?;
+
+ // If there's no settings, that means its a unit test, and migrations need to be run
+ if settings.is_none() {
+ run_migrations(&db_url);
+ }
+
+ Ok(pool)
+}
+
+fn establish_connection(config: &str) -> BoxFuture<ConnectionResult<AsyncPgConnection>> {
+ let fut = async {
+ let rustls_config = rustls::ClientConfig::builder()
+ .with_safe_defaults()
+ .with_custom_certificate_verifier(Arc::new(NoCertVerifier {}))
+ .with_no_client_auth();
+
+ let tls = tokio_postgres_rustls::MakeRustlsConnect::new(rustls_config);
+ let (client, conn) = tokio_postgres::connect(config, tls)
+ .await
+ .map_err(|e| ConnectionError::BadConnection(e.to_string()))?;
+ tokio::spawn(async move {
+ if let Err(e) = conn.await {
+ error!("Database connection failed: {e}");
+ }
+ });
+ AsyncPgConnection::try_from(client).await