Skip to content

Commit 213be85

Browse files
authored
Fix add_version calls when no history exists on the server (#149)
This fixes a bug in 25911b4 where the check in the storage implementation was too strict (not allowing `clients.latest_version_id == Uuid::nil()`), causing spurious failures. Well, two bugs, one in each storage implementation.
1 parent b57dd24 commit 213be85

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

postgres/src/lib.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,13 @@ impl StorageTxn for Txn {
272272
"UPDATE clients
273273
SET latest_version_id = $1,
274274
versions_since_snapshot = versions_since_snapshot + 1
275-
WHERE client_id = $2 and latest_version_id = $3",
276-
&[&version_id, &self.client_id, &parent_version_id],
275+
WHERE client_id = $2 and (latest_version_id = $3 or latest_version_id = $4)",
276+
&[
277+
&version_id,
278+
&self.client_id,
279+
&parent_version_id,
280+
&Uuid::nil(),
281+
],
277282
)
278283
.await
279284
.context("error updating latest_version_id")?;
@@ -689,4 +694,22 @@ mod test {
689694
})
690695
.await
691696
}
697+
698+
#[tokio::test]
699+
/// When an add_version call specifies a `parent_version_id` that does not exist in the
700+
/// DB, but no other versions exist, the call succeeds.
701+
async fn test_add_version_no_history() -> anyhow::Result<()> {
702+
with_db(async |connection_string, db_client| {
703+
let storage = PostgresStorage::new(connection_string).await?;
704+
let client_id = make_client(&db_client).await?;
705+
706+
let mut txn = storage.txn(client_id).await?;
707+
let version_id = Uuid::new_v4();
708+
let parent_version_id = Uuid::new_v4();
709+
txn.add_version(version_id, parent_version_id, b"v1".to_vec())
710+
.await?;
711+
Ok(())
712+
})
713+
.await
714+
}
692715
}

sqlite/src/lib.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,12 @@ impl StorageTxn for Txn {
276276
SET
277277
latest_version_id = ?,
278278
versions_since_snapshot = versions_since_snapshot + 1
279-
WHERE client_id = ? and latest_version_id = ?",
279+
WHERE client_id = ? and (latest_version_id = ? or latest_version_id = ?)",
280280
params![
281281
StoredUuid(version_id),
282282
StoredUuid(self.client_id),
283-
StoredUuid(parent_version_id)
283+
StoredUuid(parent_version_id),
284+
StoredUuid(Uuid::nil())
284285
],
285286
)
286287
.context("Error updating client for new version")?;
@@ -489,4 +490,21 @@ mod test {
489490

490491
Ok(())
491492
}
493+
494+
#[tokio::test]
495+
/// When an add_version call specifies a `parent_version_id` that does not exist in the
496+
/// DB, but no other versions exist, the call succeeds.
497+
async fn test_add_version_no_history() -> anyhow::Result<()> {
498+
let tmp_dir = TempDir::new()?;
499+
let storage = SqliteStorage::new(tmp_dir.path())?;
500+
let client_id = Uuid::new_v4();
501+
let mut txn = storage.txn(client_id).await?;
502+
txn.new_client(Uuid::nil()).await?;
503+
504+
let version_id = Uuid::new_v4();
505+
let parent_version_id = Uuid::new_v4();
506+
txn.add_version(version_id, parent_version_id, b"v1".to_vec())
507+
.await?;
508+
Ok(())
509+
}
492510
}

0 commit comments

Comments
 (0)