Skip user upsert when nothing changed to avoid row accumulation
All checks were successful
CI / build (push) Successful in 1m11s
CI / docker (push) Successful in 45s
CI / deploy (push) Successful in 30s

ReplacingMergeTree only deduplicates during background merges, so
every login was inserting a new row even when all fields were identical.
Now compares the existing record and skips the write if nothing changed.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
hsiegeln
2026-03-14 16:37:06 +01:00
parent dbf53aa8e8
commit 31b8695420

View File

@@ -47,11 +47,16 @@ public class ClickHouseUserRepository implements UserRepository {
@Override
public void upsert(UserInfo user) {
// Preserve created_at from existing record on re-login.
// ReplacingMergeTree deduplicates by ORDER BY (user_id), keeping the row
// with the highest updated_at.
Optional<UserInfo> existing = findById(user.userId());
if (existing.isPresent()) {
UserInfo ex = existing.get();
// Skip write if nothing changed — avoids accumulating un-merged rows
if (ex.provider().equals(user.provider())
&& ex.email().equals(user.email())
&& ex.displayName().equals(user.displayName())
&& ex.roles().equals(user.roles())) {
return;
}
jdbc.update(
"INSERT INTO users (user_id, provider, email, display_name, roles, created_at, updated_at) "
+ "SELECT user_id, ?, ?, ?, ?, created_at, now64(3, 'UTC') "