Preserve created_at on user upsert to avoid accumulating un-merged rows
On re-login the upsert was inserting a new row with created_at=now(), causing ClickHouse ReplacingMergeTree to accumulate rows until background compaction. Now preserves the original created_at via INSERT...SELECT from the existing record. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -47,14 +47,32 @@ public class ClickHouseUserRepository implements UserRepository {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void upsert(UserInfo user) {
|
public void upsert(UserInfo user) {
|
||||||
jdbc.update(
|
// Preserve created_at from existing record on re-login.
|
||||||
"INSERT INTO users (user_id, provider, email, display_name, roles, updated_at) VALUES (?, ?, ?, ?, ?, now64(3, 'UTC'))",
|
// ReplacingMergeTree deduplicates by ORDER BY (user_id), keeping the row
|
||||||
user.userId(),
|
// with the highest updated_at.
|
||||||
user.provider(),
|
Optional<UserInfo> existing = findById(user.userId());
|
||||||
user.email(),
|
if (existing.isPresent()) {
|
||||||
user.displayName(),
|
jdbc.update(
|
||||||
user.roles().toArray(new String[0])
|
"INSERT INTO users (user_id, provider, email, display_name, roles, created_at, updated_at) "
|
||||||
);
|
+ "SELECT user_id, ?, ?, ?, ?, created_at, now64(3, 'UTC') "
|
||||||
|
+ "FROM users FINAL WHERE user_id = ?",
|
||||||
|
user.provider(),
|
||||||
|
user.email(),
|
||||||
|
user.displayName(),
|
||||||
|
user.roles().toArray(new String[0]),
|
||||||
|
user.userId()
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
jdbc.update(
|
||||||
|
"INSERT INTO users (user_id, provider, email, display_name, roles, updated_at) "
|
||||||
|
+ "VALUES (?, ?, ?, ?, ?, now64(3, 'UTC'))",
|
||||||
|
user.userId(),
|
||||||
|
user.provider(),
|
||||||
|
user.email(),
|
||||||
|
user.displayName(),
|
||||||
|
user.roles().toArray(new String[0])
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user