diff --git a/atuin-client/src/api_client.rs b/atuin-client/src/api_client.rs index b4066197..7306546d 100644 --- a/atuin-client/src/api_client.rs +++ b/atuin-client/src/api_client.rs @@ -242,20 +242,11 @@ impl<'a> Client<'a> { ); let url = format!( - "{}/record/next?host={}&tag={}&count={}", - self.sync_addr, host.0, tag, count + "{}/record/next?host={}&tag={}&count={}&start={}", + self.sync_addr, host.0, tag, count, start ); - let mut url = Url::parse(url.as_str())?; - if let Some(start) = start { - url.set_query(Some( - format!( - "host={}&tag={}&count={}&start={}", - host.0, tag, count, start.0 - ) - .as_str(), - )); - } + let url = Url::parse(url.as_str())?; let resp = self.client.get(url).send().await?; diff --git a/atuin-client/src/record/sync.rs b/atuin-client/src/record/sync.rs index 11b4c268..dac5f5d4 100644 --- a/atuin-client/src/record/sync.rs +++ b/atuin-client/src/record/sync.rs @@ -154,7 +154,8 @@ async fn sync_upload( local: RecordIdx, remote: Option, ) -> Result { - let expected = local - remote.unwrap_or(0); + let remote = remote.unwrap_or(0); + let expected = local - remote; let upload_page_size = 100; let mut progress = 0; @@ -168,12 +169,7 @@ async fn sync_upload( // preload with the first entry if remote does not know of this store while progress < expected { let page = store - .next( - host, - tag.as_str(), - remote.unwrap_or(0) + progress, - upload_page_size, - ) + .next(host, tag.as_str(), remote + progress, upload_page_size) .await .map_err(|_| SyncError::LocalStoreError)?; @@ -203,7 +199,7 @@ async fn sync_download( remote: RecordIdx, ) -> Result { let local = local.unwrap_or(0); - let expected = remote - local.unwrap_or(0); + let expected = remote - local; let download_page_size = 100; let mut progress = 0; @@ -216,25 +212,27 @@ async fn sync_download( // preload with the first entry if remote does not know of this store while progress < expected { - let page = client.next_records(host, tag, Some(local + progress), download_page_size); - - let _ = client - .post_records(&page) + let page = client + .next_records(host, tag.clone(), local + progress, download_page_size) .await .map_err(|_| SyncError::RemoteRequestError)?; + store + .push_batch(page.iter()) + .await + .map_err(|_| SyncError::LocalStoreError)?; + println!( - "uploaded {} to remote, progress {}/{}", + "downloaded {} records from remote, progress {}/{}", page.len(), progress, expected ); + progress += page.len() as u64; } Ok(progress as i64) - - Ok(0) } pub async fn sync_remote( diff --git a/atuin-server-postgres/migrations/20231203124112_create-store-idx.sql b/atuin-server-postgres/migrations/20231203124112_create-store-idx.sql new file mode 100644 index 00000000..56d67145 --- /dev/null +++ b/atuin-server-postgres/migrations/20231203124112_create-store-idx.sql @@ -0,0 +1,2 @@ +-- Add migration script here +create unique index record_uniq ON store(user_id, host, tag, idx);