Skip to content

Commit 718bd8c

Browse files
committed
fix: always use the build version of postgresql when the bundled feature is enabled to avoid network access
1 parent a7bd101 commit 718bd8c

File tree

9 files changed

+56
-28
lines changed

9 files changed

+56
-28
lines changed

examples/portal_corp_extension/src/main.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,21 @@ async fn main() -> Result<()> {
1818
tracing_subscriber::fmt().compact().init();
1919

2020
info!("Installing PostgreSQL");
21+
let postgresql_version = VersionReq::parse("=16.4.0")?;
2122
let settings = Settings {
22-
version: VersionReq::parse("=16.4.0")?,
23+
version: postgresql_version.clone(),
2324
..Default::default()
2425
};
2526
let mut postgresql = PostgreSQL::new(settings);
2627
postgresql.setup().await?;
2728

29+
let settings = postgresql.settings();
30+
// Skip the test if the PostgreSQL version does not match; when testing with the 'bundled'
31+
// feature, the version may vary and the test will fail.
32+
if settings.version != postgresql_version {
33+
return Ok(());
34+
}
35+
2836
info!("Installing the vector extension from PortalCorp");
2937
postgresql_extensions::install(
3038
postgresql.settings(),

postgresql_embedded/src/postgresql.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use crate::error::Error::{DatabaseInitializationError, DatabaseStartError, DatabaseStopError};
22
use crate::error::Result;
33
use crate::settings::{BOOTSTRAP_DATABASE, BOOTSTRAP_SUPERUSER, Settings};
4+
use postgresql_archive::extract;
5+
#[cfg(not(feature = "bundled"))]
6+
use postgresql_archive::get_archive;
47
use postgresql_archive::get_version;
58
use postgresql_archive::{ExactVersion, ExactVersionReq};
6-
use postgresql_archive::{extract, get_archive};
79
#[cfg(feature = "tokio")]
810
use postgresql_commands::AsyncCommandExecutor;
911
use postgresql_commands::CommandBuilder;
@@ -179,6 +181,11 @@ impl PostgreSQL {
179181
/// returned.
180182
#[instrument(skip(self))]
181183
async fn install(&mut self) -> Result<()> {
184+
#[cfg(feature = "bundled")]
185+
{
186+
self.settings.version = crate::settings::ARCHIVE_VERSION.clone();
187+
}
188+
182189
debug!(
183190
"Starting installation process for version {}",
184191
self.settings.version
@@ -201,28 +208,21 @@ impl PostgreSQL {
201208

202209
let url = &self.settings.releases_url;
203210

211+
// When the `bundled` feature is enabled, use the bundled archive instead of downloading it
212+
// from the internet.
204213
#[cfg(feature = "bundled")]
205-
// If the requested version is the same as the version of the bundled archive, use the bundled
206-
// archive. This avoids downloading the archive in environments where internet access is
207-
// restricted or undesirable.
208-
let (version, bytes) = if *crate::settings::ARCHIVE_VERSION == self.settings.version {
214+
let bytes = {
209215
debug!("Using bundled installation archive");
210-
(
211-
self.settings.version.clone(),
212-
crate::settings::ARCHIVE.to_vec(),
213-
)
214-
} else {
215-
let (version, bytes) = get_archive(url, &self.settings.version).await?;
216-
(version.exact_version_req()?, bytes)
216+
crate::settings::ARCHIVE.to_vec()
217217
};
218218

219219
#[cfg(not(feature = "bundled"))]
220-
let (version, bytes) = {
220+
let bytes = {
221221
let (version, bytes) = get_archive(url, &self.settings.version).await?;
222-
(version.exact_version_req()?, bytes)
222+
self.settings.version = version.exact_version_req()?;
223+
bytes
223224
};
224225

225-
self.settings.version = version;
226226
extract(url, &bytes, &self.settings.installation_dir).await?;
227227

228228
debug!(

postgresql_embedded/src/settings.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::error::{Error, Result};
22
use postgresql_archive::VersionReq;
3+
#[cfg(feature = "bundled")]
4+
use postgresql_archive::{ExactVersionReq, Version};
35
use rand::Rng;
46
use rand::distr::Alphanumeric;
57
use std::collections::HashMap;
@@ -8,8 +10,6 @@ use std::env::{current_dir, home_dir};
810
use std::ffi::OsString;
911
use std::path::PathBuf;
1012
#[cfg(feature = "bundled")]
11-
use std::str::FromStr;
12-
#[cfg(feature = "bundled")]
1313
use std::sync::LazyLock;
1414
use std::time::Duration;
1515
use url::Url;
@@ -18,7 +18,8 @@ use url::Url;
1818
#[expect(clippy::unwrap_used)]
1919
pub(crate) static ARCHIVE_VERSION: LazyLock<VersionReq> = LazyLock::new(|| {
2020
let version_string = include_str!(concat!(std::env!("OUT_DIR"), "/postgresql.version"));
21-
let version_req = VersionReq::from_str(&format!("={version_string}")).unwrap();
21+
let version = Version::parse(version_string).unwrap();
22+
let version_req = version.exact_version_req().unwrap();
2223
tracing::debug!("Bundled installation archive version {version_string}");
2324
version_req
2425
});

postgresql_embedded/tests/blocking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use test_log::test;
77

88
#[cfg(feature = "blocking")]
99
#[test]
10-
fn test_lifecycle() -> Result<()> {
10+
fn test_embedded_blocking_lifecycle() -> Result<()> {
1111
let mut postgresql = PostgreSQL::default();
1212
let settings = postgresql.settings();
1313

postgresql_embedded/tests/postgresql.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async fn lifecycle() -> Result<()> {
3434
}
3535

3636
#[test(tokio::test)]
37-
async fn test_lifecycle() -> Result<()> {
37+
async fn test_embedded_async_lifecycle() -> Result<()> {
3838
lifecycle().await
3939
}
4040

postgresql_extensions/tests/blocking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn test_get_available_extensions() -> anyhow::Result<()> {
2222

2323
#[cfg(all(target_os = "linux", feature = "blocking", feature = "tensor-chord"))]
2424
#[test]
25-
fn test_lifecycle() -> anyhow::Result<()> {
25+
fn test_extensions_blocking_lifecycle() -> anyhow::Result<()> {
2626
let installation_dir = tempfile::tempdir()?.path().to_path_buf();
2727
let settings = postgresql_embedded::Settings {
2828
version: postgresql_embedded::VersionReq::parse("=16.4.0")?,

postgresql_extensions/tests/extensions.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ async fn test_get_available_extensions() -> Result<()> {
2121

2222
#[cfg(all(target_os = "linux", feature = "tensor-chord"))]
2323
#[tokio::test]
24-
async fn test_lifecycle() -> Result<()> {
24+
async fn test_extensions_tensor_chord_lifecycle() -> Result<()> {
2525
let installation_dir = tempfile::tempdir()?.path().to_path_buf();
26+
let postgresql_version = semver::VersionReq::parse("=16.4.0")?;
2627
let settings = postgresql_embedded::Settings {
27-
version: postgresql_embedded::VersionReq::parse("=16.4.0")?,
28+
version: postgresql_version.clone(),
2829
installation_dir: installation_dir.clone(),
2930
..Default::default()
3031
};
@@ -33,6 +34,12 @@ async fn test_lifecycle() -> Result<()> {
3334
postgresql.setup().await?;
3435

3536
let settings = postgresql.settings();
37+
// Skip the test if the PostgreSQL version does not match; when testing with the 'bundled'
38+
// feature, the version may vary and the test will fail.
39+
if settings.version != postgresql_version {
40+
return Ok(());
41+
}
42+
3643
let namespace = "tensor-chord";
3744
let name = "pgvecto.rs";
3845
let version = semver::VersionReq::parse("=0.3.0")?;

postgresql_extensions/tests/portal_corp.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
)))]
55
#[cfg(feature = "portal-corp")]
66
#[tokio::test]
7-
async fn test_lifecycle() -> anyhow::Result<()> {
7+
async fn test_extensions_portal_corp_lifecycle() -> anyhow::Result<()> {
88
let installation_dir = tempfile::tempdir()?.path().to_path_buf();
99
let postgresql_version = semver::VersionReq::parse("=16.4.0")?;
1010
let settings = postgresql_embedded::Settings {
11-
version: postgresql_version,
11+
version: postgresql_version.clone(),
1212
installation_dir: installation_dir.clone(),
1313
..Default::default()
1414
};
@@ -17,6 +17,12 @@ async fn test_lifecycle() -> anyhow::Result<()> {
1717
postgresql.setup().await?;
1818

1919
let settings = postgresql.settings();
20+
// Skip the test if the PostgreSQL version does not match; when testing with the 'bundled'
21+
// feature, the version may vary and the test will fail.
22+
if settings.version != postgresql_version {
23+
return Ok(());
24+
}
25+
2026
let namespace = "portal-corp";
2127
let name = "pgvector_compiled";
2228
let version = semver::VersionReq::parse("=0.16.12")?;

postgresql_extensions/tests/steampipe.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#[cfg(any(target_os = "linux", target_os = "macos"))]
22
#[cfg(feature = "steampipe")]
33
#[tokio::test]
4-
async fn test_lifecycle() -> anyhow::Result<()> {
4+
async fn test_extensions_steampipe_lifecycle() -> anyhow::Result<()> {
55
let installation_dir = tempfile::tempdir()?.path().to_path_buf();
66
let postgresql_version = semver::VersionReq::parse("=15.7.0")?;
77
let settings = postgresql_embedded::Settings {
8-
version: postgresql_version,
8+
version: postgresql_version.clone(),
99
installation_dir: installation_dir.clone(),
1010
..Default::default()
1111
};
@@ -14,6 +14,12 @@ async fn test_lifecycle() -> anyhow::Result<()> {
1414
postgresql.setup().await?;
1515

1616
let settings = postgresql.settings();
17+
// Skip the test if the PostgreSQL version does not match; when testing with the 'bundled'
18+
// feature, the version may vary and the test will fail.
19+
if settings.version != postgresql_version {
20+
return Ok(());
21+
}
22+
1723
let namespace = "steampipe";
1824
let name = "csv";
1925
let version = semver::VersionReq::parse("=0.12.0")?;

0 commit comments

Comments
 (0)