console/integration-tests/tests/migrations/retention.spec.ts
Copilot aac23596ec
refactor: shared internal postgres package around slonik (#7887)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: n1ru4l <14338007+n1ru4l@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Laurin Quast <laurinquast@googlemail.com>
2026-03-27 10:20:05 +01:00

86 lines
3 KiB
TypeScript

import { clickHouseQuery } from '../../testkit/clickhouse';
import { ensureEnv } from '../../testkit/env';
import { getServiceHost } from '../../testkit/utils';
test('update-retention script applies TTL to ClickHouse tables', async () => {
const originalEnv = { ...process.env };
try {
// Set up ClickHouse connection env vars for migrations module
const clickhouseAddress = await getServiceHost('clickhouse', 8123);
const [host, port] = clickhouseAddress.split(':');
process.env.CLICKHOUSE_PROTOCOL = 'http';
process.env.CLICKHOUSE_HOST = host;
process.env.CLICKHOUSE_PORT = port;
process.env.CLICKHOUSE_USERNAME = ensureEnv('CLICKHOUSE_USER');
process.env.CLICKHOUSE_PASSWORD = ensureEnv('CLICKHOUSE_PASSWORD');
// Set retention TTL values
process.env.CLICKHOUSE_TTL_TABLES = '1 YEAR';
process.env.CLICKHOUSE_TTL_DAILY_MV_TABLES = '30 DAY';
process.env.CLICKHOUSE_TTL_HOURLY_MV_TABLES = '7 DAY';
process.env.CLICKHOUSE_TTL_MINUTELY_MV_TABLES = '1 DAY';
// Dynamic import to pick up env vars
const { updateRetention } = await import(
'../../../packages/migrations/src/scripts/update-retention'
);
await updateRetention();
// Verify TTL was applied to a MergeTree table
const operationsTable = await clickHouseQuery<{ engine_full: string }>(`
SELECT engine_full
FROM system.tables
WHERE database = 'default' AND name = 'operations'
LIMIT 1
`);
expect(operationsTable.rows).toBe(1);
expect(operationsTable.data[0].engine_full).toContain('TTL');
expect(operationsTable.data[0].engine_full).toContain('toIntervalYear(1)');
// Verify TTL was applied to a daily materialized view inner table
const operationsDailyTable = await clickHouseQuery<{ uuid: string }>(`
SELECT uuid
FROM system.tables
WHERE database = 'default' AND name = 'operations_daily'
LIMIT 1
`);
expect(operationsDailyTable.rows).toBe(1);
const innerTableName = `.inner_id.${operationsDailyTable.data[0].uuid}`;
const innerTable = await clickHouseQuery<{ engine_full: string }>(`
SELECT engine_full
FROM system.tables
WHERE database = 'default' AND name = '${innerTableName}'
LIMIT 1
`);
expect(innerTable.rows).toBe(1);
expect(innerTable.data[0].engine_full).toContain('TTL');
expect(innerTable.data[0].engine_full).toContain('toIntervalDay(30)');
} finally {
process.env = originalEnv;
}
});
test('update-retention script skips gracefully when no env vars are set', async () => {
const originalEnv = { ...process.env };
try {
delete process.env.CLICKHOUSE_TTL_TABLES;
delete process.env.CLICKHOUSE_TTL_DAILY_MV_TABLES;
delete process.env.CLICKHOUSE_TTL_HOURLY_MV_TABLES;
delete process.env.CLICKHOUSE_TTL_MINUTELY_MV_TABLES;
const { updateRetention } = await import(
'../../../packages/migrations/src/scripts/update-retention'
);
// Should not throw
await expect(updateRetention()).resolves.toBeUndefined();
} finally {
process.env = originalEnv;
}
});