From 8096217d9e1b4dc345b851f29962cfe088cf87ec Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 19 Jan 2022 19:10:11 +0000 Subject: [PATCH] Ensure we bump the change ID on sqlite --- keyserver/storage/sqlite3/key_changes_table.go | 3 ++- keyserver/storage/storage_test.go | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/keyserver/storage/sqlite3/key_changes_table.go b/keyserver/storage/sqlite3/key_changes_table.go index b576a5e4..eff371da 100644 --- a/keyserver/storage/sqlite3/key_changes_table.go +++ b/keyserver/storage/sqlite3/key_changes_table.go @@ -40,7 +40,8 @@ const upsertKeyChangeSQL = "" + "INSERT INTO keyserver_key_changes (user_id)" + " VALUES ($1)" + " ON CONFLICT" + - " DO UPDATE SET user_id = $1" + + // this only works because we rely on a single writer + " DO UPDATE SET change_id = change_id + 1" + " RETURNING change_id" // select the highest offset for each user in the range. The grouping by user gives distinct entries and then we just diff --git a/keyserver/storage/storage_test.go b/keyserver/storage/storage_test.go index 6fb06fb7..2f8cf809 100644 --- a/keyserver/storage/storage_test.go +++ b/keyserver/storage/storage_test.go @@ -65,10 +65,13 @@ func TestKeyChanges(t *testing.T) { func TestKeyChangesNoDupes(t *testing.T) { db, clean := MustCreateDatabase(t) defer clean() - _, err := db.StoreKeyChange(ctx, "@alice:localhost") + deviceChangeIDA, err := db.StoreKeyChange(ctx, "@alice:localhost") MustNotError(t, err) - _, err = db.StoreKeyChange(ctx, "@alice:localhost") + deviceChangeIDB, err := db.StoreKeyChange(ctx, "@alice:localhost") MustNotError(t, err) + if deviceChangeIDA == deviceChangeIDB { + t.Fatalf("Expected change ID to be different even when inserting key change for the same user, got %d for both changes", deviceChangeIDA) + } deviceChangeID, err := db.StoreKeyChange(ctx, "@alice:localhost") MustNotError(t, err) userIDs, latest, err := db.KeyChanges(ctx, 0, sarama.OffsetNewest)