Skip to content

Commit 5f398e3

Browse files
author
Gemini
committed
feat(migrations): add configurable data retention TTL for self-hosted instances
1 parent 6f4861a commit 5f398e3

File tree

5 files changed

+502
-0
lines changed

5 files changed

+502
-0
lines changed

.changeset/brave-mangos-visit.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
'@hive/migrations': minor
3+
---
4+
5+
Add configurable data retention TTL for self-hosted Hive instances. Self-hosted users can now configure retention periods via environment variables instead of hardcoded values.
6+
7+
New environment variables:
8+
- `CLICKHOUSE_TTL_TABLES` - Retention for ClickHouse mergetree tables (Default: 1 YEAR)
9+
- `CLICKHOUSE_TTL_DAILY_MV_TABLES` - Retention for daily materialized view tables (Default: 1 YEAR)
10+
- `CLICKHOUSE_TTL_HOURLY_MV_TABLES` - Retention for hourly materialized view tables (Default: 30 DAYS)
11+
- `CLICKHOUSE_TTL_MINUTELY_MV_TABLES` - Retention for minutely materialized view tables (Default: 24 HOURS)
12+
13+
Supports both numeric days (e.g., `365`) and ClickHouse interval syntax (e.g., `"1 YEAR"`, `"30 DAY"`, `"24 HOUR"`).
14+
15+
The retention update runs automatically if any retention environment variable is set.

docker/docker-compose.community.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ services:
170170
CLICKHOUSE_PORT: '8123'
171171
CLICKHOUSE_USERNAME: '${CLICKHOUSE_USER}'
172172
CLICKHOUSE_PASSWORD: '${CLICKHOUSE_PASSWORD}'
173+
CLICKHOUSE_TTL_TABLES: '${CLICKHOUSE_TTL_TABLES:-}'
174+
CLICKHOUSE_TTL_DAILY_MV_TABLES: '${CLICKHOUSE_TTL_DAILY_MV_TABLES:-}'
175+
CLICKHOUSE_TTL_HOURLY_MV_TABLES: '${CLICKHOUSE_TTL_HOURLY_MV_TABLES:-}'
176+
CLICKHOUSE_TTL_MINUTELY_MV_TABLES: '${CLICKHOUSE_TTL_MINUTELY_MV_TABLES:-}'
173177
TS_NODE_TRANSPILE_ONLY: 'true'
174178
LOG_LEVEL: '${LOG_LEVEL:-debug}'
175179

packages/migrations/.env.template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ CLICKHOUSE_HOST="localhost"
55
CLICKHOUSE_PORT="8123"
66
CLICKHOUSE_USERNAME="test"
77
CLICKHOUSE_PASSWORD="test"
8+
# CLICKHOUSE_TTL_TABLES="1 YEAR"
9+
# CLICKHOUSE_TTL_DAILY_MV_TABLES="30 DAYS"
10+
# CLICKHOUSE_TTL_HOURLY_MV_TABLES="7 DAYS"
11+
# CLICKHOUSE_TTL_MINUTELY_MV_TABLES="1 DAY"
812

913
POSTGRES_USER=postgres
1014
POSTGRES_PASSWORD=postgres

packages/migrations/src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { migrateClickHouse } from './clickhouse';
55
import { createConnectionString } from './connection-string';
66
import { env } from './environment';
77
import { runPGMigrations } from './run-pg-migrations';
8+
import { updateRetention } from './scripts/update-retention';
89

910
const slonik = await createPool(createConnectionString(env.postgres), {
1011
// 10 minute timeout per statement
@@ -38,6 +39,23 @@ try {
3839
env.clickhouse,
3940
);
4041
}
42+
43+
// Automatically apply retention if any retention setting is configured
44+
// eslint-disable-next-line no-process-env
45+
if (
46+
process.env.CLICKHOUSE_TTL_TABLES ||
47+
process.env.CLICKHOUSE_TTL_DAILY_MV_TABLES ||
48+
process.env.CLICKHOUSE_TTL_HOURLY_MV_TABLES ||
49+
process.env.CLICKHOUSE_TTL_MINUTELY_MV_TABLES
50+
) {
51+
console.log('Applying clickhouse retention settings...');
52+
try {
53+
await updateRetention();
54+
} catch (error) {
55+
console.error('Failed to update retention (non-fatal):', error);
56+
}
57+
}
58+
4159
process.exit(0);
4260
} catch (error) {
4361
console.error(error);

0 commit comments

Comments
 (0)