Skip to content

Commit 6efb248

Browse files
committed
fix: custom release url not working and compilation failure
1 parent 680e3db commit 6efb248

File tree

7 files changed

+74
-11
lines changed

7 files changed

+74
-11
lines changed

postgresql_archive/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ default = [
5353
]
5454
blocking = ["dep:tokio"]
5555
github = [
56+
"dep:target-triple",
5657
"dep:serde_json",
5758
]
5859
indicatif = [
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use semver::Version;
2+
3+
/// Matcher for PostgreSQL binaries from custom GitHub release repositories
4+
/// following the same pattern as <https://github.com/theseus-rs/postgresql-binaries>
5+
///
6+
/// # Errors
7+
/// * If the asset matcher fails.
8+
/// TODO: support more custom settings
9+
pub fn matcher(_url: &str, name: &str, version: &Version) -> crate::Result<bool> {
10+
let target = target_triple::TARGET;
11+
let expected_name = format!("postgresql-{version}-{target}.tar.gz");
12+
Ok(name == expected_name)
13+
}
14+
15+
#[cfg(test)]
16+
mod tests {
17+
use std::sync::Arc;
18+
19+
use super::*;
20+
use crate::{
21+
Result,
22+
matcher::{self, registry::SupportsFn},
23+
};
24+
25+
#[test]
26+
fn test_register_custom_repo() -> Result<()> {
27+
let custom_url = "https://github.com/Owner/Repo";
28+
let wrapped_url = Arc::new(custom_url.to_string());
29+
let supports_fn: SupportsFn = Box::new(move |url| Ok(url == wrapped_url.as_str()));
30+
matcher::registry::register(supports_fn, matcher)?;
31+
32+
let matcher = matcher::registry::get(custom_url)?;
33+
let version = Version::new(16, 3, 0);
34+
let expected_name = format!("postgresql-{}-{}.tar.gz", version, target_triple::TARGET);
35+
assert!(matcher("", &expected_name, &version)?);
36+
37+
Ok(())
38+
}
39+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod matcher;

postgresql_archive/src/configuration/mod.rs

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

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_archive/src/matcher/registry.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::sync::{Arc, LazyLock, Mutex, RwLock};
1010
static REGISTRY: LazyLock<Arc<Mutex<MatchersRegistry>>> =
1111
LazyLock::new(|| Arc::new(Mutex::new(MatchersRegistry::default())));
1212

13-
pub type SupportsFn = fn(&str) -> Result<bool>;
13+
pub type SupportsFn = Box<dyn Fn(&str) -> Result<bool> + Send + Sync + 'static>;
1414
pub type MatcherFn = fn(&str, &str, &Version) -> Result<bool>;
1515

1616
/// Singleton struct to store matchers
@@ -66,9 +66,9 @@ impl Default for MatchersRegistry {
6666
fn default() -> Self {
6767
let mut registry = Self::new();
6868
#[cfg(feature = "theseus")]
69-
registry.register(|url| Ok(url == theseus::URL), theseus::matcher);
69+
registry.register(Box::new(|url| Ok(url == theseus::URL)), theseus::matcher);
7070
#[cfg(feature = "zonky")]
71-
registry.register(|url| Ok(url == zonky::URL), zonky::matcher);
71+
registry.register(Box::new(|url| Ok(url == zonky::URL)), zonky::matcher);
7272
registry
7373
}
7474
}
@@ -78,11 +78,14 @@ impl Default for MatchersRegistry {
7878
///
7979
/// # Errors
8080
/// * If the registry is poisoned.
81-
pub fn register(supports_fn: SupportsFn, matcher_fn: MatcherFn) -> Result<()> {
81+
pub fn register<F>(supports_fn: F, matcher_fn: MatcherFn) -> Result<()>
82+
where
83+
F: Fn(&str) -> Result<bool> + Send + Sync + 'static,
84+
{
8285
let mut registry = REGISTRY
8386
.lock()
8487
.map_err(|error| PoisonedLock(error.to_string()))?;
85-
registry.register(supports_fn, matcher_fn);
88+
registry.register(Box::new(supports_fn), matcher_fn);
8689
Ok(())
8790
}
8891

postgresql_embedded/build/bundle.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#![allow(dead_code)]
22

33
use anyhow::Result;
4-
use postgresql_archive::VersionReq;
4+
use postgresql_archive::configuration::custom;
5+
use postgresql_archive::matcher::registry::SupportsFn;
56
use postgresql_archive::repository::github::repository::GitHub;
7+
use postgresql_archive::{VersionReq, matcher};
68
use postgresql_archive::{get_archive, repository};
79
use std::fs::File;
810
use std::io::Write;
911
use std::path::PathBuf;
1012
use std::str::FromStr;
13+
use std::sync::Arc;
1114
use std::{env, fs};
1215
use url::Url;
1316

@@ -20,7 +23,16 @@ pub(crate) async fn stage_postgresql_archive() -> Result<()> {
2023
let default_releases_url = postgresql_archive::configuration::theseus::URL.to_string();
2124
#[cfg(not(feature = "theseus"))]
2225
let default_releases_url = String::new();
23-
let releases_url = env::var("POSTGRESQL_RELEASES_URL").unwrap_or(default_releases_url);
26+
27+
let releases_url = match env::var("POSTGRESQL_RELEASES_URL") {
28+
Ok(custom_url) => {
29+
if !custom_url.is_empty() {
30+
register_github_repository(&custom_url)?;
31+
}
32+
custom_url
33+
}
34+
Err(_) => default_releases_url,
35+
};
2436
println!("PostgreSQL releases URL: {releases_url}");
2537
let postgres_version_req = env::var("POSTGRESQL_VERSION").unwrap_or("*".to_string());
2638
let version_req = VersionReq::from_str(postgres_version_req.as_str())?;
@@ -40,7 +52,6 @@ pub(crate) async fn stage_postgresql_archive() -> Result<()> {
4052
return Ok(());
4153
}
4254

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

4657
fs::write(archive_version_file.clone(), asset_version.to_string())?;
@@ -52,7 +63,7 @@ pub(crate) async fn stage_postgresql_archive() -> Result<()> {
5263
Ok(())
5364
}
5465

55-
fn register_github_repository() -> Result<()> {
66+
fn register_github_repository(custom_url: &str) -> Result<()> {
5667
repository::registry::register(
5768
|url| {
5869
let parsed_url = Url::parse(url)?;
@@ -61,5 +72,12 @@ fn register_github_repository() -> Result<()> {
6172
},
6273
Box::new(GitHub::new),
6374
)?;
75+
76+
// make custom_url as Send + Sync + 'static
77+
let custom_url = Arc::new(custom_url.to_string());
78+
let supports_fn: SupportsFn = Box::new(move |url| Ok(url == custom_url.as_str()));
79+
// register the matcher
80+
matcher::registry::register(supports_fn, custom::matcher::matcher)?;
81+
6482
Ok(())
6583
}

0 commit comments

Comments
 (0)