From 65cca7bd043d7bbfa032eb0478da2ba531398474 Mon Sep 17 00:00:00 2001 From: Andrey Avtomonov Date: Mon, 25 May 2026 13:47:14 +0200 Subject: [PATCH] refactor(historic-sql): route dialect support through driver registry --- .../historic-sql/connection-dialect.ts | 18 +++++++++++---- .../historic-sql/connection-dialect.test.ts | 23 +++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 packages/cli/test/context/ingest/adapters/historic-sql/connection-dialect.test.ts diff --git a/packages/cli/src/context/ingest/adapters/historic-sql/connection-dialect.ts b/packages/cli/src/context/ingest/adapters/historic-sql/connection-dialect.ts index c6b4c53b..dd95f87a 100644 --- a/packages/cli/src/context/ingest/adapters/historic-sql/connection-dialect.ts +++ b/packages/cli/src/context/ingest/adapters/historic-sql/connection-dialect.ts @@ -1,5 +1,9 @@ +import { getDriverRegistration } from '../../../connections/drivers.js'; +import type { KtxConnectionDriver } from '../../../scan/types.js'; import type { HistoricSqlDialect } from './types.js'; +const historicSqlDialects: readonly HistoricSqlDialect[] = ['postgres', 'bigquery', 'snowflake']; + function recordOrNull(value: unknown): Record | null { return value && typeof value === 'object' && !Array.isArray(value) ? (value as Record) : null; } @@ -10,6 +14,14 @@ function queryHistoryRecord(connection: unknown): Record | null return context ? recordOrNull(context.queryHistory) : null; } +function historicSqlDialectForDriver(driver: KtxConnectionDriver): HistoricSqlDialect { + const dialect = historicSqlDialects.find((candidate) => candidate === driver); + if (!dialect) { + throw new Error(`Driver "${driver}" is marked as historic-SQL capable but has no HistoricSqlDialect mapping.`); + } + return dialect; +} + export function isQueryHistoryEnabled(connection: unknown): boolean { return queryHistoryRecord(connection)?.enabled === true; } @@ -25,8 +37,6 @@ export function queryHistoryDialectForConnection(connection: unknown): HistoricS } const conn = recordOrNull(connection); const driver = String(conn?.driver ?? '').toLowerCase(); - if (driver === 'postgres') return 'postgres'; - if (driver === 'bigquery') return 'bigquery'; - if (driver === 'snowflake') return 'snowflake'; - return null; + const registration = getDriverRegistration(driver); + return registration?.hasHistoricSqlReader ? historicSqlDialectForDriver(registration.driver) : null; } diff --git a/packages/cli/test/context/ingest/adapters/historic-sql/connection-dialect.test.ts b/packages/cli/test/context/ingest/adapters/historic-sql/connection-dialect.test.ts new file mode 100644 index 00000000..8dc2ec88 --- /dev/null +++ b/packages/cli/test/context/ingest/adapters/historic-sql/connection-dialect.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, it } from 'vitest'; +import { queryHistoryDialectForConnection } from '../../../../../src/context/ingest/adapters/historic-sql/connection-dialect.js'; + +describe('queryHistoryDialectForConnection', () => { + it.each([ + ['postgres', 'postgres'], + ['bigquery', 'bigquery'], + ['snowflake', 'snowflake'], + ] as const)('returns %s when query history is enabled', (driver, dialect) => { + expect(queryHistoryDialectForConnection({ driver, context: { queryHistory: { enabled: true } } })).toBe(dialect); + }); + + it.each(['sqlite', 'mysql', 'clickhouse', 'sqlserver'] as const)( + 'returns null for %s because no historic-SQL reader is registered', + (driver) => { + expect(queryHistoryDialectForConnection({ driver, context: { queryHistory: { enabled: true } } })).toBeNull(); + }, + ); + + it('returns null when query history is disabled', () => { + expect(queryHistoryDialectForConnection({ driver: 'postgres', context: { queryHistory: { enabled: false } } })).toBeNull(); + }); +});