Skip to content

Commit a48b2e8

Browse files
committed
Correctly transform control file version number in sql entity graph
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.
1 parent f3c1c25 commit a48b2e8

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo-pgx/src/command/install.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
use cargo_metadata::MetadataCommand;
99
use colored::Colorize;
1010
use eyre::{eyre, WrapErr};
11-
use pgx_utils::get_target_dir;
11+
use pgx_utils::{get_package_version, get_target_dir};
1212
use pgx_utils::pg_config::{PgConfig, Pgx};
1313
use std::path::PathBuf;
1414
use std::process::{Command, Stdio};
@@ -326,10 +326,7 @@ pub(crate) fn get_version() -> eyre::Result<String> {
326326
match get_property("default_version")? {
327327
Some(v) => {
328328
if v == "@CARGO_VERSION@" {
329-
let metadata = MetadataCommand::new()
330-
.exec()
331-
.wrap_err("failed to parse Cargo.toml")?;
332-
Ok(metadata.root_package().unwrap().version.to_string())
329+
get_package_version()
333330
} else {
334331
Ok(v)
335332
}

pgx-utils/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ documentation = "https://docs.rs/pgx-utils"
1111
readme = "README.md"
1212

1313
[dependencies]
14+
cargo_metadata = "0.14.0"
1415
colored = "2.0.0"
1516
convert_case = "0.4.0"
1617
dirs = "4.0.0"

pgx-utils/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright 2020 ZomboDB, LLC <[email protected]>. All rights reserved. Use of this source code is
22
// governed by the MIT license that can be found in the LICENSE file.
33

4+
use cargo_metadata::MetadataCommand;
45
use crate::{pg_config::PgConfig, sql_entity_graph::PositioningRef};
56
use colored::Colorize;
67
use eyre::{eyre, WrapErr};
@@ -533,6 +534,12 @@ pub fn anonymonize_lifetimes(value: &mut syn::Type) {
533534
}
534535
}
535536

537+
pub fn get_package_version() -> eyre::Result<String> {
538+
let metadata = MetadataCommand::new().exec()?;
539+
let root_package = metadata.root_package().ok_or(eyre!("no root package found"))?;
540+
Ok(root_package.version.to_string())
541+
}
542+
536543
#[cfg(test)]
537544
mod tests {
538545
use crate::{parse_extern_attributes, ExternArgs};

pgx/src/datum/sql_entity_graph/control_file.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::{SqlGraphEntity, SqlGraphIdentifier, ToSql};
22
use core::convert::TryFrom;
33
use std::collections::HashMap;
44
use tracing_error::SpanTrace;
5+
use pgx_utils::get_package_version;
56

67
/// The parsed contents of a `.control` file.
78
///
@@ -60,13 +61,21 @@ impl ControlFile {
6061
context: SpanTrace::capture(),
6162
})?
6263
.to_string(),
63-
default_version: temp
64-
.get("default_version")
65-
.ok_or(ControlFileError::MissingField {
64+
default_version: {
65+
let default_version = temp.get("default_version").ok_or(ControlFileError::MissingField {
6666
field: "default_version",
6767
context: SpanTrace::capture(),
68-
})?
69-
.to_string(),
68+
})?;
69+
if *default_version == "@CARGO_VERSION@" {
70+
get_package_version().map_err(|_| {
71+
ControlFileError::CannotResolveVersion {
72+
context: SpanTrace::capture(),
73+
}
74+
})?
75+
} else {
76+
Ok(String::from(*default_version))?
77+
}
78+
},
7079
module_pathname: temp
7180
.get("module_pathname")
7281
.ok_or(ControlFileError::MissingField {
@@ -106,6 +115,9 @@ pub enum ControlFileError {
106115
field: &'static str,
107116
context: SpanTrace,
108117
},
118+
CannotResolveVersion {
119+
context: SpanTrace
120+
}
109121
}
110122

111123
impl std::fmt::Display for ControlFileError {
@@ -115,6 +127,10 @@ impl std::fmt::Display for ControlFileError {
115127
write!(f, "Missing field in control file! Please add `{}`.", field)?;
116128
context.fmt(f)?;
117129
}
130+
ControlFileError::CannotResolveVersion { context } => {
131+
write!(f, "Unable to resolve version placeholder in control file!")?;
132+
context.fmt(f)?;
133+
}
118134
};
119135
Ok(())
120136
}

0 commit comments

Comments
 (0)