Skip to content

Commit

Permalink
Merge pull request #55 from spacemeshos/version_in_url
Browse files Browse the repository at this point in the history
  • Loading branch information
pigmej authored Oct 9, 2024
2 parents 6b66b32 + 69f3f12 commit 33bdba8
Showing 1 changed file with 69 additions and 6 deletions.
75 changes: 69 additions & 6 deletions src/partial_quicksync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,25 @@ fn download_file(
.extension()
.is_some_and(|ext| ext == "zst")
.then_some(".zst");
let url = format!("{}/{}", base_url, file_url(user_version, point, suffix));
println!("Downloading from {}", url);
let mut resp = client.get(&url).send().context("Failed to send request")?;
let version = env!("CARGO_PKG_VERSION");
let url_version = format!(
"{}/{}?version={}",
base_url,
file_url(user_version, point, suffix),
version
);
println!(
"Downloading from {}",
url_version.split('?').next().unwrap_or(&url_version)
);
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 {}",
url,
url_version,
resp.status()
);
}
Expand Down Expand Up @@ -149,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()?;

Expand All @@ -162,7 +179,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()?;

Expand Down Expand Up @@ -233,6 +255,7 @@ impl RestorePoint {
#[cfg(test)]
mod tests {
use super::*;
use mockito::Matcher;
use rusqlite::{Connection, DatabaseName};
use tempfile::tempdir;

Expand Down Expand Up @@ -355,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();
Expand Down Expand Up @@ -394,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();

Expand All @@ -402,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;"#,
Expand All @@ -425,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()
})
Expand Down Expand Up @@ -472,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();

Expand All @@ -480,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;"#,
Expand All @@ -502,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()
})
Expand Down Expand Up @@ -537,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();

Expand All @@ -564,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();

Expand Down

0 comments on commit 33bdba8

Please sign in to comment.