From a03790245ad97482ce59015f34d03431a5ecb019 Mon Sep 17 00:00:00 2001 From: Jedrzej Nowak Date: Wed, 9 Oct 2024 13:00:55 +0200 Subject: [PATCH 1/3] Added quicksync version to the download urls --- src/partial_quicksync.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/partial_quicksync.rs b/src/partial_quicksync.rs index 1cdbf48..ec26066 100644 --- a/src/partial_quicksync.rs +++ b/src/partial_quicksync.rs @@ -104,9 +104,19 @@ fn download_file( .extension() .is_some_and(|ext| ext == "zst") .then_some(".zst"); + let version = env!("CARGO_PKG_VERSION"); let url = format!("{}/{}", base_url, file_url(user_version, point, suffix)); + let url_version = format!( + "{}/{}?version={}", + base_url, + file_url(user_version, point, suffix), + version + ); println!("Downloading from {}", url); - let mut resp = client.get(&url).send().context("Failed to send request")?; + let mut resp = client + .get(&url_version) + .send() + .context("Failed to send request")?; if !resp.status().is_success() { anyhow::bail!( "Failed to download file {}: HTTP status {}", @@ -162,7 +172,12 @@ pub fn partial_restore( ); let restore_string = client - .get(format!("{}/{}/restore.sql", base_url, user_version)) + .get(format!( + "{}/{}/restore.sql?version={}", + base_url, + user_version, + env!("CARGO_PKG_VERSION") + )) .send()? .text()?; From cd0fdd8afbcd6d6404624dcf7b27b4519a303d05 Mon Sep 17 00:00:00 2001 From: Jedrzej Nowak Date: Wed, 9 Oct 2024 14:37:21 +0200 Subject: [PATCH 2/3] A bit cleaner implementation --- src/partial_quicksync.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/partial_quicksync.rs b/src/partial_quicksync.rs index ec26066..1c2e17e 100644 --- a/src/partial_quicksync.rs +++ b/src/partial_quicksync.rs @@ -105,14 +105,16 @@ fn download_file( .is_some_and(|ext| ext == "zst") .then_some(".zst"); let version = env!("CARGO_PKG_VERSION"); - let url = format!("{}/{}", base_url, file_url(user_version, point, suffix)); let url_version = format!( "{}/{}?version={}", base_url, file_url(user_version, point, suffix), version ); - println!("Downloading from {}", url); + println!( + "Downloading from {}", + url_version.split('?').next().unwrap_or(&url_version) + ); let mut resp = client .get(&url_version) .send() @@ -120,7 +122,7 @@ fn download_file( if !resp.status().is_success() { anyhow::bail!( "Failed to download file {}: HTTP status {}", - url, + url_version, resp.status() ); } From 69f3f12a5e9c9f23e15e448ca9648ccd67d772a2 Mon Sep 17 00:00:00 2001 From: Jedrzej Nowak Date: Wed, 9 Oct 2024 14:59:15 +0200 Subject: [PATCH 3/3] Tests adjusted for versions --- src/partial_quicksync.rs | 48 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/partial_quicksync.rs b/src/partial_quicksync.rs index 1c2e17e..b64a6f6 100644 --- a/src/partial_quicksync.rs +++ b/src/partial_quicksync.rs @@ -161,7 +161,12 @@ pub fn partial_restore( let conn = Connection::open(target_db_path)?; let user_version = get_user_version(&conn)?; let remote_metadata = client - .get(format!("{}/{}/metadata.csv", base_url, user_version)) + .get(format!( + "{}/{}/metadata.csv?version={}", + base_url, + user_version, + env!("CARGO_PKG_VERSION") + )) .send()? .text()?; @@ -250,6 +255,7 @@ impl RestorePoint { #[cfg(test)] mod tests { use super::*; + use mockito::Matcher; use rusqlite::{Connection, DatabaseName}; use tempfile::tempdir; @@ -372,6 +378,10 @@ mod tests { let mut server = mockito::Server::new(); let mock = server .mock("GET", format!("/{file_url}").as_str()) + .match_query(Matcher::UrlEncoded( + "version".into(), + env!("CARGO_PKG_VERSION").into(), + )) .with_status(200) .with_body("file contents") .create(); @@ -411,6 +421,10 @@ mod tests { let mock_metadata = server .mock("GET", "/0/metadata.csv") + .match_query(Matcher::UrlEncoded( + "version".into(), + env!("CARGO_PKG_VERSION").into(), + )) .with_body(metadata) .create(); @@ -419,6 +433,10 @@ mod tests { // doesn't do this (it causes problems). let mock_query = server .mock("GET", "/0/restore.sql") + .match_query(Matcher::UrlEncoded( + "version".into(), + env!("CARGO_PKG_VERSION").into(), + )) .with_body(format!( r#"ATTACH DATABASE '{}' AS src; INSERT OR IGNORE INTO layers SELECT * from src.layers;"#, @@ -442,6 +460,10 @@ mod tests { let file_url = file_url(0, point, None); server .mock("GET", format!("/{file_url}").as_str()) + .match_query(Matcher::UrlEncoded( + "version".into(), + env!("CARGO_PKG_VERSION").into(), + )) .with_body(std::fs::read(&checkpoint).unwrap()) .create() }) @@ -489,6 +511,10 @@ mod tests { let mock_metadata = server .mock("GET", "/0/metadata.csv") + .match_query(Matcher::UrlEncoded( + "version".into(), + env!("CARGO_PKG_VERSION").into(), + )) .with_body(metadata) .create(); @@ -497,6 +523,10 @@ mod tests { // doesn't do this (it causes problems). let mock_query = server .mock("GET", "/0/restore.sql") + .match_query(Matcher::UrlEncoded( + "version".into(), + env!("CARGO_PKG_VERSION").into(), + )) .with_body(format!( r#"ATTACH DATABASE '{}' AS src; INSERT OR IGNORE INTO layers SELECT * from src.layers;"#, @@ -519,6 +549,10 @@ mod tests { let file_url = file_url(0, point, None); server .mock("GET", format!("/{file_url}").as_str()) + .match_query(Matcher::UrlEncoded( + "version".into(), + env!("CARGO_PKG_VERSION").into(), + )) .with_body(std::fs::read(&checkpoint).unwrap()) .create() }) @@ -554,11 +588,19 @@ mod tests { let metadata = RestorePoint::new(100, 200, "aaaa".to_string()).to_string(); let mock_metadata = server .mock("GET", "/0/metadata.csv") + .match_query(Matcher::UrlEncoded( + "version".into(), + env!("CARGO_PKG_VERSION").into(), + )) .with_body(metadata) .create(); let mock_query = server .mock("GET", "/0/restore.sql") + .match_query(Matcher::UrlEncoded( + "version".into(), + env!("CARGO_PKG_VERSION").into(), + )) .with_body(".import backup_source.db layers") .create(); @@ -581,6 +623,10 @@ mod tests { let metadata = RestorePoint::new(200, 300, "aaaa".to_string()).to_string(); let mock_metadata = server .mock("GET", "/0/metadata.csv") + .match_query(Matcher::UrlEncoded( + "version".into(), + env!("CARGO_PKG_VERSION").into(), + )) .with_body(metadata) .create();