pgrx icon indicating copy to clipboard operation
pgrx copied to clipboard

Running postgres code in test setup

Open einarmo opened this issue 3 years ago • 3 comments

I'm trying to run some SQL in the test setup method, but when I add anything there referencing anything that requires any extern code, compilation fails with undefined reference to .... Is there something I'm missing here? I couldn't find any usage of setup in the code base at all.

einarmo avatar Apr 01 '22 07:04 einarmo

Hi @einarmo ,

I'm not 100% sure what your issue is, do you mean the setup function like here?

#[cfg(test)]
pub mod pg_test {
    pub fn setup(_options: Vec<&str>) {
        // perform one-off initialization when the pg_test framework starts
    }

    pub fn postgresql_conf_options() -> Vec<&'static str> {
        vec![]
    }
}

The setup function is run inside the test framework, not postgres! You can see how #[pg_test] expands to better understand how it works:

https://github.com/tcdi/pgx/blob/4cc6a134e961b8d07ff6be5eef296762d5231345/pgx-macros/src/lib.rs#L95-L108

Hoverbear avatar Apr 07 '22 15:04 Hoverbear

Yep. I got it. I ended up essentially implementing the same thing with a mutex to ensure it is only called once, then calling it from within each test function. It feels like having setup run from within postgres as well would be useful, but a workaround isn't that difficult.

einarmo avatar Apr 07 '22 15:04 einarmo

A postgres-side setup function is an interesting idea. I'd have to think about what that means since the #[pg_test] functions (also) run in parallel.

eeeebbbbrrrr avatar Apr 07 '22 19:04 eeeebbbbrrrr