Skip to content

Commit

Permalink
Correctly transform control file version number in sql entity graph
Browse files Browse the repository at this point in the history
Commit f00c335 added the ability to use '@CARGO_VERSION@' as a
placeholder for 'default_version' in the extension's .control file.

That change missed the fact that the contents of the .control file are
loaded into the root node of the sql entity graph. This means that when
using the placeholder, the default_version in the sql entity graph's
control file is '@CARGO_VERSION@'.

This change replaces the placeholder with the package version when
executing the compiled sql-generator.
  • Loading branch information
JamesGuthrie committed Jan 28, 2022
1 parent f3c1c25 commit a48b2e8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions cargo-pgx/src/command/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
use cargo_metadata::MetadataCommand;
use colored::Colorize;
use eyre::{eyre, WrapErr};
use pgx_utils::get_target_dir;
use pgx_utils::{get_package_version, get_target_dir};
use pgx_utils::pg_config::{PgConfig, Pgx};
use std::path::PathBuf;
use std::process::{Command, Stdio};
Expand Down Expand Up @@ -326,10 +326,7 @@ pub(crate) fn get_version() -> eyre::Result<String> {
match get_property("default_version")? {
Some(v) => {
if v == "@CARGO_VERSION@" {
let metadata = MetadataCommand::new()
.exec()
.wrap_err("failed to parse Cargo.toml")?;
Ok(metadata.root_package().unwrap().version.to_string())
get_package_version()
} else {
Ok(v)
}
Expand Down
1 change: 1 addition & 0 deletions pgx-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ documentation = "https://docs.rs/pgx-utils"
readme = "README.md"

[dependencies]
cargo_metadata = "0.14.0"
colored = "2.0.0"
convert_case = "0.4.0"
dirs = "4.0.0"
Expand Down
7 changes: 7 additions & 0 deletions pgx-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020 ZomboDB, LLC <[email protected]>. All rights reserved. Use of this source code is
// governed by the MIT license that can be found in the LICENSE file.

use cargo_metadata::MetadataCommand;
use crate::{pg_config::PgConfig, sql_entity_graph::PositioningRef};
use colored::Colorize;
use eyre::{eyre, WrapErr};
Expand Down Expand Up @@ -533,6 +534,12 @@ pub fn anonymonize_lifetimes(value: &mut syn::Type) {
}
}

pub fn get_package_version() -> eyre::Result<String> {
let metadata = MetadataCommand::new().exec()?;
let root_package = metadata.root_package().ok_or(eyre!("no root package found"))?;
Ok(root_package.version.to_string())
}

#[cfg(test)]
mod tests {
use crate::{parse_extern_attributes, ExternArgs};
Expand Down
26 changes: 21 additions & 5 deletions pgx/src/datum/sql_entity_graph/control_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{SqlGraphEntity, SqlGraphIdentifier, ToSql};
use core::convert::TryFrom;
use std::collections::HashMap;
use tracing_error::SpanTrace;
use pgx_utils::get_package_version;

/// The parsed contents of a `.control` file.
///
Expand Down Expand Up @@ -60,13 +61,21 @@ impl ControlFile {
context: SpanTrace::capture(),
})?
.to_string(),
default_version: temp
.get("default_version")
.ok_or(ControlFileError::MissingField {
default_version: {
let default_version = temp.get("default_version").ok_or(ControlFileError::MissingField {
field: "default_version",
context: SpanTrace::capture(),
})?
.to_string(),
})?;
if *default_version == "@CARGO_VERSION@" {
get_package_version().map_err(|_| {
ControlFileError::CannotResolveVersion {
context: SpanTrace::capture(),
}
})?
} else {
Ok(String::from(*default_version))?
}
},
module_pathname: temp
.get("module_pathname")
.ok_or(ControlFileError::MissingField {
Expand Down Expand Up @@ -106,6 +115,9 @@ pub enum ControlFileError {
field: &'static str,
context: SpanTrace,
},
CannotResolveVersion {
context: SpanTrace
}
}

impl std::fmt::Display for ControlFileError {
Expand All @@ -115,6 +127,10 @@ impl std::fmt::Display for ControlFileError {
write!(f, "Missing field in control file! Please add `{}`.", field)?;
context.fmt(f)?;
}
ControlFileError::CannotResolveVersion { context } => {
write!(f, "Unable to resolve version placeholder in control file!")?;
context.fmt(f)?;
}
};
Ok(())
}
Expand Down

0 comments on commit a48b2e8

Please sign in to comment.