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 compiles the package version into the sql-generator, which
performs the replacement at runtime.
  • Loading branch information
JamesGuthrie committed Jan 31, 2022
1 parent f3c1c25 commit 624a1b3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
9 changes: 4 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_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,9 @@ 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())
let metadata = MetadataCommand::new().exec()?;
let root_package = metadata.root_package().ok_or(eyre!("no root package found"))?;
Ok(root_package.version.to_string())
} else {
Ok(v)
}
Expand Down
9 changes: 6 additions & 3 deletions pgx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,17 @@ macro_rules! pg_sql_graph_magic {
> {
use pgx::datum::sql_entity_graph::reexports::eyre::WrapErr;
use std::convert::TryFrom;
let package_version = env!("CARGO_PKG_VERSION");
let context = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/",
env!("CARGO_CRATE_NAME"),
".control"
));
let control_file = pgx::datum::sql_entity_graph::ControlFile::try_from(context)
.wrap_err_with(|| "Could not parse control file, is it valid?")?;
))
.replace("@CARGO_VERSION@", package_version);
let control_file =
pgx::datum::sql_entity_graph::ControlFile::try_from(context.as_str())
.wrap_err_with(|| "Could not parse control file, is it valid?")?;
Ok(control_file)
}
};
Expand Down

0 comments on commit 624a1b3

Please sign in to comment.