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)