Skip user upsert when nothing changed to avoid row accumulation
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:
@@ -47,11 +47,16 @@ public class ClickHouseUserRepository implements UserRepository {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void upsert(UserInfo user) {
|
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());
|
Optional<UserInfo> existing = findById(user.userId());
|
||||||
if (existing.isPresent()) {
|
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(
|
jdbc.update(
|
||||||
"INSERT INTO users (user_id, provider, email, display_name, roles, created_at, updated_at) "
|
"INSERT INTO users (user_id, provider, email, display_name, roles, created_at, updated_at) "
|
||||||
+ "SELECT user_id, ?, ?, ?, ?, created_at, now64(3, 'UTC') "
|
+ "SELECT user_id, ?, ?, ?, ?, created_at, now64(3, 'UTC') "
|
||||||
|
|||||||
Reference in New Issue
Block a user