Skip to content

Commit

Permalink
If there's a "pg_config" described in the environment, use it. (#1020)
Browse files Browse the repository at this point in the history
  • Loading branch information
eeeebbbbrrrr authored Jan 25, 2023
1 parent 501bf99 commit 383bf7d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
13 changes: 6 additions & 7 deletions pgx-pg-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ impl PgConfig {
///
/// It also requires that the `PGX_PG_CONFIG_AS_ENV` variable be set to some value that isn't
/// the string `"false"`.
pub fn from_env() -> Option<Self> {
pub fn from_env() -> eyre::Result<Self> {
if !Self::is_in_environment() {
None
Err(eyre::eyre!("`PgConfig` not described in the environment"))
} else {
const PREFIX: &str = "PGX_PG_CONFIG_";

Expand All @@ -153,7 +153,7 @@ impl PgConfig {
}
}

Some(Self {
Ok(Self {
version: None,
pg_config: None,
known_props: Some(known_props),
Expand Down Expand Up @@ -466,8 +466,7 @@ impl Pgx {
) -> impl std::iter::Iterator<Item = eyre::Result<PgConfig>> {
match (which, PgConfig::is_in_environment()) {
(PgConfigSelector::All, true) | (PgConfigSelector::Environment, _) => {
vec![PgConfig::from_env().ok_or(eyre!("PGX_PG_CONFIG_AS_ENV not found"))]
.into_iter()
vec![PgConfig::from_env()].into_iter()
}

(PgConfigSelector::All, _) => {
Expand Down Expand Up @@ -677,15 +676,15 @@ fn parse_version() {
fn from_empty_env() -> eyre::Result<()> {
// without "PGX_PG_CONFIG_AS_ENV" we can't get one of these
let pg_config = PgConfig::from_env();
assert!(pg_config.is_none());
assert!(pg_config.is_err());

// but now we can
std::env::set_var("PGX_PG_CONFIG_AS_ENV", "true");
std::env::set_var("PGX_PG_CONFIG_VERSION", "PostgresSQL 15.1");
std::env::set_var("PGX_PG_CONFIG_INCLUDEDIR-SERVER", "/path/to/server/headers");
std::env::set_var("PGX_PG_CONFIG_CPPFLAGS", "some cpp flags");

let pg_config = PgConfig::from_env().expect("failed to make a PgConfig from the environment");
let pg_config = PgConfig::from_env().unwrap();
assert_eq!(pg_config.major_version()?, 15, "Major version should match");
assert_eq!(pg_config.minor_version()?, 1, "Minor version should match");
assert_eq!(
Expand Down
14 changes: 12 additions & 2 deletions pgx-pg-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,18 @@ fn main() -> eyre::Result<()> {
.join(", ")
)
})?;
let specific = pgx.get(&found_feat)?;
vec![(found_ver, specific)]

if let Ok(pg_config) = PgConfig::from_env() {
let major_version = pg_config.major_version()?;

if major_version != found_ver {
panic!("Feature flag `pg{found_ver}` does not match version from the environment-described PgConfig (`{major_version}`)")
}
vec![(major_version, pg_config)]
} else {
let specific = pgx.get(&found_feat)?;
vec![(found_ver, specific)]
}
};
std::thread::scope(|scope| {
// This is pretty much either always 1 (normally) or 5 (for releases),
Expand Down

0 comments on commit 383bf7d

Please sign in to comment.