Skip to main content

chronon_backend_postgres/
bootstrap.rs

1//! Test helpers for PostgreSQL scheduler store integration.
2
3use chronon_core::Result;
4
5use crate::PostgresSchedulerStore;
6
7/// Resolve a PostgreSQL URL for tests.
8///
9/// Checks `CHRONON_POSTGRES_URL`, then `CHRONON_TEST_POSTGRES_URL`, then
10/// `postgres://localhost/chronon_test`.
11#[must_use]
12pub fn postgres_test_url() -> String {
13    std::env::var("CHRONON_POSTGRES_URL")
14        .or_else(|_| std::env::var("CHRONON_TEST_POSTGRES_URL"))
15        .unwrap_or_else(|_| "postgres://localhost/chronon_test".into())
16}
17
18/// Connect using `CHRONON_POSTGRES_SCHEMA` when set (isolated schema for multi-process E2E).
19///
20/// When `CHRONON_POSTGRES_SKIP_BOOTSTRAP` is set, attaches to an existing schema without DDL.
21///
22/// # Errors
23///
24/// Returns a storage error when the pool cannot be opened or schema bootstrap fails.
25pub async fn postgres_store_from_env() -> Result<PostgresSchedulerStore> {
26    let url = postgres_test_url();
27    if let Ok(schema) = std::env::var("CHRONON_POSTGRES_SCHEMA") {
28        if std::env::var("CHRONON_POSTGRES_SKIP_BOOTSTRAP").is_ok() {
29            PostgresSchedulerStore::attach_isolated(&url, &schema).await
30        } else {
31            PostgresSchedulerStore::connect_isolated(&url, &schema).await
32        }
33    } else {
34        PostgresSchedulerStore::connect(&url).await
35    }
36}