|
| 1 | +import { clickHouseQuery } from '../../testkit/clickhouse'; |
| 2 | +import { ensureEnv } from '../../testkit/env'; |
| 3 | +import { getServiceHost } from '../../testkit/utils'; |
| 4 | + |
| 5 | +test('update-retention script applies TTL to ClickHouse tables', async () => { |
| 6 | + const originalEnv = { ...process.env }; |
| 7 | + |
| 8 | + try { |
| 9 | + // Set up ClickHouse connection env vars for migrations module |
| 10 | + const clickhouseAddress = await getServiceHost('clickhouse', 8123); |
| 11 | + const [host, port] = clickhouseAddress.split(':'); |
| 12 | + process.env.CLICKHOUSE_PROTOCOL = 'http'; |
| 13 | + process.env.CLICKHOUSE_HOST = host; |
| 14 | + process.env.CLICKHOUSE_PORT = port; |
| 15 | + process.env.CLICKHOUSE_USERNAME = ensureEnv('CLICKHOUSE_USER'); |
| 16 | + process.env.CLICKHOUSE_PASSWORD = ensureEnv('CLICKHOUSE_PASSWORD'); |
| 17 | + |
| 18 | + // Set retention TTL values |
| 19 | + process.env.CLICKHOUSE_TTL_TABLES = '1 YEAR'; |
| 20 | + process.env.CLICKHOUSE_TTL_DAILY_MV_TABLES = '30 DAY'; |
| 21 | + process.env.CLICKHOUSE_TTL_HOURLY_MV_TABLES = '7 DAY'; |
| 22 | + process.env.CLICKHOUSE_TTL_MINUTELY_MV_TABLES = '1 DAY'; |
| 23 | + |
| 24 | + // Dynamic import to pick up env vars |
| 25 | + const { updateRetention } = await import( |
| 26 | + '../../../packages/migrations/src/scripts/update-retention' |
| 27 | + ); |
| 28 | + |
| 29 | + await updateRetention(); |
| 30 | + |
| 31 | + // Verify TTL was applied to a MergeTree table |
| 32 | + const operationsTable = await clickHouseQuery<{ engine_full: string }>(` |
| 33 | + SELECT engine_full |
| 34 | + FROM system.tables |
| 35 | + WHERE database = 'default' AND name = 'operations' |
| 36 | + LIMIT 1 |
| 37 | + `); |
| 38 | + |
| 39 | + expect(operationsTable.rows).toBe(1); |
| 40 | + expect(operationsTable.data[0].engine_full).toContain('TTL'); |
| 41 | + expect(operationsTable.data[0].engine_full).toContain('toIntervalYear(1)'); |
| 42 | + |
| 43 | + // Verify TTL was applied to a daily materialized view inner table |
| 44 | + const operationsDailyTable = await clickHouseQuery<{ uuid: string }>(` |
| 45 | + SELECT uuid |
| 46 | + FROM system.tables |
| 47 | + WHERE database = 'default' AND name = 'operations_daily' |
| 48 | + LIMIT 1 |
| 49 | + `); |
| 50 | + |
| 51 | + expect(operationsDailyTable.rows).toBe(1); |
| 52 | + const innerTableName = `.inner_id.${operationsDailyTable.data[0].uuid}`; |
| 53 | + const innerTable = await clickHouseQuery<{ engine_full: string }>(` |
| 54 | + SELECT engine_full |
| 55 | + FROM system.tables |
| 56 | + WHERE database = 'default' AND name = '${innerTableName}' |
| 57 | + LIMIT 1 |
| 58 | + `); |
| 59 | + |
| 60 | + expect(innerTable.rows).toBe(1); |
| 61 | + expect(innerTable.data[0].engine_full).toContain('TTL'); |
| 62 | + expect(innerTable.data[0].engine_full).toContain('toIntervalDay(30)'); |
| 63 | + } finally { |
| 64 | + process.env = originalEnv; |
| 65 | + } |
| 66 | +}); |
| 67 | + |
| 68 | +test('update-retention script skips gracefully when no env vars are set', async () => { |
| 69 | + const originalEnv = { ...process.env }; |
| 70 | + |
| 71 | + try { |
| 72 | + delete process.env.CLICKHOUSE_TTL_TABLES; |
| 73 | + delete process.env.CLICKHOUSE_TTL_DAILY_MV_TABLES; |
| 74 | + delete process.env.CLICKHOUSE_TTL_HOURLY_MV_TABLES; |
| 75 | + delete process.env.CLICKHOUSE_TTL_MINUTELY_MV_TABLES; |
| 76 | + |
| 77 | + vi.resetModules(); |
| 78 | + const { updateRetention } = await import( |
| 79 | + '../../../packages/migrations/src/scripts/update-retention' |
| 80 | + ); |
| 81 | + |
| 82 | + // Should not throw |
| 83 | + await expect(updateRetention()).resolves.toBeUndefined(); |
| 84 | + } finally { |
| 85 | + process.env = originalEnv; |
| 86 | + } |
| 87 | +}); |
0 commit comments