pg_embed/
pg_fetch.rs

1//!
2//! Fetch postgresql binaries
3//!
4//! Download and unpack postgresql binaries
5//!
6
7use bytes::Bytes;
8use futures::TryFutureExt;
9use reqwest::Response;
10
11use crate::pg_enums::{Architecture, OperationSystem};
12use crate::pg_errors::{PgEmbedError, PgEmbedErrorType};
13use crate::pg_types::PgResult;
14
15/// Postgresql version struct (simple version wrapper)
16pub struct PostgresVersion(pub &'static str);
17/// Latest postgres version 15
18pub const PG_V15: PostgresVersion = PostgresVersion("15.1.0");
19/// Latest postgres version 14
20pub const PG_V14: PostgresVersion = PostgresVersion("14.6.0");
21/// Latest postgres version 13
22pub const PG_V13: PostgresVersion = PostgresVersion("13.9.0");
23/// Latest postgres version 12
24pub const PG_V12: PostgresVersion = PostgresVersion("12.13.0");
25/// Latest pstgres version 11
26pub const PG_V11: PostgresVersion = PostgresVersion("11.18.0");
27/// Latest postgres version 10
28pub const PG_V10: PostgresVersion = PostgresVersion("10.23.0");
29/// Latest postgres version 9
30pub const PG_V9: PostgresVersion = PostgresVersion("9.6.24");
31
32/// Settings that determine the postgres binary to be fetched
33pub struct PgFetchSettings {
34    /// The repository host
35    pub host: String,
36    /// The operation system
37    pub operating_system: OperationSystem,
38    /// The cpu architecture
39    pub architecture: Architecture,
40    /// The postgresql version
41    pub version: PostgresVersion,
42}
43
44impl Default for PgFetchSettings {
45    fn default() -> Self {
46        PgFetchSettings {
47            host: "https://repo1.maven.org".to_string(),
48            operating_system: OperationSystem::default(),
49            architecture: Architecture::default(),
50            version: PG_V13,
51        }
52    }
53}
54
55impl PgFetchSettings {
56    /// The platform string (*needed to determine the download path*)
57    pub fn platform(&self) -> String {
58        let os = self.operating_system.to_string();
59        let arch = if self.operating_system == OperationSystem::AlpineLinux {
60            format!("{}-{}", self.architecture.to_string(), "alpine")
61        } else {
62            self.architecture.to_string()
63        };
64        format!("{}-{}", os, arch)
65    }
66
67    ///
68    /// Fetch postgres binaries
69    ///
70    /// Returns the data of the downloaded binary in an `Ok([u8])` on success, otherwise returns an error.
71    ///
72    pub async fn fetch_postgres(&self) -> PgResult<Box<Bytes>> {
73        let platform = &self.platform();
74        let version = self.version.0;
75        let download_url = format!(
76            "{}/maven2/io/zonky/test/postgres/embedded-postgres-binaries-{}/{}/embedded-postgres-binaries-{}-{}.jar",
77            &self.host,
78            &platform,
79            version,
80            &platform,
81            version);
82        let response: Response = reqwest::get(download_url)
83            .map_err(|e| PgEmbedError {
84                error_type: PgEmbedErrorType::DownloadFailure,
85                source: Some(Box::new(e)),
86                message: None,
87            })
88            .await?;
89
90        let content: Bytes = response
91            .bytes()
92            .map_err(|e| PgEmbedError {
93                error_type: PgEmbedErrorType::ConversionFailure,
94                source: Some(Box::new(e)),
95                message: None,
96            })
97            .await?;
98
99        Ok(Box::new(content))
100    }
101}