From b7da216d5ea1f7aa50331511c313ab44eb7a8e83 Mon Sep 17 00:00:00 2001 From: Alex Luong Date: Fri, 14 Aug 2026 23:30:44 +0700 Subject: [PATCH] fix(logretention): wrap ClickHouse TTL column in toDateTime The events/attempts time columns are DateTime64(3). ClickHouse rejects a TTL expression evaluating to DateTime64 until 25.7 (ClickHouse#80710), so setting CLICKHOUSE_LOG_RETENTION_TTL_DAYS crash-loops the service at startup on 24.8 LTS through 25.6, including 25.3 LTS. toDateTime() is accepted by all versions. Co-Authored-By: Claude Opus 5 (1M context) --- internal/logretention/clickhouse_ttl.go | 2 +- internal/logretention/clickhouse_ttl_test.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/logretention/clickhouse_ttl.go b/internal/logretention/clickhouse_ttl.go index 4dc9b920e..a22ae241a 100644 --- a/internal/logretention/clickhouse_ttl.go +++ b/internal/logretention/clickhouse_ttl.go @@ -64,7 +64,7 @@ func (c *ClickHouseTTL) alterTableTTL(ctx context.Context, tableName, timeColumn if ttlDays == 0 { query = fmt.Sprintf("ALTER TABLE %s REMOVE TTL", tableName) } else { - query = fmt.Sprintf("ALTER TABLE %s MODIFY TTL %s + INTERVAL %d DAY", tableName, timeColumn, ttlDays) + query = fmt.Sprintf("ALTER TABLE %s MODIFY TTL toDateTime(%s) + INTERVAL %d DAY", tableName, timeColumn, ttlDays) } return c.conn.Exec(ctx, query) diff --git a/internal/logretention/clickhouse_ttl_test.go b/internal/logretention/clickhouse_ttl_test.go index b00b31978..2b153c687 100644 --- a/internal/logretention/clickhouse_ttl_test.go +++ b/internal/logretention/clickhouse_ttl_test.go @@ -48,8 +48,8 @@ func TestClickHouseTTL_ApplyTTL(t *testing.T) { deploymentID: "", ttlDays: 30, wantQueries: []string{ - "ALTER TABLE events MODIFY TTL event_time + INTERVAL 30 DAY", - "ALTER TABLE attempts MODIFY TTL attempt_time + INTERVAL 30 DAY", + "ALTER TABLE events MODIFY TTL toDateTime(event_time) + INTERVAL 30 DAY", + "ALTER TABLE attempts MODIFY TTL toDateTime(attempt_time) + INTERVAL 30 DAY", }, wantQueryCount: 2, }, @@ -58,8 +58,8 @@ func TestClickHouseTTL_ApplyTTL(t *testing.T) { deploymentID: "dpm_001", ttlDays: 7, wantQueries: []string{ - "ALTER TABLE dpm_001_events MODIFY TTL event_time + INTERVAL 7 DAY", - "ALTER TABLE dpm_001_attempts MODIFY TTL attempt_time + INTERVAL 7 DAY", + "ALTER TABLE dpm_001_events MODIFY TTL toDateTime(event_time) + INTERVAL 7 DAY", + "ALTER TABLE dpm_001_attempts MODIFY TTL toDateTime(attempt_time) + INTERVAL 7 DAY", }, wantQueryCount: 2, },