Skip to content

Commit 574d754

Browse files
Merge pull request #182 from YageGeng/main
Fix Custom Release URL Not Working and Compilation Failure
2 parents 39a9206 + f5401a0 commit 574d754

File tree

6 files changed

+72
-14
lines changed

6 files changed

+72
-14
lines changed

postgresql_archive/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ default = [
5454
blocking = ["dep:tokio"]
5555
github = [
5656
"dep:serde_json",
57+
"dep:target-triple",
5758
]
5859
indicatif = [
5960
"dep:tracing-indicatif"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use semver::Version;
2+
3+
/// Matcher for PostgreSQL binaries from custom GitHub release repositories following the same
4+
/// pattern as <https://github.com/theseus-rs/postgresql-binaries>
5+
///
6+
/// # Errors
7+
/// * If the asset matcher fails.
8+
pub fn matcher(_url: &str, name: &str, version: &Version) -> crate::Result<bool> {
9+
let target = target_triple::TARGET;
10+
// TODO: consider relaxing the version format to allow for more flexibility in where the version
11+
// and target appear in the filename.
12+
let expected_name = format!("postgresql-{version}-{target}.tar.gz");
13+
Ok(name == expected_name)
14+
}
15+
16+
#[cfg(test)]
17+
mod tests {
18+
use super::*;
19+
use crate::{Result, matcher};
20+
21+
const TEST_URL: &str = "https://github.com/owner/repo";
22+
23+
#[test]
24+
fn test_register_custom_repo() -> Result<()> {
25+
#[expect(clippy::unnecessary_wraps)]
26+
fn supports_fn(url: &str) -> Result<bool> {
27+
Ok(url == TEST_URL)
28+
}
29+
matcher::registry::register(supports_fn, matcher)?;
30+
31+
let matcher = matcher::registry::get(TEST_URL)?;
32+
let version = Version::new(16, 3, 0);
33+
let expected_name = format!("postgresql-{}-{}.tar.gz", version, target_triple::TARGET);
34+
assert!(matcher("", &expected_name, &version)?);
35+
Ok(())
36+
}
37+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub mod matcher;
2+
3+
pub use matcher::matcher;

postgresql_archive/src/configuration/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod custom;
12
#[cfg(feature = "theseus")]
23
pub mod theseus;
34
#[cfg(feature = "zonky")]

postgresql_archive/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ mod test {
175175
#[test]
176176
fn test_from_strip_prefix_error() {
177177
let path = PathBuf::from("test");
178-
let stip_prefix_error = path.strip_prefix("foo").expect_err("strip prefix error");
179-
let error = Error::from(stip_prefix_error);
178+
let strip_prefix_error = path.strip_prefix("foo").expect_err("strip prefix error");
179+
let error = Error::from(strip_prefix_error);
180180
assert_eq!(error.to_string(), "prefix not found");
181181
}
182182

postgresql_embedded/build/bundle.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#![allow(dead_code)]
22

33
use anyhow::Result;
4-
use postgresql_archive::VersionReq;
4+
use postgresql_archive::configuration::{custom, theseus};
55
use postgresql_archive::repository::github::repository::GitHub;
6+
use postgresql_archive::{VersionReq, matcher};
67
use postgresql_archive::{get_archive, repository};
78
use std::fs::File;
89
use std::io::Write;
@@ -20,7 +21,17 @@ pub(crate) async fn stage_postgresql_archive() -> Result<()> {
2021
let default_releases_url = postgresql_archive::configuration::theseus::URL.to_string();
2122
#[cfg(not(feature = "theseus"))]
2223
let default_releases_url = String::new();
23-
let releases_url = env::var("POSTGRESQL_RELEASES_URL").unwrap_or(default_releases_url);
24+
25+
let releases_url = match env::var("POSTGRESQL_RELEASES_URL") {
26+
Ok(custom_url) if !default_releases_url.is_empty() => {
27+
register_custom_repository()?;
28+
custom_url
29+
}
30+
_ => {
31+
register_theseus_repository()?;
32+
default_releases_url
33+
}
34+
};
2435
println!("PostgreSQL releases URL: {releases_url}");
2536
let postgres_version_req = env::var("POSTGRESQL_VERSION").unwrap_or("*".to_string());
2637
let version_req = VersionReq::from_str(postgres_version_req.as_str())?;
@@ -40,7 +51,6 @@ pub(crate) async fn stage_postgresql_archive() -> Result<()> {
4051
return Ok(());
4152
}
4253

43-
register_github_repository()?;
4454
let (asset_version, archive) = get_archive(&releases_url, &version_req).await?;
4555

4656
fs::write(archive_version_file.clone(), asset_version.to_string())?;
@@ -52,14 +62,20 @@ pub(crate) async fn stage_postgresql_archive() -> Result<()> {
5262
Ok(())
5363
}
5464

55-
fn register_github_repository() -> Result<()> {
56-
repository::registry::register(
57-
|url| {
58-
let parsed_url = Url::parse(url)?;
59-
let host = parsed_url.host_str().unwrap_or_default();
60-
Ok(host.ends_with("github.com"))
61-
},
62-
Box::new(GitHub::new),
63-
)?;
65+
fn supports_github_url(url: &str) -> postgresql_archive::Result<bool> {
66+
let parsed_url = Url::parse(url)?;
67+
let host = parsed_url.host_str().unwrap_or_default();
68+
Ok(host.ends_with("github.com"))
69+
}
70+
71+
fn register_custom_repository() -> Result<()> {
72+
repository::registry::register(supports_github_url, Box::new(GitHub::new))?;
73+
matcher::registry::register(supports_github_url, custom::matcher)?;
74+
Ok(())
75+
}
76+
77+
fn register_theseus_repository() -> Result<()> {
78+
repository::registry::register(supports_github_url, Box::new(GitHub::new))?;
79+
matcher::registry::register(supports_github_url, theseus::matcher)?;
6480
Ok(())
6581
}

0 commit comments

Comments
 (0)