From 0b2fe7b9641c09cbeef3480f6d8795169d2bc29b Mon Sep 17 00:00:00 2001 From: Yash Date: Thu, 15 Oct 2026 16:37:16 +0530 Subject: [PATCH 01/17] feat(bridge): implement ClickHouse database connector and driver support --- bridge/package.json | 4 +- bridge/pnpm-lock.yaml | 9 + bridge/src/connectors/clickhouse.ts | 1250 ++++++++++++++++++ bridge/src/queries/clickhouse/constraints.ts | 54 + bridge/src/queries/clickhouse/migrations.ts | 29 + bridge/src/queries/clickhouse/schema.ts | 75 ++ bridge/src/queries/clickhouse/stats.ts | 24 + bridge/src/services/connectionBuilder.ts | 35 +- bridge/src/services/connectorRegistry.ts | 5 +- bridge/src/services/databaseService.ts | 40 +- bridge/src/services/dbStore.ts | 3 + bridge/src/services/discoveryService.ts | 28 +- bridge/src/services/projectStore.ts | 2 +- bridge/src/services/queryExecutor.ts | 1 + bridge/src/types/clickhouse.ts | 42 + bridge/src/types/index.ts | 3 + bridge/src/utils/clickhouseUrl.ts | 78 ++ bridge/src/utils/dbTypeDetector.ts | 1 + 18 files changed, 1672 insertions(+), 11 deletions(-) create mode 100644 bridge/src/connectors/clickhouse.ts create mode 100644 bridge/src/queries/clickhouse/constraints.ts create mode 100644 bridge/src/queries/clickhouse/migrations.ts create mode 100644 bridge/src/queries/clickhouse/schema.ts create mode 100644 bridge/src/queries/clickhouse/stats.ts create mode 100644 bridge/src/types/clickhouse.ts create mode 100644 bridge/src/utils/clickhouseUrl.ts diff --git a/bridge/package.json b/bridge/package.json index fb8d5bb8..a733202b 100644 --- a/bridge/package.json +++ b/bridge/package.json @@ -14,10 +14,12 @@ "build:pkg:linux": "npm run build && npm run build:bundle-for-pkg && npm run rebuild:native && npm run copy:native && npx @yao-pkg/pkg dist/index.bundled.cjs --target node22-linux-x64 --output ../src-tauri/resources/bridge-x86_64-unknown-linux-gnu", "test": "jest", "test:watch": "jest --watchAll --detectOpenHandles", - "seed:duckdb": "ts-node seed.ts" + "seed:duckdb": "ts-node seed.ts", + "seed:clickhouse": "ts-node seed-clickhouse.ts" }, "dependencies": { "@anthropic-ai/sdk": "^0.100.1", + "@clickhouse/client": "^1.24.1", "@duckdb/node-api": "1.5.6-r.1", "@google/generative-ai": "^0.24.1", "@jest/globals": "^30.2.0", diff --git a/bridge/pnpm-lock.yaml b/bridge/pnpm-lock.yaml index 19f915da..47fc76ed 100644 --- a/bridge/pnpm-lock.yaml +++ b/bridge/pnpm-lock.yaml @@ -11,6 +11,9 @@ importers: '@anthropic-ai/sdk': specifier: ^0.100.1 version: 0.100.1(zod@4.4.3) + '@clickhouse/client': + specifier: ^1.24.1 + version: 1.24.1 '@duckdb/node-api': specifier: 1.5.6-r.1 version: 1.5.6-r.1 @@ -283,6 +286,10 @@ packages: '@bcoe/v8-coverage@0.2.3': resolution: {integrity: sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==} + '@clickhouse/client@1.24.1': + resolution: {integrity: sha512-1kjKJ4uuD4J9JlRU4CpwEGfecstvtoGreBorhh7CxgB7r7PL54DCLD6gajKZhLCri84XlyD3CUxEm6rYkKC+dA==} + engines: {node: '>=20'} + '@cspotcode/source-map-support@0.8.1': resolution: {integrity: sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==} engines: {node: '>=12'} @@ -2578,6 +2585,8 @@ snapshots: '@bcoe/v8-coverage@0.2.3': {} + '@clickhouse/client@1.24.1': {} + '@cspotcode/source-map-support@0.8.1': dependencies: '@jridgewell/trace-mapping': 0.3.9 diff --git a/bridge/src/connectors/clickhouse.ts b/bridge/src/connectors/clickhouse.ts new file mode 100644 index 00000000..39cf0a43 --- /dev/null +++ b/bridge/src/connectors/clickhouse.ts @@ -0,0 +1,1250 @@ +import { createClient, type ClickHouseClient } from "@clickhouse/client"; +import { + ClickHouseConfig, + ClickHouseSchemaMetadataBatch, +} from "../types/clickhouse"; +export type { ClickHouseConfig, ClickHouseSchemaMetadataBatch }; +import { + TableInfo, + PrimaryKeyInfo, + ForeignKeyInfo, + IndexInfo, + UniqueConstraintInfo, + CheckConstraintInfo, + ColumnDetail, + DBStats, + SchemaInfo, + AppliedMigration, +} from "../types/common"; +import { CacheEntry } from "../types/cache"; +import { SchemaFile } from "../services/projectStore"; +import crypto from "crypto"; +import fs from "fs"; + +import { + CLICKHOUSE_LIST_SCHEMAS, + CLICKHOUSE_LIST_TABLES, + CLICKHOUSE_LIST_TABLES_BY_SCHEMA, + CLICKHOUSE_LIST_COLUMNS, + CLICKHOUSE_BATCH_LIST_COLUMNS, +} from "../queries/clickhouse/schema"; +import { + CLICKHOUSE_GET_PRIMARY_KEYS, + CLICKHOUSE_BATCH_GET_PRIMARY_KEYS, + CLICKHOUSE_GET_INDEXES, + CLICKHOUSE_BATCH_GET_INDEXES, +} from "../queries/clickhouse/constraints"; +import { + CLICKHOUSE_GET_DB_STATS, + CLICKHOUSE_GET_DB_SIZE, +} from "../queries/clickhouse/stats"; +import { cleanClickHouseUrl } from "../utils/clickhouseUrl"; +import logger from "../services/logger"; + +export function resolveClickHouseUrl(cfg: ClickHouseConfig): string { + if (cfg.url && cfg.url.trim()) { + return cleanClickHouseUrl(cfg.url); + } + if (cfg.host && (cfg.host.startsWith("http://") || cfg.host.startsWith("https://"))) { + return cleanClickHouseUrl(cfg.host); + } + const isSsl = cfg.ssl || cfg.port === 8443 || cfg.port === 443; + const protocol = isSsl ? "https" : "http"; + const port = cfg.port || (isSsl ? 8443 : 8123); + return `${protocol}://${cfg.host || "localhost"}:${port}`; +} + +// ============================================ +// CACHING SYSTEM FOR CLICKHOUSE CONNECTOR +// ============================================ + +const CACHE_TTL = 30_000; +const STATS_CACHE_TTL = 10_000; +const SCHEMA_CACHE_TTL = 60_000; + +class ClickHouseCacheManager { + private tableListCache = new Map>(); + private primaryKeysCache = new Map>(); + private dbStatsCache = new Map>(); + private schemasCache = new Map>(); + private tableDetailsCache = new Map>(); + + private getConfigKey(cfg: ClickHouseConfig): string { + const url = resolveClickHouseUrl(cfg); + return `${url}:${cfg.user || "default"}:${cfg.database || "default"}`; + } + + private getTableKey(cfg: ClickHouseConfig, schema: string, table: string): string { + return `${this.getConfigKey(cfg)}:${schema}:${table}`; + } + + private isValid(entry: CacheEntry | undefined): boolean { + if (!entry) return false; + return Date.now() - entry.timestamp < entry.ttl; + } + + getTableList(cfg: ClickHouseConfig, schema?: string): TableInfo[] | null { + const key = `${this.getConfigKey(cfg)}:${schema || "all"}`; + const entry = this.tableListCache.get(key); + return this.isValid(entry) ? entry!.data : null; + } + setTableList(cfg: ClickHouseConfig, data: TableInfo[], schema?: string): void { + const key = `${this.getConfigKey(cfg)}:${schema || "all"}`; + this.tableListCache.set(key, { data, timestamp: Date.now(), ttl: CACHE_TTL }); + } + + getPrimaryKeys(cfg: ClickHouseConfig, schema: string, table: string): PrimaryKeyInfo[] | null { + const entry = this.primaryKeysCache.get(this.getTableKey(cfg, schema, table)); + return this.isValid(entry) ? entry!.data : null; + } + setPrimaryKeys(cfg: ClickHouseConfig, schema: string, table: string, data: PrimaryKeyInfo[]): void { + this.primaryKeysCache.set(this.getTableKey(cfg, schema, table), { + data, + timestamp: Date.now(), + ttl: CACHE_TTL, + }); + } + + getDBStats(cfg: ClickHouseConfig): DBStats | null { + const entry = this.dbStatsCache.get(this.getConfigKey(cfg)); + return this.isValid(entry) ? entry!.data : null; + } + setDBStats(cfg: ClickHouseConfig, data: DBStats): void { + this.dbStatsCache.set(this.getConfigKey(cfg), { + data, + timestamp: Date.now(), + ttl: STATS_CACHE_TTL, + }); + } + + getSchemas(cfg: ClickHouseConfig): SchemaInfo[] | null { + const entry = this.schemasCache.get(this.getConfigKey(cfg)); + return this.isValid(entry) ? entry!.data : null; + } + setSchemas(cfg: ClickHouseConfig, data: SchemaInfo[]): void { + this.schemasCache.set(this.getConfigKey(cfg), { + data, + timestamp: Date.now(), + ttl: SCHEMA_CACHE_TTL, + }); + } + + getTableDetails(cfg: ClickHouseConfig, schema: string, table: string): ColumnDetail[] | null { + const entry = this.tableDetailsCache.get(this.getTableKey(cfg, schema, table)); + return this.isValid(entry) ? entry!.data : null; + } + setTableDetails(cfg: ClickHouseConfig, schema: string, table: string, data: ColumnDetail[]): void { + this.tableDetailsCache.set(this.getTableKey(cfg, schema, table), { + data, + timestamp: Date.now(), + ttl: CACHE_TTL, + }); + } + + clearForConnection(cfg: ClickHouseConfig): void { + const configKey = this.getConfigKey(cfg); + for (const cache of [this.tableListCache, this.primaryKeysCache, this.tableDetailsCache]) { + for (const [key] of cache) { + if (key.startsWith(configKey)) cache.delete(key); + } + } + this.dbStatsCache.delete(configKey); + this.schemasCache.delete(configKey); + } + + clearTableCache(cfg: ClickHouseConfig, schema: string, table: string): void { + const key = this.getTableKey(cfg, schema, table); + this.primaryKeysCache.delete(key); + this.tableDetailsCache.delete(key); + } + + clearAll(): void { + this.tableListCache.clear(); + this.primaryKeysCache.clear(); + this.dbStatsCache.clear(); + this.schemasCache.clear(); + this.tableDetailsCache.clear(); + } +} + +export const clickhouseCache = new ClickHouseCacheManager(); + +// ============================================ +// CLIENT CONNECTION POOL +// ============================================ + +const clientPool = new Map(); + +function getClientKey(cfg: ClickHouseConfig): string { + const url = resolveClickHouseUrl(cfg); + return `${url}:${cfg.user || "default"}:${cfg.database || "default"}`; +} + +export function getClient(cfg: ClickHouseConfig): ClickHouseClient { + const key = getClientKey(cfg); + let client = clientPool.get(key); + if (!client) { + const url = resolveClickHouseUrl(cfg); + + client = createClient({ + url, + username: cfg.user || "default", + password: cfg.password || "", + database: cfg.database || "default", + request_timeout: 30_000, + clickhouse_settings: { + wait_end_of_query: 1, + }, + }); + + clientPool.set(key, client); + } + return client; +} + +export async function closeClient(cfg: ClickHouseConfig): Promise { + const key = getClientKey(cfg); + const client = clientPool.get(key); + if (client) { + try { + await client.close(); + } catch { + // ignore + } + clientPool.delete(key); + } +} + +export async function closeAllClients(): Promise { + for (const [key, client] of clientPool.entries()) { + try { + await client.close(); + } catch { + // ignore + } + clientPool.delete(key); + } +} + +function formatValue(val: any): string { + if (val === null || val === undefined) return "NULL"; + if (typeof val === "number" || typeof val === "boolean") return String(val); + if (val instanceof Date) return `'${val.toISOString().replace("T", " ").replace("Z", "")}'`; + return `'${String(val).replace(/'/g, "\\'")}'`; +} + +// ============================================ +// CORE CONNECTOR OPERATIONS +// ============================================ + +export async function testConnection( + cfg: ClickHouseConfig, +): Promise<{ ok: boolean; message?: string; status: "connected" | "disconnected" }> { + const url = resolveClickHouseUrl(cfg); + + const tempClient = createClient({ + url, + username: cfg.user || "default", + password: cfg.password || "", + database: cfg.database || "default", + request_timeout: 30_000, + }); + + try { + const pingResult = await tempClient.ping(); + if (!pingResult.success) { + const errMsg = (pingResult as { error?: Error }).error?.message || "Ping failed"; + return { + ok: false, + status: "disconnected", + message: errMsg, + }; + } + + // Ping verifies server is reachable; now verify credentials & permissions + await tempClient.query({ + query: "SELECT 1", + format: "JSONEachRow", + }); + + return { ok: true, status: "connected", message: "Connection successful" }; + } catch (err: any) { + return { + ok: false, + status: "disconnected", + message: (err as Error).message || "Connection failed", + }; + } finally { + try { + await tempClient.close(); + } catch {} + } +} + +export async function listTables( + cfg: ClickHouseConfig, + schemaName?: string, +): Promise { + const cached = clickhouseCache.getTableList(cfg, schemaName); + if (cached) return cached; + + const client = getClient(cfg); + const query = schemaName ? CLICKHOUSE_LIST_TABLES_BY_SCHEMA : CLICKHOUSE_LIST_TABLES; + const query_params = schemaName ? { schema: schemaName } : undefined; + + const res = await client.query({ + query, + query_params, + format: "JSONEachRow", + }); + const data = (await res.json()) as any[]; + + const tables: TableInfo[] = data.map((row) => ({ + schema: row.schema, + name: row.name, + type: row.type || "BASE TABLE", + })); + + clickhouseCache.setTableList(cfg, tables, schemaName); + return tables; +} + +export async function listPrimaryKeys( + cfg: ClickHouseConfig, + schemaNameOrTable: string, + tableNameOrSchema?: string, +): Promise { + let schema = cfg.database || "default"; + let table = schemaNameOrTable; + + if (tableNameOrSchema) { + schema = schemaNameOrTable || cfg.database || "default"; + table = tableNameOrSchema; + } + + const cached = clickhouseCache.getPrimaryKeys(cfg, schema, table); + if (cached) return cached; + + const client = getClient(cfg); + const res = await client.query({ + query: CLICKHOUSE_GET_PRIMARY_KEYS, + query_params: { schema, table }, + format: "JSONEachRow", + }); + const data = (await res.json()) as any[]; + + let pks: PrimaryKeyInfo[] = data.map((r) => ({ + column_name: r.column_name, + })); + + // If no primary key or sorting key is defined, fall back to the first column + if (pks.length === 0) { + try { + const cols = await getTableDetails(cfg, schema, table); + if (cols.length > 0) { + pks = [{ column_name: cols[0].name }]; + } + } catch {} + } + + clickhouseCache.setPrimaryKeys(cfg, schema, table, pks); + return pks; +} + +export async function listForeignKeys( + _cfg: ClickHouseConfig, + _schemaNameOrTable: string, + _tableNameOrSchema?: string, +): Promise { + // ClickHouse is an analytical OLAP database and does not enforce foreign keys + return []; +} + +export async function listIndexes( + cfg: ClickHouseConfig, + schemaNameOrTable: string, + tableNameOrSchema?: string, +): Promise { + let schema = cfg.database || "default"; + let table = schemaNameOrTable; + + if (tableNameOrSchema) { + schema = schemaNameOrTable || cfg.database || "default"; + table = tableNameOrSchema; + } + + const client = getClient(cfg); + + try { + const res = await client.query({ + query: CLICKHOUSE_GET_INDEXES, + query_params: { schema, table }, + format: "JSONEachRow", + }); + const data = (await res.json()) as any[]; + + return data.map((r) => ({ + table_name: table, + index_name: r.index_name, + column_name: r.column_name, + is_unique: false, + is_primary: false, + index_type: r.index_type, + })); + } catch { + return []; + } +} + +export async function listUniqueConstraints( + _cfg: ClickHouseConfig, + _schemaNameOrTable: string, + _tableNameOrSchema?: string, +): Promise { + return []; +} + +export async function listCheckConstraints( + _cfg: ClickHouseConfig, + _schemaNameOrTable: string, + _tableNameOrSchema?: string, +): Promise { + return []; +} + +export async function getDBStats(cfg: ClickHouseConfig): Promise { + const cached = clickhouseCache.getDBStats(cfg); + if (cached) return cached; + + const client = getClient(cfg); + const database = cfg.database || "default"; + + let total_tables = 0; + let total_rows = 0; + let total_db_size_mb = 0; + + try { + const statsRes = await client.query({ + query: CLICKHOUSE_GET_DB_STATS, + query_params: { database }, + format: "JSONEachRow", + }); + const statsData = (await statsRes.json()) as any[]; + if (statsData.length > 0) { + total_tables = Number(statsData[0].total_tables) || 0; + total_rows = Number(statsData[0].total_rows) || 0; + } + } catch { } + + try { + const sizeRes = await client.query({ + query: CLICKHOUSE_GET_DB_SIZE, + query_params: { database }, + format: "JSONEachRow", + }); + const sizeData = (await sizeRes.json()) as any[]; + if (sizeData.length > 0) { + total_db_size_mb = Number(sizeData[0].total_db_size_mb) || 0; + } + } catch { } + + const stats: DBStats = { + total_tables, + total_rows, + total_db_size_mb, + }; + + clickhouseCache.setDBStats(cfg, stats); + return stats; +} + +export async function listSchemas(cfg: ClickHouseConfig): Promise { + const cached = clickhouseCache.getSchemas(cfg); + if (cached) return cached; + + const client = getClient(cfg); + const res = await client.query({ + query: CLICKHOUSE_LIST_SCHEMAS, + format: "JSONEachRow", + }); + const data = (await res.json()) as any[]; + + const schemas: SchemaInfo[] = data.map((r) => ({ name: r.name })); + clickhouseCache.setSchemas(cfg, schemas); + return schemas; +} + +export async function listSchemaNames(cfg: ClickHouseConfig): Promise { + const schemas = await listSchemas(cfg); + return schemas.map((s) => s.name); +} + +export async function getTableDetails( + cfg: ClickHouseConfig, + schemaNameOrTable: string, + tableNameOrSchema?: string, +): Promise { + let schema = cfg.database || "default"; + let table = schemaNameOrTable; + + if (tableNameOrSchema) { + schema = schemaNameOrTable || cfg.database || "default"; + table = tableNameOrSchema; + } + + const cached = clickhouseCache.getTableDetails(cfg, schema, table); + if (cached) return cached; + + const client = getClient(cfg); + const res = await client.query({ + query: CLICKHOUSE_LIST_COLUMNS, + query_params: { schema, table }, + format: "JSONEachRow", + }); + const data = (await res.json()) as any[]; + + const columns: ColumnDetail[] = data.map((r) => ({ + name: r.name, + type: r.type, + not_nullable: Boolean(r.not_nullable), + default_value: r.default_value || null, + is_primary_key: Boolean(r.is_in_primary_key || r.is_in_sorting_key), + is_foreign_key: false, + comment: r.comment || null, + })); + + clickhouseCache.setTableDetails(cfg, schema, table, columns); + return columns; +} + +export async function getSchemaMetadataBatch( + cfg: ClickHouseConfig, + schemaNameOrTables?: string | string[], + maybeSchemaName?: string, +): Promise { + let schema = cfg.database || "default"; + let filterTables: Set | null = null; + + if (Array.isArray(schemaNameOrTables)) { + filterTables = new Set(schemaNameOrTables); + if (maybeSchemaName) schema = maybeSchemaName; + } else if (typeof schemaNameOrTables === "string") { + schema = schemaNameOrTables; + } + + const client = getClient(cfg); + + const tablesMap = new Map< + string, + { + columns: ColumnDetail[]; + primaryKeys: PrimaryKeyInfo[]; + foreignKeys: ForeignKeyInfo[]; + indexes: IndexInfo[]; + uniqueConstraints: UniqueConstraintInfo[]; + checkConstraints: CheckConstraintInfo[]; + } + >(); + + function getOrCreateTable(tableName: string) { + let entry = tablesMap.get(tableName); + if (!entry) { + entry = { + columns: [], + primaryKeys: [], + foreignKeys: [], + indexes: [], + uniqueConstraints: [], + checkConstraints: [], + }; + tablesMap.set(tableName, entry); + } + return entry; + } + + try { + const tables = await listTables(cfg, schema); + for (const t of tables) { + if (!filterTables || filterTables.has(t.name)) { + getOrCreateTable(t.name); + } + } + } catch (err: any) { + logger.warn({ err: err?.message, schema }, "ClickHouse getSchemaMetadataBatch listTables failed"); + } + + try { + const colRes = await client.query({ + query: CLICKHOUSE_BATCH_LIST_COLUMNS, + query_params: { schema }, + format: "JSONEachRow", + }); + const colData = (await colRes.json()) as any[]; + + for (const r of colData) { + if (filterTables && !filterTables.has(r.table)) continue; + const entry = getOrCreateTable(r.table); + entry.columns.push({ + name: r.name, + type: r.type, + not_nullable: Boolean(r.not_nullable), + default_value: r.default_value || null, + is_primary_key: Boolean(r.is_in_primary_key || r.is_in_sorting_key), + is_foreign_key: false, + comment: r.comment || null, + }); + } + } catch (err: any) { + logger.warn({ err: err?.message, schema }, "ClickHouse CLICKHOUSE_BATCH_LIST_COLUMNS failed"); + } + + try { + const pkRes = await client.query({ + query: CLICKHOUSE_BATCH_GET_PRIMARY_KEYS, + query_params: { schema }, + format: "JSONEachRow", + }); + const pkData = (await pkRes.json()) as any[]; + + for (const r of pkData) { + if (filterTables && !filterTables.has(r.table)) continue; + const entry = getOrCreateTable(r.table); + entry.primaryKeys.push({ column_name: r.column_name }); + } + } catch (err: any) { + logger.warn({ err: err?.message, schema }, "ClickHouse CLICKHOUSE_BATCH_GET_PRIMARY_KEYS failed"); + } + + try { + const idxRes = await client.query({ + query: CLICKHOUSE_BATCH_GET_INDEXES, + query_params: { schema }, + format: "JSONEachRow", + }); + const idxData = (await idxRes.json()) as any[]; + + for (const r of idxData) { + if (filterTables && !filterTables.has(r.table)) continue; + const entry = getOrCreateTable(r.table); + entry.indexes.push({ + table_name: r.table, + index_name: r.index_name, + column_name: r.column_name, + is_unique: false, + is_primary: false, + index_type: r.index_type, + }); + } + } catch (err: any) { + logger.debug({ err: err?.message, schema }, "ClickHouse CLICKHOUSE_BATCH_GET_INDEXES skipped/failed"); + } + + // Fallback: Ensure every table has columns and primary keys populated + for (const [tableName, entry] of tablesMap.entries()) { + if (entry.columns.length === 0) { + try { + const fallbackCols = await getTableDetails(cfg, schema, tableName); + if (fallbackCols && fallbackCols.length > 0) { + entry.columns = fallbackCols; + } + } catch (err: any) { + logger.warn({ err: err?.message, schema, tableName }, "Fallback getTableDetails failed"); + } + } + if (entry.primaryKeys.length === 0) { + try { + const fallbackPks = await listPrimaryKeys(cfg, schema, tableName); + if (fallbackPks && fallbackPks.length > 0) { + entry.primaryKeys = fallbackPks; + } + } catch (err: any) { + logger.debug({ err: err?.message, schema, tableName }, "Fallback listPrimaryKeys failed"); + } + } + } + + return { tables: tablesMap }; +} + +export function streamQueryCancelable( + cfg: ClickHouseConfig, + sql: string, + batchSize: number = 1000, + onBatch: (rows: any[], columns: { name: string }[]) => Promise | void, + onDone?: () => void, +): { promise: Promise; cancel: () => Promise } { + let cancelled = false; + const abortController = new AbortController(); + + const promise = (async () => { + const client = getClient(cfg); + const resultSet = await client.query({ + query: sql, + format: "JSONEachRow", + abort_signal: abortController.signal, + }); + + if (cancelled) return; + + let columns: { name: string }[] | null = null; + const buffer: any[] = []; + + const flush = async () => { + if (buffer.length === 0) return; + const batch = buffer.splice(0, buffer.length); + await onBatch(batch, columns || []); + }; + + const stream = resultSet.stream(); + for await (const rows of stream) { + if (cancelled) break; + for (const row of rows as any[]) { + if (cancelled) break; + const jsonObj = typeof row.json === "function" ? row.json() : row; + if (!columns && jsonObj && typeof jsonObj === "object") { + columns = Object.keys(jsonObj).map((k) => ({ name: k })); + } + buffer.push(jsonObj); + if (buffer.length >= batchSize) { + await flush(); + } + } + } + + if (!cancelled) { + await flush(); + if (onDone) onDone(); + } + })(); + + async function cancel() { + cancelled = true; + abortController.abort(); + } + + return { promise, cancel }; +} + +export async function fetchTableData( + cfg: ClickHouseConfig, + schemaName: string, + tableName: string, + limit: number, + page: number, + sortBy?: string, + sortOrder: "ASC" | "DESC" = "ASC", +): Promise<{ rows: any[]; total: number }> { + const client = getClient(cfg); + const offset = Math.max(0, (page - 1) * limit); + + // Total rows + const countRes = await client.query({ + query: `SELECT count() AS total FROM \`${schemaName}\`.\`${tableName}\``, + format: "JSONEachRow", + }); + const countData = (await countRes.json()) as any[]; + const total = countData.length > 0 ? Number(countData[0].total) : 0; + + // Data rows + const orderClause = sortBy ? ` ORDER BY \`${sortBy}\` ${sortOrder}` : ""; + const dataRes = await client.query({ + query: `SELECT * FROM \`${schemaName}\`.\`${tableName}\`${orderClause} LIMIT ${limit} OFFSET ${offset}`, + format: "JSONEachRow", + }); + const rows = (await dataRes.json()) as any[]; + + return { rows, total }; +} + +export async function createTable( + cfg: ClickHouseConfig, + schemaName: string, + tableName: string, + columns: any[], + primaryKey?: string | string[], + ifNotExists: boolean = true, +): Promise { + const client = getClient(cfg); + const colDefs = columns + .map((col) => { + let def = `\`${col.name}\` ${col.type}`; + if (col.default_value) def += ` DEFAULT ${col.default_value}`; + return def; + }) + .join(",\n "); + + const pkCols = Array.isArray(primaryKey) + ? primaryKey + : primaryKey + ? [primaryKey] + : []; + const orderClause = pkCols.length > 0 ? pkCols.map((c) => `\`${c}\``).join(", ") : "tuple()"; + const ifNotExistsClause = ifNotExists ? "IF NOT EXISTS " : ""; + + const sql = `CREATE TABLE ${ifNotExistsClause}\`${schemaName}\`.\`${tableName}\` ( + ${colDefs} +) ENGINE = MergeTree() ORDER BY (${orderClause});`; + + await client.command({ query: sql }); + clickhouseCache.clearForConnection(cfg); +} + +export async function createIndexes( + cfg: ClickHouseConfig, + schemaName: string, + tableName: string, + indexes: any[], +): Promise { + const client = getClient(cfg); + for (const idx of indexes) { + const cols = Array.isArray(idx.columns) ? idx.columns.join(", ") : idx.column; + const type = idx.type || "minmax"; + const granularity = idx.granularity || 1; + await client.command({ + query: `ALTER TABLE \`${schemaName}\`.\`${tableName}\` ADD INDEX \`${idx.name}\` (${cols}) TYPE ${type} GRANULARITY ${granularity}`, + }); + } + clickhouseCache.clearTableCache(cfg, schemaName, tableName); +} + +export async function alterTable( + cfg: ClickHouseConfig, + schemaName: string, + tableName: string, + alterations: any[], +): Promise { + const client = getClient(cfg); + for (const alt of alterations) { + let clause = ""; + if (alt.type === "ADD_COLUMN") { + clause = `ADD COLUMN \`${alt.columnName}\` ${alt.columnDef}`; + } else if (alt.type === "DROP_COLUMN") { + clause = `DROP COLUMN \`${alt.columnName}\``; + } else if (alt.type === "RENAME_COLUMN") { + clause = `RENAME COLUMN \`${alt.oldName}\` TO \`${alt.newName}\``; + } else if (alt.type === "ALTER_COLUMN" || alt.type === "MODIFY_COLUMN") { + clause = `MODIFY COLUMN \`${alt.columnName}\` ${alt.newDef}`; + } else if (alt.type === "RENAME_TABLE") { + await client.command({ + query: `RENAME TABLE \`${schemaName}\`.\`${tableName}\` TO \`${schemaName}\`.\`${alt.newName}\``, + }); + continue; + } + if (clause) { + await client.command({ + query: `ALTER TABLE \`${schemaName}\`.\`${tableName}\` ${clause}`, + }); + } + } + clickhouseCache.clearForConnection(cfg); +} + +export async function dropTable( + cfg: ClickHouseConfig, + schemaName: string, + tableName: string, + ifExists: boolean = true, +): Promise { + const client = getClient(cfg); + const ifExistsClause = ifExists ? "IF EXISTS " : ""; + await client.command({ + query: `DROP TABLE ${ifExistsClause}\`${schemaName}\`.\`${tableName}\``, + }); + clickhouseCache.clearForConnection(cfg); +} + +export async function ensureMigrationTable( + cfg: ClickHouseConfig, + schemaName?: string, +): Promise { + const schema = schemaName || cfg.database || "default"; + const client = getClient(cfg); + await client.command({ + query: `CREATE DATABASE IF NOT EXISTS \`${schema}\``, + }); + await client.command({ + query: `CREATE TABLE IF NOT EXISTS \`${schema}\`.schema_migrations ( + version String, + name String, + checksum String, + applied_at DateTime DEFAULT now() + ) ENGINE = MergeTree() ORDER BY version;`, + }); +} + +export async function hasAnyMigrations( + cfg: ClickHouseConfig, + schemaName?: string, +): Promise { + const schema = schemaName || cfg.database || "default"; + const client = getClient(cfg); + try { + const res = await client.query({ + query: `SELECT count() AS cnt FROM \`${schema}\`.schema_migrations`, + format: "JSONEachRow", + }); + const data = (await res.json()) as any[]; + return data.length > 0 && Number(data[0].cnt) > 0; + } catch (err: any) { + if (String(err).includes("doesn't exist") || String(err).includes("UNKNOWN_TABLE")) { + return false; + } + throw err; + } +} + +export async function insertBaseline( + cfg: ClickHouseConfig, + version: string, + name: string, + checksum: string, + schemaName?: string, +): Promise { + await ensureMigrationTable(cfg, schemaName); + const schema = schemaName || cfg.database || "default"; + const client = getClient(cfg); + await client.insert({ + table: `\`${schema}\`.schema_migrations`, + values: [{ version, name, checksum }], + format: "JSONEachRow", + }); +} + +export async function baselineIfNeeded( + cfg: ClickHouseConfig, + migrationsDir: string, + snapshot?: SchemaFile, + schemaName?: string, +): Promise<{ baselined: boolean; version?: string }> { + await ensureMigrationTable(cfg, schemaName); + const hasMigrations = await hasAnyMigrations(cfg, schemaName); + if (hasMigrations) return { baselined: false }; + + const version = Date.now().toString(); + const name = "baseline_existing_schema"; + + const fakeSnapshot = snapshot || { + version: 2, + projectId: "", + databaseId: "", + dialect: "clickhouse", + schemas: [], + cachedAt: "", + relwaveVersion: "", + schemaHash: "", + }; + + const { writeBaselineMigration } = await import("../utils/baselineMigration"); + const filePath = writeBaselineMigration(migrationsDir, version, name, fakeSnapshot as any); + const checksum = crypto.createHash("sha256").update(fs.readFileSync(filePath)).digest("hex"); + + await insertBaseline(cfg, version, name, checksum, schemaName); + return { baselined: true, version }; +} + +export async function listAppliedMigrations( + cfg: ClickHouseConfig, + schemaName?: string, +): Promise { + const schema = schemaName || cfg.database || "default"; + await ensureMigrationTable(cfg, schema); + const client = getClient(cfg); + + try { + const res = await client.query({ + query: `SELECT version, name, checksum, toString(applied_at) AS applied_at FROM \`${schema}\`.schema_migrations ORDER BY version ASC`, + format: "JSONEachRow", + }); + const rows = (await res.json()) as any[]; + return rows.map((r) => ({ + version: r.version, + name: r.name, + checksum: r.checksum || "", + applied_at: new Date(r.applied_at), + })); + } catch { + return []; + } +} + +export function splitClickHouseStatements(sql: string): string[] { + const cleanSql = sql + .split("\n") + .map((line) => { + const trimmed = line.trim(); + if (trimmed.startsWith("--")) return ""; + return line; + }) + .join("\n"); + + return cleanSql + .split(";") + .map((s) => s.trim()) + .map((s) => { + if (/^\s*DROP\s+TABLE\b/i.test(s)) { + return s.replace(/\s+(?:CASCADE|RESTRICT)\s*$/i, "").trim(); + } + return s; + }) + .filter((s) => s.length > 0); +} + +export async function connectToDatabase( + cfg: ClickHouseConfig, + connectionId: string, + options?: { readOnly?: boolean }, +) { + let baselineResult = { baselined: false }; + const { projectStoreInstance } = await import("../services/projectStore"); + const { loadLocalMigrations } = await import("../utils/baselineMigration"); + const migrationsDir = await projectStoreInstance.resolveMigrationsDir(connectionId); + if (!fs.existsSync(migrationsDir)) { + fs.mkdirSync(migrationsDir, { recursive: true }); + } + + if (!options?.readOnly) { + let snapshot: any = undefined; + try { + const project = await projectStoreInstance.getProjectByDatabaseId(connectionId); + if (project) { + snapshot = (await projectStoreInstance.getSchema(project.id)) || undefined; + } + } catch {} + baselineResult = await baselineIfNeeded(cfg, migrationsDir, snapshot); + } + + const schema = await listSchemas(cfg); + const localMigrations = await loadLocalMigrations(migrationsDir); + const appliedMigrations = await listAppliedMigrations(cfg); + + return { + baselined: baselineResult.baselined, + schema, + migrations: { + local: localMigrations, + applied: appliedMigrations, + }, + }; +} + +export async function applyMigration( + cfg: ClickHouseConfig, + migrationFilePath: string, + schemaName?: string, +): Promise { + const schema = schemaName || cfg.database || "default"; + await ensureMigrationTable(cfg, schema); + const { readMigrationFile } = await import("../utils/migrationFileReader"); + const migration = readMigrationFile(migrationFilePath); + + const client = getClient(cfg); + const stmts = splitClickHouseStatements(migration.upSQL); + + for (const stmt of stmts) { + await client.command({ query: stmt }); + } + + await client.insert({ + table: `\`${schema}\`.schema_migrations`, + values: [{ version: migration.version, name: migration.name, checksum: migration.checksum }], + format: "JSONEachRow", + }); + clickhouseCache.clearForConnection(cfg); +} + +export async function rollbackMigration( + cfg: ClickHouseConfig, + version: string, + migrationFilePath: string, + schemaName?: string, +): Promise { + const schema = schemaName || cfg.database || "default"; + await ensureMigrationTable(cfg, schema); + const { readMigrationFile } = await import("../utils/migrationFileReader"); + const migration = readMigrationFile(migrationFilePath); + + const client = getClient(cfg); + const stmts = splitClickHouseStatements(migration.downSQL); + + if (stmts.length === 0) { + throw new Error( + `Cannot rollback migration "${migration.name}": no executable SQL found in DOWN section.` + ); + } + + for (const stmt of stmts) { + await client.command({ query: stmt }); + } + + await client.command({ + query: `ALTER TABLE \`${schema}\`.schema_migrations DELETE WHERE version = '${version.replace(/'/g, "\\'")}'`, + clickhouse_settings: { + mutations_sync: "1", + }, + }); + clickhouseCache.clearForConnection(cfg); +} + +export async function insertRow( + cfg: ClickHouseConfig, + schemaName: string, + tableName: string, + row: Record, +): Promise { + const client = getClient(cfg); + await client.insert({ + table: `\`${schemaName}\`.\`${tableName}\``, + values: [row], + format: "JSONEachRow", + }); + clickhouseCache.clearTableCache(cfg, schemaName, tableName); + return row; +} + +export async function updateRow( + cfg: ClickHouseConfig, + schemaName: string, + tableName: string, + primaryKeyColumnOrObj: string | { column: string; value: any } | Record, + primaryKeyValueOrRow?: any, + rowData?: Record, +): Promise { + const client = getClient(cfg); + let updates: Record = {}; + let whereClause = ""; + + if (rowData !== undefined && typeof primaryKeyColumnOrObj === "string") { + // Called as: (cfg, schemaName, tableName, primaryKeyColumn, primaryKeyValue, rowData) + updates = rowData; + whereClause = `\`${primaryKeyColumnOrObj}\` = ${formatValue(primaryKeyValueOrRow)}`; + } else if (typeof primaryKeyColumnOrObj === "object" && primaryKeyColumnOrObj !== null) { + if ("column" in primaryKeyColumnOrObj && "value" in primaryKeyColumnOrObj) { + whereClause = `\`${primaryKeyColumnOrObj.column}\` = ${formatValue(primaryKeyColumnOrObj.value)}`; + } else { + whereClause = Object.entries(primaryKeyColumnOrObj) + .map(([col, val]) => `\`${col}\` = ${formatValue(val)}`) + .join(" AND "); + } + updates = primaryKeyValueOrRow || {}; + } else if (typeof primaryKeyColumnOrObj === "string" && primaryKeyValueOrRow && typeof primaryKeyValueOrRow === "object") { + updates = primaryKeyValueOrRow; + whereClause = `\`${primaryKeyColumnOrObj}\` = ${formatValue(rowData)}`; + } + + const updateEntries = Object.entries(updates); + if (updateEntries.length === 0) { + throw new Error("No data provided for update"); + } + if (!whereClause) { + throw new Error("Primary key condition required for update"); + } + + const sets = updateEntries + .map(([col, val]) => `\`${col}\` = ${formatValue(val)}`) + .join(", "); + + await client.command({ + query: `ALTER TABLE \`${schemaName}\`.\`${tableName}\` UPDATE ${sets} WHERE ${whereClause}`, + clickhouse_settings: { + mutations_sync: "1", + }, + }); + clickhouseCache.clearTableCache(cfg, schemaName, tableName); + + try { + const fetchRes = await client.query({ + query: `SELECT * FROM \`${schemaName}\`.\`${tableName}\` WHERE ${whereClause} LIMIT 1`, + format: "JSONEachRow", + }); + const fetched = (await fetchRes.json()) as any[]; + return fetched[0] || updates; + } catch { + return updates; + } +} + +export async function deleteRow( + cfg: ClickHouseConfig, + schemaName: string, + tableName: string, + primaryKeyColumnOrObj: string | { column: string; value: any } | Record, + primaryKeyValue?: any, +): Promise { + const client = getClient(cfg); + let whereClause = ""; + + if (typeof primaryKeyColumnOrObj === "string") { + whereClause = `\`${primaryKeyColumnOrObj}\` = ${formatValue(primaryKeyValue)}`; + } else if (typeof primaryKeyColumnOrObj === "object" && primaryKeyColumnOrObj !== null) { + if ("column" in primaryKeyColumnOrObj && "value" in primaryKeyColumnOrObj) { + whereClause = `\`${primaryKeyColumnOrObj.column}\` = ${formatValue(primaryKeyColumnOrObj.value)}`; + } else { + whereClause = Object.entries(primaryKeyColumnOrObj) + .map(([col, val]) => `\`${col}\` = ${formatValue(val)}`) + .join(" AND "); + } + } + + if (!whereClause) { + throw new Error("Primary key condition required for delete"); + } + + await client.command({ + query: `ALTER TABLE \`${schemaName}\`.\`${tableName}\` DELETE WHERE ${whereClause}`, + clickhouse_settings: { + mutations_sync: "1", + }, + }); + clickhouseCache.clearTableCache(cfg, schemaName, tableName); + return true; +} + +export async function searchTable( + cfg: ClickHouseConfig, + schemaName: string, + tableName: string, + searchTerm: string, + column?: string, + page: number = 1, + pageSize: number = 50, +): Promise<{ rows: any[]; total: number }> { + const client = getClient(cfg); + const offset = Math.max(0, (page - 1) * pageSize); + const escapedTerm = searchTerm.replace(/'/g, "\\'"); + + let whereConditions = ""; + if (column && column.trim()) { + whereConditions = `\`${column.trim()}\` ILIKE '%${escapedTerm}%'`; + } else { + const cols = await getTableDetails(cfg, schemaName, tableName); + const textCols = cols.filter( + (c) => + c.type.toLowerCase().includes("string") || + c.type.toLowerCase().includes("char") || + c.type.toLowerCase().includes("text"), + ); + + if (textCols.length === 0) { + return fetchTableData(cfg, schemaName, tableName, pageSize, page); + } + + whereConditions = textCols + .map((c) => `\`${c.name}\` ILIKE '%${escapedTerm}%'`) + .join(" OR "); + } + + const countRes = await client.query({ + query: `SELECT count() AS total FROM \`${schemaName}\`.\`${tableName}\` WHERE ${whereConditions}`, + format: "JSONEachRow", + }); + const countData = (await countRes.json()) as any[]; + const total = countData.length > 0 ? Number(countData[0].total) : 0; + + const dataRes = await client.query({ + query: `SELECT * FROM \`${schemaName}\`.\`${tableName}\` WHERE ${whereConditions} LIMIT ${pageSize} OFFSET ${offset}`, + format: "JSONEachRow", + }); + const rows = (await dataRes.json()) as any[]; + + return { rows, total }; +} diff --git a/bridge/src/queries/clickhouse/constraints.ts b/bridge/src/queries/clickhouse/constraints.ts new file mode 100644 index 00000000..19081de6 --- /dev/null +++ b/bridge/src/queries/clickhouse/constraints.ts @@ -0,0 +1,54 @@ +/** + * ClickHouse Constraint & Index Queries + */ + +/** + * List primary key / sorting key columns for a table. + */ +export const CLICKHOUSE_GET_PRIMARY_KEYS = ` + SELECT name AS column_name + FROM system.columns + WHERE database = {schema: String} + AND table = {table: String} + AND (is_in_primary_key = 1 OR is_in_sorting_key = 1) + ORDER BY position; +`; + +/** + * Batch get primary keys for all tables in a schema. + */ +export const CLICKHOUSE_BATCH_GET_PRIMARY_KEYS = ` + SELECT table, name AS column_name + FROM system.columns + WHERE database = {schema: String} + AND (is_in_primary_key = 1 OR is_in_sorting_key = 1) + ORDER BY table, position; +`; + +/** + * List secondary (data skipping) indexes for a table. + */ +export const CLICKHOUSE_GET_INDEXES = ` + SELECT + name AS index_name, + expr AS column_name, + type AS index_type + FROM system.data_skipping_indices + WHERE database = {schema: String} + AND table = {table: String} + ORDER BY name; +`; + +/** + * Batch get indexes for all tables in a schema. + */ +export const CLICKHOUSE_BATCH_GET_INDEXES = ` + SELECT + table, + name AS index_name, + expr AS column_name, + type AS index_type + FROM system.data_skipping_indices + WHERE database = {schema: String} + ORDER BY table, name; +`; diff --git a/bridge/src/queries/clickhouse/migrations.ts b/bridge/src/queries/clickhouse/migrations.ts new file mode 100644 index 00000000..ca129304 --- /dev/null +++ b/bridge/src/queries/clickhouse/migrations.ts @@ -0,0 +1,29 @@ +/** + * ClickHouse Migration Queries + */ + +export const CLICKHOUSE_CREATE_MIGRATION_TABLE = (schema: string) => ` + CREATE TABLE IF NOT EXISTS \`${schema}\`.schema_migrations ( + version String, + name String, + applied_at DateTime DEFAULT now() + ) ENGINE = MergeTree() ORDER BY version; +`; + +export const CLICKHOUSE_CHECK_MIGRATIONS_EXIST = (schema: string) => ` + SELECT count() AS cnt + FROM \`${schema}\`.schema_migrations; +`; + +export const CLICKHOUSE_LIST_APPLIED_MIGRATIONS = (schema: string) => ` + SELECT + version, + name, + toString(applied_at) AS applied_at + FROM \`${schema}\`.schema_migrations + ORDER BY version ASC; +`; + +export const CLICKHOUSE_DELETE_MIGRATION = (schema: string) => ` + ALTER TABLE \`${schema}\`.schema_migrations DELETE WHERE version = {version: String}; +`; diff --git a/bridge/src/queries/clickhouse/schema.ts b/bridge/src/queries/clickhouse/schema.ts new file mode 100644 index 00000000..dd538d26 --- /dev/null +++ b/bridge/src/queries/clickhouse/schema.ts @@ -0,0 +1,75 @@ +/** + * ClickHouse Schema-related Queries + */ + +/** + * List all user databases/schemas (excludes internal system schemas). + */ +export const CLICKHOUSE_LIST_SCHEMAS = ` + SELECT name + FROM system.databases + WHERE name NOT IN ('system', 'information_schema', 'INFORMATION_SCHEMA') + ORDER BY name; +`; + +/** + * List all tables across all non-system databases. + */ +export const CLICKHOUSE_LIST_TABLES = ` + SELECT + database AS schema, + name, + if(engine LIKE '%View%', 'VIEW', 'BASE TABLE') AS type + FROM system.tables + WHERE database NOT IN ('system', 'information_schema', 'INFORMATION_SCHEMA') + ORDER BY database, name; +`; + +/** + * List tables filtered by schema/database. + */ +export const CLICKHOUSE_LIST_TABLES_BY_SCHEMA = ` + SELECT + database AS schema, + name, + if(engine LIKE '%View%', 'VIEW', 'BASE TABLE') AS type + FROM system.tables + WHERE database = {schema: String} + ORDER BY name; +`; + +/** + * List all column details for a specific table. + */ +export const CLICKHOUSE_LIST_COLUMNS = ` + SELECT + name, + type, + if(type LIKE 'Nullable(%)', 0, 1) AS not_nullable, + default_expression AS default_value, + is_in_primary_key, + is_in_sorting_key, + comment + FROM system.columns + WHERE database = {schema: String} AND table = {table: String} + ORDER BY position; +`; + +/** + * Batch query column details for multiple tables. + */ +export const CLICKHOUSE_BATCH_LIST_COLUMNS = ` + SELECT + database AS schema, + table, + name, + type, + if(type LIKE 'Nullable(%)', 0, 1) AS not_nullable, + default_expression AS default_value, + is_in_primary_key, + is_in_sorting_key, + comment + FROM system.columns + WHERE database = {schema: String} + ORDER BY table, position; +`; diff --git a/bridge/src/queries/clickhouse/stats.ts b/bridge/src/queries/clickhouse/stats.ts new file mode 100644 index 00000000..96460590 --- /dev/null +++ b/bridge/src/queries/clickhouse/stats.ts @@ -0,0 +1,24 @@ +/** + * ClickHouse Statistics Queries + */ + +/** + * Get total tables and total rows for a database. + */ +export const CLICKHOUSE_GET_DB_STATS = ` + SELECT + count() AS total_tables, + coalesce(sum(total_rows), 0) AS total_rows + FROM system.tables + WHERE database = {database: String}; +`; + +/** + * Get active data size on disk in MB for a database. + */ +export const CLICKHOUSE_GET_DB_SIZE = ` + SELECT + coalesce(round(sum(bytes_on_disk) / (1024 * 1024), 2), 0) AS total_db_size_mb + FROM system.parts + WHERE database = {database: String} AND active = 1; +`; diff --git a/bridge/src/services/connectionBuilder.ts b/bridge/src/services/connectionBuilder.ts index 7816eb6c..92b0f77b 100644 --- a/bridge/src/services/connectionBuilder.ts +++ b/bridge/src/services/connectionBuilder.ts @@ -1,6 +1,7 @@ import { DBType, DatabaseConfig } from "../types"; import { SQLiteConfig } from "../types/sqlite"; import { isWindowsDriveRootPath, normalizeSQLitePath } from "../utils/sqlitePath"; +import { parseClickHouseUrl } from "../utils/clickhouseUrl"; import { sshTunnelServiceInstance, TunnelInfo } from "./sshTunnelService"; export interface BuildResult { @@ -32,13 +33,37 @@ export class ConnectionBuilder { }; } + if (dbType === DBType.CLICKHOUSE) { + const rawTarget = db.url || db.host; + if (!rawTarget) { + throw new Error("ClickHouse connection requires a URL or host"); + } + const parsed = parseClickHouseUrl(rawTarget); + return { + config: { + url: parsed.url, + host: db.host || parsed.host, + port: db.port || parsed.port, + user: db.user || "default", + password: pwd ?? undefined, + database: db.database || parsed.database || "default", + ssl: db.ssl !== undefined ? db.ssl : parsed.ssl, + } as any, + }; + } + + const defaultPort = + dbType === DBType.MYSQL || dbType === DBType.MARIADB + ? 3306 + : 5432; + const base: DatabaseConfig = { host: db.host, - port: db.port || (dbType === DBType.MYSQL || dbType === DBType.MARIADB ? 3306 : 5432), - user: db.user, + port: db.port || defaultPort, + user: db.user || "", password: pwd ?? undefined, ssl: db.ssl, - database: db.database, + database: db.database || "", ssh: db.ssh, }; @@ -77,4 +102,8 @@ export class ConnectionBuilder { static async buildDuckDBConnection(db: any): Promise { return this.buildConnection(db, null, DBType.DUCKDB); } + + static async buildClickHouseConnection(db: any, pwd: string | null): Promise { + return this.buildConnection(db, pwd, DBType.CLICKHOUSE); + } } diff --git a/bridge/src/services/connectorRegistry.ts b/bridge/src/services/connectorRegistry.ts index 286215f7..fe84ca45 100644 --- a/bridge/src/services/connectorRegistry.ts +++ b/bridge/src/services/connectorRegistry.ts @@ -12,13 +12,15 @@ import * as mysqlConnector from "../connectors/mysql"; import * as mariadbConnector from "../connectors/mariadb"; import * as sqliteConnector from "../connectors/sqlite"; import * as duckdbConnector from "../connectors/duckdb"; +import * as clickhouseConnector from "../connectors/clickhouse"; export type Connector = | typeof postgresConnector | typeof mysqlConnector | typeof mariadbConnector | typeof sqliteConnector - | typeof duckdbConnector; + | typeof duckdbConnector + | typeof clickhouseConnector; const registry: Record = { [DBType.POSTGRES]: postgresConnector, @@ -26,6 +28,7 @@ const registry: Record = { [DBType.MARIADB]: mariadbConnector, [DBType.SQLITE]: sqliteConnector, [DBType.DUCKDB]: duckdbConnector, + [DBType.CLICKHOUSE]: clickhouseConnector, }; /** diff --git a/bridge/src/services/databaseService.ts b/bridge/src/services/databaseService.ts index bd481c4b..321dbae1 100644 --- a/bridge/src/services/databaseService.ts +++ b/bridge/src/services/databaseService.ts @@ -62,9 +62,13 @@ export class DatabaseService { const isFileBasedDB = ["sqlite", "duckdb"].includes( (payload.type as string | undefined)?.toLowerCase() || "", ); + const isClickHouse = + (payload.type as string | undefined)?.toLowerCase() === "clickhouse"; const required = isFileBasedDB ? ["name", "database", "type"] - : ["name", "host", "port", "user", "database", "type"]; + : isClickHouse + ? ["name", "type"] + : ["name", "host", "port", "user", "database", "type"]; for (const field of required) { if (!payload[field]) throw new Error(`Missing required field: ${field}`); } @@ -84,6 +88,21 @@ export class DatabaseService { ...payload, database: (config as any).path, }; + } else if (isClickHouse) { + if (!payload.url && !payload.host) { + throw new Error("Missing required field: url"); + } + const { parseClickHouseUrl } = await import("../utils/clickhouseUrl"); + const parsed = parseClickHouseUrl(payload.url || payload.host); + payload = { + ...payload, + url: parsed.url, + host: payload.host || parsed.host, + port: payload.port || parsed.port, + user: payload.user || "default", + database: payload.database || parsed.database || "default", + ssl: payload.ssl !== undefined ? payload.ssl : parsed.ssl, + }; } const res = await dbStoreInstance.addDB(payload as Parameters[0]); @@ -104,6 +123,8 @@ export class DatabaseService { const isFileBasedDB = ["sqlite", "duckdb"].includes( (payload.type as string | undefined)?.toLowerCase() || "", ); + const isClickHouse = + (payload.type as string | undefined)?.toLowerCase() === "clickhouse"; // Extract SSH credentials const sshPwd = payload.ssh?.password; @@ -113,6 +134,23 @@ export class DatabaseService { delete payload.ssh.passphrase; } + if (isClickHouse && (payload.url || payload.host)) { + const { parseClickHouseUrl } = await import("../utils/clickhouseUrl"); + const parsed = parseClickHouseUrl(payload.url || payload.host); + payload = { + ...payload, + url: parsed.url, + host: payload.host || parsed.host, + port: payload.port || parsed.port, + database: payload.database || parsed.database || "default", + ssl: payload.ssl !== undefined ? payload.ssl : parsed.ssl, + }; + } + if (payload.ssh) { + delete payload.ssh.password; + delete payload.ssh.passphrase; + } + if (isFileBasedDB || typeof payload.database === "string") { const current = await dbStoreInstance.getDB(id); const currentIsFileBasedDB = ["sqlite", "duckdb"].includes( diff --git a/bridge/src/services/dbStore.ts b/bridge/src/services/dbStore.ts index 516cdc29..b1b3fb8c 100644 --- a/bridge/src/services/dbStore.ts +++ b/bridge/src/services/dbStore.ts @@ -178,6 +178,7 @@ type DBMeta = { user: string; database: string; type?: string; + url?: string; credentialId?: string; notes?: string; tags?: string[]; @@ -507,6 +508,7 @@ export class DbStore { user: string; database: string; type?: string; + url?: string; ssl?: boolean; sslmode?: string; password?: string; @@ -526,6 +528,7 @@ export class DbStore { user: payload.user, database: payload.database, type: payload.type, + url: payload.url, ssl: payload.ssl, sslmode: payload.sslmode, credentialId, diff --git a/bridge/src/services/discoveryService.ts b/bridge/src/services/discoveryService.ts index 238821b6..e5a49dc8 100644 --- a/bridge/src/services/discoveryService.ts +++ b/bridge/src/services/discoveryService.ts @@ -16,6 +16,7 @@ const DATABASE_PORTS: Record = { postgresql: [5432, 5433, 5434], mysql: [3306, 3307, 3308], mariadb: [3306, 3307, 3308], + clickhouse: [8123, 8124], }; // Fun adjectives and nouns for generating database names @@ -90,7 +91,7 @@ const NOUNS = [ ]; export interface DiscoveredDatabase { - type: "postgresql" | "mysql" | "mariadb"; + type: "postgresql" | "mysql" | "mariadb" | "clickhouse"; host: string; port: number; source: "local" | "docker"; @@ -124,6 +125,8 @@ export class DiscoveryService { case "mysql": case "mariadb": return 3306; + case "clickhouse": + return 8123; default: return 0; } @@ -139,6 +142,8 @@ export class DiscoveryService { case "mysql": case "mariadb": return "root"; + case "clickhouse": + return "default"; default: return ""; } @@ -154,6 +159,8 @@ export class DiscoveryService { case "mysql": case "mariadb": return "mysql"; + case "clickhouse": + return "default"; default: return ""; } @@ -206,12 +213,14 @@ export class DiscoveryService { scannedPorts.add(key); // Determine the actual type based on the port - let actualType: "postgresql" | "mysql" | "mariadb" = dbType as any; + let actualType: "postgresql" | "mysql" | "mariadb" | "clickhouse" = dbType as any; if (port >= 5432 && port <= 5434) { actualType = "postgresql"; } else if (port >= 3306 && port <= 3308) { // Can't distinguish MySQL from MariaDB by port alone actualType = "mysql"; + } else if (port === 8123 || port === 8124) { + actualType = "clickhouse"; } discovered.push({ @@ -263,7 +272,7 @@ export class DiscoveryService { * Extract database credentials from container environment variables */ private extractCredentialsFromEnv( - dbType: "postgresql" | "mysql" | "mariadb", + dbType: "postgresql" | "mysql" | "mariadb" | "clickhouse", envVars: Map, ): { user: string; password: string; database: string } { if (dbType === "postgresql") { @@ -284,6 +293,15 @@ export class DiscoveryService { password, database: envVars.get("MYSQL_DATABASE") || "mysql", }; + } else if (dbType === "clickhouse") { + return { + user: envVars.get("CLICKHOUSE_USER") || envVars.get("CLICKHOUSE_DEFAULT_USER") || "default", + password: + envVars.get("CLICKHOUSE_PASSWORD") || + envVars.get("CLICKHOUSE_DEFAULT_PASSWORD") || + "", + database: envVars.get("CLICKHOUSE_DB") || "default", + }; } else { // MariaDB - similar to MySQL const user = envVars.get("MARIADB_USER") || envVars.get("MYSQL_USER") || "root"; @@ -317,13 +335,15 @@ export class DiscoveryService { const [containerName, image, ports] = line.split("|"); // Determine database type from image name - let dbType: "postgresql" | "mysql" | "mariadb" | null = null; + let dbType: "postgresql" | "mysql" | "mariadb" | "clickhouse" | null = null; if (image.includes("postgres") || image.includes("pg") || image.includes("postgresql")) { dbType = "postgresql"; } else if (image.includes("mariadb")) { dbType = "mariadb"; } else if (image.includes("mysql")) { dbType = "mysql"; + } else if (image.includes("clickhouse")) { + dbType = "clickhouse"; } if (dbType && ports) { diff --git a/bridge/src/services/projectStore.ts b/bridge/src/services/projectStore.ts index 2e5dcabd..6efb71db 100644 --- a/bridge/src/services/projectStore.ts +++ b/bridge/src/services/projectStore.ts @@ -73,7 +73,7 @@ export type SchemaFile = { version: number; // bumped to 2 projectId: string; databaseId: string; - dialect: "postgresql" | "mysql" | "sqlite" | "unknown"; + dialect: "postgresql" | "mysql" | "sqlite" | "duckdb" | "clickhouse" | "unknown"; schemas: SchemaSnapshot[]; cachedAt: string; relwaveVersion: string; diff --git a/bridge/src/services/queryExecutor.ts b/bridge/src/services/queryExecutor.ts index 1fc22259..5a625ef7 100644 --- a/bridge/src/services/queryExecutor.ts +++ b/bridge/src/services/queryExecutor.ts @@ -56,6 +56,7 @@ export class QueryExecutor { public mariadb = getConnector(DBType.MARIADB) as typeof import("../connectors/mariadb"); public sqlite = getConnector(DBType.SQLITE) as typeof import("../connectors/sqlite"); public duckdb = getConnector(DBType.DUCKDB) as typeof import("../connectors/duckdb"); + public clickhouse = getConnector(DBType.CLICKHOUSE) as typeof import("../connectors/clickhouse"); async executeQuery( params: QueryParams, diff --git a/bridge/src/types/clickhouse.ts b/bridge/src/types/clickhouse.ts new file mode 100644 index 00000000..2e27868d --- /dev/null +++ b/bridge/src/types/clickhouse.ts @@ -0,0 +1,42 @@ +/** + * ClickHouse-specific Types + */ + +import type { + ColumnDetail, + ForeignKeyInfo, + IndexInfo, + UniqueConstraintInfo, + CheckConstraintInfo, + PrimaryKeyInfo, +} from "./common"; + +/** + * ClickHouse connection configuration + */ +export type ClickHouseConfig = { + host: string; + port?: number; + user?: string; + password?: string; + database?: string; + ssl?: boolean; + url?: string; +}; + +/** + * ClickHouse schema metadata batch result + */ +export type ClickHouseSchemaMetadataBatch = { + tables: Map< + string, + { + columns: ColumnDetail[]; + primaryKeys: PrimaryKeyInfo[]; + foreignKeys: ForeignKeyInfo[]; + indexes: IndexInfo[]; + uniqueConstraints: UniqueConstraintInfo[]; + checkConstraints: CheckConstraintInfo[]; + } + >; +}; diff --git a/bridge/src/types/index.ts b/bridge/src/types/index.ts index 9c946fe1..b05ea952 100644 --- a/bridge/src/types/index.ts +++ b/bridge/src/types/index.ts @@ -15,6 +15,7 @@ export * from "./mysql"; export * from "./postgres"; export * from "./sqlite"; export * from "./duckdb"; +export * from "./clickhouse"; export * from "./ai"; export { SSHConfig } from "./common"; @@ -24,6 +25,7 @@ export enum DBType { MARIADB = "mariadb", SQLITE = "sqlite", DUCKDB = "duckdb", + CLICKHOUSE = "clickhouse", } export type Rpc = { @@ -40,6 +42,7 @@ export interface DatabaseConfig { ssl?: boolean; database: string; ssh?: SSHConfig; + url?: string; } export interface QueryParams { diff --git a/bridge/src/utils/clickhouseUrl.ts b/bridge/src/utils/clickhouseUrl.ts new file mode 100644 index 00000000..0726fa6b --- /dev/null +++ b/bridge/src/utils/clickhouseUrl.ts @@ -0,0 +1,78 @@ +/** + * Utility to clean and resolve ClickHouse URLs + */ + +export function cleanClickHouseUrl(raw?: string): string { + let url = (raw || "").trim(); + if (!url) return ""; + + // Strip markdown link brackets e.g. [https://...](https://...) + const mdMatch = url.match(/\[(.*?)\]\((.*?)\)/); + if (mdMatch) { + url = mdMatch[2] || mdMatch[1]; + } + + // Strip enclosing quotes + url = url.replace(/^['"`]+|['"`]+$/g, "").trim(); + + // If starts with clickhouses:// -> https:// + if (url.startsWith("clickhouses://")) { + url = "https://" + url.slice("clickhouses://".length); + } else if (url.startsWith("clickhouse://")) { + const rest = url.slice("clickhouse://".length); + const isSsl = rest.includes(":8443") || rest.includes(":443") || rest.includes("clickhouse.cloud"); + url = (isSsl ? "https://" : "http://") + rest; + } + + // If no scheme was provided, determine http vs https + if (!url.startsWith("http://") && !url.startsWith("https://")) { + const isSsl = url.includes(":8443") || url.includes(":443") || url.includes("clickhouse.cloud"); + url = (isSsl ? "https://" : "http://") + url; + } + + // Strip trailing slashes + url = url.replace(/\/+$/, ""); + + return url; +} + +export function parseClickHouseUrl(raw?: string): { + url: string; + host: string; + port: number; + ssl: boolean; + database: string; + user?: string; + password?: string; +} { + const cleanUrl = cleanClickHouseUrl(raw); + try { + const u = new URL(cleanUrl); + const ssl = u.protocol === "https:"; + const defaultPort = ssl ? 8443 : 8123; + const port = u.port ? parseInt(u.port, 10) : defaultPort; + const database = decodeURIComponent(u.pathname.replace(/^\//, "")) || "default"; + const user = decodeURIComponent(u.username) || undefined; + const password = decodeURIComponent(u.password) || undefined; + + return { + url: cleanUrl, + host: u.hostname, + port, + ssl, + database, + user, + password, + }; + } catch { + const isSsl = cleanUrl.includes(":8443") || cleanUrl.startsWith("https:"); + const port = isSsl ? 8443 : 8123; + return { + url: cleanUrl, + host: cleanUrl, + port, + ssl: isSsl, + database: "default", + }; + } +} diff --git a/bridge/src/utils/dbTypeDetector.ts b/bridge/src/utils/dbTypeDetector.ts index 33fc9560..9df8046c 100644 --- a/bridge/src/utils/dbTypeDetector.ts +++ b/bridge/src/utils/dbTypeDetector.ts @@ -11,6 +11,7 @@ export class DBTypeDetector { } if (normalized.includes("sqlite")) return DBType.SQLITE; if (normalized.includes("duckdb")) return DBType.DUCKDB; + if (normalized.includes("clickhouse")) return DBType.CLICKHOUSE; } return DBType.POSTGRES; // default } From 7a47c19f6e497453a317324a5333cada1aff14f8 Mon Sep 17 00:00:00 2001 From: Yash Date: Thu, 15 Oct 2026 16:38:06 +0530 Subject: [PATCH 02/17] feat(migrations): add ClickHouse DDL generation and reversible drop table migrations --- bridge/src/handlers/migrationHandlers.ts | 137 +++++++++++++++++++++-- bridge/src/utils/baselineMigration.ts | 31 +++-- bridge/src/utils/migrationGenerator.ts | 89 +++++++++++---- 3 files changed, 211 insertions(+), 46 deletions(-) diff --git a/bridge/src/handlers/migrationHandlers.ts b/bridge/src/handlers/migrationHandlers.ts index 32d4b2c3..391e0351 100644 --- a/bridge/src/handlers/migrationHandlers.ts +++ b/bridge/src/handlers/migrationHandlers.ts @@ -98,7 +98,14 @@ export class MigrationHandlers { async handleGenerateDropMigration(params: any, id: number | string) { try { - const { dbId, schemaName, tableName, mode = "RESTRICT" } = params || {}; + const { + dbId, + schemaName, + tableName, + mode = "RESTRICT", + columns: passedColumns, + foreignKeys: passedForeignKeys, + } = params || {}; if (!dbId || !schemaName || !tableName) { return this.rpc.sendError(id, { @@ -107,16 +114,68 @@ export class MigrationHandlers { }); } - const { dbType } = await this.dbService.getDatabaseConnection(dbId); + const { conn, dbType } = await this.dbService.getDatabaseConnection(dbId); const migrationsDir = await projectStoreInstance.resolveMigrationsDir(dbId); - // Generate migration file + let columns = passedColumns; + let foreignKeys = passedForeignKeys; + + // 1. Introspect columns and foreign keys from live database before dropping + if (!columns || !Array.isArray(columns) || columns.length === 0) { + try { + const connector = getConnector(dbType) as any; + if (typeof connector.getTableDetails === "function") { + const tableDetails = await connector.getTableDetails(conn, schemaName, tableName); + if (tableDetails && tableDetails.length > 0) { + columns = tableDetails.map((col: any) => ({ + name: col.name, + type: col.type, + not_nullable: Boolean(col.not_nullable), + default_value: col.default_value, + is_primary_key: Boolean(col.is_primary_key), + })); + } + } + if ((!foreignKeys || foreignKeys.length === 0) && typeof connector.listForeignKeys === "function") { + foreignKeys = await connector.listForeignKeys(conn, schemaName, tableName); + } + } catch (inspectErr) { + this.logger?.warn({ err: inspectErr }, "Could not inspect table before drop migration generation"); + } + } + + // 2. If table was already dropped or live inspection failed, fall back to project schema snapshot + if (!columns || columns.length === 0) { + try { + const project = await projectStoreInstance.getProjectByDatabaseId(dbId); + if (project) { + const schemaFile = await projectStoreInstance.getSchema(project.id); + const schemaGroup = schemaFile?.schemas?.find((s: any) => s.name === schemaName); + const tableSnap = schemaGroup?.tables?.find((t: any) => t.name === tableName); + if (tableSnap?.columns?.length) { + columns = tableSnap.columns.map((c: any) => ({ + name: c.name, + type: c.type, + not_nullable: !c.nullable, + default_value: c.defaultValue, + is_primary_key: Boolean(c.isPrimaryKey), + })); + } + } + } catch (snapErr) { + this.logger?.warn({ err: snapErr }, "Could not load schema snapshot before drop migration generation"); + } + } + + // Generate migration file with reverse CREATE TABLE in downSQL const { generateDropTableMigration, writeMigrationFile } = await import("../utils/migrationGenerator"); const migration = generateDropTableMigration({ schemaName, tableName, mode, + columns, + foreignKeys, dbType, }); @@ -175,13 +234,28 @@ export class MigrationHandlers { const project = await projectStoreInstance.getProjectByDatabaseId(dbId); if (project) { + // Refresh schema from DB so project schema.json stays up-to-date with newly created tables/columns + let schemaHash = ""; + try { + const dbSchema = (await this.queryExecutor.listSchemas(conn, dbType)) as any; + const liveSchemas = dbSchema?.schemas || []; + const crypto = await import("crypto"); + schemaHash = crypto.createHash("sha256").update(JSON.stringify(liveSchemas)).digest("hex"); + await projectStoreInstance.saveSchema(project.id, liveSchemas, schemaHash, dbType); + this.rpc?.sendNotification?.("project.schema_changed", { projectId: project.id, newHash: schemaHash }); + } catch (schemaErr) { + this.logger?.warn({ err: schemaErr }, "Failed to auto-refresh project schema after migration apply"); + } + // Update Migration Lock first so it gets included in the git sync commit const appliedMigrations = await connector.listAppliedMigrations(conn); - const schemaFile = await projectStoreInstance.getSchema(project.id); - const schemaHash = (schemaFile as any)?.schemaHash || ""; + if (!schemaHash) { + const schemaFile = await projectStoreInstance.getSchema(project.id); + schemaHash = (schemaFile as any)?.schemaHash || ""; + } - const appliedVersions = appliedMigrations.map((m) => m.version); + const appliedVersions = appliedMigrations.map((m: any) => m.version); await writeMigrationLock(dbId, schemaHash, appliedVersions); const projectDir = await projectStoreInstance.resolveProjectDir(project.id); @@ -193,6 +267,9 @@ export class MigrationHandlers { "Git sync failed after migration apply", ); } + try { + await gitServiceInstance.syncSchemaFile(projectDir); + } catch {} } } } catch (syncErr) { @@ -258,14 +335,37 @@ export class MigrationHandlers { const { writeMigrationLock } = await import("../services/migrationLock"); const project = await projectStoreInstance.getProjectByDatabaseId(dbId); if (project) { - const schemaFile = await projectStoreInstance.getSchema(project.id); - const schemaHash = (schemaFile as any)?.schemaHash || ""; + let schemaHash = ""; + try { + const dbSchema = (await this.queryExecutor.listSchemas(conn, dbType)) as any; + const liveSchemas = dbSchema?.schemas || []; + const crypto = await import("crypto"); + schemaHash = crypto.createHash("sha256").update(JSON.stringify(liveSchemas)).digest("hex"); + await projectStoreInstance.saveSchema(project.id, liveSchemas, schemaHash, dbType); + this.rpc?.sendNotification?.("project.schema_changed", { projectId: project.id, newHash: schemaHash }); + } catch (schemaErr) { + this.logger?.warn({ err: schemaErr }, "Failed to auto-refresh project schema after applyMigrations"); + } + + if (!schemaHash) { + const schemaFile = await projectStoreInstance.getSchema(project.id); + schemaHash = (schemaFile as any)?.schemaHash || ""; + } const updatedApplied = await connector.listAppliedMigrations(conn); await writeMigrationLock( dbId, schemaHash, updatedApplied.map((m: any) => m.version), ); + + const projectDir = await projectStoreInstance.resolveProjectDir(project.id); + if (projectDir) { + try { + const { gitServiceInstance } = await import("../services/gitService"); + await gitServiceInstance.syncMigrationFiles(projectDir); + await gitServiceInstance.syncSchemaFile(projectDir); + } catch {} + } } } catch (syncErr) { this.logger?.error({ err: syncErr }, "Lock hook failed"); @@ -385,11 +485,25 @@ export class MigrationHandlers { const project = await projectStoreInstance.getProjectByDatabaseId(dbId); if (project) { + let schemaHash = ""; + try { + const dbSchema = (await this.queryExecutor.listSchemas(conn, dbType)) as any; + const liveSchemas = dbSchema?.schemas || []; + const crypto = await import("crypto"); + schemaHash = crypto.createHash("sha256").update(JSON.stringify(liveSchemas)).digest("hex"); + await projectStoreInstance.saveSchema(project.id, liveSchemas, schemaHash, dbType); + this.rpc?.sendNotification?.("project.schema_changed", { projectId: project.id, newHash: schemaHash }); + } catch (schemaErr) { + this.logger?.warn({ err: schemaErr }, "Failed to auto-refresh project schema after migration rollback"); + } + // Update Migration Lock first so it gets included in the git sync commit const appliedMigrations = await connector.listAppliedMigrations(conn); - const schemaFile = await projectStoreInstance.getSchema(project.id); - const schemaHash = (schemaFile as any)?.schemaHash || ""; + if (!schemaHash) { + const schemaFile = await projectStoreInstance.getSchema(project.id); + schemaHash = (schemaFile as any)?.schemaHash || ""; + } const appliedVersions = appliedMigrations.map((m) => m.version); await writeMigrationLock(dbId, schemaHash, appliedVersions); @@ -403,6 +517,9 @@ export class MigrationHandlers { "Git sync failed after migration rollback", ); } + try { + await gitServiceInstance.syncSchemaFile(projectDir); + } catch {} } } } catch (syncErr) { diff --git a/bridge/src/utils/baselineMigration.ts b/bridge/src/utils/baselineMigration.ts index 55171026..cfcefea8 100644 --- a/bridge/src/utils/baselineMigration.ts +++ b/bridge/src/utils/baselineMigration.ts @@ -4,7 +4,7 @@ import path from "path"; import { SchemaFile } from "../services/projectStore"; function quoteIdent(name: string, dbType: string): string { - if (dbType === "mysql" || dbType === "mariadb") { + if (dbType === "mysql" || dbType === "mariadb" || dbType === "clickhouse") { return `\`${name.replace(/`/g, "``")}\``; } else { return `"${name.replace(/"/g, '""')}"`; @@ -102,18 +102,25 @@ export function generateBaselineSQL(snapshot: SchemaFile, version: string, name: return ` CONSTRAINT ${quoteIdent(fk.name, dbType)} FOREIGN KEY (${srcCols}) REFERENCES ${targetRef}(${tgtCols}) ON DELETE ${onDelete} ON UPDATE ${onUpdate}`; }); - const allDefs = [...columnDefs, ...fkDefs].join(",\n"); - - upSQL += `CREATE TABLE ${tableRef} (\n${allDefs}\n);\n\n`; - - // Indexes - const indexesToCreate = table.indexes || []; - for (const idx of indexesToCreate) { - const idxCols = idx.columns.map((c) => quoteIdent(c, dbType)).join(", "); - const uniqueStr = idx.unique ? "UNIQUE " : ""; - upSQL += `CREATE ${uniqueStr}INDEX ${quoteIdent(idx.name, dbType)} ON ${tableRef} (${idxCols});\n`; + if (dbType === "clickhouse") { + const pkCols = table.columns + .filter((c) => c.isPrimaryKey) + .map((c) => quoteIdent(c.name, dbType)); + const orderClause = pkCols.length > 0 ? pkCols.join(", ") : "tuple()"; + upSQL += `CREATE TABLE ${tableRef} (\n${columnDefs.join(",\n")}\n) ENGINE = MergeTree() ORDER BY (${orderClause});\n\n`; + } else { + const allDefs = [...columnDefs, ...fkDefs].join(",\n"); + upSQL += `CREATE TABLE ${tableRef} (\n${allDefs}\n);\n\n`; + + // Indexes + const indexesToCreate = table.indexes || []; + for (const idx of indexesToCreate) { + const idxCols = idx.columns.map((c) => quoteIdent(c, dbType)).join(", "); + const uniqueStr = idx.unique ? "UNIQUE " : ""; + upSQL += `CREATE ${uniqueStr}INDEX ${quoteIdent(idx.name, dbType)} ON ${tableRef} (${idxCols});\n`; + } + if (indexesToCreate.length > 0) upSQL += "\n"; } - if (indexesToCreate.length > 0) upSQL += "\n"; } // Views diff --git a/bridge/src/utils/migrationGenerator.ts b/bridge/src/utils/migrationGenerator.ts index fe2db899..17913dc8 100644 --- a/bridge/src/utils/migrationGenerator.ts +++ b/bridge/src/utils/migrationGenerator.ts @@ -33,7 +33,7 @@ export function generateCreateTableMigration(params: { tableName: string; columns: any[]; foreignKeys?: any[]; - dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb"; + dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb" | "clickhouse"; }): MigrationFile { const { schemaName, tableName, columns, foreignKeys = [], dbType } = params; const version = generateMigrationVersion(); @@ -44,30 +44,44 @@ export function generateCreateTableMigration(params: { const columnDefs = columns .map((col) => { let def = ` ${quoteIdent(col.name, dbType)} ${col.type}`; - if (col.not_nullable) def += " NOT NULL"; + if (col.not_nullable && dbType !== "clickhouse") def += " NOT NULL"; if (col.default_value) def += ` DEFAULT ${col.default_value}`; - if (col.is_primary_key) def += " PRIMARY KEY"; + if (col.is_primary_key && dbType !== "clickhouse") def += " PRIMARY KEY"; return def; }) .join(",\n"); // Build foreign key constraints - const fkDefs = foreignKeys.map((fk) => { - const onDelete = fk.on_delete || "NO ACTION"; - const onUpdate = fk.on_update || "NO ACTION"; - return ` CONSTRAINT ${quoteIdent(fk.constraint_name, dbType)} FOREIGN KEY (${quoteIdent(fk.source_column, dbType)}) REFERENCES ${quoteIdent(fk.target_schema, dbType)}.${quoteIdent(fk.target_table, dbType)}(${quoteIdent(fk.target_column, dbType)}) ON DELETE ${onDelete} ON UPDATE ${onUpdate}`; - }); + const fkDefs = + dbType === "clickhouse" + ? [] + : foreignKeys.map((fk) => { + const onDelete = fk.on_delete || "NO ACTION"; + const onUpdate = fk.on_update || "NO ACTION"; + return ` CONSTRAINT ${quoteIdent(fk.constraint_name, dbType)} FOREIGN KEY (${quoteIdent(fk.source_column, dbType)}) REFERENCES ${quoteIdent(fk.target_schema, dbType)}.${quoteIdent(fk.target_table, dbType)}(${quoteIdent(fk.target_column, dbType)}) ON DELETE ${onDelete} ON UPDATE ${onUpdate}`; + }); const allDefs = [...[columnDefs], ...fkDefs].filter(Boolean).join(",\n"); - // For MySQL/MariaDB, use database.table format (schemas are databases) + // For MySQL/MariaDB/ClickHouse, use database.table format (schemas are databases) // For Postgres, use schema.table format const tableRef = `${quoteIdent(schemaName, dbType)}.${quoteIdent(tableName, dbType)}`; // Generate UP SQL - const upSQL = `CREATE TABLE ${tableRef} ( + let upSQL = ""; + if (dbType === "clickhouse") { + const pkCols = columns + .filter((c) => c.is_primary_key) + .map((c) => quoteIdent(c.name, dbType)); + const orderClause = pkCols.length > 0 ? pkCols.join(", ") : "tuple()"; + upSQL = `CREATE TABLE ${tableRef} ( +${allDefs} +) ENGINE = MergeTree() ORDER BY (${orderClause});`; + } else { + upSQL = `CREATE TABLE ${tableRef} ( ${allDefs} );`; + } // Generate DOWN SQL const downSQL = `DROP TABLE ${tableRef};`; @@ -88,7 +102,7 @@ export function generateAlterTableMigration(params: { schemaName: string; tableName: string; operations: any[]; - dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb"; + dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb" | "clickhouse"; }): MigrationFile { const { schemaName, tableName, operations, dbType } = params; const version = generateMigrationVersion(); @@ -142,9 +156,19 @@ export function generateAlterTableMigration(params: { break; case "ALTER_TYPE": - upStatements.push( - `ALTER TABLE ${fullTableName} ${dbType === "postgres" ? "ALTER COLUMN" : "MODIFY"} ${quoteIdent(op.column_name, dbType)} ${dbType === "postgres" ? "TYPE " : ""}${op.new_type};`, - ); + if (dbType === "postgres") { + upStatements.push( + `ALTER TABLE ${fullTableName} ALTER COLUMN ${quoteIdent(op.column_name, dbType)} TYPE ${op.new_type};`, + ); + } else if (dbType === "clickhouse") { + upStatements.push( + `ALTER TABLE ${fullTableName} MODIFY COLUMN ${quoteIdent(op.column_name, dbType)} ${op.new_type};`, + ); + } else { + upStatements.push( + `ALTER TABLE ${fullTableName} MODIFY ${quoteIdent(op.column_name, dbType)} ${op.new_type};`, + ); + } downStatements.push( `-- WARNING: Cannot automatically reverse ALTER TYPE. Manual intervention required.`, ); @@ -242,10 +266,12 @@ export function generateAlterTableMigration(params: { export function generateDropTableMigration(params: { schemaName: string; tableName: string; - mode: "RESTRICT" | "DETACH_FKS" | "CASCADE"; - dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb"; + mode?: "RESTRICT" | "DETACH_FKS" | "CASCADE"; + columns?: any[]; + foreignKeys?: any[]; + dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb" | "clickhouse"; }): MigrationFile { - const { schemaName, tableName, mode, dbType } = params; + const { schemaName, tableName, mode = "RESTRICT", columns = [], foreignKeys = [], dbType } = params; const version = generateMigrationVersion(); const name = `drop_${tableName}_table`; const filename = `${version}_${name}.sql`; @@ -254,18 +280,33 @@ export function generateDropTableMigration(params: { const fullTableName = `${quoteIdent(schemaName, dbType)}.${quoteIdent(tableName, dbType)}`; let upSQL = ""; - if (mode === "CASCADE") { - upSQL = `DROP TABLE ${fullTableName} CASCADE;`; + if (dbType === "clickhouse" || dbType === "sqlite") { + upSQL = `DROP TABLE IF EXISTS ${fullTableName};`; + } else if (mode === "CASCADE") { + upSQL = `DROP TABLE IF EXISTS ${fullTableName} CASCADE;`; } else if (mode === "DETACH_FKS") { upSQL = `-- First drop foreign key constraints, then drop table -- WARNING: FK constraint names need to be specified manually -DROP TABLE ${fullTableName};`; +DROP TABLE IF EXISTS ${fullTableName};`; } else { - upSQL = `DROP TABLE ${fullTableName};`; + upSQL = `DROP TABLE IF EXISTS ${fullTableName};`; } - const downSQL = `-- WARNING: Cannot automatically reverse DROP TABLE. + let downSQL = ""; + if (columns && columns.length > 0) { + const createParams = { + schemaName, + tableName, + columns, + foreignKeys, + dbType, + }; + const createMigration = generateCreateTableMigration(createParams); + downSQL = createMigration.upSQL; + } else { + downSQL = `-- WARNING: Cannot automatically reverse DROP TABLE. -- You must manually recreate the table with all its data, constraints, and indexes.`; + } return { version, @@ -305,9 +346,9 @@ ${migration.downSQL} */ function quoteIdent( name: string, - dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb", + dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb" | "clickhouse", ): string { - if (dbType === "mysql" || dbType === "mariadb") { + if (dbType === "mysql" || dbType === "mariadb" || dbType === "clickhouse") { return `\`${name.replace(/`/g, "``")}\``; } else { return `"${name.replace(/"/g, '""')}"`; From 7cd1882c34e592aed1f092ee4ac3d17e06ab346f Mon Sep 17 00:00:00 2001 From: Yash Date: Thu, 15 Oct 2026 16:39:06 +0530 Subject: [PATCH 03/17] feat(frontend): add ClickHouse connection UI, SQL editor support, and reactive cache invalidation --- package.json | 3 +- src/components/shared/DataTable.tsx | 162 ++++++++++++------ .../components/ContentViewerPanel.tsx | 5 +- .../database/components/DataViewPanel.tsx | 3 + .../database/hooks/useDatabaseDetails.ts | 136 +++++++++------ .../database/hooks/useMigrationsPanel.ts | 22 +-- src/features/database/types.ts | 9 +- .../home/components/AddConnectionDialog.tsx | 147 ++++++++++++++-- .../home/components/DatabasePreview.tsx | 1 + .../components/DiscoveredDatabasesCard.tsx | 5 + src/features/home/components/WelcomeView.tsx | 1 + src/features/home/hooks/useIndexPage.ts | 17 +- src/features/home/types.ts | 3 + .../components/ImportProjectDialog.tsx | 1 + src/features/project/hooks/useDbQueries.ts | 16 +- .../components/SQLWorkspacePanel.tsx | 113 ++++++++---- .../workspace/components/SqlEditor.tsx | 20 ++- src/lib/parseConnectionUrl.ts | 125 +++++++++++++- src/lib/utils.ts | 1 + src/pages/DatabaseDetails.tsx | 2 + src/services/bridge/database.ts | 14 +- 21 files changed, 613 insertions(+), 193 deletions(-) diff --git a/package.json b/package.json index f75a34ba..094ae54c 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "format:check": "prettier . --check", "bridge:copy-native": "npm --prefix bridge run copy:native", "bridge:package": "node scripts/package-bridge.mjs", - "seed:duckdb": "npm --prefix bridge run seed:duckdb" + "seed:duckdb": "npm --prefix bridge run seed:duckdb", + "seed:clickhouse": "npm --prefix bridge run seed:clickhouse" }, "dependencies": { "@aptabase/tauri": "github:aptabase/tauri-plugin-aptabase", diff --git a/src/components/shared/DataTable.tsx b/src/components/shared/DataTable.tsx index fec24a20..705cebf9 100644 --- a/src/components/shared/DataTable.tsx +++ b/src/components/shared/DataTable.tsx @@ -11,22 +11,45 @@ import { Database, Pencil, Trash2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { formatTimestamp } from "@/lib/utils"; +export interface DataTableColumnDef { + name: string; + type?: string; +} + interface DataTableProps { data: Array>; + columns?: Array; maxHeight?: string; showRowNumbers?: boolean; onEditRow?: (row: Record) => void; onDeleteRow?: (row: Record) => void; + emptyMessage?: string; } export const DataTable = ({ data, + columns: propColumns, maxHeight = "100%", showRowNumbers = true, onEditRow, onDeleteRow, + emptyMessage, }: DataTableProps) => { - if (!data || data.length === 0) { + // Normalize columns from either props or first row of data + const columns: DataTableColumnDef[] = (() => { + if (propColumns && propColumns.length > 0) { + return propColumns.map((col) => (typeof col === "string" ? { name: col } : col)); + } + if (data && data.length > 0) { + return Object.keys(data[0]).map((name) => ({ name })); + } + return []; + })(); + + const isEmpty = !data || data.length === 0; + + // If there is no data AND no columns defined, show generic placeholder + if (isEmpty && columns.length === 0) { return (
@@ -34,14 +57,15 @@ export const DataTable = ({

No results found

- This table is empty or the query returned no data + {emptyMessage || "This table is empty or the query returned no data"}

); } - const columns = Object.keys(data[0]); + const columnNames = columns.map((col) => col.name); const hasRowActions = onEditRow || onDeleteRow; + const colSpan = columnNames.length + (showRowNumbers ? 1 : 0) + (hasRowActions ? 1 : 0); const tableContent = ( @@ -54,10 +78,17 @@ export const DataTable = ({ )} {columns.map((column) => ( - {column} +
+ {column.name} + {column.type && ( + + {column.type} + + )} +
))} {hasRowActions && ( @@ -68,59 +99,76 @@ export const DataTable = ({ - {data.map((row, index) => ( - - {showRowNumbers && ( - - {index + 1} - - )} - {columns.map((column) => ( - - {row[column] !== null && row[column] !== undefined ? ( - formatCellValue(row[column], column) - ) : ( - null - )} - - ))} - {hasRowActions && ( - -
- {onEditRow && ( - - )} - {onDeleteRow && ( - - )} -
-
- )} + {isEmpty ? ( + + +
+ + Table is empty + + 0 rows • Defined with {columns.length} columns ({columnNames.join(", ")}) + +
+
- ))} + ) : ( + data.map((row, index) => ( + + {showRowNumbers && ( + + {index + 1} + + )} + {columnNames.map((column) => ( + + {row[column] !== null && row[column] !== undefined ? ( + formatCellValue(row[column], column) + ) : ( + null + )} + + ))} + {hasRowActions && ( + +
+ {onEditRow && ( + + )} + {onDeleteRow && ( + + )} +
+
+ )} +
+ )) + )}
); diff --git a/src/features/database/components/ContentViewerPanel.tsx b/src/features/database/components/ContentViewerPanel.tsx index fb6d0d04..56178c1d 100644 --- a/src/features/database/components/ContentViewerPanel.tsx +++ b/src/features/database/components/ContentViewerPanel.tsx @@ -7,6 +7,7 @@ import { TableRow } from "@/features/database/types"; interface ContentViewerPanelProps { selectedTable: string | null; tableData: TableRow[]; + tableColumns?: Array<{ name: string; type?: string } | string>; totalRows: number; currentPage: number; pageSize: number; @@ -28,6 +29,7 @@ interface ContentViewerPanelProps { export default function ContentViewerPanel({ selectedTable, tableData, + tableColumns, totalRows, currentPage, pageSize, @@ -132,7 +134,7 @@ export default function ContentViewerPanel({
- ) : tableData.length === 0 ? ( + ) : tableData.length === 0 && (!tableColumns || tableColumns.length === 0) ? (

No data

@@ -140,6 +142,7 @@ export default function ContentViewerPanel({
; totalRows: number; currentPage: number; pageSize: number; @@ -80,6 +81,7 @@ export const DataViewPanel = ({ selectedSchema, setSelectedSchema, tableData, + tableColumns, totalRows, currentPage, pageSize, @@ -222,6 +224,7 @@ export const DataViewPanel = ({
void; handleTableSelect: (tableName: string, schemaName: string) => Promise; - handleExecuteQuery: () => Promise; + handleExecuteQuery: (sqlOverride?: string) => Promise; handleCancelQuery: () => Promise; fetchTables: () => Promise; handlePageChange: (page: number) => Promise; @@ -105,6 +108,15 @@ export function useDatabaseDetails({ // Cache invalidation const { invalidateTable, invalidateDatabase } = useInvalidateCache(); + // Full schema for column metadata fallback + const { data: fullSchema } = useFullSchema(dbId); + const selectedTableColumns = useMemo(() => { + if (!fullSchema?.schemas || !selectedTable) return []; + const schema = fullSchema.schemas.find((s) => s.name === selectedTable.schema); + const table = schema?.tables?.find((t) => t.name === selectedTable.name); + return table?.columns || []; + }, [fullSchema, selectedTable]); + // Derived state from table data query const tableData = tableDataResult?.rows || []; const rowCount = tableData.length; @@ -118,9 +130,13 @@ export function useDatabaseDetails({ const [queryResults, setQueryResults] = useState([]); const [queryRowCount, setQueryRowCount] = useState(0); const [queryError, setQueryError] = useState(null); + const [executionTime, setExecutionTime] = useState(null); const [hasExecutedQuery, setHasExecutedQuery] = useState(false); const [error, _setError] = useState(null); + const querySessionIdRef = useRef(null); + const lastExecutedSqlRef = useRef(""); + // Combined loading state const loading = loadingTables || isLoadingTableData; @@ -138,6 +154,7 @@ export function useDatabaseDetails({ setQueryResults([]); setQueryRowCount(0); setQueryError(null); + setExecutionTime(null); // React Query will automatically fetch the data }, [dbId, selectedTable, pageSize], @@ -174,10 +191,11 @@ export function useDatabaseDetails({ }, [dbId, selectedTable, invalidateTable]); const handleCancelQuery = useCallback(async () => { - if (!querySessionId) return; + const activeSession = querySessionIdRef.current || querySessionId; + if (!activeSession) return; try { - const cancelled = await sessionService.cancelSession(querySessionId); + const cancelled = await sessionService.cancelSession(activeSession); if (cancelled) { toast.info("Cancelling query...", { description: "Stopping query execution" }); } @@ -187,56 +205,67 @@ export function useDatabaseDetails({ } }, [querySessionId]); - const handleExecuteQuery = useCallback(async () => { - if (!dbId || !query.trim()) { - toast.error("Invalid query", { description: "Please enter a SQL query to execute" }); - return; - } - - try { - if (querySessionId) { - toast.warning("Query already running", { description: "Cancelling previous query first." }); - await handleCancelQuery(); + const handleExecuteQuery = useCallback( + async (sqlOverride?: string) => { + const sqlToRun = (sqlOverride !== undefined ? sqlOverride : query).trim(); + if (!dbId || !sqlToRun) { + toast.error("Invalid query", { description: "Please enter a SQL query to execute" }); + return; } - setQueryResults([]); - setQueryRowCount(0); - setQueryProgress(null); - setQueryError(null); - setHasExecutedQuery(true); - setIsExecuting(true); - analyticsService.trackQueryExecuted(); - - const sessionId = await sessionService.createSession(); - setQuerySessionId(sessionId); - - toast.info("Executing query...", { description: "Query started, receiving results..." }); - - await queryService.runQuery({ - sessionId, - dbId, - sql: query, - batchSize: 1000, - }); - } catch (err: any) { - console.error("Error executing query:", err); - setIsExecuting(false); - setQuerySessionId(null); - setQueryProgress(null); - toast.error("Query execution failed", { description: err.message }); - } - }, [dbId, query, querySessionId, handleCancelQuery]); + try { + if (querySessionIdRef.current) { + toast.warning("Query already running", { + description: "Cancelling previous query first.", + }); + await handleCancelQuery(); + } + + setQueryResults([]); + setQueryRowCount(0); + setQueryProgress(null); + setQueryError(null); + setExecutionTime(null); + setHasExecutedQuery(true); + setIsExecuting(true); + analyticsService.trackQueryExecuted(); + + const sessionId = await sessionService.createSession(); + querySessionIdRef.current = sessionId; + lastExecutedSqlRef.current = sqlToRun; + setQuerySessionId(sessionId); + + toast.info("Executing query...", { description: "Query started, receiving results..." }); + + await queryService.runQuery({ + sessionId, + dbId, + sql: sqlToRun, + batchSize: 1000, + }); + } catch (err: any) { + console.error("Error executing query:", err); + querySessionIdRef.current = null; + setIsExecuting(false); + setQuerySessionId(null); + setQueryProgress(null); + setQueryError(err.message || String(err)); + toast.error("Query execution failed", { description: err.message }); + } + }, + [dbId, query, handleCancelQuery], + ); // Setup query result listeners useEffect(() => { const handleResult = (event: CustomEvent) => { - if (event.detail.sessionId !== querySessionId) return; + if (event.detail.sessionId !== querySessionIdRef.current) return; setQueryResults((prev) => [...prev, ...event.detail.rows]); setQueryRowCount((prev) => prev + event.detail.rows.length); }; const handleProgress = (event: CustomEvent) => { - if (event.detail.sessionId !== querySessionId) return; + if (event.detail.sessionId !== querySessionIdRef.current) return; setQueryProgress({ rows: event.detail.rowsSoFar, elapsed: Math.round(event.detail.elapsedMs / 1000), @@ -244,13 +273,14 @@ export function useDatabaseDetails({ }; const handleDone = (event: CustomEvent) => { - if (event.detail.sessionId !== querySessionId) return; + if (event.detail.sessionId !== querySessionIdRef.current) return; + querySessionIdRef.current = null; setIsExecuting(false); setQuerySessionId(null); setQueryProgress(null); - const isDDL = query + const isDDL = lastExecutedSqlRef.current .trim() .toUpperCase() .match(/^(CREATE|DROP|ALTER|TRUNCATE)/); @@ -259,11 +289,14 @@ export function useDatabaseDetails({ } const { rows, timeMs, status } = event.detail; + const elapsedSec = timeMs !== undefined ? Number((timeMs / 1000).toFixed(2)) : null; + setExecutionTime(elapsedSec); + const statusType = status === "success" ? "success" : "warning"; const message = status === "success" - ? `Retrieved ${rows.toLocaleString()} rows in ${(timeMs / 1000).toFixed(2)}s` - : `Stopped after retrieving ${rows.toLocaleString()} rows.`; + ? `Retrieved ${(rows || 0).toLocaleString()} rows in ${(timeMs / 1000).toFixed(2)}s` + : `Stopped after retrieving ${(rows || 0).toLocaleString()} rows.`; toast[statusType](statusType === "success" ? "Query Complete" : "Query Cancelled", { description: message, @@ -271,8 +304,9 @@ export function useDatabaseDetails({ }; const handleError = (event: CustomEvent) => { - if (event.detail.sessionId !== querySessionId) return; + if (event.detail.sessionId !== querySessionIdRef.current) return; + querySessionIdRef.current = null; setIsExecuting(false); setQuerySessionId(null); setQueryProgress(null); @@ -300,7 +334,7 @@ export function useDatabaseDetails({ window.removeEventListener(listener.name, listener.handler as EventListener); }); }; - }, [querySessionId]); + }, [dbId, invalidateDatabase]); // Auto-select first table when tables are loaded useEffect(() => { @@ -325,6 +359,7 @@ export function useDatabaseDetails({ tables, selectedTable, tableData: showQueryResults ? queryResults : tableData, + tableColumns: selectedTableColumns, queryResults, hasExecutedQuery, rowCount: showQueryResults ? queryRowCount : rowCount, @@ -332,6 +367,7 @@ export function useDatabaseDetails({ query, queryProgress, queryError, + executionTime, isExecuting, isLoadingData: isFetchingTableData, loading, diff --git a/src/features/database/hooks/useMigrationsPanel.ts b/src/features/database/hooks/useMigrationsPanel.ts index 869103b4..daa2b1aa 100644 --- a/src/features/database/hooks/useMigrationsPanel.ts +++ b/src/features/database/hooks/useMigrationsPanel.ts @@ -1,8 +1,8 @@ import { MigrationsData } from "@/features/database/types"; -import { useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; import { migrationService } from "@/services/bridge/migration"; import { useState } from "react"; +import { useInvalidateCache } from "@/features/project/hooks/useDbQueries"; interface MigrationsPanelProps { migrations: MigrationsData; @@ -12,7 +12,7 @@ interface MigrationsPanelProps { export function useMigrationsPanel({ migrations, baselined, dbId }: MigrationsPanelProps) { const { local, applied } = migrations; - const queryClient = useQueryClient(); + const { invalidateDatabase } = useInvalidateCache(); const [selectedMigration, setSelectedMigration] = useState<{ version: string; name: string; @@ -24,12 +24,7 @@ export function useMigrationsPanel({ migrations, baselined, dbId }: MigrationsPa const handleRefresh = async () => { setIsRefreshing(true); try { - await Promise.all([ - queryClient.invalidateQueries({ queryKey: ["migrations", dbId] }), - queryClient.invalidateQueries({ queryKey: ["tables", dbId] }), - queryClient.invalidateQueries({ queryKey: ["schema", dbId] }), - queryClient.invalidateQueries({ queryKey: ["schemaNames", dbId] }), - ]); + await invalidateDatabase(dbId); toast.success("Refreshed successfully"); } finally { setIsRefreshing(false); @@ -62,10 +57,7 @@ export function useMigrationsPanel({ migrations, baselined, dbId }: MigrationsPa toast.success("Migration applied successfully", { description: `Applied migration: ${name}`, }); - // Invalidate migrations query to refresh - queryClient.invalidateQueries({ queryKey: ["migrations", dbId] }); - queryClient.invalidateQueries({ queryKey: ["tables", dbId] }); - queryClient.invalidateQueries({ queryKey: ["schema", dbId] }); + await invalidateDatabase(dbId); } catch (error: any) { toast.error("Failed to apply migration", { description: error.message, @@ -79,9 +71,7 @@ export function useMigrationsPanel({ migrations, baselined, dbId }: MigrationsPa toast.success("Migration rolled back successfully", { description: `Rolled back migration: ${name}`, }); - queryClient.invalidateQueries({ queryKey: ["migrations", dbId] }); - queryClient.invalidateQueries({ queryKey: ["tables", dbId] }); - queryClient.invalidateQueries({ queryKey: ["schema", dbId] }); + await invalidateDatabase(dbId); } catch (error: any) { toast.error("Failed to rollback migration", { description: error.message, @@ -95,7 +85,7 @@ export function useMigrationsPanel({ migrations, baselined, dbId }: MigrationsPa toast.success("Migration deleted successfully", { description: `Deleted migration: ${name}`, }); - queryClient.invalidateQueries({ queryKey: ["migrations", dbId] }); + await invalidateDatabase(dbId); } catch (error: any) { toast.error("Failed to delete migration", { description: error.message, diff --git a/src/features/database/types.ts b/src/features/database/types.ts index 97d78860..092a1065 100644 --- a/src/features/database/types.ts +++ b/src/features/database/types.ts @@ -1,4 +1,4 @@ -export type DatabaseType = "postgresql" | "mysql" | "sqlite" | "duckdb"; +export type DatabaseType = "postgresql" | "mysql" | "sqlite" | "duckdb" | "clickhouse"; export interface SSHConfig { host: string; @@ -11,7 +11,7 @@ export interface SSHConfig { } export interface DiscoveredDatabase { - type: "postgresql" | "mysql" | "mariadb" | "sqlite" | "duckdb"; + type: "postgresql" | "mysql" | "mariadb" | "sqlite" | "duckdb" | "clickhouse"; host: string; port: number; source: "local" | "docker"; @@ -37,6 +37,7 @@ export interface DatabaseConnection { lastAccessedAt?: string; credentialId?: string; ssh?: SSHConfig; + url?: string; } export interface AddDatabaseParams { @@ -52,6 +53,7 @@ export interface AddDatabaseParams { ssl?: boolean; sslmode?: string; ssh?: SSHConfig; + url?: string; } export interface UpdateDatabaseParams { @@ -65,6 +67,7 @@ export interface UpdateDatabaseParams { notes?: string; tags?: string[]; ssh?: SSHConfig; + url?: string; } export interface ConnectionTestResult { @@ -202,7 +205,7 @@ export interface MonitoringActiveQuery { } export interface MonitoringSnapshot { - databaseType: "postgres" | "mysql" | "mariadb" | "duckdb"; + databaseType: "postgres" | "mysql" | "mariadb" | "duckdb" | "clickhouse"; sampledAt: string; health: { ok: boolean; diff --git a/src/features/home/components/AddConnectionDialog.tsx b/src/features/home/components/AddConnectionDialog.tsx index 34164e2d..71ed4675 100644 --- a/src/features/home/components/AddConnectionDialog.tsx +++ b/src/features/home/components/AddConnectionDialog.tsx @@ -32,7 +32,7 @@ import { import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Checkbox } from "@/components/ui/checkbox"; import { Switch } from "@/components/ui/switch"; -import { parseConnectionUrl } from "@/lib/parseConnectionUrl"; +import { parseConnectionUrl, cleanClickHouseUrl, parseClickHouseUrl } from "@/lib/parseConnectionUrl"; import { AddConnectionDialogProps, INITIAL_FORM_DATA, ConnectionFormData } from "../types"; export function AddConnectionDialog({ @@ -51,8 +51,24 @@ export function AddConnectionDialog({ useEffect(() => { if (open) { if (initialData) { - setFormData((prev) => ({ ...prev, ...INITIAL_FORM_DATA, ...initialData })); - if (isDiscoveredMode) { + const reconstructedUrl = + initialData.url || + (initialData.type === "clickhouse" && initialData.host + ? `${initialData.ssl ? "https" : "http"}://${initialData.host}:${initialData.port || (initialData.ssl ? "8443" : "8123")}` + : ""); + + setFormData((prev) => ({ + ...prev, + ...INITIAL_FORM_DATA, + ...initialData, + url: reconstructedUrl || initialData.url || "", + })); + if ( + isDiscoveredMode || + initialData.type === "clickhouse" || + initialData.type === "sqlite" || + initialData.type === "duckdb" + ) { setUseUrl(false); } } else { @@ -62,7 +78,7 @@ export function AddConnectionDialog({ setUseUrl(true); } } - }, [open, initialData]); + }, [open, initialData, isDiscoveredMode]); const handleInputChange = (field: string, value: any) => { setFormData((prev) => ({ ...prev, [field]: value })); @@ -72,6 +88,22 @@ export function AddConnectionDialog({ setConnectionUrl(url); const parsed = parseConnectionUrl(url); if (parsed) { + if (parsed.type === "clickhouse") { + setUseUrl(false); + setFormData((prev) => ({ + ...prev, + type: "clickhouse", + url: cleanClickHouseUrl(url), + host: parsed.host, + port: parsed.port, + user: parsed.user || "default", + password: parsed.password || prev.password, + database: parsed.database || "default", + ssl: parsed.ssl, + sslmode: parsed.sslmode, + })); + return; + } setFormData((prev) => ({ ...prev, type: parsed.type, @@ -87,6 +119,26 @@ export function AddConnectionDialog({ }; const handleSubmit = () => { + if (formData.type === "clickhouse") { + const parsed = parseClickHouseUrl(formData.url); + const cleanedUrl = cleanClickHouseUrl(formData.url); + onSubmit( + { + ...formData, + url: cleanedUrl, + host: parsed.host || formData.host || "localhost", + port: parsed.port || formData.port || (parsed.ssl ? "8443" : "8123"), + ssl: parsed.ssl, + sslmode: parsed.ssl ? "require" : "disable", + user: formData.user || parsed.user || "default", + password: formData.password || parsed.password || "", + database: formData.database || parsed.database || "default", + }, + false, + cleanedUrl, + ); + return; + } onSubmit(formData, useUrl, connectionUrl); }; @@ -100,7 +152,10 @@ export function AddConnectionDialog({ }; const isFileBasedDB = formData.type === "sqlite" || formData.type === "duckdb"; - const showSslOption = formData.type === "postgresql" || formData.type === "mariadb"; + const isClickHouse = formData.type === "clickhouse"; + const showSslOption = + formData.type === "postgresql" || + formData.type === "mariadb"; return ( @@ -122,12 +177,12 @@ export function AddConnectionDialog({ {initialData ? "Edit Connection" : "New Connection"} - Connect to PostgreSQL, MySQL, MariaDB, SQLite, or DuckDB + Connect to PostgreSQL, MySQL, MariaDB, SQLite, DuckDB, or ClickHouse
- {!isFileBasedDB && !isDiscoveredMode && ( + {!isFileBasedDB && !isClickHouse && !isDiscoveredMode && ( setUseUrl(v === "url")}> @@ -142,7 +197,7 @@ export function AddConnectionDialog({ )} - {useUrl && !isFileBasedDB && ( + {useUrl && !isFileBasedDB && !isClickHouse && (
({ + ...prev, + type: val, + user: prev.user || "default", + database: prev.database || "default", + useSsh: false, + })); } }} > @@ -206,6 +271,7 @@ export function AddConnectionDialog({ MariaDB SQLite DuckDB + ClickHouse
@@ -252,6 +318,55 @@ export function AddConnectionDialog({
+ ) : isClickHouse ? ( + <> +
+ + { + const val = e.target.value; + handleInputChange("url", val); + const parsed = parseClickHouseUrl(val); + setFormData((prev) => ({ + ...prev, + url: val, + host: parsed.host || prev.host, + port: parsed.port || prev.port, + ssl: parsed.ssl, + user: prev.user || parsed.user || "default", + database: prev.database || parsed.database || "default", + })); + }} + readOnly={isDiscoveredMode} + className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`} + /> +
+ +
+ + handleInputChange("user", e.target.value)} + readOnly={isDiscoveredMode} + className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`} + /> +
+ +
+ + handleInputChange("password", e.target.value)} + readOnly={isDiscoveredMode} + className={`h-9 text-sm ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`} + /> +
+ ) : ( <>
@@ -269,7 +384,11 @@ export function AddConnectionDialog({ handleInputChange("port", e.target.value)} @@ -283,7 +402,13 @@ export function AddConnectionDialog({
handleInputChange("user", e.target.value)} readOnly={isDiscoveredMode} @@ -306,7 +431,7 @@ export function AddConnectionDialog({
handleInputChange("database", e.target.value)} readOnly={isDiscoveredMode} diff --git a/src/features/home/components/DatabasePreview.tsx b/src/features/home/components/DatabasePreview.tsx index a3fe4e2a..479812d4 100644 --- a/src/features/home/components/DatabasePreview.tsx +++ b/src/features/home/components/DatabasePreview.tsx @@ -34,6 +34,7 @@ const DB_COLORS: Record = { mariadb: { bg: "bg-teal-500/10", text: "text-teal-500" }, sqlite: { bg: "bg-cyan-500/10", text: "text-cyan-500" }, duckdb: { bg: "bg-yellow-500/10", text: "text-yellow-600" }, + clickhouse: { bg: "bg-amber-500/10", text: "text-amber-500" }, }; function getDbColors(type: string) { diff --git a/src/features/home/components/DiscoveredDatabasesCard.tsx b/src/features/home/components/DiscoveredDatabasesCard.tsx index 095f9267..c6882c06 100644 --- a/src/features/home/components/DiscoveredDatabasesCard.tsx +++ b/src/features/home/components/DiscoveredDatabasesCard.tsx @@ -37,6 +37,11 @@ const DB_TYPE_COLORS = { text: "text-yellow-600", border: "border-yellow-500/20", }, + clickhouse: { + bg: "bg-amber-500/10", + text: "text-amber-500", + border: "border-amber-500/20", + }, }; export function DiscoveredDatabasesCard({ diff --git a/src/features/home/components/WelcomeView.tsx b/src/features/home/components/WelcomeView.tsx index 42d02d01..f4d56637 100644 --- a/src/features/home/components/WelcomeView.tsx +++ b/src/features/home/components/WelcomeView.tsx @@ -24,6 +24,7 @@ const DB_COLORS: Record = { mariadb: { bg: "bg-teal-500/10", text: "text-teal-500" }, sqlite: { bg: "bg-cyan-500/10", text: "text-cyan-500" }, duckdb: { bg: "bg-yellow-500/10", text: "text-yellow-600" }, + clickhouse: { bg: "bg-amber-500/10", text: "text-amber-500" }, }; function getDbColors(type: string) { diff --git a/src/features/home/hooks/useIndexPage.ts b/src/features/home/hooks/useIndexPage.ts index 1995f8c9..2335a441 100644 --- a/src/features/home/hooks/useIndexPage.ts +++ b/src/features/home/hooks/useIndexPage.ts @@ -9,6 +9,7 @@ import { projectKeys, useProjects } from "@/features/project/hooks/useProjectQue import { ConnectionFormData, DUCKDB_REQUIRED_FIELDS, + CLICKHOUSE_REQUIRED_FIELDS, REQUIRED_FIELDS, SQLITE_REQUIRED_FIELDS, } from "@/features/home/types"; @@ -107,11 +108,20 @@ export const useIndexPage = (bridgeReady: boolean) => { const handleAddDatabase = async (formData: ConnectionFormData) => { const isSQLite = formData.type === "sqlite"; const isDuckDB = formData.type === "duckdb"; + const isClickHouse = formData.type === "clickhouse"; + + if (isClickHouse) { + if (!formData.user) formData.user = "default"; + if (!formData.database) formData.database = "default"; + } + const requiredFields = isSQLite ? SQLITE_REQUIRED_FIELDS : isDuckDB ? DUCKDB_REQUIRED_FIELDS - : REQUIRED_FIELDS; + : isClickHouse + ? CLICKHOUSE_REQUIRED_FIELDS + : REQUIRED_FIELDS; const missing = requiredFields.filter((field) => !formData[field as keyof typeof formData]); if (missing.length) { @@ -156,10 +166,13 @@ export const useIndexPage = (bridgeReady: boolean) => { } = formData; const isFileBasedDB = isSQLite || isDuckDB; + const port = isFileBasedDB + ? 0 + : parseInt(formData.port) || (isClickHouse ? (formData.ssl ? 8443 : 8123) : 5432); const payload: any = { ...rest, - port: isFileBasedDB ? 0 : parseInt(formData.port), + port, sslmode: isFileBasedDB ? "disable" : formData.ssl diff --git a/src/features/home/types.ts b/src/features/home/types.ts index 8e2a67d6..1682dd4f 100644 --- a/src/features/home/types.ts +++ b/src/features/home/types.ts @@ -76,6 +76,7 @@ export interface ConnectionFormData { database: string; sslmode: string; ssl: boolean; + url?: string; useSsh: boolean; sshHost: string; sshPort: string; @@ -96,6 +97,7 @@ export const INITIAL_FORM_DATA: ConnectionFormData = { database: "", sslmode: "", ssl: false, + url: "", useSsh: false, sshHost: "", sshPort: "22", @@ -109,3 +111,4 @@ export const INITIAL_FORM_DATA: ConnectionFormData = { export const REQUIRED_FIELDS = ["name", "type", "host", "port", "user", "database"]; export const SQLITE_REQUIRED_FIELDS = ["name", "type", "database"]; export const DUCKDB_REQUIRED_FIELDS = ["name", "type", "database"]; +export const CLICKHOUSE_REQUIRED_FIELDS = ["name", "type", "url"]; diff --git a/src/features/project/components/ImportProjectDialog.tsx b/src/features/project/components/ImportProjectDialog.tsx index f0324a2d..368365eb 100644 --- a/src/features/project/components/ImportProjectDialog.tsx +++ b/src/features/project/components/ImportProjectDialog.tsx @@ -425,6 +425,7 @@ export function ImportProjectDialog({ MariaDB SQLite DuckDB + ClickHouse
diff --git a/src/features/project/hooks/useDbQueries.ts b/src/features/project/hooks/useDbQueries.ts index 869fe84b..a1b1a3e2 100644 --- a/src/features/project/hooks/useDbQueries.ts +++ b/src/features/project/hooks/useDbQueries.ts @@ -354,11 +354,17 @@ export function useInvalidateCache() { return { invalidateDatabase: (dbId: string) => { - queryClient.invalidateQueries({ queryKey: ["tables", dbId] }); - queryClient.invalidateQueries({ queryKey: queryKeys.stats(dbId) }); - queryClient.invalidateQueries({ queryKey: queryKeys.schemas(dbId) }); - queryClient.invalidateQueries({ queryKey: ["fullSchema", dbId] }); // Also invalidate fullSchema - queryClient.invalidateQueries({ queryKey: ["migrations", dbId] }); // Also invalidate migrations + return Promise.all([ + queryClient.invalidateQueries({ queryKey: ["tables", dbId] }), + queryClient.invalidateQueries({ queryKey: queryKeys.stats(dbId) }), + queryClient.invalidateQueries({ queryKey: queryKeys.schemas(dbId) }), + queryClient.invalidateQueries({ queryKey: ["schemaNames", dbId] }), + queryClient.invalidateQueries({ queryKey: ["schema", dbId] }), + queryClient.invalidateQueries({ queryKey: ["fullSchema", dbId] }), // Also invalidate fullSchema + queryClient.invalidateQueries({ queryKey: ["migrations", dbId] }), // Also invalidate migrations + queryClient.invalidateQueries({ queryKey: ["tableData", dbId] }), + queryClient.invalidateQueries({ queryKey: ["projects"] }), + ]); }, /** Invalidate table-specific caches */ diff --git a/src/features/workspace/components/SQLWorkspacePanel.tsx b/src/features/workspace/components/SQLWorkspacePanel.tsx index 2eeecd7c..6bb53136 100644 --- a/src/features/workspace/components/SQLWorkspacePanel.tsx +++ b/src/features/workspace/components/SQLWorkspacePanel.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect, useCallback } from "react"; +import { useState, useEffect, useCallback, useRef } from "react"; import { useBridgeQuery } from "@/services/bridge/useBridgeQuery"; import { useDatabaseDetails } from "@/features/database/hooks/useDatabaseDetails"; import { WorkspaceHeader } from "./WorkspaceHeader"; @@ -36,6 +36,8 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => { }, ]); const [activeTabId, setActiveTabId] = useState("1"); + const executingTabIdRef = useRef(activeTabId); + const wasExecutingRef = useRef(false); // Query history const [queryHistory, setQueryHistory] = useState([]); @@ -44,15 +46,16 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => { const { databaseName, + databaseType, tables, queryProgress, queryError, + executionTime, isExecuting, setQuery, handleExecuteQuery, handleCancelQuery, queryResults, - hasExecutedQuery, rowCount, } = useDatabaseDetails({ dbId, @@ -79,34 +82,68 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => { [activeTabId, setQuery], ); - // Update tab results when query completes + const handleRunActiveQuery = useCallback(() => { + if (!activeTab?.query.trim() || isExecuting) return; + const targetId = activeTabId; + executingTabIdRef.current = targetId; + wasExecutingRef.current = true; + setTabs((prev) => + prev.map((tab) => + tab.id === targetId ? { ...tab, status: "running", error: null } : tab, + ), + ); + handleExecuteQuery(activeTab.query); + }, [activeTab?.query, isExecuting, activeTabId, handleExecuteQuery]); + + // Keyboard shortcut Ctrl+Enter / Cmd+Enter to execute active query + useEffect(() => { + const handleKeyDown = (e: KeyboardEvent) => { + if ((e.ctrlKey || e.metaKey) && e.key === "Enter") { + e.preventDefault(); + handleRunActiveQuery(); + } + }; + window.addEventListener("keydown", handleKeyDown); + return () => window.removeEventListener("keydown", handleKeyDown); + }, [handleRunActiveQuery]); + + // Update tab results when query transitions from running to finished useEffect(() => { - if (hasExecutedQuery && !isExecuting) { + if (isExecuting) { + wasExecutingRef.current = true; + return; + } + + if (wasExecutingRef.current && !isExecuting) { + wasExecutingRef.current = false; + const targetTabId = executingTabIdRef.current || activeTabId; + + let executedQuery = ""; setTabs((prev) => - prev.map((tab) => - tab.id === activeTabId - ? { - ...tab, - results: queryResults, - rowCount: rowCount, - error: queryError, - status: queryError ? "error" : "success", - executionTime: queryProgress?.elapsed || null, - } - : tab, - ), + prev.map((tab) => { + if (tab.id === targetTabId) { + executedQuery = tab.query; + return { + ...tab, + results: queryResults, + rowCount: rowCount, + error: queryError, + status: queryError ? "error" : "success", + executionTime: executionTime, + }; + } + return tab; + }), ); - // Add to history - if (activeTab?.query.trim() && !queryError) { + if (executedQuery.trim() && !queryError) { setQueryHistory((prev) => { - // Avoid duplicating the exact same query consecutively - if (prev.length > 0 && prev[0].query === activeTab.query) { + if (prev.length > 0 && prev[0].query === executedQuery) { return prev; } return [ { - query: activeTab.query, + query: executedQuery, timestamp: new Date(), rowCount: rowCount, success: true, @@ -116,16 +153,7 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => { }); } } - }, [isExecuting, hasExecutedQuery, queryResults, rowCount, queryError]); - - // Update tab status when executing - useEffect(() => { - if (isExecuting) { - setTabs((prev) => - prev.map((tab) => (tab.id === activeTabId ? { ...tab, status: "running" } : tab)), - ); - } - }, [isExecuting, activeTabId]); + }, [isExecuting, queryResults, rowCount, queryError, executionTime, activeTabId]); const addNewTab = useCallback(() => { const newId = Date.now().toString(); @@ -162,10 +190,21 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => { const insertTableQuery = useCallback( (tableName: string, schema: string) => { - const newQuery = `SELECT * FROM "${schema}"."${tableName}" LIMIT 100;`; + let newQuery = ""; + if (databaseType === "sqlite") { + newQuery = `SELECT * FROM "${tableName}" LIMIT 100;`; + } else if ( + databaseType === "mysql" || + databaseType === "mariadb" || + databaseType === "clickhouse" + ) { + newQuery = `SELECT * FROM \`${schema}\`.\`${tableName}\` LIMIT 100;`; + } else { + newQuery = `SELECT * FROM "${schema}"."${tableName}" LIMIT 100;`; + } updateActiveTabQuery(newQuery); }, - [updateActiveTabQuery], + [databaseType, updateActiveTabQuery], ); const loadFromHistory = useCallback( @@ -211,7 +250,7 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => { queryProgress={queryProgress} canExecute={!!activeTab?.query.trim()} activeQuery={activeTab?.query} - onExecute={handleExecuteQuery} + onExecute={handleRunActiveQuery} onCancel={handleCancelQuery} onNLQueryClick={() => setIsNLDialogOpen(true)} /> @@ -246,6 +285,12 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => { void; } // Light theme customization @@ -58,6 +59,7 @@ const SqlEditor: FC = ({ placeholder = "-- Enter your SQL query here\nSELECT * FROM users;", minHeight = "200px", dialect = "postgresql", + onExecute, }) => { const { theme } = useTheme(); const isDark = @@ -80,6 +82,22 @@ const SqlEditor: FC = ({ sql({ dialect: sqlDialect }), EditorView.lineWrapping, EditorView.editable.of(!disabled), + ...(onExecute + ? [ + keymap.of([ + { + key: "Mod-Enter", + run: () => { + if (!disabled) { + onExecute(); + return true; + } + return false; + }, + }, + ]), + ] + : []), isDark ? darkThemeExtension : lightTheme, ]; diff --git a/src/lib/parseConnectionUrl.ts b/src/lib/parseConnectionUrl.ts index d8498a94..361d529c 100644 --- a/src/lib/parseConnectionUrl.ts +++ b/src/lib/parseConnectionUrl.ts @@ -41,6 +41,82 @@ function encodeSqlitePathSegments(database: string): string { .join("/"); } +export function cleanClickHouseUrl(raw?: string): string { + let url = (raw || "").trim(); + if (!url) return ""; + + // Strip markdown link brackets e.g. [https://...](https://...) + const mdMatch = url.match(/\[(.*?)\]\((.*?)\)/); + if (mdMatch) { + url = mdMatch[2] || mdMatch[1]; + } + + // Strip enclosing quotes + url = url.replace(/^['"`]+|['"`]+$/g, "").trim(); + + // If starts with clickhouses:// -> https:// + if (url.startsWith("clickhouses://")) { + url = "https://" + url.slice("clickhouses://".length); + } else if (url.startsWith("clickhouse://")) { + const rest = url.slice("clickhouse://".length); + const isSsl = rest.includes(":8443") || rest.includes(":443") || rest.includes("clickhouse.cloud"); + url = (isSsl ? "https://" : "http://") + rest; + } + + // If no scheme was provided, determine http vs https + if (!url.startsWith("http://") && !url.startsWith("https://")) { + const isSsl = url.includes(":8443") || url.includes(":443") || url.includes("clickhouse.cloud"); + url = (isSsl ? "https://" : "http://") + url; + } + + // Strip trailing slashes + url = url.replace(/\/+$/, ""); + + return url; +} + +export function parseClickHouseUrl(raw?: string): { + url: string; + host: string; + port: string; + ssl: boolean; + database: string; + user: string; + password?: string; +} { + const cleanUrl = cleanClickHouseUrl(raw); + try { + const u = new URL(cleanUrl); + const ssl = u.protocol === "https:"; + const defaultPort = ssl ? "8443" : "8123"; + const port = u.port || defaultPort; + const database = decodeURIComponent(u.pathname.replace(/^\//, "")) || "default"; + const user = decodeURIComponent(u.username) || "default"; + const password = decodeURIComponent(u.password) || undefined; + + return { + url: cleanUrl, + host: u.hostname, + port, + ssl, + database, + user, + password, + }; + } catch { + const isSsl = cleanUrl.includes(":8443") || cleanUrl.startsWith("https:"); + const port = isSsl ? "8443" : "8123"; + return { + url: cleanUrl, + host: cleanUrl, + port, + ssl: isSsl, + database: "default", + user: "default", + }; + } +} + export function parseConnectionUrl(url: string): ParsedConnectionUrl | null { try { // Trim whitespace @@ -50,10 +126,11 @@ export function parseConnectionUrl(url: string): ParsedConnectionUrl | null { if (!url) return null; // Parse the URL - const parsed = new URL(url); + const parsed = new URL(cleanClickHouseUrl(url)); // Determine database type from protocol let type = ""; + let isSslProtocol = false; const protocol = parsed.protocol.replace(":", "").toLowerCase(); if (protocol === "postgres" || protocol === "postgresql") { @@ -64,6 +141,18 @@ export function parseConnectionUrl(url: string): ParsedConnectionUrl | null { type = "sqlite"; } else if (protocol === "duckdb") { type = "duckdb"; + } else if (protocol === "clickhouse" || protocol === "clickhouses") { + type = "clickhouse"; + if (protocol === "clickhouses") { + isSslProtocol = true; + } + } else if ( + parsed.hostname.includes("clickhouse") || + parsed.port === "8443" || + parsed.port === "8123" + ) { + type = "clickhouse"; + isSslProtocol = protocol === "https"; } else { return null; // Unsupported protocol } @@ -85,16 +174,26 @@ export function parseConnectionUrl(url: string): ParsedConnectionUrl | null { // Extract components const host = parsed.hostname || "localhost"; - const port = parsed.port || (type === "postgresql" ? "5432" : "3306"); - const user = decodeURIComponent(parsed.username || ""); + const defaultPort = + type === "postgresql" + ? "5432" + : type === "clickhouse" + ? isSslProtocol + ? "8443" + : "8123" + : "3306"; + const port = parsed.port || defaultPort; + const user = decodeURIComponent(parsed.username || (type === "clickhouse" ? "default" : "")); const password = decodeURIComponent(parsed.password || ""); // Database name is the pathname without leading slash - const database = decodeURIComponent(parsed.pathname.replace(/^\//, "") || ""); + const database = decodeURIComponent( + parsed.pathname.replace(/^\//, "") || (type === "clickhouse" ? "default" : ""), + ); // Parse SSL parameters from query string - const sslmode = parsed.searchParams.get("sslmode") || ""; - const ssl = sslmode !== "" && sslmode !== "disable"; + const sslmode = parsed.searchParams.get("sslmode") || (isSslProtocol ? "require" : ""); + const ssl = isSslProtocol || (sslmode !== "" && sslmode !== "disable"); return { type, @@ -127,14 +226,24 @@ export function buildConnectionUrl(params: { if (params.type === "sqlite" || params.type === "duckdb") { return `${params.type}:///${encodeSqlitePathSegments(params.database)}`; } - const protocol = params.type === "mysql" ? "mysql" : "postgres"; + const isClickHouseSsl = + params.type === "clickhouse" && + (params.sslmode === "require" || params.port === "8443"); + const protocol = + params.type === "mysql" + ? "mysql" + : params.type === "clickhouse" + ? isClickHouseSsl + ? "clickhouses" + : "clickhouse" + : "postgres"; const auth = params.password ? `${encodeURIComponent(params.user)}:${encodeURIComponent(params.password)}` : encodeURIComponent(params.user); let url = `${protocol}://${auth}@${params.host}:${params.port}/${encodeURIComponent(params.database)}`; - if (params.sslmode && params.sslmode !== "disable") { + if (params.sslmode && params.sslmode !== "disable" && !isClickHouseSsl) { url += `?sslmode=${params.sslmode}`; } diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 356c3a31..ed6b2c4a 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -49,6 +49,7 @@ export function formatDbType(databaseType?: string) { if (databaseType === "mariadb") return "MariaDB"; if (databaseType === "sqlite") return "SQLite"; if (databaseType === "duckdb") return "DuckDB"; + if (databaseType === "clickhouse") return "ClickHouse"; return databaseType; } export function formatDuration(seconds: number) { diff --git a/src/pages/DatabaseDetails.tsx b/src/pages/DatabaseDetails.tsx index a2b2a5bc..1ef3c3d3 100644 --- a/src/pages/DatabaseDetails.tsx +++ b/src/pages/DatabaseDetails.tsx @@ -58,6 +58,7 @@ const DatabaseDetail = () => { tables, selectedTable, tableData, + tableColumns, totalRows, currentPage, pageSize, @@ -153,6 +154,7 @@ const DatabaseDetail = () => { selectedSchema={selectedSchema} setSelectedSchema={setSelectedSchema} tableData={tableData} + tableColumns={tableColumns} totalRows={totalRows} currentPage={currentPage} pageSize={pageSize} diff --git a/src/services/bridge/database.ts b/src/services/bridge/database.ts index ef43d65c..f75088be 100644 --- a/src/services/bridge/database.ts +++ b/src/services/bridge/database.ts @@ -66,14 +66,20 @@ class DatabaseService { try { // Validate required fields const isFileBasedDB = params.type === "sqlite" || params.type === "duckdb"; + const isClickHouse = params.type === "clickhouse"; const required = isFileBasedDB ? ["name", "type", "database"] - : ["name", "type", "host", "port", "user", "database"]; + : isClickHouse + ? ["name", "type"] + : ["name", "type", "host", "port", "user", "database"]; for (const field of required) { if (!params[field as keyof AddDatabaseParams]) { throw new Error(`Missing required field: ${field}`); } } + if (isClickHouse && !params.url && !params.host) { + throw new Error("Missing required field: url"); + } const result = await bridgeRequest("db.add", params); if (!result?.ok) { throw new Error("Failed to add database"); @@ -303,9 +309,9 @@ class DatabaseService { tableName, }); return ( - result?.primaryKeys[0].column_name || - result?.primaryKeys[0].COLUMN_NAME || - result.primaryKeys[0] || + result?.primaryKeys?.[0]?.column_name || + result?.primaryKeys?.[0]?.COLUMN_NAME || + (typeof result?.primaryKeys?.[0] === "string" ? result.primaryKeys[0] : "") || "" ); } catch (error: any) { From f94aa94da3b0f5899ab11b81a8df02bffdd59178 Mon Sep 17 00:00:00 2001 From: Yash Date: Thu, 15 Oct 2026 16:40:49 +0530 Subject: [PATCH 04/17] test(clickhouse): add Docker compose service, seed scripts, and integration test suite --- bridge/.env.example | 32 ++ bridge/.gitignore | 3 +- bridge/__tests__/clickhouseUrl.test.ts | 99 ++++ .../connectors/clickhouse.cache.test.ts | 126 +++++ .../__tests__/connectors/clickhouse.test.ts | 264 +++++++++++ bridge/__tests__/discoveryService.test.ts | 63 ++- bridge/docker-compose.test.yml | 18 + bridge/scripts/seed-clickhouse.sql | 42 ++ bridge/seed-clickhouse.ts | 439 ++++++++++++++++++ 9 files changed, 1084 insertions(+), 2 deletions(-) create mode 100644 bridge/.env.example create mode 100644 bridge/__tests__/clickhouseUrl.test.ts create mode 100644 bridge/__tests__/connectors/clickhouse.cache.test.ts create mode 100644 bridge/__tests__/connectors/clickhouse.test.ts create mode 100644 bridge/scripts/seed-clickhouse.sql create mode 100644 bridge/seed-clickhouse.ts diff --git a/bridge/.env.example b/bridge/.env.example new file mode 100644 index 00000000..d56d33b2 --- /dev/null +++ b/bridge/.env.example @@ -0,0 +1,32 @@ +# Bridge test and local development environment +# Copy this file to .env and adjust values if you need custom database endpoints. + +REAL_POSTGRES_HOST=localhost +REAL_POSTGRES_PORT=5432 +REAL_POSTGRES_USER=testuser +REAL_POSTGRES_PASSWORD=testpass +REAL_POSTGRES_DATABASE=testdb +REAL_POSTGRES_SSL=false +REAL_POSTGRES_SSLMODE=disable + +REAL_MYSQL_HOST=localhost +REAL_MYSQL_PORT=3306 +REAL_MYSQL_USER=testuser +REAL_MYSQL_PASSWORD=testpass +REAL_MYSQL_DATABASE=testdb +REAL_MYSQL_SSL=false + +REAL_MARIADB_HOST=localhost +REAL_MARIADB_PORT=3307 +REAL_MARIADB_USER=testuser +REAL_MARIADB_PASSWORD=testpass +REAL_MARIADB_DATABASE=testdb +REAL_MARIADB_SSL=false + +REAL_CLICKHOUSE_HOST=localhost +REAL_CLICKHOUSE_PORT=8123 +REAL_CLICKHOUSE_USER=testuser +REAL_CLICKHOUSE_PASSWORD=testpass +REAL_CLICKHOUSE_DATABASE=testdb +REAL_CLICKHOUSE_SSL=false + diff --git a/bridge/.gitignore b/bridge/.gitignore index 20ca7eb4..c07dd57b 100644 --- a/bridge/.gitignore +++ b/bridge/.gitignore @@ -2,4 +2,5 @@ node_modules dist coverage .env -.env* \ No newline at end of file +.env.* +!.env.example \ No newline at end of file diff --git a/bridge/__tests__/clickhouseUrl.test.ts b/bridge/__tests__/clickhouseUrl.test.ts new file mode 100644 index 00000000..49109154 --- /dev/null +++ b/bridge/__tests__/clickhouseUrl.test.ts @@ -0,0 +1,99 @@ +import { cleanClickHouseUrl, parseClickHouseUrl } from "../src/utils/clickhouseUrl"; +import { describe, expect, it } from "@jest/globals"; + +describe("ClickHouse URL Utilities", () => { + describe("cleanClickHouseUrl", () => { + it("should handle empty or undefined input", () => { + expect(cleanClickHouseUrl("")).toBe(""); + expect(cleanClickHouseUrl(undefined)).toBe(""); + expect(cleanClickHouseUrl(" ")).toBe(""); + }); + + it("should strip markdown links", () => { + expect( + cleanClickHouseUrl("[https://demo.clickhouse.cloud:8443](https://demo.clickhouse.cloud:8443)"), + ).toBe("https://demo.clickhouse.cloud:8443"); + expect( + cleanClickHouseUrl("[https://demo.clickhouse.cloud:8443]()"), + ).toBe("https://demo.clickhouse.cloud:8443"); + }); + + it("should strip enclosing single/double quotes and backticks", () => { + expect(cleanClickHouseUrl("'https://demo.clickhouse.cloud:8443'")).toBe( + "https://demo.clickhouse.cloud:8443", + ); + expect(cleanClickHouseUrl('"https://demo.clickhouse.cloud:8443"')).toBe( + "https://demo.clickhouse.cloud:8443", + ); + expect(cleanClickHouseUrl("`https://demo.clickhouse.cloud:8443`")).toBe( + "https://demo.clickhouse.cloud:8443", + ); + }); + + it("should convert clickhouses:// protocol to https://", () => { + expect(cleanClickHouseUrl("clickhouses://demo.clickhouse.cloud:8443")).toBe( + "https://demo.clickhouse.cloud:8443", + ); + }); + + it("should convert clickhouse:// protocol properly based on SSL / port", () => { + expect(cleanClickHouseUrl("clickhouse://demo.clickhouse.cloud:8443")).toBe( + "https://demo.clickhouse.cloud:8443", + ); + expect(cleanClickHouseUrl("clickhouse://localhost:8123")).toBe( + "http://localhost:8123", + ); + }); + + it("should prepend https:// for clickhouse.cloud or port 8443 if no scheme provided", () => { + expect(cleanClickHouseUrl("demo.clickhouse.cloud:8443")).toBe( + "https://demo.clickhouse.cloud:8443", + ); + expect(cleanClickHouseUrl("demo.clickhouse.cloud")).toBe( + "https://demo.clickhouse.cloud", + ); + }); + + it("should prepend http:// for localhost:8123 if no scheme provided", () => { + expect(cleanClickHouseUrl("localhost:8123")).toBe("http://localhost:8123"); + }); + + it("should strip trailing slashes", () => { + expect(cleanClickHouseUrl("https://demo.clickhouse.cloud:8443/")).toBe( + "https://demo.clickhouse.cloud:8443", + ); + }); + }); + + describe("parseClickHouseUrl", () => { + it("should parse standard cloud url", () => { + const parsed = parseClickHouseUrl( + "https://of8remuy3t.ap-south-1.aws.clickhouse.cloud:8443", + ); + expect(parsed.host).toBe("of8remuy3t.ap-south-1.aws.clickhouse.cloud"); + expect(parsed.port).toBe(8443); + expect(parsed.ssl).toBe(true); + expect(parsed.database).toBe("default"); + }); + + it("should parse url with embedded user and password", () => { + const parsed = parseClickHouseUrl( + "https://myuser:secret123@of8remuy3t.ap-south-1.aws.clickhouse.cloud:8443/analytics", + ); + expect(parsed.host).toBe("of8remuy3t.ap-south-1.aws.clickhouse.cloud"); + expect(parsed.port).toBe(8443); + expect(parsed.ssl).toBe(true); + expect(parsed.user).toBe("myuser"); + expect(parsed.password).toBe("secret123"); + expect(parsed.database).toBe("analytics"); + }); + + it("should parse localhost http url", () => { + const parsed = parseClickHouseUrl("http://localhost:8123/default"); + expect(parsed.host).toBe("localhost"); + expect(parsed.port).toBe(8123); + expect(parsed.ssl).toBe(false); + expect(parsed.database).toBe("default"); + }); + }); +}); diff --git a/bridge/__tests__/connectors/clickhouse.cache.test.ts b/bridge/__tests__/connectors/clickhouse.cache.test.ts new file mode 100644 index 00000000..45175c2b --- /dev/null +++ b/bridge/__tests__/connectors/clickhouse.cache.test.ts @@ -0,0 +1,126 @@ +import { clickhouseCache } from "../../src/connectors/clickhouse"; +import { describe, it, expect, beforeEach } from "@jest/globals"; +import { ClickHouseConfig } from "../../src/types/clickhouse"; + +const mockConnection: ClickHouseConfig = { + host: "localhost", + port: 8123, + user: "testuser", + password: "testpass", + database: "testdb", +}; + +const mockConnection2: ClickHouseConfig = { + host: "localhost", + port: 8123, + user: "testuser", + password: "testpass", + database: "otherdb", +}; + +describe("ClickhouseCacheManager", () => { + beforeEach(() => { + clickhouseCache.clearAll(); + }); + + describe("Connection Key Generation & Isolation", () => { + it("should generate consistent keys and isolate different databases", () => { + const tables1 = [{ schema: "testdb", name: "users", type: "BASE TABLE" }]; + const tables2 = [{ schema: "otherdb", name: "orders", type: "BASE TABLE" }]; + + clickhouseCache.setTableList(mockConnection, tables1, "testdb"); + clickhouseCache.setTableList(mockConnection2, tables2, "otherdb"); + + expect(clickhouseCache.getTableList(mockConnection, "testdb")).toEqual(tables1); + expect(clickhouseCache.getTableList(mockConnection2, "otherdb")).toEqual(tables2); + }); + }); + + describe("Table List Cache", () => { + const mockTables = [ + { schema: "testdb", name: "users", type: "BASE TABLE" }, + { schema: "testdb", name: "orders", type: "BASE TABLE" }, + ]; + + it("should cache and retrieve table list", () => { + clickhouseCache.setTableList(mockConnection, mockTables, "testdb"); + const cached = clickhouseCache.getTableList(mockConnection, "testdb"); + expect(cached).toEqual(mockTables); + }); + + it("should return null for non-existent cache entry", () => { + const cached = clickhouseCache.getTableList(mockConnection, "nonexistent"); + expect(cached).toBeNull(); + }); + }); + + describe("Table Details Cache", () => { + const mockColumns = [ + { + name: "id", + type: "UInt64", + not_nullable: true, + default_value: null, + is_primary_key: true, + is_foreign_key: false, + comment: null, + }, + { + name: "email", + type: "String", + not_nullable: true, + default_value: null, + is_primary_key: false, + is_foreign_key: false, + comment: null, + }, + ]; + + it("should cache and retrieve table details", () => { + clickhouseCache.setTableDetails(mockConnection, "testdb", "users", mockColumns); + const cached = clickhouseCache.getTableDetails(mockConnection, "testdb", "users"); + expect(cached).toEqual(mockColumns); + }); + + it("should clear specific table cache", () => { + clickhouseCache.setTableDetails(mockConnection, "testdb", "users", mockColumns); + clickhouseCache.clearTableCache(mockConnection, "testdb", "users"); + expect(clickhouseCache.getTableDetails(mockConnection, "testdb", "users")).toBeNull(); + }); + }); + + describe("DB Stats Cache", () => { + const mockStats = { + total_tables: 3, + total_rows: 1000, + total_db_size_mb: 2.5, + }; + + it("should cache and retrieve db stats", () => { + clickhouseCache.setDBStats(mockConnection, mockStats); + const cached = clickhouseCache.getDBStats(mockConnection); + expect(cached).toEqual(mockStats); + }); + }); + + describe("Clear for Connection and Clear All", () => { + it("should clear only the target connection cache", () => { + const tables1 = [{ schema: "testdb", name: "users", type: "BASE TABLE" }]; + const tables2 = [{ schema: "otherdb", name: "orders", type: "BASE TABLE" }]; + + clickhouseCache.setTableList(mockConnection, tables1, "testdb"); + clickhouseCache.setTableList(mockConnection2, tables2, "otherdb"); + + clickhouseCache.clearForConnection(mockConnection); + + expect(clickhouseCache.getTableList(mockConnection, "testdb")).toBeNull(); + expect(clickhouseCache.getTableList(mockConnection2, "otherdb")).toEqual(tables2); + }); + + it("should clear all caches on clearAll", () => { + clickhouseCache.setTableList(mockConnection, [{ schema: "testdb", name: "users", type: "BASE TABLE" }], "testdb"); + clickhouseCache.clearAll(); + expect(clickhouseCache.getTableList(mockConnection, "testdb")).toBeNull(); + }); + }); +}); diff --git a/bridge/__tests__/connectors/clickhouse.test.ts b/bridge/__tests__/connectors/clickhouse.test.ts new file mode 100644 index 00000000..d0da846a --- /dev/null +++ b/bridge/__tests__/connectors/clickhouse.test.ts @@ -0,0 +1,264 @@ +import { describe, expect, test, jest, afterAll } from "@jest/globals"; +import * as clickhouseConnector from "../../src/connectors/clickhouse"; + +const invalidConfig: clickhouseConnector.ClickHouseConfig = { + host: "nonexistent.invalid.host", + port: 8123, + user: "testuser", + password: "wrongpassword", + database: "testdb", +}; + +const validConfig: clickhouseConnector.ClickHouseConfig = { + host: process.env.REAL_CLICKHOUSE_HOST || "127.0.0.1", + port: Number(process.env.REAL_CLICKHOUSE_PORT || 8123), + user: process.env.REAL_CLICKHOUSE_USER || "testuser", + password: process.env.REAL_CLICKHOUSE_PASSWORD || "testpass", + database: process.env.REAL_CLICKHOUSE_DATABASE || "testdb", + ssl: process.env.REAL_CLICKHOUSE_SSL === "true", +}; + +describe("ClickHouse Connector", () => { + jest.setTimeout(20000); + + afterAll(async () => { + await clickhouseConnector.closeAllClients(); + }); + + test("Should Fail to Connect to ClickHouse with Invalid Config", async () => { + const connection = await clickhouseConnector.testConnection(invalidConfig); + expect(connection.ok).toBe(false); + expect(connection.status).toBe("disconnected"); + }); + + test("Should Connect to ClickHouse Database", async () => { + const connection = await clickhouseConnector.testConnection(validConfig); + expect(connection).toStrictEqual({ + message: "Connection successful", + status: "connected", + ok: true, + }); + }); + + test("Should Get Database Statistics", async () => { + const stats = await clickhouseConnector.getDBStats(validConfig); + expect(stats).toHaveProperty("total_tables"); + expect(stats).toHaveProperty("total_rows"); + expect(stats).toHaveProperty("total_db_size_mb"); + expect(stats.total_tables).toBeGreaterThanOrEqual(1); + }); + + test("Should Get List of Schemas / Databases", async () => { + const schemas = await clickhouseConnector.listSchemas(validConfig); + expect(Array.isArray(schemas)).toBe(true); + expect(schemas.some((s) => s.name === validConfig.database)).toBe(true); + }); + + test("Should Get List of Tables in testdb", async () => { + const tables = await clickhouseConnector.listTables(validConfig, validConfig.database); + expect(Array.isArray(tables)).toBe(true); + const tableNames = tables.map((t) => t.name); + expect(tableNames).toContain("persons"); + expect(tableNames).toContain("student"); + expect(tableNames).toContain("TestTable"); + }); + + test("Should Get Table Details for student Table", async () => { + const details = await clickhouseConnector.getTableDetails( + validConfig, + validConfig.database, + "student", + ); + expect(Array.isArray(details)).toBe(true); + expect(details.length).toBeGreaterThanOrEqual(3); + + const colNames = details.map((c) => c.name); + expect(colNames).toContain("id"); + expect(colNames).toContain("name"); + expect(colNames).toContain("address"); + + const idCol = details.find((c) => c.name === "id"); + expect(idCol?.is_primary_key).toBe(true); + }); + + test("Should Get Table Primary Keys for student Table", async () => { + const pks = await clickhouseConnector.listPrimaryKeys( + validConfig, + validConfig.database, + "student", + ); + expect(Array.isArray(pks)).toBe(true); + expect(pks.length).toBeGreaterThanOrEqual(1); + expect(pks[0].column_name).toBe("id"); + }); + + test("Should Fetch Table Data from persons", async () => { + const result = await clickhouseConnector.fetchTableData( + validConfig, + validConfig.database, + "persons", + 10, + 1, + ); + expect(result).toHaveProperty("total"); + expect(result.total).toBeGreaterThanOrEqual(3); + expect(Array.isArray(result.rows)).toBe(true); + expect(result.rows.length).toBeGreaterThanOrEqual(3); + expect(result.rows[0]).toHaveProperty("id"); + expect(result.rows[0]).toHaveProperty("name"); + expect(result.rows[0]).toHaveProperty("email"); + }); + + test("Should Stream Query on student Table", async () => { + const rows: any[] = []; + let doneCalled = false; + + const { promise } = clickhouseConnector.streamQueryCancelable( + validConfig, + `SELECT * FROM ${validConfig.database}.student ORDER BY id;`, + 10, + (batch) => { + rows.push(...batch); + }, + () => { + doneCalled = true; + }, + ); + + await promise; + + expect(doneCalled).toBe(true); + expect(rows.length).toBeGreaterThanOrEqual(3); + expect(rows[0]).toHaveProperty("name"); + }); + + test("Should Insert, Update, and Delete Rows", async () => { + const testId = 88888; + const testName = "CRUD Integration Test"; + const updatedName = "Updated CRUD Test"; + + // 1. Insert + const inserted = await clickhouseConnector.insertRow( + validConfig, + validConfig.database, + "TestTable", + { id: testId, name: testName }, + ); + expect(inserted).toHaveProperty("id", testId); + + // Verify insertion + const dataAfterInsert = await clickhouseConnector.fetchTableData( + validConfig, + validConfig.database, + "TestTable", + 50, + 1, + ); + const foundInserted = dataAfterInsert.rows.find((r) => Number(r.id) === testId); + expect(foundInserted).toBeDefined(); + expect(foundInserted.name).toBe(testName); + + // 2. Update + await clickhouseConnector.updateRow( + validConfig, + validConfig.database, + "TestTable", + "id", + testId, + { name: updatedName }, + ); + + const dataAfterUpdate = await clickhouseConnector.fetchTableData( + validConfig, + validConfig.database, + "TestTable", + 50, + 1, + ); + const foundUpdated = dataAfterUpdate.rows.find((r) => Number(r.id) === testId); + expect(foundUpdated).toBeDefined(); + expect(foundUpdated.name).toBe(updatedName); + + // 3. Delete + const deleted = await clickhouseConnector.deleteRow( + validConfig, + validConfig.database, + "TestTable", + "id", + testId, + ); + expect(deleted).toBe(true); + + const dataAfterDelete = await clickhouseConnector.fetchTableData( + validConfig, + validConfig.database, + "TestTable", + 50, + 1, + ); + const foundDeleted = dataAfterDelete.rows.find((r) => Number(r.id) === testId); + expect(foundDeleted).toBeUndefined(); + }); + + test("Should Search Table by Keyword", async () => { + const searchResult = await clickhouseConnector.searchTable( + validConfig, + validConfig.database, + "persons", + "John", + ); + expect(searchResult.total).toBeGreaterThanOrEqual(1); + expect(searchResult.rows.some((r) => r.name.includes("John"))).toBe(true); + }); + + test("Should Get Schema Metadata Batch for ER Diagram", async () => { + const batch = await clickhouseConnector.getSchemaMetadataBatch( + validConfig, + validConfig.database, + ); + expect(batch.tables instanceof Map).toBe(true); + expect(batch.tables.size).toBeGreaterThanOrEqual(3); + + const studentMeta = batch.tables.get("student"); + expect(studentMeta).toBeDefined(); + expect(studentMeta!.columns.length).toBeGreaterThanOrEqual(3); + expect(studentMeta!.primaryKeys.length).toBeGreaterThanOrEqual(1); + expect(studentMeta!.primaryKeys[0].column_name).toBe("id"); + }); + + test("Should Return Empty Arrays for Foreign Keys and Unique Constraints", async () => { + const fks = await clickhouseConnector.listForeignKeys( + validConfig, + validConfig.database, + "student", + ); + expect(fks).toEqual([]); + + const uqs = await clickhouseConnector.listUniqueConstraints( + validConfig, + validConfig.database, + "student", + ); + expect(uqs).toEqual([]); + + const checks = await clickhouseConnector.listCheckConstraints( + validConfig, + validConfig.database, + "student", + ); + expect(checks).toEqual([]); + }); + + test("Should Parse and Split Multi-statement Migrations", () => { + const sql = ` + -- First statement + CREATE TABLE \`testdb\`.\`temp_1\` (id UInt32) ENGINE = MergeTree() ORDER BY id; + -- Second statement + CREATE TABLE \`testdb\`.\`temp_2\` (id UInt32) ENGINE = MergeTree() ORDER BY id; + `; + const stmts = clickhouseConnector.splitClickHouseStatements(sql); + expect(stmts).toHaveLength(2); + expect(stmts[0]).toContain("`testdb`.`temp_1`"); + expect(stmts[1]).toContain("`testdb`.`temp_2`"); + }); +}); diff --git a/bridge/__tests__/discoveryService.test.ts b/bridge/__tests__/discoveryService.test.ts index 40f9e475..e88e205b 100644 --- a/bridge/__tests__/discoveryService.test.ts +++ b/bridge/__tests__/discoveryService.test.ts @@ -34,6 +34,16 @@ describe("DiscoveryService", () => { expect(name).not.toContain("-3306"); }); + test("should not add port suffix for default ClickHouse port", () => { + const name = service.generateFunName("clickhouse", 8123); + expect(name).not.toContain("-8123"); + }); + + test("should add port suffix for non-default ClickHouse port", () => { + const name = service.generateFunName("clickhouse", 8124); + expect(name).toMatch(/^[a-z]+-[a-z]+-8124$/); + }); + test("should add port suffix for non-default MySQL port", () => { const name = service.generateFunName("mysql", 3307); expect(name).toMatch(/^[a-z]+-[a-z]+-3307$/); @@ -54,7 +64,7 @@ describe("DiscoveryService", () => { // Access private method via prototype for testing const extractCredentials = ( svc: DiscoveryService, - dbType: "postgresql" | "mysql" | "mariadb", + dbType: "postgresql" | "mysql" | "mariadb" | "clickhouse", envVars: Map, ) => { return (svc as any).extractCredentialsFromEnv(dbType, envVars); @@ -181,6 +191,45 @@ describe("DiscoveryService", () => { expect(result.password).toBe("mysqlrootpass"); }); }); + + describe("ClickHouse", () => { + test("should extract ClickHouse credentials with CLICKHOUSE_USER", () => { + const envVars = new Map([ + ["CLICKHOUSE_USER", "chuser"], + ["CLICKHOUSE_PASSWORD", "chpass"], + ["CLICKHOUSE_DB", "analytics"], + ]); + + const result = extractCredentials(service, "clickhouse", envVars); + + expect(result.user).toBe("chuser"); + expect(result.password).toBe("chpass"); + expect(result.database).toBe("analytics"); + }); + + test("should fall back to CLICKHOUSE_DEFAULT_USER and CLICKHOUSE_DEFAULT_PASSWORD", () => { + const envVars = new Map([ + ["CLICKHOUSE_DEFAULT_USER", "defaultadmin"], + ["CLICKHOUSE_DEFAULT_PASSWORD", "secret123"], + ]); + + const result = extractCredentials(service, "clickhouse", envVars); + + expect(result.user).toBe("defaultadmin"); + expect(result.password).toBe("secret123"); + expect(result.database).toBe("default"); + }); + + test("should use defaults when ClickHouse env vars are missing", () => { + const envVars = new Map(); + + const result = extractCredentials(service, "clickhouse", envVars); + + expect(result.user).toBe("default"); + expect(result.password).toBe(""); + expect(result.database).toBe("default"); + }); + }); }); describe("getDefaultPort (private)", () => { @@ -200,6 +249,10 @@ describe("DiscoveryService", () => { expect(getDefaultPort(service, "mariadb")).toBe(3306); }); + test("should return 8123 for clickhouse", () => { + expect(getDefaultPort(service, "clickhouse")).toBe(8123); + }); + test("should return 0 for unknown type", () => { expect(getDefaultPort(service, "unknown")).toBe(0); }); @@ -222,6 +275,10 @@ describe("DiscoveryService", () => { expect(getDefaultUser(service, "mariadb")).toBe("root"); }); + test("should return default for clickhouse", () => { + expect(getDefaultUser(service, "clickhouse")).toBe("default"); + }); + test("should return empty string for unknown type", () => { expect(getDefaultUser(service, "unknown")).toBe(""); }); @@ -244,6 +301,10 @@ describe("DiscoveryService", () => { expect(getDefaultDatabase(service, "mariadb")).toBe("mysql"); }); + test("should return default for clickhouse", () => { + expect(getDefaultDatabase(service, "clickhouse")).toBe("default"); + }); + test("should return empty string for unknown type", () => { expect(getDefaultDatabase(service, "unknown")).toBe(""); }); diff --git a/bridge/docker-compose.test.yml b/bridge/docker-compose.test.yml index 1a045cc0..190aab41 100644 --- a/bridge/docker-compose.test.yml +++ b/bridge/docker-compose.test.yml @@ -49,6 +49,24 @@ services: timeout: 5s retries: 5 + clickhouse: + image: clickhouse/clickhouse-server:latest + environment: + CLICKHOUSE_USER: testuser + CLICKHOUSE_PASSWORD: testpass + CLICKHOUSE_DB: testdb + CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT: 1 + ports: + - "8123:8123" + - "9000:9000" + volumes: + - ./scripts/seed-clickhouse.sql:/docker-entrypoint-initdb.d/seed.sql + healthcheck: + test: ["CMD-SHELL", "wget -q --spider http://localhost:8123/ping || exit 1"] + interval: 10s + timeout: 5s + retries: 5 + ssh-server: image: linuxserver/openssh-server environment: diff --git a/bridge/scripts/seed-clickhouse.sql b/bridge/scripts/seed-clickhouse.sql new file mode 100644 index 00000000..a0a82729 --- /dev/null +++ b/bridge/scripts/seed-clickhouse.sql @@ -0,0 +1,42 @@ +-- ClickHouse seed script for test databases +-- Mounted to /docker-entrypoint-initdb.d/seed.sql + +CREATE DATABASE IF NOT EXISTS testdb; + +CREATE TABLE IF NOT EXISTS testdb.TestTable ( + id UInt32, + name String, + created_at DateTime DEFAULT now() +) ENGINE = MergeTree() +ORDER BY id; + +CREATE TABLE IF NOT EXISTS testdb.persons ( + id UInt32, + name String, + email Nullable(String), + created_at DateTime DEFAULT now() +) ENGINE = MergeTree() +ORDER BY id; + +CREATE TABLE IF NOT EXISTS testdb.student ( + id UInt32, + name String, + address Nullable(String), + created_at DateTime DEFAULT now() +) ENGINE = MergeTree() +ORDER BY id; + +INSERT INTO testdb.TestTable (id, name) VALUES + (1, 'Test Item 1'), + (2, 'Test Item 2'), + (3, 'Test Item 3'); + +INSERT INTO testdb.persons (id, name, email) VALUES + (1, 'John Doe', 'john@example.com'), + (2, 'Jane Smith', 'jane@example.com'), + (3, 'Bob Wilson', 'bob@example.com'); + +INSERT INTO testdb.student (id, name, address) VALUES + (1, 'Alice Johnson', '123 Main St'), + (2, 'Charlie Brown', '456 Oak Ave'), + (3, 'Diana Ross', '789 Pine Rd'); diff --git a/bridge/seed-clickhouse.ts b/bridge/seed-clickhouse.ts new file mode 100644 index 00000000..8e6f17f4 --- /dev/null +++ b/bridge/seed-clickhouse.ts @@ -0,0 +1,439 @@ +import { createClient } from "@clickhouse/client"; +import { cleanClickHouseUrl } from "./src/utils/clickhouseUrl"; + +declare const process: { + argv: string[]; + env: Record; + exit(code?: number): never; + stdout: { + write(chunk: string): boolean; + }; +}; + +interface ScaleConfig { + users: number; + products: number; + orders: number; + events: number; + metrics: number; +} + +const SCALE_PRESETS: Record = { + small: { + users: 10_000, + products: 1_000, + orders: 25_000, + events: 50_000, + metrics: 20_000, + }, + medium: { + users: 50_000, + products: 5_000, + orders: 150_000, + events: 300_000, + metrics: 100_000, + }, + large: { + users: 100_000, + products: 10_000, + orders: 300_000, + events: 600_000, + metrics: 250_000, + }, + huge: { + users: 500_000, + products: 50_000, + orders: 1_500_000, + events: 3_000_000, + metrics: 1_000_000, + }, +}; + +function parseArgs(): { + url: string; + username: string; + password?: string; + database: string; + scale: string; + drop: boolean; +} { + const args = process.argv.slice(2); + let url = + process.env.CLICKHOUSE_URL || + "https://of8remuy3t.ap-south-1.aws.clickhouse.cloud:8443"; + let username = process.env.CLICKHOUSE_USER || "default"; + let password = process.env.CLICKHOUSE_PASSWORD || "HpD2b9hQ5KW.3"; + let database = process.env.CLICKHOUSE_DATABASE || "default"; + let scale = "medium"; + let drop = true; + + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--help" || arg === "-h") { + console.log(` +ClickHouse High-Performance Seed Tool for RelWave +================================================== +Usage: + npx ts-node seed-clickhouse.ts [options] + npm run seed:clickhouse -- [options] + +Options: + --url, -u ClickHouse URL (default: cloud cluster or CLICKHOUSE_URL) + --user ClickHouse username (default: default) + --password, -p ClickHouse password + --database, -d Target database name (default: default) + --scale, -s Volume preset: small | medium | large | huge (default: medium) + - small: ~106,000 rows + - medium: ~605,000 rows + - large: ~1,260,000 rows + - huge: ~6,050,000 rows + --no-drop Do not drop existing tables before inserting + --help, -h Show this help message + +Examples: + npm run seed:clickhouse + npm run seed:clickhouse -- --scale small + npm run seed:clickhouse -- --url http://localhost:8123 --database test_db + `); + process.exit(0); + } else if (arg === "--url" || arg === "-u") { + url = args[++i]; + } else if (arg === "--user") { + username = args[++i]; + } else if (arg === "--password" || arg === "-p") { + password = args[++i]; + } else if (arg === "--database" || arg === "-d") { + database = args[++i]; + } else if (arg === "--scale" || arg === "-s") { + scale = args[++i]?.toLowerCase() || "medium"; + } else if (arg === "--no-drop") { + drop = false; + } + } + + return { + url: cleanClickHouseUrl(url), + username, + password, + database, + scale, + drop, + }; +} + +async function main() { + const { url, username, password, database, scale, drop } = parseArgs(); + const config = SCALE_PRESETS[scale] || SCALE_PRESETS.medium; + const totalPlanned = + config.users + + config.products + + config.orders + + config.events + + config.metrics; + + console.log("\n======================================================="); + console.log(" RelWave ClickHouse Benchmark & Seed Generator"); + console.log("======================================================="); + console.log(`Target URL: ${url}`); + console.log(`Database: ${database}`); + console.log(`User: ${username}`); + console.log(`Preset: ${scale.toUpperCase()}`); + console.log(`Planned Rows:`); + console.log(` - users: ${config.users.toLocaleString()}`); + console.log(` - products: ${config.products.toLocaleString()}`); + console.log(` - orders: ${config.orders.toLocaleString()}`); + console.log(` - analytics_events: ${config.events.toLocaleString()}`); + console.log(` - server_metrics: ${config.metrics.toLocaleString()}`); + console.log(` - Total: ~${totalPlanned.toLocaleString()}`); + console.log("-------------------------------------------------------\n"); + + const startTime = Date.now(); + const client = createClient({ + url, + username, + password, + database, + request_timeout: 60_000, + }); + + console.log("1. Verifying ClickHouse connection..."); + const ping = await client.ping(); + if (!ping.success) { + throw new Error("Failed to ping ClickHouse server. Please check URL & credentials."); + } + console.log(" Connected successfully!\n"); + + if (drop) { + console.log("2. Dropping existing tables (clean slate)..."); + const dropTables = [ + "server_metrics", + "analytics_events", + "orders", + "products", + "users", + "schema_migrations", + ]; + for (const tbl of dropTables) { + await client.command({ + query: `DROP TABLE IF EXISTS ${database}.${tbl}`, + }); + } + console.log(" Existing tables dropped.\n"); + } + + console.log("3. Creating tables (MergeTree engines)..."); + + // Users table + await client.command({ + query: ` + CREATE TABLE IF NOT EXISTS ${database}.users ( + id UInt64, + name String, + email String, + role LowCardinality(String), + country LowCardinality(String), + is_active UInt8, + signup_date Date32, + created_at DateTime + ) ENGINE = MergeTree() + ORDER BY id + `, + }); + + // Products table + await client.command({ + query: ` + CREATE TABLE IF NOT EXISTS ${database}.products ( + id UInt64, + name String, + category LowCardinality(String), + price Decimal(10, 2), + stock UInt32, + rating Float32, + created_at DateTime + ) ENGINE = MergeTree() + ORDER BY id + `, + }); + + // Orders table with date partitioning + await client.command({ + query: ` + CREATE TABLE IF NOT EXISTS ${database}.orders ( + id UInt64, + user_id UInt64, + product_id UInt64, + quantity UInt16, + total_amount Decimal(10, 2), + status LowCardinality(String), + payment_method LowCardinality(String), + order_date DateTime + ) ENGINE = MergeTree() + PARTITION BY toYYYYMM(order_date) + ORDER BY (order_date, id) + `, + }); + + // Analytics events table (typical ClickHouse workload) + await client.command({ + query: ` + CREATE TABLE IF NOT EXISTS ${database}.analytics_events ( + id UInt64, + user_id UInt64, + event_type LowCardinality(String), + url String, + device LowCardinality(String), + ip_address String, + duration_ms UInt32, + event_time DateTime + ) ENGINE = MergeTree() + PARTITION BY toYYYYMM(event_time) + ORDER BY (event_type, event_time, id) + `, + }); + + // Server metrics timeseries table + await client.command({ + query: ` + CREATE TABLE IF NOT EXISTS ${database}.server_metrics ( + timestamp DateTime, + hostname LowCardinality(String), + cpu_usage Float32, + memory_usage Float32, + disk_io_read UInt64, + disk_io_write UInt64 + ) ENGINE = MergeTree() + ORDER BY (hostname, timestamp) + `, + }); + + // Schema migrations table for RelWave migration tracking + await client.command({ + query: ` + CREATE TABLE IF NOT EXISTS ${database}.schema_migrations ( + version String, + name String, + applied_at DateTime DEFAULT now(), + checksum String + ) ENGINE = MergeTree() + ORDER BY version + `, + }); + + console.log(" All tables created.\n"); + + console.log("4. Generating data with ClickHouse vectorized execution engine..."); + + // Seed users + process.stdout.write(` -> Inserting ${config.users.toLocaleString()} users... `); + const uStart = Date.now(); + await client.command({ + query: ` + INSERT INTO ${database}.users + SELECT + number + 1 AS id, + concat('User_', toString(number + 1)) AS name, + concat('user_', toString(number + 1), '@example.com') AS email, + ['admin', 'member', 'viewer', 'editor'][(number % 4) + 1] AS role, + ['US', 'IN', 'DE', 'GB', 'CA', 'FR', 'JP', 'AU'][(number % 8) + 1] AS country, + toUInt8(number % 5 != 0) AS is_active, + toDate(today() - (number % 365)) AS signup_date, + toDateTime(now() - (number % 86400)) AS created_at + FROM numbers(${config.users}) + `, + }); + console.log(`done in ${Date.now() - uStart}ms`); + + // Seed products + process.stdout.write(` -> Inserting ${config.products.toLocaleString()} products... `); + const pStart = Date.now(); + await client.command({ + query: ` + INSERT INTO ${database}.products + SELECT + number + 1 AS id, + concat('Product ', toString(number + 1)) AS name, + ['Electronics', 'Books', 'Clothing', 'Home', 'Toys', 'Sports', 'Garden', 'Automotive'][(number % 8) + 1] AS category, + toDecimal64(10.0 + ((number % 500) * 1.5), 2) AS price, + toUInt32((number % 100) * 5) AS stock, + toFloat32(round(3.0 + ((number % 20) * 0.1), 1)) AS rating, + toDateTime(now() - ((number % 1000) * 86400)) AS created_at + FROM numbers(${config.products}) + `, + }); + console.log(`done in ${Date.now() - pStart}ms`); + + // Seed orders + process.stdout.write(` -> Inserting ${config.orders.toLocaleString()} orders... `); + const oStart = Date.now(); + await client.command({ + query: ` + INSERT INTO ${database}.orders + SELECT + number + 1 AS id, + (number % ${config.users}) + 1 AS user_id, + (number % ${config.products}) + 1 AS product_id, + toUInt16((number % 5) + 1) AS quantity, + toDecimal64(((number % 5) + 1) * (15.0 + (number % 100)), 2) AS total_amount, + ['pending', 'processing', 'completed', 'cancelled', 'refunded'][(number % 5) + 1] AS status, + ['credit_card', 'paypal', 'apple_pay', 'crypto', 'bank_transfer'][(number % 5) + 1] AS payment_method, + toDateTime(now() - ((number % 300) * 86400)) AS order_date + FROM numbers(${config.orders}) + `, + }); + console.log(`done in ${Date.now() - oStart}ms`); + + // Seed analytics_events + process.stdout.write(` -> Inserting ${config.events.toLocaleString()} analytics events... `); + const eStart = Date.now(); + await client.command({ + query: ` + INSERT INTO ${database}.analytics_events + SELECT + number + 1 AS id, + (number % ${config.users}) + 1 AS user_id, + ['page_view', 'click', 'scroll', 'add_to_cart', 'search', 'checkout'][(number % 6) + 1] AS event_type, + ['/home', '/products', '/cart', '/checkout', '/profile', '/settings'][(number % 6) + 1] AS url, + ['mobile', 'desktop', 'tablet'][(number % 3) + 1] AS device, + concat('192.168.', toString(number % 255), '.', toString((number * 7) % 255)) AS ip_address, + toUInt32((number % 3000) + 50) AS duration_ms, + toDateTime(now() - ((number % 10000) * 60)) AS event_time + FROM numbers(${config.events}) + `, + }); + console.log(`done in ${Date.now() - eStart}ms`); + + // Seed server_metrics + process.stdout.write(` -> Inserting ${config.metrics.toLocaleString()} server metrics... `); + const mStart = Date.now(); + await client.command({ + query: ` + INSERT INTO ${database}.server_metrics + SELECT + toDateTime(now() - (number * 10)) AS timestamp, + ['prod-api-1', 'prod-api-2', 'prod-db-1', 'worker-1', 'cache-1'][(number % 5) + 1] AS hostname, + toFloat32(round(10.0 + ((number % 80) * 0.9), 2)) AS cpu_usage, + toFloat32(round(30.0 + ((number % 60) * 0.8), 2)) AS memory_usage, + toUInt64(number * 1024) AS disk_io_read, + toUInt64(number * 512) AS disk_io_write + FROM numbers(${config.metrics}) + `, + }); + console.log(`done in ${Date.now() - mStart}ms`); + + // Seed schema_migrations baseline + await client.command({ + query: ` + INSERT INTO ${database}.schema_migrations (version, name, checksum) + VALUES ('20261014000000', 'initial_clickhouse_seed', 'clickhouse_seed_baseline') + `, + }); + + const durationMs = Date.now() - startTime; + + // Retrieve verification stats + console.log("\n5. Verifying seeded table metrics from ClickHouse system catalog..."); + const statsRes = await client.query({ + query: ` + SELECT + table, + formatReadableQuantity(sum(rows)) AS total_rows, + formatReadableSize(sum(data_compressed_bytes)) AS compressed_size, + formatReadableSize(sum(data_uncompressed_bytes)) AS uncompressed_size + FROM system.parts + WHERE database = '${database}' + AND active = 1 + AND table IN ('users', 'products', 'orders', 'analytics_events', 'server_metrics', 'schema_migrations') + GROUP BY table + ORDER BY table + `, + format: "JSONEachRow", + }); + const rows = (await statsRes.json()) as Array<{ + table: string; + total_rows: string; + compressed_size: string; + uncompressed_size: string; + }>; + + console.log("\n======================================================="); + console.log(" ClickHouse Seeding Completed Successfully! 🎉"); + console.log("======================================================="); + console.log(`Execution Time: ${durationMs} ms (${(durationMs / 1000).toFixed(2)}s)`); + console.log(`Target: ${url}`); + console.log(`Database: ${database}`); + console.log("\nTable Breakdown:"); + for (const r of rows) { + console.log( + ` - ${r.table.padEnd(20)}: ${r.total_rows.padStart(10)} rows | ${r.compressed_size.padStart(10)} compressed (${r.uncompressed_size} uncompressed)`, + ); + } + console.log("=======================================================\n"); + + await client.close(); +} + +main().catch((err) => { + console.error("\n[Error] ClickHouse seed generation failed:", err); + process.exit(1); +}); From 4f98c60b72d22c2fb47eade20999f1ccbf48bdf5 Mon Sep 17 00:00:00 2001 From: Yash Date: Thu, 15 Oct 2026 16:41:30 +0530 Subject: [PATCH 05/17] docs: document ClickHouse database support in README, FEATURES, and AGENTS --- AGENTS.md | 2 +- FEATURES.md | 14 +++++++++++++- README.md | 4 ++-- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 3f50bbd0..83baa99a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -24,7 +24,7 @@ RelWave uses a **Bridge Architecture** to balance performance, security, and dev ### Bridge (Node.js) - **Runtime:** Node.js (bundled via `pkg`) -- **Database Drivers:** `pg` (PostgreSQL), `mysql2` (MySQL/MariaDB), `better-sqlite3` (SQLite), `@duckdb/node-api` (DuckDB) +- **Database Drivers:** `pg` (PostgreSQL), `mysql2` (MySQL/MariaDB), `better-sqlite3` (SQLite), `@duckdb/node-api` (DuckDB), `@clickhouse/client` (ClickHouse) - **Version Control:** `simple-git` - **Logging:** `pino` - **Security:** `@napi-rs/keyring` for encrypted credential storage diff --git a/FEATURES.md b/FEATURES.md index 2b14dec8..0103873a 100644 --- a/FEATURES.md +++ b/FEATURES.md @@ -2,7 +2,7 @@ This document provides a comprehensive breakdown of all features and capabilities in RelWave. For installation and setup instructions, see the main [README](README.md). -**Tech Stack:** Tauri + React 19 + TypeScript + Tailwind CSS + shadcn/ui + React Query + ReactFlow + Recharts + CodeMirror + better-sqlite3 + DuckDB +**Tech Stack:** Tauri + React 19 + TypeScript + Tailwind CSS + shadcn/ui + React Query + ReactFlow + Recharts + CodeMirror + better-sqlite3 + DuckDB + ClickHouse --- @@ -30,6 +30,18 @@ This document provides a comprehensive breakdown of all features and capabilitie | MariaDB | Host/port + SSL | ✅ | ✅ | ✅ | ✅ | ✅ | | SQLite | Local file path | ✅ | ✅ | ✅ | ✅ | ✅ | | DuckDB | Local file path | ✅ | ✅ | ✅ | ✅ | ✅ | +| ClickHouse | Host/port + SSL | ✅ | ✅ | ✅ | ✅ | ✅ | + +### ClickHouse-Specific Features + +- **Column-oriented analytical engine** — powered by `@clickhouse/client` over high-performance HTTP/HTTPS interface +- **Flexible connection modes** — connect via standard HTTP (`8123`) or secure HTTPS/TLS (`8443`), with custom user credentials and databases +- **URL & Parameter parsing** — parses `clickhouse://` and `clickhouses://` connection strings seamlessly +- **Introspection & Key detection** — queries `system.tables`, `system.columns`, and `system.data_skipping_indices` with `ENGINE = MergeTree()` awareness and sorting/primary key recognition +- **Cancelable streaming queries** — streams large analytical query result sets with `AbortController` support +- **Lightweight mutations** — executes `ALTER TABLE ... UPDATE` and `ALTER TABLE ... DELETE` with synchronous mutation options (`mutations_sync = 1`) +- **DDL & Schema migrations** — automated `schema_migrations` table tracking (`MergeTree() ORDER BY version`), table creations, and column migrations +- **Local & Docker discovery** — automatically scans ports `8123`/`8124` and detects ClickHouse Docker containers ### DuckDB-Specific Features diff --git a/README.md b/README.md index 3b42efa5..a8c5b13b 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ _RelWave brings the power of native Git versioning, visual ER diagrams, and seam Native Core

- Direct connections via native drivers (PostgreSQL, MySQL, SQLite, DuckDB). Zero browser overhead. Pure speed. + Direct connections via native drivers (PostgreSQL, MySQL, SQLite, DuckDB, ClickHouse). Zero browser overhead. Pure speed.
@@ -133,7 +133,7 @@ RelWave uses a **three-layer bridge architecture**. React owns the user experien image -The request path is **React UI -> Tauri command -> Node.js JSON-RPC handler -> service -> database, Git, local storage, monitoring, or LLM provider**. Responses return through the same boundary in reverse. Long-running SQL queries additionally stream `query.result`, `query.progress`, and `query.done` notifications back to the React client. The Node.js bridge contains the database connectors (PostgreSQL, MySQL/MariaDB, SQLite, DuckDB), Git service, AI services, monitoring services, project stores, credential access, and SSH tunnel handling. +The request path is **React UI -> Tauri command -> Node.js JSON-RPC handler -> service -> database, Git, local storage, monitoring, or LLM provider**. Responses return through the same boundary in reverse. Long-running SQL queries additionally stream `query.result`, `query.progress`, and `query.done` notifications back to the React client. The Node.js bridge contains the database connectors (PostgreSQL, MySQL/MariaDB, SQLite, DuckDB, ClickHouse), Git service, AI services, monitoring services, project stores, credential access, and SSH tunnel handling.
From 26bb4530dbb08ece065af82f05f4da9a439d2186 Mon Sep 17 00:00:00 2001 From: Yash Date: Thu, 15 Oct 2026 21:24:55 +0530 Subject: [PATCH 06/17] refac: improve the UX for adding DB --- index.html | 7 + .../home/components/AddConnectionDialog.tsx | 959 +++++++++++------- .../home/components/DatabasePreview.tsx | 5 +- src/features/home/components/WelcomeView.tsx | 9 +- src/lib/databases.tsx | 185 ++++ 5 files changed, 772 insertions(+), 393 deletions(-) create mode 100644 src/lib/databases.tsx diff --git a/index.html b/index.html index 84ec2709..fd38402a 100644 --- a/index.html +++ b/index.html @@ -11,6 +11,13 @@ href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;500&family=IBM+Plex+Sans:wght@400;500;600&display=swap" rel="stylesheet" /> + + + RelWave diff --git a/src/features/home/components/AddConnectionDialog.tsx b/src/features/home/components/AddConnectionDialog.tsx index 71ed4675..cf08e541 100644 --- a/src/features/home/components/AddConnectionDialog.tsx +++ b/src/features/home/components/AddConnectionDialog.tsx @@ -10,6 +10,7 @@ import { Key, FileKey, Settings, + ArrowLeft, } from "lucide-react"; import { open as openDialog } from "@tauri-apps/plugin-dialog"; import { Button } from "@/components/ui/button"; @@ -32,9 +33,16 @@ import { import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Checkbox } from "@/components/ui/checkbox"; import { Switch } from "@/components/ui/switch"; -import { parseConnectionUrl, cleanClickHouseUrl, parseClickHouseUrl } from "@/lib/parseConnectionUrl"; +import { + parseConnectionUrl, + cleanClickHouseUrl, + parseClickHouseUrl, +} from "@/lib/parseConnectionUrl"; +import { databases, getDatabaseMeta, DatabaseConfig } from "@/lib/databases"; import { AddConnectionDialogProps, INITIAL_FORM_DATA, ConnectionFormData } from "../types"; +type DialogStep = "select" | "configure"; + export function AddConnectionDialog({ open, onOpenChange, @@ -43,6 +51,7 @@ export function AddConnectionDialog({ initialData, isDiscoveredMode, }: AddConnectionDialogProps) { + const [step, setStep] = useState("select"); const [useUrl, setUseUrl] = useState(true); const [connectionUrl, setConnectionUrl] = useState(""); const [formData, setFormData] = useState(INITIAL_FORM_DATA); @@ -50,11 +59,12 @@ export function AddConnectionDialog({ // Apply initial data when dialog opens with prefilled values useEffect(() => { if (open) { - if (initialData) { + if (initialData && initialData.type) { const reconstructedUrl = initialData.url || (initialData.type === "clickhouse" && initialData.host - ? `${initialData.ssl ? "https" : "http"}://${initialData.host}:${initialData.port || (initialData.ssl ? "8443" : "8123")}` + ? `${initialData.ssl ? "https" : "http"}://${initialData.host}:${initialData.port || (initialData.ssl ? "8443" : "8123") + }` : ""); setFormData((prev) => ({ @@ -63,6 +73,7 @@ export function AddConnectionDialog({ ...initialData, url: reconstructedUrl || initialData.url || "", })); + if ( isDiscoveredMode || initialData.type === "clickhouse" || @@ -71,11 +82,13 @@ export function AddConnectionDialog({ ) { setUseUrl(false); } + setStep("configure"); } else { - // Reset to empty form when opening without initial data + // Reset to initial state when opening fresh setFormData(INITIAL_FORM_DATA); setConnectionUrl(""); setUseUrl(true); + setStep("select"); } } }, [open, initialData, isDiscoveredMode]); @@ -84,6 +97,34 @@ export function AddConnectionDialog({ setFormData((prev) => ({ ...prev, [field]: value })); }; + const handleSelectDatabase = (db: DatabaseConfig) => { + const isFileBased = db.isFileBased || db.type === "sqlite" || db.type === "duckdb"; + const isCh = db.type === "clickhouse"; + + setFormData((prev) => ({ + ...prev, + type: db.type, + name: prev.name || `${db.name} Local`, + host: isFileBased ? "" : prev.host || (isCh ? "localhost" : "localhost"), + port: isFileBased ? "" : prev.port || db.defaultPort || "", + user: isFileBased ? "" : prev.user || db.defaultUser || "", + password: isFileBased ? "" : prev.password || "", + database: isFileBased ? "" : prev.database || db.defaultDatabase || "", + ssl: isCh ? false : prev.ssl, + sslmode: "", + url: isCh && !prev.url ? `http://localhost:${db.defaultPort || "8123"}` : prev.url, + useSsh: false, + })); + + if (isFileBased || isCh) { + setUseUrl(false); + } else { + setUseUrl(true); + } + + setStep("configure"); + }; + const handleUrlChange = (url: string) => { setConnectionUrl(url); const parsed = parseConnectionUrl(url); @@ -128,8 +169,8 @@ export function AddConnectionDialog({ url: cleanedUrl, host: parsed.host || formData.host || "localhost", port: parsed.port || formData.port || (parsed.ssl ? "8443" : "8123"), - ssl: parsed.ssl, - sslmode: parsed.ssl ? "require" : "disable", + ssl: parsed.ssl ?? formData.ssl, + sslmode: (parsed.ssl ?? formData.ssl) ? "require" : "disable", user: formData.user || parsed.user || "default", password: formData.password || parsed.password || "", database: formData.database || parsed.database || "default", @@ -147,22 +188,22 @@ export function AddConnectionDialog({ setFormData(INITIAL_FORM_DATA); setConnectionUrl(""); setUseUrl(true); + setStep("select"); } onOpenChange(newOpen); }; const isFileBasedDB = formData.type === "sqlite" || formData.type === "duckdb"; const isClickHouse = formData.type === "clickhouse"; - const showSslOption = - formData.type === "postgresql" || - formData.type === "mariadb"; + const showSslOption = formData.type === "postgresql" || formData.type === "mariadb"; + const selectedDbMeta = getDatabaseMeta(formData.type); return ( { - if (e.key === "Enter") { + if (e.key === "Enter" && step === "configure") { const target = e.target as HTMLElement; if (target.tagName !== "BUTTON" && target.tagName !== "A") { e.preventDefault(); @@ -171,441 +212,585 @@ export function AddConnectionDialog({ } }} > - - - - {initialData ? "Edit Connection" : "New Connection"} - - - Connect to PostgreSQL, MySQL, MariaDB, SQLite, DuckDB, or ClickHouse - - - -
- {!isFileBasedDB && !isClickHouse && !isDiscoveredMode && ( - setUseUrl(v === "url")}> - - - - URL - - - - Parameters - - - - )} - - {useUrl && !isFileBasedDB && !isClickHouse && ( -
- - handleUrlChange(e.target.value)} - className="font-mono text-xs h-9" - /> -
- )} - -
- - handleInputChange("name", e.target.value)} - className="h-9 text-sm" - /> -
- - {!useUrl && ( -
- - + {step === "select" ? ( + <> + + + + Select Database + + + Choose a database engine to configure your connection. + + + +
+ {databases.map((db) => ( + + ))}
- )} - {!useUrl && - (isFileBasedDB ? ( -
- -
- handleInputChange("database", e.target.value)} - readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono flex-1 ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`} - /> +
+ +
+ + ) : ( + <> + + {!isDiscoveredMode && !initialData && ( +
-
- ) : isClickHouse ? ( - <> -
- - { - const val = e.target.value; - handleInputChange("url", val); - const parsed = parseClickHouseUrl(val); + )} + + +
+ {selectedDbMeta?.icon || } +
+ + {initialData + ? `Edit ${selectedDbMeta?.name || "Connection"}` + : `Connect to ${selectedDbMeta?.name || "Database"}`} + +
+ + {selectedDbMeta?.description || "Enter connection credentials and details."} + + + +
+ {/* Tabs for URL vs Parameters (PostgreSQL, MySQL, MariaDB, ClickHouse) */} + {!isFileBasedDB && !isDiscoveredMode && ( + { + const isUrlMode = v === "url"; + setUseUrl(isUrlMode); + if (isClickHouse && !isUrlMode && !formData.url) { setFormData((prev) => ({ ...prev, - url: val, - host: parsed.host || prev.host, - port: parsed.port || prev.port, - ssl: parsed.ssl, - user: prev.user || parsed.user || "default", - database: prev.database || parsed.database || "default", + url: `${prev.ssl ? "https" : "http"}://${prev.host || "localhost"}:${prev.port || (prev.ssl ? "8443" : "8123") + }`, })); - }} - readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`} - /> -
+ } + }} + > + + + + URL + + + + Parameters + + + + )} + {/* Connection URL Mode */} + {useUrl && !isFileBasedDB && !isClickHouse && (
- + handleInputChange("user", e.target.value)} - readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`} + placeholder={ + selectedDbMeta?.placeholderUrl || "postgres://user:pass@localhost:5432/db" + } + value={connectionUrl} + onChange={(e) => handleUrlChange(e.target.value)} + className="font-mono text-xs h-9" />
+ )} -
- - handleInputChange("password", e.target.value)} - readOnly={isDiscoveredMode} - className={`h-9 text-sm ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`} - /> -
- - ) : ( - <> -
-
- - handleInputChange("host", e.target.value)} - readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`} - /> -
-
- + {/* Common Connection Name */} +
+ + handleInputChange("name", e.target.value)} + className="h-9 text-sm" + /> +
+ + {/* File-based DB (SQLite or DuckDB) */} + {isFileBasedDB ? ( +
+ +
handleInputChange("port", e.target.value)} + value={formData.database} + onChange={(e) => handleInputChange("database", e.target.value)} readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`} + className={`h-9 text-sm font-mono flex-1 ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} /> +
+

+ {formData.type === "duckdb" + ? "Select a local DuckDB file (.duckdb, .db, .ddb). Direct fast in-process analytical execution." + : "Select a local SQLite file (.db, .sqlite, .sqlite3). Direct zero-configuration database."} +

+ ) : isClickHouse ? ( + /* ClickHouse Specific Form */ + <> + {useUrl ? ( +
+ + { + const val = e.target.value; + handleInputChange("url", val); + const parsed = parseClickHouseUrl(val); + setFormData((prev) => ({ + ...prev, + url: val, + host: parsed.host || prev.host, + port: parsed.port || prev.port, + ssl: parsed.ssl, + user: prev.user || parsed.user || "default", + database: prev.database || parsed.database || "default", + })); + }} + readOnly={isDiscoveredMode} + className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} + /> +
+ ) : ( +
+
+ + { + const val = e.target.value; + handleInputChange("host", val); + setFormData((prev) => ({ + ...prev, + host: val, + url: `${prev.ssl ? "https" : "http"}://${val || "localhost"}:${prev.port || (prev.ssl ? "8443" : "8123") + }`, + })); + }} + readOnly={isDiscoveredMode} + className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} + /> +
+
+ + { + const val = e.target.value; + handleInputChange("port", val); + setFormData((prev) => ({ + ...prev, + port: val, + url: `${prev.ssl ? "https" : "http"}://${prev.host || "localhost"}:${val || (prev.ssl ? "8443" : "8123") + }`, + })); + }} + readOnly={isDiscoveredMode} + className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} + /> +
+
+ )} -
-
- - handleInputChange("user", e.target.value)} - readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`} - /> +
+
+ + handleInputChange("user", e.target.value)} + readOnly={isDiscoveredMode} + className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} + /> +
+
+ + handleInputChange("password", e.target.value)} + readOnly={isDiscoveredMode} + className={`h-9 text-sm ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} + /> +
+
- + handleInputChange("password", e.target.value)} + placeholder="default" + value={formData.database} + onChange={(e) => handleInputChange("database", e.target.value)} readOnly={isDiscoveredMode} - className={`h-9 text-sm ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`} + className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} />
-
-
- - handleInputChange("database", e.target.value)} - readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : ""}`} - /> -
+ {!isDiscoveredMode && !useUrl && ( +
+ { + const isSsl = Boolean(checked); + setFormData((prev) => ({ + ...prev, + ssl: isSsl, + port: isSsl + ? prev.port === "8123" + ? "8443" + : prev.port + : prev.port === "8443" + ? "8123" + : prev.port, + url: `${isSsl ? "https" : "http"}://${prev.host || "localhost"}:${isSsl ? (prev.port === "8123" ? "8443" : prev.port) : prev.port + }`, + })); + }} + /> + +
+ )} + + ) : !useUrl ? ( + /* Parameters Mode for PostgreSQL, MySQL, MariaDB */ + <> +
+
+ + handleInputChange("host", e.target.value)} + readOnly={isDiscoveredMode} + className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} + /> +
+
+ + handleInputChange("port", e.target.value)} + readOnly={isDiscoveredMode} + className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} + /> +
+
- {showSslOption && !isDiscoveredMode && ( -
- - setFormData((prev) => ({ ...prev, ssl: checked as boolean })) - } +
+
+ + handleInputChange("user", e.target.value)} + readOnly={isDiscoveredMode} + className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} + /> +
+
+ + handleInputChange("password", e.target.value)} + readOnly={isDiscoveredMode} + className={`h-9 text-sm ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} + /> +
+
+ +
+ + handleInputChange("database", e.target.value)} + readOnly={isDiscoveredMode} + className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} /> -
- )} - {/* SSH Tunnel Section */} - {!isDiscoveredMode && ( -
-
-
- - -
- handleInputChange("useSsh", checked)} + {showSslOption && !isDiscoveredMode && ( +
+ + setFormData((prev) => ({ ...prev, ssl: checked as boolean })) + } /> +
+ )} - {formData.useSsh && ( -
-
-
- - handleInputChange("sshHost", e.target.value)} - className="h-8 text-xs font-mono" - /> -
-
- - handleInputChange("sshPort", e.target.value)} - className="h-8 text-xs font-mono" - /> -
-
- -
-
+ )} +
+ )} + + ) : null} +
+ +
+
+ {!isDiscoveredMode && !initialData && ( + )} - - ))} -
- -
- - -
+
+
+ + +
+
+ + )}
); diff --git a/src/features/home/components/DatabasePreview.tsx b/src/features/home/components/DatabasePreview.tsx index 479812d4..1591c5d0 100644 --- a/src/features/home/components/DatabasePreview.tsx +++ b/src/features/home/components/DatabasePreview.tsx @@ -19,6 +19,7 @@ import { import { cn } from "@/lib/utils"; import { DatabasePreviewProps } from "../types"; import { DatabaseOverviewPanel } from "./DatabaseOverviewPanel"; +import { DatabaseLogo } from "@/lib/databases"; import { useProjectByDatabaseId, useProjectSchema, @@ -88,11 +89,11 @@ export function DatabasePreview({
- +
diff --git a/src/features/home/components/WelcomeView.tsx b/src/features/home/components/WelcomeView.tsx index f4d56637..1589963f 100644 --- a/src/features/home/components/WelcomeView.tsx +++ b/src/features/home/components/WelcomeView.tsx @@ -17,6 +17,7 @@ import { useDbStats, useTables } from "@/features/project/hooks/useDbQueries"; import { bytesToMBString } from "@/lib/bytesToMB"; import { DatabaseConnection } from "@/features/database/types"; import { useCountUp } from "../hooks/useCountUp"; +import { DatabaseLogo } from "@/lib/databases"; const DB_COLORS: Record = { postgresql: { bg: "bg-blue-500/10", text: "text-blue-500" }, @@ -37,7 +38,7 @@ function ConnectionSizeItem({ db }: { db: DatabaseConnection }) { return (
- + {db.name}
@@ -60,7 +61,7 @@ function ConnectionTablesList({ db }: { db: DatabaseConnection }) {
- +

{db.name}

@@ -291,11 +292,11 @@ export function WelcomeView({ >
- +

{db.name}

diff --git a/src/lib/databases.tsx b/src/lib/databases.tsx new file mode 100644 index 00000000..1535f967 --- /dev/null +++ b/src/lib/databases.tsx @@ -0,0 +1,185 @@ +import type { ReactNode } from "react"; +import type { DatabaseType } from "@/features/database/types"; +import { cn } from "@/lib/utils"; +import { Database } from "lucide-react"; + +export type SupportedDatabaseType = DatabaseType | "mariadb"; + +export interface DatabaseConfig { + id: SupportedDatabaseType; + type: SupportedDatabaseType; + name: string; + icon: ReactNode; + deviconClass?: string; + category: "Relational" | "Embedded" | "OLAP"; + description: string; + defaultPort?: string; + defaultUser?: string; + defaultDatabase?: string; + placeholderUrl?: string; + isFileBased?: boolean; + fileExtensions?: string[]; + supportsSsl?: boolean; + supportsSsh?: boolean; +} + +export const databases: DatabaseConfig[] = [ + { + id: "postgresql", + type: "postgresql", + name: "PostgreSQL", + category: "Relational", + description: "Powerful open-source object-relational SQL database", + defaultPort: "5432", + defaultUser: "postgres", + defaultDatabase: "postgres", + placeholderUrl: "postgresql://postgres:password@localhost:5432/mydb", + supportsSsl: true, + supportsSsh: true, + deviconClass: "devicon-postgresql-plain colored", + icon: ( + + + + + + + ), + }, + { + id: "mysql", + type: "mysql", + name: "MySQL", + category: "Relational", + description: "High-performance open-source relational database", + defaultPort: "3306", + defaultUser: "root", + defaultDatabase: "mysql", + placeholderUrl: "mysql://root:password@localhost:3306/mydb", + supportsSsl: false, + supportsSsh: true, + deviconClass: "devicon-mysql-original colored", + icon: ( + + + + ), + }, + { + id: "mariadb", + type: "mariadb", + name: "MariaDB", + category: "Relational", + description: "Fast, community-driven relational database fork of MySQL", + defaultPort: "3306", + defaultUser: "root", + defaultDatabase: "mariadb", + placeholderUrl: "mariadb://root:password@localhost:3306/mydb", + supportsSsl: true, + supportsSsh: true, + deviconClass: "devicon-mariadb-original colored", + icon: ( + + + + ), + }, + { + id: "sqlite", + type: "sqlite", + name: "SQLite", + category: "Embedded", + description: "Lightweight, zero-config file-based embedded SQL database", + isFileBased: true, + fileExtensions: ["db", "sqlite", "sqlite3", "s3db"], + deviconClass: "devicon-sqlite-plain colored", + icon: ( + + + + + + + + + + + + + ), + }, + { + id: "duckdb", + type: "duckdb", + name: "DuckDB", + category: "Embedded", + description: "Fast in-process columnar analytical SQL database file", + isFileBased: true, + fileExtensions: ["db", "duckdb", "ddb"], + icon: ( + + + + + ), + }, + { + id: "clickhouse", + type: "clickhouse", + name: "ClickHouse", + category: "OLAP", + description: "Ultra-fast columnar database for real-time analytics", + defaultPort: "8123", + defaultUser: "default", + defaultDatabase: "default", + placeholderUrl: "http://default:password@localhost:8123/default", + supportsSsl: true, + supportsSsh: false, + icon: ( + + + + + ), + }, +]; + +export const SUPPORTED_DATABASES = databases; + +export function getDatabaseMeta(typeOrName?: string | null): DatabaseConfig | undefined { + if (!typeOrName) return undefined; + const normalized = typeOrName.toLowerCase().trim(); + return databases.find( + (db) => db.id === normalized || db.type === normalized || db.name.toLowerCase() === normalized, + ); +} + +export function getDatabaseIcon(typeOrName?: string | null): ReactNode | null { + return getDatabaseMeta(typeOrName)?.icon ?? null; +} + +export function DatabaseLogo({ + type, + className = "w-5 h-5", + useDevicon = false, +}: { + type?: string | null; + className?: string; + useDevicon?: boolean; +}) { + const meta = getDatabaseMeta(type); + + if (!meta) { + return ; + } + + if (useDevicon && meta.deviconClass) { + return ; + } + + return ( +
svg]:w-full [&>svg]:h-full", className)}> + {meta.icon} +
+ ); +} \ No newline at end of file From 680796ffea05a869bcfff50c87b581bafef6eb09 Mon Sep 17 00:00:00 2001 From: Yash Date: Thu, 15 Oct 2026 23:12:21 +0530 Subject: [PATCH 07/17] perf(ai): optimize AI settings loading and update Groq and Mistral models - Convert bridge ai.loadSettings and ai.saveSettings to synchronous disk I/O - Add fast timeouts (3s for loadSettings, 5s for saveSettings, 15s for testConnection) - Add in-memory cache and request deduplication in ai.ts - Pre-warm AI settings on SettingsDialog mount - Memoize AISettings components to eliminate re-rendering lag - Update Groq and Mistral provider model lists and error handling --- bridge/src/ai/providers/groq.provider.ts | 52 +++++- bridge/src/ai/providers/mistral.provider.ts | 18 +- bridge/src/ai/providers/types.ts | 100 +++++++++-- bridge/src/handlers/aiHandlers.ts | 33 +++- bridge/src/services/aiCacheService.ts | 2 +- src/features/ai/hooks/useAISettings.ts | 39 +++-- .../settings/components/AISettings.tsx | 163 +++++++++++++----- .../settings/components/SettingsDialog.tsx | 10 +- src/services/bridge/ai.ts | 92 +++++++--- src/services/bridge/bridgeClient.ts | 5 + 10 files changed, 407 insertions(+), 107 deletions(-) diff --git a/bridge/src/ai/providers/groq.provider.ts b/bridge/src/ai/providers/groq.provider.ts index 06142c32..7f9c2a55 100644 --- a/bridge/src/ai/providers/groq.provider.ts +++ b/bridge/src/ai/providers/groq.provider.ts @@ -13,7 +13,15 @@ import { parseChartRecommendation, } from "../prompts/chart-recommendation"; -const DEFAULT_MODEL = "llama-3.3-70b-versatile"; + +const DEFAULT_MODEL = "openai/gpt-oss-20b"; +const FALLBACK_MODELS = [ + "openai/gpt-oss-20b", + "qwen/qwen3.8-27b", + "openai/gpt-oss-120b", + "llama-3.1-8b-instant", + "llama-3.3-70b-versatile", +]; export class GroqProvider implements AIProvider { private client: Groq; @@ -36,7 +44,27 @@ export class GroqProvider implements AIProvider { }); return res.choices[0]?.message?.content ?? ""; } catch (err) { - throw classifyError(err, "groq"); + const classified = classifyError(err, "groq"); + if (classified.code === "INVALID_MODEL") { + // Attempt fallback to a supported chat model so ongoing requests don't fail + for (const fallback of FALLBACK_MODELS) { + if (fallback === this.model) continue; + try { + const res = await this.client.chat.completions.create({ + model: fallback, + messages: [ + { role: "system", content: system }, + { role: "user", content: user }, + ], + max_tokens: 4096, + }); + return res.choices[0]?.message?.content ?? ""; + } catch { + // Continue to next fallback + } + } + } + throw classified; } } @@ -57,6 +85,18 @@ export class GroqProvider implements AIProvider { } async testConnection(): Promise { + let availableChatModels: string[] = []; + try { + const modelsRes = await this.client.models.list(); + const nonChatKeywords = ["whisper", "prompt-guard", "orpheus", "safeguard", "vision", "embed"]; + availableChatModels = (modelsRes.data ?? []) + .map((m) => m.id) + .filter((id) => !nonChatKeywords.some((kw) => id.toLowerCase().includes(kw))); + } catch (err) { + // If models.list fails with invalid key or network issue, classify and throw + throw classifyError(err, "groq"); + } + try { await this.client.chat.completions.create({ model: this.model, @@ -65,7 +105,12 @@ export class GroqProvider implements AIProvider { }); return ""; } catch (err) { - throw classifyError(err, "groq"); + const classified = classifyError(err, "groq"); + if (classified.code === "INVALID_MODEL") { + const sample = availableChatModels.slice(0, 3).join(", "); + return `Connected to Groq (API key verified)! However, model '${this.model}' is not accessible on your account. Available models include: ${sample || "openai/gpt-oss-20b, qwen/qwen3.8-27b"}. Please select one in settings.`; + } + throw classified; } } @@ -73,3 +118,4 @@ export class GroqProvider implements AIProvider { return this.complete(system, user); } } + diff --git a/bridge/src/ai/providers/mistral.provider.ts b/bridge/src/ai/providers/mistral.provider.ts index 976e92e2..3844bffd 100644 --- a/bridge/src/ai/providers/mistral.provider.ts +++ b/bridge/src/ai/providers/mistral.provider.ts @@ -63,6 +63,15 @@ export class MistralProvider implements AIProvider { } async testConnection(): Promise { + // 1. Verify credentials and network connectivity via models.list() + // This confirms the API key is genuine without consuming chat tokens or triggering chat RPM limits. + try { + await this.client.models.list(); + } catch (err) { + throw classifyError(err, "mistral"); + } + + // 2. Test chat completion endpoint with the selected model try { await this.client.chat.complete({ model: this.model, @@ -71,7 +80,14 @@ export class MistralProvider implements AIProvider { }); return ""; } catch (err) { - throw classifyError(err, "mistral"); + const classified = classifyError(err, "mistral"); + if (classified.code === "RATE_LIMIT") { + return "Connected to Mistral (API key verified)! Note: Chat completions are currently rate-limited (code 1300). Free tier accounts have strict quotas (1 req/sec). Please wait a moment or check your plan at console.mistral.ai."; + } + if (classified.code === "INVALID_MODEL") { + return `Connected to Mistral (API key verified)! However, model '${this.model}' is not accessible on your account. Please select 'Mistral Small' or check console.mistral.ai.`; + } + throw classified; } } diff --git a/bridge/src/ai/providers/types.ts b/bridge/src/ai/providers/types.ts index 1aef5d3f..3c707950 100644 --- a/bridge/src/ai/providers/types.ts +++ b/bridge/src/ai/providers/types.ts @@ -48,56 +48,132 @@ export class AIError extends Error { readonly code: AIErrorCode; readonly provider: string; readonly originalMessage: string; + readonly rawMessage?: string; - constructor(code: AIErrorCode, provider: string, message: string) { + constructor(code: AIErrorCode, provider: string, message: string, rawMessage?: string) { super(`[${provider}] ${message}`); this.name = "AIError"; this.code = code; this.provider = provider; this.originalMessage = message; + this.rawMessage = rawMessage; } } +function capitalize(str: string): string { + if (!str) return ""; + return str.charAt(0).toUpperCase() + str.slice(1); +} + +function extractCleanMessage(raw: string): string { + try { + const jsonMatch = raw.match(/\{[\s\S]*\}/); + if (jsonMatch) { + const parsed = JSON.parse(jsonMatch[0]); + if (parsed.error?.message) { + return parsed.error.message; + } + if (parsed.message) { + return parsed.message; + } + } + } catch { + // Ignore JSON parsing errors + } + + return raw + .replace(/^API error occurred:\s*/i, "") + .replace(/^Status \d+\s*(Body:\s*)?/i, "") + .replace(/^\d{3}\s+/i, "") + .trim(); +} + /** * Classify a raw SDK/network error into an AIErrorCode. */ export function classifyError(err: unknown, provider: string): AIError { - const msg = err instanceof Error ? err.message : String(err); - const lower = msg.toLowerCase(); + const rawMsg = err instanceof Error ? err.message : String(err); + const lower = rawMsg.toLowerCase(); + const cleanMsg = extractCleanMessage(rawMsg); + const provName = capitalize(provider); if ( lower.includes("api key") || lower.includes("apikey") || lower.includes("authentication") || lower.includes("unauthorized") || + lower.includes("invalid x-api-key") || lower.includes("401") ) { - return new AIError("INVALID_API_KEY", provider, msg); + return new AIError( + "INVALID_API_KEY", + provider, + `Invalid or unauthorized API key for ${provName}. Please check your credentials in the ${provName} console.`, + rawMsg, + ); } + if ( lower.includes("rate limit") || lower.includes("429") || - lower.includes("too many requests") + lower.includes("too many requests") || + lower.includes("rate_limited") || + lower.includes("quota") ) { - return new AIError("RATE_LIMIT", provider, msg); + let friendly = `Rate limit exceeded for ${provName}. Please wait a moment before trying again.`; + if (provider.toLowerCase() === "mistral") { + friendly = `Mistral rate limit exceeded (code 1300). Free tier accounts are limited to 1 request/second and monthly limits. Please wait a moment or upgrade to pay-as-you-go at console.mistral.ai.`; + } else if (provider.toLowerCase() === "groq") { + friendly = `Groq rate limit exceeded. Please wait a moment or check your account limits at console.groq.com.`; + } + return new AIError("RATE_LIMIT", provider, friendly, rawMsg); } + if (lower.includes("timeout") || lower.includes("timed out")) { - return new AIError("TIMEOUT", provider, msg); + return new AIError( + "TIMEOUT", + provider, + `Request to ${provName} timed out. Please check your network connection and try again.`, + rawMsg, + ); } + if ( lower.includes("econnrefused") || lower.includes("enotfound") || lower.includes("fetch failed") || lower.includes("network") ) { - return new AIError("NETWORK_FAILURE", provider, msg); + return new AIError( + "NETWORK_FAILURE", + provider, + `Unable to reach ${provName} servers. Please check your internet connection or proxy settings.`, + rawMsg, + ); } - if (lower.includes("model") && (lower.includes("not found") || lower.includes("invalid"))) { - return new AIError("INVALID_MODEL", provider, msg); + + if ( + lower.includes("model") && + (lower.includes("not found") || + lower.includes("invalid") || + lower.includes("does not exist") || + lower.includes("do not have access")) + ) { + let friendly = `The selected model is not available for ${provName} (${cleanMsg}). Please select a different model in AI settings.`; + if (provider.toLowerCase() === "groq") { + friendly = `The selected model is not accessible on your Groq tier or organization (${cleanMsg}). Please switch model in settings (e.g., GPT-OSS 20B or Qwen 3.8 27B) or check console.groq.com.`; + } + return new AIError("INVALID_MODEL", provider, friendly, rawMsg); } + if (lower.includes("unavailable") || lower.includes("503") || lower.includes("overloaded")) { - return new AIError("PROVIDER_UNAVAILABLE", provider, msg); + return new AIError( + "PROVIDER_UNAVAILABLE", + provider, + `${provName} service is temporarily unavailable or overloaded. Please try again shortly.`, + rawMsg, + ); } - return new AIError("UNKNOWN", provider, msg); + return new AIError("UNKNOWN", provider, cleanMsg || rawMsg, rawMsg); } diff --git a/bridge/src/handlers/aiHandlers.ts b/bridge/src/handlers/aiHandlers.ts index 6155b3ba..65acd12b 100644 --- a/bridge/src/handlers/aiHandlers.ts +++ b/bridge/src/handlers/aiHandlers.ts @@ -40,13 +40,26 @@ export class AIHandlers { async handleTestConnection(params: AITestConnectionParams, id: number | string) { try { const provider = this.aiService.resolveProvider(params.settings); - await provider.testConnection(); - this.rpc.sendResponse(id, { ok: true, data: { connected: true } }); + const warning = await provider.testConnection(); + this.rpc.sendResponse(id, { + ok: true, + data: { + connected: true, + warning: warning || undefined, + }, + }); } catch (err) { this.logger?.warn({ err }, "ai.testConnection failed"); const msg = err instanceof AIError ? err.originalMessage : String(err); const code = err instanceof AIError ? err.code : "UNKNOWN"; - this.rpc.sendError(id, { code, message: msg }); + this.rpc.sendResponse(id, { + ok: true, + data: { + connected: false, + code, + message: msg, + }, + }); } } @@ -284,7 +297,7 @@ export class AIHandlers { // ── Settings persistence (reads/writes ai-settings.json) ────────────── - async handleLoadSettings(_params: unknown, id: number | string) { + handleLoadSettings(_params: unknown, id: number | string) { try { ensureDir(CONFIG_FOLDER); if (!fsSync.existsSync(AI_SETTINGS_FILE)) { @@ -292,7 +305,7 @@ export class AIHandlers { this.rpc.sendResponse(id, { ok: true, data: {} }); return; } - const raw = await fs.readFile(AI_SETTINGS_FILE, "utf-8"); + const raw = fsSync.readFileSync(AI_SETTINGS_FILE, "utf-8"); const settings = JSON.parse(raw) as AISettings; this.rpc.sendResponse(id, { ok: true, data: settings }); } catch (err: any) { @@ -302,13 +315,17 @@ export class AIHandlers { } } - async handleSaveSettings(params: { settings: AISettings }, id: number | string) { + handleSaveSettings(params: { settings: AISettings }, id: number | string) { try { ensureDir(CONFIG_FOLDER); - await fs.writeFile(AI_SETTINGS_FILE, JSON.stringify(params.settings, null, 2), "utf-8"); + fsSync.writeFileSync(AI_SETTINGS_FILE, JSON.stringify(params.settings, null, 2), "utf-8"); // On non-Windows platforms, restrict file permissions (contains API keys) if (process.platform !== "win32") { - await fs.chmod(AI_SETTINGS_FILE, 0o600); + try { + fsSync.chmodSync(AI_SETTINGS_FILE, 0o600); + } catch { + // ignore chmod errors on unsupported filesystems + } } this.rpc.sendResponse(id, { ok: true, data: { saved: true } }); } catch (err: any) { diff --git a/bridge/src/services/aiCacheService.ts b/bridge/src/services/aiCacheService.ts index 0ef66487..a1873417 100644 --- a/bridge/src/services/aiCacheService.ts +++ b/bridge/src/services/aiCacheService.ts @@ -114,7 +114,7 @@ function resolveModelName(settings: AISettings): string { case "gemini": return settings.geminiModel ?? "gemini-1.5-flash"; case "groq": - return settings.groqModel ?? "llama-3.3-70b-versatile"; + return settings.groqModel ?? "openai/gpt-oss-20b"; case "mistral": return settings.mistralModel ?? "mistral-small-latest"; case "ollama": diff --git a/src/features/ai/hooks/useAISettings.ts b/src/features/ai/hooks/useAISettings.ts index bf8c8b02..abbf870c 100644 --- a/src/features/ai/hooks/useAISettings.ts +++ b/src/features/ai/hooks/useAISettings.ts @@ -1,30 +1,39 @@ -import { loadAISettings, type AISettings } from "@/services/bridge/ai"; -import { useEffect, useState } from "react"; +import { loadAISettings, getCachedAISettings, type AISettings } from "@/services/bridge/ai"; +import { useEffect, useState, useCallback } from "react"; const DEFAULT: AISettings = { defaultProvider: "openai" }; /** - * Reads AISettings from the bridge (ai-settings.json on disk). - * Returns a stable object that is refreshed whenever the settings dialog saves. - * - * Because saving goes through the bridge and NOT localStorage, the old - * StorageEvent trick no longer applies. Instead, callers that need fresh - * settings after a save should re-mount or call `reload()`. + * Reads AISettings from the bridge (ai-settings.json on disk) with instant memory cache. */ export function useAISettings(): { settings: AISettings; isLoading: boolean; reload: () => void } { - const [settings, setSettings] = useState(DEFAULT); - const [isLoading, setIsLoading] = useState(true); + const cached = getCachedAISettings(); + const [settings, setSettings] = useState(cached ?? DEFAULT); + const [isLoading, setIsLoading] = useState(!cached); const [tick, setTick] = useState(0); useEffect(() => { - setIsLoading(true); + let active = true; + if (!cached) { + setIsLoading(true); + } loadAISettings().then((loaded) => { - setSettings(loaded); - setIsLoading(false); + if (active) { + setSettings(loaded); + setIsLoading(false); + } }); - }, [tick]); + return () => { + active = false; + }; + }, [tick, cached]); - const reload = () => setTick((t) => t + 1); + const reload = useCallback(() => { + loadAISettings({ forceRefresh: true }).then((loaded) => { + setSettings(loaded); + }); + setTick((t) => t + 1); + }, []); return { settings, isLoading, reload }; } diff --git a/src/features/settings/components/AISettings.tsx b/src/features/settings/components/AISettings.tsx index 3b85fa02..f9b0d2de 100644 --- a/src/features/settings/components/AISettings.tsx +++ b/src/features/settings/components/AISettings.tsx @@ -1,10 +1,12 @@ -import { useState, useEffect } from "react"; +import { useState, useEffect, useMemo, useCallback, memo } from "react"; import { Bot, Eye, EyeOff, CheckCircle2, XCircle, + AlertCircle, + AlertTriangle, Loader2, ChevronDown, Zap, @@ -29,6 +31,7 @@ import { type AISettings as AISettingsData, loadAISettings, saveAISettings, + getCachedAISettings, aiService, } from "@/services/bridge/ai"; @@ -63,9 +66,11 @@ const MODEL_OPTIONS: Partial> = { { value: "gemini-2.5-pro-preview-06-05", label: "Gemini 2.5 Pro", tier: "powerful" }, ], groq: [ + { value: "openai/gpt-oss-20b", label: "GPT-OSS 20B", tier: "fast" }, + { value: "qwen/qwen3.8-27b", label: "Qwen 3.8 27B", tier: "balanced" }, + { value: "openai/gpt-oss-120b", label: "GPT-OSS 120B", tier: "powerful" }, { value: "llama-3.1-8b-instant", label: "Llama 3.1 8B", tier: "fast" }, { value: "llama-3.3-70b-versatile", label: "Llama 3.3 70B", tier: "balanced" }, - { value: "moonshotai/kimi-k2-instruct", label: "Kimi K2", tier: "powerful" }, ], mistral: [ { value: "mistral-small-latest", label: "Mistral Small", tier: "fast" }, @@ -133,7 +138,7 @@ const PROVIDERS: ProviderMeta[] = [ { name: "groq", label: "Groq", - defaultModelLabel: "Llama 3.3 70B", + defaultModelLabel: "GPT-OSS 20B", requiresKey: true, keyField: "groqApiKey", keyPlaceholder: "gsk_…", @@ -188,7 +193,7 @@ const TIER_CONFIG: Record ); -} +}); // ── Model selector ──────────────────────────────────────────────────────── -function ModelSelector({ +const ModelSelector = memo(function ModelSelector({ provider, value, onChange, @@ -283,13 +288,13 @@ function ModelSelector({
); -} +}); // ── Connection status ───────────────────────────────────────────────────── -type ConnectionStatus = "idle" | "testing" | "ok" | "error"; +type ConnectionStatus = "idle" | "testing" | "ok" | "warning" | "error"; -function StatusBadge({ status, message }: { status: ConnectionStatus; message?: string }) { +const StatusBadge = memo(function StatusBadge({ status }: { status: ConnectionStatus }) { if (status === "idle") return null; return (
{status === "testing" && } {status === "ok" && } + {status === "warning" && } {status === "error" && } - {status === "testing" ? "Testing…" : status === "ok" ? "Connected" : (message ?? "Failed")} + {status === "testing" + ? "Testing…" + : status === "ok" + ? "Connected" + : status === "warning" + ? "Notice" + : "Connection Failed"}
); -} +}); // ── Password field ──────────────────────────────────────────────────────── -function SecretInput({ +const SecretInput = memo(function SecretInput({ value, onChange, placeholder, @@ -345,69 +358,108 @@ function SecretInput({
); -} +}); // ── Main component ──────────────────────────────────────────────────────── export default function AISettings() { - const [settings, setSettings] = useState({ defaultProvider: "ollama" }); - const [isLoading, setIsLoading] = useState(true); + const cached = getCachedAISettings(); + const [settings, setSettings] = useState( + () => cached ?? { defaultProvider: "ollama" }, + ); + const [isLoading, setIsLoading] = useState(() => !cached); const [dirty, setDirty] = useState(false); const [status, setStatus] = useState("idle"); const [statusMessage, setStatusMessage] = useState(); - // Load from bridge (ai-settings.json) on mount + // Load from bridge (ai-settings.json) on mount or background refresh useEffect(() => { + let isMounted = true; loadAISettings().then((loaded) => { - setSettings(loaded); - setIsLoading(false); + if (isMounted) { + setSettings((prev) => (dirty ? prev : loaded)); + setIsLoading(false); + } }); - }, []); - - const activeProvider = PROVIDERS.find((p) => p.name === settings.defaultProvider) ?? PROVIDERS[0]; + return () => { + isMounted = false; + }; + }, [dirty]); + + const activeProvider = useMemo( + () => PROVIDERS.find((p) => p.name === settings.defaultProvider) ?? PROVIDERS[0], + [settings.defaultProvider], + ); - const update = (patch: Partial) => { + const update = useCallback((patch: Partial) => { setSettings((prev) => ({ ...prev, ...patch })); setDirty(true); setStatus("idle"); - }; + setStatusMessage(undefined); + }, []); - const handleSave = async () => { + const handleSave = useCallback(async () => { await saveAISettings(settings); setDirty(false); setStatus("idle"); - }; + setStatusMessage(undefined); + }, [settings]); - const handleTest = async () => { + const handleTest = useCallback(async () => { await saveAISettings(settings); setDirty(false); setStatus("testing"); setStatusMessage(undefined); - const result = await aiService.testConnection(settings); - if (result.connected) { - setStatus("ok"); - } else { + try { + const result = await aiService.testConnection(settings); + if (result.connected) { + if (result.warning) { + setStatus("warning"); + setStatusMessage(result.warning); + } else { + setStatus("ok"); + setStatusMessage(undefined); + } + } else { + setStatus("error"); + setStatusMessage(result.message); + } + } catch (err) { setStatus("error"); - setStatusMessage(result.message); + setStatusMessage(err instanceof Error ? err.message : String(err)); } - }; + }, [settings]); // Get the currently selected model label for the provider description const activeModelField = MODEL_FIELD[activeProvider.name]; const activeModelValue = activeModelField ? (settings[activeModelField] as string | undefined) : undefined; - const activeModelOptions = MODEL_OPTIONS[activeProvider.name] ?? []; - const activeModelInfo = - activeModelOptions.find((o) => o.value === activeModelValue) ?? activeModelOptions[0]; + const activeModelOptions = useMemo( + () => MODEL_OPTIONS[activeProvider.name] ?? [], + [activeProvider.name], + ); + const activeModelInfo = useMemo( + () => activeModelOptions.find((o) => o.value === activeModelValue) ?? activeModelOptions[0], + [activeModelOptions, activeModelValue], + ); // Which providers already have keys saved (excluding ollama which needs no key) - const configuredProviders = PROVIDERS.filter((p) => { - if (!p.requiresKey || !p.keyField) return false; - return !!(settings[p.keyField] as string | undefined)?.trim(); - }); + const configuredProviders = useMemo( + () => + PROVIDERS.filter((p) => { + if (!p.requiresKey || !p.keyField) return false; + return !!(settings[p.keyField] as string | undefined)?.trim(); + }), + [settings], + ); const configuredCount = configuredProviders.length; + const otherProviders = useMemo( + () => PROVIDERS.filter((p) => p.name !== settings.defaultProvider && p.requiresKey), + [settings.defaultProvider], + ); + if (isLoading) { return (
@@ -592,7 +644,7 @@ export default function AISettings() { {/* Actions */}
- +
+ + {/* Warning / Notice banner */} + {status === "warning" && statusMessage && ( +
+ +
+

Notice

+

+ {statusMessage} +

+
+
+ )} + + {/* Error banner */} + {status === "error" && statusMessage && ( +
+ +
+

Connection Test Failed

+

+ {statusMessage} +

+
+
+ )}
{/* Configure other providers (collapsed) */} @@ -618,9 +696,8 @@ export default function AISettings() { Configure other providers
- {PROVIDERS.filter((p) => p.name !== settings.defaultProvider && p.requiresKey).map( - (provider) => { - const modelField = MODEL_FIELD[provider.name]; + {otherProviders.map((provider) => { + const modelField = MODEL_FIELD[provider.name]; const modelOptions = MODEL_OPTIONS[provider.name] ?? []; const selectedModel = modelField ? (settings[modelField] as string | undefined) diff --git a/src/features/settings/components/SettingsDialog.tsx b/src/features/settings/components/SettingsDialog.tsx index afe5b556..91460360 100644 --- a/src/features/settings/components/SettingsDialog.tsx +++ b/src/features/settings/components/SettingsDialog.tsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useState, useEffect } from "react"; import { AISettings, CheckForUpdates, @@ -11,6 +11,7 @@ import { import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog"; +import { loadAISettings } from "@/services/bridge/ai"; type TabId = "general" | "ai" | "analytics" | "shortcuts" | "about"; @@ -35,6 +36,13 @@ export interface SettingsDialogProps { export function SettingsDialog({ open, onOpenChange }: SettingsDialogProps) { const [activeTab, setActiveTab] = useState("general"); + // Pre-warm AI settings as soon as settings dialog opens so AI tab is instant + useEffect(() => { + if (open) { + loadAISettings().catch(() => {}); + } + }, [open]); + return ( diff --git a/src/services/bridge/ai.ts b/src/services/bridge/ai.ts index 4e2cdead..46c6337b 100644 --- a/src/services/bridge/ai.ts +++ b/src/services/bridge/ai.ts @@ -117,40 +117,74 @@ const LS_LEGACY_KEY = "relwave:ai-settings"; const DEFAULT_SETTINGS: AISettings = { defaultProvider: "ollama" }; +let cachedAISettings: AISettings | null = null; +let pendingLoadPromise: Promise | null = null; + +/** + * Synchronous accessor for currently in-memory cached AI settings. + * Returns null if settings haven't been loaded yet. + */ +export function getCachedAISettings(): AISettings | null { + return cachedAISettings; +} + /** - * Load AI settings from the bridge (reads ai-settings.json on disk). + * Load AI settings from memory cache or the bridge (reads ai-settings.json on disk). * Falls back to empty defaults if the file doesn't exist yet. * Also performs a one-time migration of any settings previously saved in localStorage. + * + * Concurrent calls are deduplicated into a single in-flight promise. */ -export async function loadAISettings(): Promise { - try { - const result = await bridgeRequest("ai.loadSettings", {}); - const fromFile = (result?.data ?? {}) as Partial; - - // If nothing is on disk yet, check localStorage for a legacy migration - if (!fromFile.defaultProvider) { - const migrated = migrateFromLocalStorage(); - if (migrated) { - // Persist the migrated settings to disk right away - await saveAISettings(migrated); - return migrated; +export async function loadAISettings(options?: { forceRefresh?: boolean }): Promise { + if (cachedAISettings && !options?.forceRefresh) { + return cachedAISettings; + } + + if (pendingLoadPromise && !options?.forceRefresh) { + return pendingLoadPromise; + } + + pendingLoadPromise = (async () => { + try { + const result = await bridgeRequest("ai.loadSettings", {}, 3000); + const fromFile = (result?.data ?? {}) as Partial; + + // If nothing is on disk yet, check localStorage for a legacy migration + if (!fromFile.defaultProvider) { + const migrated = migrateFromLocalStorage(); + if (migrated) { + cachedAISettings = migrated; + saveAISettings(migrated).catch(() => {}); + return migrated; + } + cachedAISettings = { ...DEFAULT_SETTINGS }; + return cachedAISettings; + } + + cachedAISettings = { ...DEFAULT_SETTINGS, ...fromFile }; + return cachedAISettings; + } catch { + // Bridge unavailable or timed out — return cached, migrated, or default settings + if (!cachedAISettings) { + cachedAISettings = migrateFromLocalStorage() ?? { ...DEFAULT_SETTINGS }; } - return { ...DEFAULT_SETTINGS }; + return cachedAISettings; + } finally { + pendingLoadPromise = null; } + })(); - return { ...DEFAULT_SETTINGS, ...fromFile }; - } catch { - // Bridge unavailable (e.g. during Vite standalone dev) — degrade gracefully - return migrateFromLocalStorage() ?? { ...DEFAULT_SETTINGS }; - } + return pendingLoadPromise; } /** * Save AI settings to disk via the bridge (writes ai-settings.json). + * Updates in-memory cache immediately (optimistic update). */ export async function saveAISettings(settings: AISettings): Promise { + cachedAISettings = { ...settings }; try { - await bridgeRequest("ai.saveSettings", { settings }); + await bridgeRequest("ai.saveSettings", { settings }, 5000); } catch { // Fallback: keep a copy in localStorage so settings aren’t totally lost localStorage.setItem(LS_LEGACY_KEY, JSON.stringify(settings)); @@ -182,12 +216,24 @@ class AIService { /** * Test whether the configured provider is reachable. */ - async testConnection(settings: AISettings): Promise<{ connected: boolean; message?: string }> { + async testConnection(settings: AISettings): Promise<{ + connected: boolean; + warning?: string; + code?: string; + message?: string; + }> { try { - await bridgeRequest("ai.testConnection", { settings }); + const result = await bridgeRequest("ai.testConnection", { settings }); + if (result?.data?.connected !== undefined) { + return result.data; + } return { connected: true }; } catch (err: any) { - return { connected: false, message: err?.message ?? String(err) }; + return { + connected: false, + code: err?.code ?? "UNKNOWN", + message: err?.message ?? String(err), + }; } } diff --git a/src/services/bridge/bridgeClient.ts b/src/services/bridge/bridgeClient.ts index 1c2ec6e0..78af8eb3 100644 --- a/src/services/bridge/bridgeClient.ts +++ b/src/services/bridge/bridgeClient.ts @@ -346,6 +346,11 @@ function getTimeoutForMethod(method: string): number { // Ping should be fast if (method === "ping" || method === HEALTH_PING_METHOD) return 5000; // 5 seconds + // Local settings operations should be fast + if (method === "ai.loadSettings") return 3000; // 3 seconds + if (method === "ai.saveSettings") return 5000; // 5 seconds + if (method === "ai.testConnection") return 15000; // 15 seconds + // Schema operations can be very slow on large databases if (method === "db.getSchema") return 180000; // 3 minutes From 16eca0a973839599c2926787f804c0f278c102a6 Mon Sep 17 00:00:00 2001 From: Yash Date: Thu, 15 Oct 2026 23:12:50 +0530 Subject: [PATCH 08/17] refac(settings): move Settings trigger to Connections header and support global access - Move Settings trigger button from sidebar footer to Connections header - Clean up Quick Stats footer to symmetrically display stat cards - Add Settings icon button to bottom of VerticalIconBar for direct workspace access - Wire CommandPalette Settings page action directly to SettingsDialog --- src/components/layout/CommandPalette.tsx | 28 ++++++--- src/components/layout/VerticalIconBar.tsx | 25 ++++++++ .../home/components/ConnectionList.tsx | 63 ++++++++++++++----- 3 files changed, 91 insertions(+), 25 deletions(-) diff --git a/src/components/layout/CommandPalette.tsx b/src/components/layout/CommandPalette.tsx index 50453a05..9445c0cd 100644 --- a/src/components/layout/CommandPalette.tsx +++ b/src/components/layout/CommandPalette.tsx @@ -41,6 +41,7 @@ import { useDatabases, useTables } from "@/features/project/hooks/useDbQueries"; import { useProjects } from "@/features/project/hooks/useProjectQueries"; import { useTheme } from "@/components/providers/ThemeProvider"; import { cn } from "@/lib/utils"; +import { SettingsDialog } from "@/features/settings/components"; // ─── Types ──────────────────────────────────────────────────────────────────── @@ -173,6 +174,7 @@ function HighlightMatch({ text, query }: { text: string; query: string }) { export function CommandPalette() { const [open, setOpen] = React.useState(false); + const [settingsOpen, setSettingsOpen] = React.useState(false); const [search, setSearch] = React.useState(""); const navigate = useNavigate(); const location = useLocation(); @@ -363,14 +365,23 @@ export function CommandPalette() { label={page.label} type="page" query={search} - onSelect={() => - runCommand(() => navigate(page.path), { - id: page.path, - type: "page", - label: page.label, - path: page.path, - }) - } + onSelect={() => { + if (page.path === "/settings") { + runCommand(() => setSettingsOpen(true), { + id: page.path, + type: "page", + label: page.label, + path: page.path, + }); + } else { + runCommand(() => navigate(page.path), { + id: page.path, + type: "page", + label: page.label, + path: page.path, + }); + } + }} /> ))} @@ -517,6 +528,7 @@ export function CommandPalette() {
+ ); } diff --git a/src/components/layout/VerticalIconBar.tsx b/src/components/layout/VerticalIconBar.tsx index 30f2b106..d9a006a9 100644 --- a/src/components/layout/VerticalIconBar.tsx +++ b/src/components/layout/VerticalIconBar.tsx @@ -1,3 +1,4 @@ +import { useState } from "react"; import { Activity, Home, @@ -9,10 +10,12 @@ import { Terminal, History, Sparkles, + Settings, } from "lucide-react"; import { Link, useLocation } from "react-router-dom"; import { Button } from "@/components/ui/button"; import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip"; +import { SettingsDialog } from "@/features/settings/components"; export type PanelType = | "data" @@ -46,6 +49,7 @@ export default function VerticalIconBar({ onPanelChange, }: VerticalIconBarProps) { const location = useLocation(); + const [settingsOpen, setSettingsOpen] = useState(false); const isGlobalActive = (path: string) => { if (path === "/") { @@ -144,6 +148,27 @@ export default function VerticalIconBar({
)} + + {/* Bottom Actions */} +
+ + + + + +

Settings

+
+
+
+ + ); } diff --git a/src/features/home/components/ConnectionList.tsx b/src/features/home/components/ConnectionList.tsx index ae9242f9..ea057904 100644 --- a/src/features/home/components/ConnectionList.tsx +++ b/src/features/home/components/ConnectionList.tsx @@ -404,14 +404,14 @@ export function ConnectionList({

Connections

-
+
@@ -436,9 +442,42 @@ export function ConnectionList({ )} - + + + + + + + +

Add Connection

+
+
+
+ + + + + + + +

Settings

+
+
+
@@ -603,7 +642,7 @@ export function ConnectionList({ {/* Quick Stats Footer */}
-
+
@@ -625,16 +664,6 @@ export function ConnectionList({
-
- -
{/* Dialogs */} From bb3a6df7f4bb89be9162c9902277b4a22d12439b Mon Sep 17 00:00:00 2001 From: Yash Date: Thu, 15 Oct 2026 23:42:17 +0530 Subject: [PATCH 09/17] refac(command-palette): use DatabaseLogo from databases.tsx for connections and context pill --- src/components/layout/CommandPalette.tsx | 80 +++++++++++++++--------- 1 file changed, 51 insertions(+), 29 deletions(-) diff --git a/src/components/layout/CommandPalette.tsx b/src/components/layout/CommandPalette.tsx index 9445c0cd..ce25e666 100644 --- a/src/components/layout/CommandPalette.tsx +++ b/src/components/layout/CommandPalette.tsx @@ -21,7 +21,6 @@ import * as React from "react"; import { useNavigate, useLocation } from "react-router-dom"; import { Settings, - Database, Table as TableIcon, Terminal, Home, @@ -42,6 +41,7 @@ import { useProjects } from "@/features/project/hooks/useProjectQueries"; import { useTheme } from "@/components/providers/ThemeProvider"; import { cn } from "@/lib/utils"; import { SettingsDialog } from "@/features/settings/components"; +import { DatabaseLogo } from "@/lib/databases"; // ─── Types ──────────────────────────────────────────────────────────────────── @@ -52,6 +52,7 @@ interface RecentItem { type: ItemType; label: string; path?: string; + dbType?: string; timestamp: number; } @@ -255,12 +256,16 @@ export function CommandPalette() { const isSearching = search.trim().length > 0; const showRecent = !isSearching && recentItems.length > 0; - const contextLabel = React.useMemo(() => { + const activeDatabase = React.useMemo(() => { if (!dbId) return null; - const db = databases.find((d) => d.id === dbId); - return db ? db.name : dbId; + return databases.find((d) => d.id === dbId); }, [dbId, databases]); + const contextLabel = React.useMemo(() => { + if (!dbId) return null; + return activeDatabase ? activeDatabase.name : dbId; + }, [dbId, activeDatabase]); + // ── Render ────────────────────────────────────────────────────────────────── return ( @@ -293,11 +298,11 @@ export function CommandPalette() { {contextLabel && !isSearching && (
- + {contextLabel}
)} @@ -334,25 +339,33 @@ export function CommandPalette() { /> } > - {recentItems.map((item) => ( - { + const itemDbType = item.dbType || databases.find((d) => d.id === item.id)?.type; + return ( + + ) : undefined + } + icon={ + item.type === "project" ? FolderOpen - : Home - } - label={item.label} - type={item.type} - query={search} - onSelect={() => { - if (item.path) runCommand(() => navigate(item.path!), item); - }} - /> - ))} + : item.type === "page" + ? Home + : undefined + } + label={item.label} + type={item.type} + query={search} + onSelect={() => { + if (item.path) runCommand(() => navigate(item.path!), item); + }} + /> + ); + })} )} @@ -392,17 +405,18 @@ export function CommandPalette() { {databases.map((db) => ( } label={db.name} type="connection" query={search} - meta={`${db.type} · ${db.host}`} + meta={`${db.type} · ${db.host || (db as any).database || ""}`} onSelect={() => runCommand(() => navigate(`/${db.id}`), { id: db.id, type: "connection", label: db.name, path: `/${db.id}`, + dbType: db.type, }) } /> @@ -573,7 +587,8 @@ function GroupHeading({ // ─── CommandItem ────────────────────────────────────────────────────────────── interface CommandItemProps { - icon: React.ElementType; + icon?: React.ElementType; + iconNode?: React.ReactNode; label: string; type: ItemType; query: string; @@ -589,6 +604,7 @@ interface CommandItemProps { function CommandItem({ icon: Icon, + iconNode, label, type, query, @@ -612,10 +628,16 @@ function CommandItem({
- + {iconNode ? ( + iconNode + ) : Icon ? ( + + ) : null}
{/* Label — IBM Plex Sans 450 (between regular and medium) */} From 5ab57504363243a44f8db128d7c4b4db644d7661 Mon Sep 17 00:00:00 2001 From: Yash Date: Fri, 16 Oct 2026 01:04:04 +0530 Subject: [PATCH 10/17] fix(clickhouse): escape table keyword and fix parameter collision in batch schema queries - Escape reserved keyword \ able\ in CLICKHOUSE_BATCH_LIST_COLUMNS, CLICKHOUSE_BATCH_GET_PRIMARY_KEYS, and CLICKHOUSE_BATCH_GET_INDEXES - Rename schema parameter to target_schema to prevent collision with database AS schema alias - Add fallback table discovery and per-table column/PK recovery in getSchemaMetadataBatch - Add unit tests for ClickHouse getSchemaMetadataBatch edge cases --- .../connectors/clickhouse.batch.test.ts | 299 ++++++++++++++++++ bridge/src/connectors/clickhouse.ts | 94 +++++- bridge/src/queries/clickhouse/constraints.ts | 20 +- bridge/src/queries/clickhouse/schema.ts | 10 +- 4 files changed, 393 insertions(+), 30 deletions(-) create mode 100644 bridge/__tests__/connectors/clickhouse.batch.test.ts diff --git a/bridge/__tests__/connectors/clickhouse.batch.test.ts b/bridge/__tests__/connectors/clickhouse.batch.test.ts new file mode 100644 index 00000000..47639585 --- /dev/null +++ b/bridge/__tests__/connectors/clickhouse.batch.test.ts @@ -0,0 +1,299 @@ +import { describe, expect, it, jest, beforeEach } from "@jest/globals"; + +const mockQuery = jest.fn(); +const mockCommand = jest.fn(); +const mockInsert = jest.fn(); +const mockClose = jest.fn(); + +jest.mock("@clickhouse/client", () => ({ + createClient: jest.fn(() => ({ + query: mockQuery, + command: mockCommand, + insert: mockInsert, + close: mockClose, + })), +})); + +import * as clickhouseConnector from "../../src/connectors/clickhouse"; + +const mockConfig: clickhouseConnector.ClickHouseConfig = { + host: "localhost", + port: 8123, + user: "default", + password: "", + database: "testdb", +}; + +describe("ClickHouse getSchemaMetadataBatch unit tests", () => { + beforeEach(async () => { + clickhouseConnector.clickhouseCache.clearAll(); + await clickhouseConnector.closeAllClients(); + mockQuery.mockReset(); + mockCommand.mockReset(); + mockInsert.mockReset(); + mockClose.mockReset(); + }); + + it("should assemble batch metadata correctly when batch queries succeed", async () => { + mockQuery.mockImplementation(async ({ query }: any) => { + const q = String(query); + + // listTables query + if (q.includes("FROM system.tables")) { + return { + json: async () => [ + { schema: "testdb", name: "users", type: "BASE TABLE" }, + { schema: "testdb", name: "orders", type: "BASE TABLE" }, + { schema: "testdb", name: "products", type: "BASE TABLE" }, + ], + }; + } + + // Batch columns query + if (q.includes("FROM system.columns") && !q.includes("is_in_primary_key = 1")) { + return { + json: async () => [ + { + table: "users", + name: "id", + type: "UInt64", + not_nullable: 1, + default_value: null, + is_in_primary_key: 1, + is_in_sorting_key: 1, + comment: "User ID", + }, + { + table: "users", + name: "email", + type: "String", + not_nullable: 1, + default_value: null, + is_primary_key: 0, + is_in_sorting_key: 0, + comment: null, + }, + { + table: "orders", + name: "id", + type: "UInt64", + not_nullable: 1, + default_value: null, + is_in_primary_key: 1, + is_in_sorting_key: 1, + comment: null, + }, + { + table: "products", + name: "id", + type: "UInt64", + not_nullable: 1, + default_value: null, + is_in_primary_key: 1, + is_in_sorting_key: 1, + comment: null, + }, + ], + }; + } + + // Batch primary keys query + if (q.includes("is_in_primary_key = 1")) { + return { + json: async () => [ + { table: "users", column_name: "id" }, + { table: "orders", column_name: "id" }, + { table: "products", column_name: "id" }, + ], + }; + } + + // Batch indexes query + if (q.includes("system.data_skipping_indices")) { + return { + json: async () => [ + { + table: "orders", + index_name: "idx_orders_created", + column_name: "created_at", + index_type: "minmax", + }, + ], + }; + } + + return { json: async () => [] }; + }); + + const result = await clickhouseConnector.getSchemaMetadataBatch(mockConfig, "testdb"); + expect(result.tables instanceof Map).toBe(true); + expect(result.tables.size).toBe(3); + + const userTable = result.tables.get("users"); + expect(userTable).toBeDefined(); + expect(userTable!.columns).toHaveLength(2); + expect(userTable!.columns[0].name).toBe("id"); + expect(userTable!.columns[0].is_primary_key).toBe(true); + expect(userTable!.primaryKeys).toHaveLength(1); + expect(userTable!.primaryKeys[0].column_name).toBe("id"); + + const orderTable = result.tables.get("orders"); + expect(orderTable).toBeDefined(); + expect(orderTable!.indexes).toHaveLength(1); + expect(orderTable!.indexes[0].index_name).toBe("idx_orders_created"); + }); + + it("should handle backticked `table` key name in batch query results", async () => { + mockQuery.mockImplementation(async ({ query }: any) => { + const q = String(query); + + if (q.includes("FROM system.tables")) { + return { + json: async () => [{ schema: "testdb", name: "users", type: "BASE TABLE" }], + }; + } + + if (q.includes("FROM system.columns") && !q.includes("is_in_primary_key = 1")) { + return { + json: async () => [ + { + "`table`": "users", + name: "id", + type: "UInt64", + not_nullable: 1, + default_value: null, + is_in_primary_key: 1, + is_in_sorting_key: 1, + }, + ], + }; + } + + if (q.includes("is_in_primary_key = 1")) { + return { + json: async () => [{ "`table`": "users", column_name: "id" }], + }; + } + + return { json: async () => [] }; + }); + + const result = await clickhouseConnector.getSchemaMetadataBatch(mockConfig, "testdb"); + expect(result.tables.size).toBe(1); + const userTable = result.tables.get("users"); + expect(userTable).toBeDefined(); + expect(userTable!.columns).toHaveLength(1); + expect(userTable!.primaryKeys).toHaveLength(1); + }); + + it("should fallback to per-table lookups if batch queries fail", async () => { + mockQuery.mockImplementation(async ({ query }: any) => { + const q = String(query); + + // listTables query + if (q.includes("FROM system.tables")) { + return { + json: async () => [ + { schema: "testdb", name: "student", type: "BASE TABLE" }, + { schema: "testdb", name: "persons", type: "BASE TABLE" }, + { schema: "testdb", name: "TestTable", type: "BASE TABLE" }, + ], + }; + } + + // Batch queries fail + if (q.includes("ORDER BY `table`")) { + throw new Error("Batch query failed"); + } + + // Fallback single table column queries: + if (q.includes("WHERE database = {target_schema: String} AND `table` = {target_table: String}")) { + return { + json: async () => [ + { + name: "id", + type: "UInt32", + not_nullable: 1, + default_value: null, + is_in_primary_key: 1, + is_in_sorting_key: 1, + comment: null, + }, + { + name: "name", + type: "String", + not_nullable: 1, + default_value: null, + is_in_primary_key: 0, + is_in_sorting_key: 0, + comment: null, + }, + ], + }; + } + + // Fallback single table PK queries: + if (q.includes("is_in_primary_key = 1 OR is_in_sorting_key = 1")) { + return { + json: async () => [{ column_name: "id" }], + }; + } + + return { json: async () => [] }; + }); + + const result = await clickhouseConnector.getSchemaMetadataBatch(mockConfig, "testdb"); + expect(result.tables.size).toBe(3); + expect(result.tables.has("student")).toBe(true); + expect(result.tables.has("persons")).toBe(true); + expect(result.tables.has("TestTable")).toBe(true); + + const studentMeta = result.tables.get("student"); + expect(studentMeta).toBeDefined(); + expect(studentMeta!.columns).toHaveLength(2); + expect(studentMeta!.primaryKeys[0].column_name).toBe("id"); + }); + + it("should filter tables when array of table names is passed", async () => { + mockQuery.mockImplementation(async ({ query }: any) => { + const q = String(query); + + if (q.includes("FROM system.tables")) { + return { + json: async () => [ + { schema: "testdb", name: "student", type: "BASE TABLE" }, + { schema: "testdb", name: "persons", type: "BASE TABLE" }, + { schema: "testdb", name: "TestTable", type: "BASE TABLE" }, + ], + }; + } + + if (q.includes("FROM system.columns") && !q.includes("is_in_primary_key = 1")) { + return { + json: async () => [ + { table: "student", name: "id", type: "UInt32", not_nullable: 1, is_in_primary_key: 1 }, + { table: "persons", name: "id", type: "UInt32", not_nullable: 1, is_in_primary_key: 1 }, + { table: "TestTable", name: "id", type: "UInt32", not_nullable: 1, is_in_primary_key: 1 }, + ], + }; + } + + if (q.includes("is_in_primary_key = 1")) { + return { + json: async () => [ + { table: "student", column_name: "id" }, + { table: "persons", column_name: "id" }, + { table: "TestTable", column_name: "id" }, + ], + }; + } + + return { json: async () => [] }; + }); + + const result = await clickhouseConnector.getSchemaMetadataBatch(mockConfig, ["student"]); + expect(result.tables.size).toBe(1); + expect(result.tables.has("student")).toBe(true); + expect(result.tables.has("persons")).toBe(false); + }); +}); diff --git a/bridge/src/connectors/clickhouse.ts b/bridge/src/connectors/clickhouse.ts index 39cf0a43..5258719a 100644 --- a/bridge/src/connectors/clickhouse.ts +++ b/bridge/src/connectors/clickhouse.ts @@ -290,7 +290,9 @@ export async function listTables( const client = getClient(cfg); const query = schemaName ? CLICKHOUSE_LIST_TABLES_BY_SCHEMA : CLICKHOUSE_LIST_TABLES; - const query_params = schemaName ? { schema: schemaName } : undefined; + const query_params = schemaName + ? { target_schema: schemaName, schema: schemaName } + : undefined; const res = await client.query({ query, @@ -328,7 +330,12 @@ export async function listPrimaryKeys( const client = getClient(cfg); const res = await client.query({ query: CLICKHOUSE_GET_PRIMARY_KEYS, - query_params: { schema, table }, + query_params: { + target_schema: schema, + target_table: table, + schema, + table, + }, format: "JSONEachRow", }); const data = (await res.json()) as any[]; @@ -378,7 +385,12 @@ export async function listIndexes( try { const res = await client.query({ query: CLICKHOUSE_GET_INDEXES, - query_params: { schema, table }, + query_params: { + target_schema: schema, + target_table: table, + schema, + table, + }, format: "JSONEachRow", }); const data = (await res.json()) as any[]; @@ -498,7 +510,12 @@ export async function getTableDetails( const client = getClient(cfg); const res = await client.query({ query: CLICKHOUSE_LIST_COLUMNS, - query_params: { schema, table }, + query_params: { + target_schema: schema, + target_table: table, + schema, + table, + }, format: "JSONEachRow", }); const data = (await res.json()) as any[]; @@ -562,6 +579,14 @@ export async function getSchemaMetadataBatch( return entry; } + // Pre-seed tables if filterTables was explicitly provided + if (filterTables) { + for (const tbl of filterTables) { + getOrCreateTable(tbl); + } + } + + // 1. Discover tables in target schema try { const tables = await listTables(cfg, schema); for (const t of tables) { @@ -573,17 +598,33 @@ export async function getSchemaMetadataBatch( logger.warn({ err: err?.message, schema }, "ClickHouse getSchemaMetadataBatch listTables failed"); } + // Fallback table discovery if empty and no specific table filter was provided + if (tablesMap.size === 0 && !filterTables) { + try { + const allTables = await listTables(cfg); + for (const t of allTables) { + if (!schema || t.schema === schema) { + getOrCreateTable(t.name); + } + } + } catch (err: any) { + logger.warn({ err: err?.message, schema }, "ClickHouse getSchemaMetadataBatch allTables fallback failed"); + } + } + + // 2. Batch fetch columns try { const colRes = await client.query({ query: CLICKHOUSE_BATCH_LIST_COLUMNS, - query_params: { schema }, + query_params: { target_schema: schema, schema }, format: "JSONEachRow", }); const colData = (await colRes.json()) as any[]; for (const r of colData) { - if (filterTables && !filterTables.has(r.table)) continue; - const entry = getOrCreateTable(r.table); + const tbl = r.table || r["`table`"] || r.table_name; + if (!tbl || (filterTables && !filterTables.has(tbl))) continue; + const entry = getOrCreateTable(tbl); entry.columns.push({ name: r.name, type: r.type, @@ -598,36 +639,40 @@ export async function getSchemaMetadataBatch( logger.warn({ err: err?.message, schema }, "ClickHouse CLICKHOUSE_BATCH_LIST_COLUMNS failed"); } + // 3. Batch fetch primary keys try { const pkRes = await client.query({ query: CLICKHOUSE_BATCH_GET_PRIMARY_KEYS, - query_params: { schema }, + query_params: { target_schema: schema, schema }, format: "JSONEachRow", }); const pkData = (await pkRes.json()) as any[]; for (const r of pkData) { - if (filterTables && !filterTables.has(r.table)) continue; - const entry = getOrCreateTable(r.table); + const tbl = r.table || r["`table`"] || r.table_name; + if (!tbl || (filterTables && !filterTables.has(tbl))) continue; + const entry = getOrCreateTable(tbl); entry.primaryKeys.push({ column_name: r.column_name }); } } catch (err: any) { logger.warn({ err: err?.message, schema }, "ClickHouse CLICKHOUSE_BATCH_GET_PRIMARY_KEYS failed"); } + // 4. Batch fetch secondary indexes try { const idxRes = await client.query({ query: CLICKHOUSE_BATCH_GET_INDEXES, - query_params: { schema }, + query_params: { target_schema: schema, schema }, format: "JSONEachRow", }); const idxData = (await idxRes.json()) as any[]; for (const r of idxData) { - if (filterTables && !filterTables.has(r.table)) continue; - const entry = getOrCreateTable(r.table); + const tbl = r.table || r["`table`"] || r.table_name; + if (!tbl || (filterTables && !filterTables.has(tbl))) continue; + const entry = getOrCreateTable(tbl); entry.indexes.push({ - table_name: r.table, + table_name: tbl, index_name: r.index_name, column_name: r.column_name, is_unique: false, @@ -639,7 +684,26 @@ export async function getSchemaMetadataBatch( logger.debug({ err: err?.message, schema }, "ClickHouse CLICKHOUSE_BATCH_GET_INDEXES skipped/failed"); } - // Fallback: Ensure every table has columns and primary keys populated + // 5. Direct fallback: If tablesMap is still empty after batch queries, query system.tables directly + if (tablesMap.size === 0) { + try { + const directTablesRes = await client.query({ + query: `SELECT name FROM system.tables WHERE database = {target_schema: String} ORDER BY name`, + query_params: { target_schema: schema, schema }, + format: "JSONEachRow", + }); + const directRows = (await directTablesRes.json()) as any[]; + for (const row of directRows) { + if (!filterTables || filterTables.has(row.name)) { + getOrCreateTable(row.name); + } + } + } catch (err: any) { + logger.warn({ err: err?.message, schema }, "ClickHouse direct system.tables query failed"); + } + } + + // 6. Guaranteed population: Ensure every table in tablesMap has columns and primary keys populated for (const [tableName, entry] of tablesMap.entries()) { if (entry.columns.length === 0) { try { diff --git a/bridge/src/queries/clickhouse/constraints.ts b/bridge/src/queries/clickhouse/constraints.ts index 19081de6..edfd3e87 100644 --- a/bridge/src/queries/clickhouse/constraints.ts +++ b/bridge/src/queries/clickhouse/constraints.ts @@ -8,8 +8,8 @@ export const CLICKHOUSE_GET_PRIMARY_KEYS = ` SELECT name AS column_name FROM system.columns - WHERE database = {schema: String} - AND table = {table: String} + WHERE database = {target_schema: String} + AND \`table\` = {target_table: String} AND (is_in_primary_key = 1 OR is_in_sorting_key = 1) ORDER BY position; `; @@ -18,11 +18,11 @@ export const CLICKHOUSE_GET_PRIMARY_KEYS = ` * Batch get primary keys for all tables in a schema. */ export const CLICKHOUSE_BATCH_GET_PRIMARY_KEYS = ` - SELECT table, name AS column_name + SELECT \`table\` AS \`table\`, name AS column_name FROM system.columns - WHERE database = {schema: String} + WHERE database = {target_schema: String} AND (is_in_primary_key = 1 OR is_in_sorting_key = 1) - ORDER BY table, position; + ORDER BY \`table\`, position; `; /** @@ -34,8 +34,8 @@ export const CLICKHOUSE_GET_INDEXES = ` expr AS column_name, type AS index_type FROM system.data_skipping_indices - WHERE database = {schema: String} - AND table = {table: String} + WHERE database = {target_schema: String} + AND \`table\` = {target_table: String} ORDER BY name; `; @@ -44,11 +44,11 @@ export const CLICKHOUSE_GET_INDEXES = ` */ export const CLICKHOUSE_BATCH_GET_INDEXES = ` SELECT - table, + \`table\` AS \`table\`, name AS index_name, expr AS column_name, type AS index_type FROM system.data_skipping_indices - WHERE database = {schema: String} - ORDER BY table, name; + WHERE database = {target_schema: String} + ORDER BY \`table\`, name; `; diff --git a/bridge/src/queries/clickhouse/schema.ts b/bridge/src/queries/clickhouse/schema.ts index dd538d26..a1eac422 100644 --- a/bridge/src/queries/clickhouse/schema.ts +++ b/bridge/src/queries/clickhouse/schema.ts @@ -34,7 +34,7 @@ export const CLICKHOUSE_LIST_TABLES_BY_SCHEMA = ` name, if(engine LIKE '%View%', 'VIEW', 'BASE TABLE') AS type FROM system.tables - WHERE database = {schema: String} + WHERE database = {target_schema: String} ORDER BY name; `; @@ -51,7 +51,7 @@ export const CLICKHOUSE_LIST_COLUMNS = ` is_in_sorting_key, comment FROM system.columns - WHERE database = {schema: String} AND table = {table: String} + WHERE database = {target_schema: String} AND \`table\` = {target_table: String} ORDER BY position; `; @@ -61,7 +61,7 @@ export const CLICKHOUSE_LIST_COLUMNS = ` export const CLICKHOUSE_BATCH_LIST_COLUMNS = ` SELECT database AS schema, - table, + \`table\` AS \`table\`, name, type, if(type LIKE 'Nullable(%)', 0, 1) AS not_nullable, @@ -70,6 +70,6 @@ export const CLICKHOUSE_BATCH_LIST_COLUMNS = ` is_in_sorting_key, comment FROM system.columns - WHERE database = {schema: String} - ORDER BY table, position; + WHERE database = {target_schema: String} + ORDER BY \`table\`, position; `; From 431bf299a63eda10f8ed1a99b7dd2d852b3ac77d Mon Sep 17 00:00:00 2001 From: Yash Date: Fri, 16 Oct 2026 01:06:05 +0530 Subject: [PATCH 11/17] feat(monitoring): add real-time database monitoring support for ClickHouse - Query system.metrics, system.settings, system.events, and system.processes in monitoringService - Add ClickHouse validation to monitoring WebSocket server - Enable ClickHouse in frontend navigation, panels, and dashboard with Mark Cache metrics - Add unit tests for ClickHouse monitoring snapshots --- bridge/__tests__/monitoringHandlers.test.ts | 28 +++ bridge/__tests__/monitoringService.test.ts | 193 ++++++++++++++++++ .../monitoringWebSocketServer.test.ts | 35 ++++ bridge/src/services/monitoringService.ts | 153 +++++++++++++- .../src/services/monitoringWebSocketServer.ts | 2 +- src/components/layout/VerticalIconBar.tsx | 8 +- .../components/MonitoringDashboard.tsx | 10 +- .../monitoring/components/MonitoringPanel.tsx | 8 +- 8 files changed, 431 insertions(+), 6 deletions(-) create mode 100644 bridge/__tests__/monitoringService.test.ts diff --git a/bridge/__tests__/monitoringHandlers.test.ts b/bridge/__tests__/monitoringHandlers.test.ts index 7ab2906d..6291c421 100644 --- a/bridge/__tests__/monitoringHandlers.test.ts +++ b/bridge/__tests__/monitoringHandlers.test.ts @@ -91,6 +91,34 @@ describe("MonitoringHandlers", () => { }); }); + test("returns monitoring snapshot for a ClickHouse database", async () => { + const conn = { host: "127.0.0.1", port: 8123 }; + const snapshot = { + databaseType: DBType.CLICKHOUSE, + sampledAt: "2026-05-16T00:00:00.000Z", + health: { ok: true, latencyMs: 5 }, + connections: { active: 3, max: 100, usagePct: 3 }, + throughput: { qps: 15, totalQueries: 5000 }, + cacheHitRatio: 95.5, + activeQueries: [], + }; + + (dbService.getDatabaseConnection as any).mockResolvedValue({ + conn, + dbType: DBType.CLICKHOUSE, + }); + (monitoringService.getSnapshot as any).mockResolvedValue(snapshot); + + await handlers.handleGetSnapshot({ id: "ch-1" }, 10); + + expect(dbService.getDatabaseConnection).toHaveBeenCalledWith("ch-1"); + expect(monitoringService.getSnapshot).toHaveBeenCalledWith("ch-1", conn, DBType.CLICKHOUSE); + expect(rpc.sendResponse).toHaveBeenCalledWith(10, { + ok: true, + data: snapshot, + }); + }); + test("returns MONITORING_ERROR when snapshot generation fails", async () => { (dbService.getDatabaseConnection as any).mockResolvedValue({ conn: {}, diff --git a/bridge/__tests__/monitoringService.test.ts b/bridge/__tests__/monitoringService.test.ts new file mode 100644 index 00000000..abb7e49e --- /dev/null +++ b/bridge/__tests__/monitoringService.test.ts @@ -0,0 +1,193 @@ +import { describe, expect, test, jest, beforeEach } from "@jest/globals"; +import { DBType } from "../src/types"; +import { ClickHouseConfig } from "../src/types/clickhouse"; + +const mockClickHouseClient = { + query: jest.fn() as any, +}; + +jest.mock("../src/connectors/clickhouse", () => ({ + getClient: jest.fn(() => mockClickHouseClient), +})); + +import { MonitoringService } from "../src/services/monitoringService"; + +describe("MonitoringService", () => { + let service: MonitoringService; + + beforeEach(() => { + jest.clearAllMocks(); + service = new MonitoringService(); + }); + + test("throws for unsupported database types like sqlite or duckdb", async () => { + await expect(service.getSnapshot("db-1", {}, DBType.SQLITE)).rejects.toThrow( + "Monitoring is not supported for sqlite", + ); + await expect(service.getSnapshot("db-2", {}, DBType.DUCKDB)).rejects.toThrow( + "Monitoring is not supported for duckdb", + ); + }); + + test("generates complete ClickHouse monitoring snapshot on success", async () => { + const config: ClickHouseConfig = { + host: "localhost", + port: 8123, + user: "default", + database: "default", + }; + + mockClickHouseClient.query.mockImplementation((params: any) => { + const q = params.query; + if (q.includes("SELECT 1;")) { + return Promise.resolve({ + json: async () => [{ 1: 1 }], + }); + } + if (q.includes("FROM system.metrics")) { + return Promise.resolve({ + json: async () => [ + { metric: "TCPConnection", value: 3 }, + { metric: "HTTPConnection", value: 2 }, + { metric: "Query", value: 1 }, + ], + }); + } + if (q.includes("FROM system.settings")) { + return Promise.resolve({ + json: async () => [{ value: "200" }], + }); + } + if (q.includes("FROM system.events")) { + return Promise.resolve({ + json: async () => [ + { event: "Query", value: 1000 }, + { event: "MarkCacheHits", value: 950 }, + { event: "MarkCacheMisses", value: 50 }, + ], + }); + } + if (q.includes("FROM system.processes")) { + return Promise.resolve({ + json: async () => [ + { + id: "query-abc", + user: "analyst", + query: "SELECT count() FROM logs", + state: "running", + durationSeconds: 3.45, + }, + ], + }); + } + return Promise.resolve({ json: async () => [] }); + }); + + const snapshot = await service.getSnapshot("ch-1", config, DBType.CLICKHOUSE); + + expect(snapshot.databaseType).toBe(DBType.CLICKHOUSE); + expect(snapshot.health.ok).toBe(true); + expect(typeof snapshot.health.latencyMs).toBe("number"); + expect(snapshot.connections).toEqual({ + active: 5, + max: 200, + usagePct: 2.5, + }); + expect(snapshot.throughput.totalQueries).toBe(1000); + expect(snapshot.throughput.qps).toBe(0); // first sample + expect(snapshot.cacheHitRatio).toBe(95); // 950 / (950 + 50) * 100 + expect(snapshot.activeQueries).toEqual([ + { + id: "query-abc", + user: "analyst", + query: "SELECT count() FROM logs", + state: "running", + durationSeconds: 3.45, + }, + ]); + }); + + test("calculates QPS correctly on subsequent snapshot calls", async () => { + const config: ClickHouseConfig = { + host: "localhost", + port: 8123, + }; + + mockClickHouseClient.query.mockImplementation((params: any) => { + const q = params.query; + if (q.includes("FROM system.events")) { + return Promise.resolve({ + json: async () => [{ event: "Query", value: 1000 }], + }); + } + return Promise.resolve({ json: async () => [] }); + }); + + await service.getSnapshot("ch-qps", config, DBType.CLICKHOUSE); + + // Mock next sample with 100 additional queries + mockClickHouseClient.query.mockImplementation((params: any) => { + const q = params.query; + if (q.includes("FROM system.events")) { + return Promise.resolve({ + json: async () => [{ event: "Query", value: 1100 }], + }); + } + return Promise.resolve({ json: async () => [] }); + }); + + const secondSnapshot = await service.getSnapshot("ch-qps", config, DBType.CLICKHOUSE); + expect(secondSnapshot.throughput.totalQueries).toBe(1100); + expect(secondSnapshot.throughput.qps).toBeGreaterThanOrEqual(0); + }); + + test("handles partial metric query failures gracefully with defaults", async () => { + const config: ClickHouseConfig = { host: "localhost" }; + + mockClickHouseClient.query.mockImplementation((params: any) => { + const q = params.query; + if (q.includes("SELECT 1;")) { + return Promise.resolve({ json: async () => [{ 1: 1 }] }); + } + // Settings and processes fail (e.g. permission denied) + if (q.includes("FROM system.settings") || q.includes("FROM system.processes")) { + return Promise.reject(new Error("Permission denied")); + } + if (q.includes("FROM system.metrics")) { + return Promise.resolve({ + json: async () => [{ metric: "TCPConnection", value: 1 }], + }); + } + if (q.includes("FROM system.events")) { + return Promise.resolve({ + json: async () => [{ event: "Query", value: 50 }], + }); + } + return Promise.resolve({ json: async () => [] }); + }); + + const snapshot = await service.getSnapshot("ch-partial", config, DBType.CLICKHOUSE); + + expect(snapshot.health.ok).toBe(true); + expect(snapshot.connections.active).toBe(1); + expect(snapshot.connections.max).toBe(100); // fallback default + expect(snapshot.activeQueries).toEqual([]); // fallback gracefully + expect(snapshot.cacheHitRatio).toBeNull(); + }); + + test("returns empty snapshot when ClickHouse connection fails", async () => { + const config: ClickHouseConfig = { host: "unreachable" }; + + mockClickHouseClient.query.mockRejectedValue(new Error("Connection refused") as any); + + const snapshot = await service.getSnapshot("ch-down", config, DBType.CLICKHOUSE); + + expect(snapshot.databaseType).toBe(DBType.CLICKHOUSE); + expect(snapshot.health.ok).toBe(false); + expect(snapshot.health.latencyMs).toBeNull(); + expect(snapshot.health.message).toContain("Connection refused"); + expect(snapshot.connections).toEqual({ active: 0, max: 0, usagePct: 0 }); + expect(snapshot.throughput).toEqual({ qps: 0, totalQueries: 0 }); + expect(snapshot.activeQueries).toEqual([]); + }); +}); diff --git a/bridge/__tests__/monitoringWebSocketServer.test.ts b/bridge/__tests__/monitoringWebSocketServer.test.ts index 667c0cf6..fd27983b 100644 --- a/bridge/__tests__/monitoringWebSocketServer.test.ts +++ b/bridge/__tests__/monitoringWebSocketServer.test.ts @@ -171,4 +171,39 @@ describe("MonitoringWebSocketServer", () => { expect(setIntervalSpy).toHaveBeenCalledWith(expect.any(Function), 2000); expect(clearIntervalSpy).not.toHaveBeenCalled(); }); + + test("sends snapshots over websocket for clickhouse database", async () => { + server.start(); + const wsServer = mockWebSocketServerInstances[0]; + const socket = createMockSocket(); + + (dbService.getDatabaseConnection as any).mockResolvedValue({ + conn: { host: "localhost", port: 8123 }, + dbType: DBType.CLICKHOUSE, + }); + (monitoringService.getSnapshot as any).mockResolvedValue({ + databaseType: DBType.CLICKHOUSE, + sampledAt: "2026-05-16T00:00:00.000Z", + health: { ok: true, latencyMs: 8 }, + connections: { active: 2, max: 100, usagePct: 2 }, + throughput: { qps: 20, totalQueries: 500 }, + cacheHitRatio: 90.0, + activeQueries: [], + }); + + await wsServer.emit("connection", socket, { + url: "/monitoring?dbId=ch-1", + } as any); + + expect(dbService.getDatabaseConnection).toHaveBeenCalledWith("ch-1"); + expect(monitoringService.getSnapshot).toHaveBeenCalledWith( + "ch-1", + { host: "localhost", port: 8123 }, + DBType.CLICKHOUSE, + ); + expect(JSON.parse(socket.send.mock.calls[0][0] as string)).toMatchObject({ + type: "snapshot", + data: expect.objectContaining({ databaseType: DBType.CLICKHOUSE }), + }); + }); }); diff --git a/bridge/src/services/monitoringService.ts b/bridge/src/services/monitoringService.ts index 0b0fa828..559965f5 100644 --- a/bridge/src/services/monitoringService.ts +++ b/bridge/src/services/monitoringService.ts @@ -3,8 +3,10 @@ import mysql, { PoolConnection, RowDataPacket } from "mysql2/promise"; import { DBType } from "../types"; import { MySQLConfig } from "../types/mysql"; import { PGConfig } from "../types/postgres"; +import { ClickHouseConfig } from "../types/clickhouse"; import { createPoolConfig as createMySQLPoolConfig } from "../connectors/mysql"; import { createPoolConfig as createMariaDBPoolConfig, MariaDBConfig } from "../connectors/mariadb"; +import { getClient as getClickHouseClient } from "../connectors/clickhouse"; export interface MonitoringActiveQuery { id: string | number; @@ -44,7 +46,7 @@ export class MonitoringService { private previousTotals = new Map(); async getSnapshot(dbId: string, conn: any, dbType: DBType): Promise { - if (![DBType.POSTGRES, DBType.MYSQL, DBType.MARIADB].includes(dbType)) { + if (![DBType.POSTGRES, DBType.MYSQL, DBType.MARIADB, DBType.CLICKHOUSE].includes(dbType)) { throw new Error(`Monitoring is not supported for ${dbType}`); } @@ -56,6 +58,10 @@ export class MonitoringService { return this.getMySQLLikeSnapshot(dbId, conn as MySQLConfig, dbType); } + if (dbType === DBType.CLICKHOUSE) { + return this.getClickHouseSnapshot(dbId, conn as ClickHouseConfig); + } + throw new Error(`Monitoring is not supported for ${dbType}`); } @@ -223,6 +229,151 @@ export class MonitoringService { } } + private async getClickHouseSnapshot( + dbId: string, + cfg: ClickHouseConfig, + ): Promise { + const startedAt = Date.now(); + try { + const client = getClickHouseClient(cfg); + await client.query({ query: "SELECT 1;", format: "JSONEachRow" }); + const latencyMs = Date.now() - startedAt; + + const [metricsSettled, settingsSettled, eventsSettled, processesSettled] = + await Promise.allSettled([ + client + .query({ + query: + "SELECT metric, toInt64(value) AS value FROM system.metrics WHERE metric IN ('TCPConnection', 'HTTPConnection', 'Query');", + format: "JSONEachRow", + }) + .then( + async (res) => + (await res.json()) as Array<{ metric: string; value: string | number }>, + ), + + client + .query({ + query: + "SELECT value FROM system.settings WHERE name = 'max_concurrent_queries' LIMIT 1;", + format: "JSONEachRow", + }) + .then(async (res) => (await res.json()) as Array<{ value: string | number }>), + + client + .query({ + query: + "SELECT event, toUInt64(value) AS value FROM system.events WHERE event IN ('Query', 'MarkCacheHits', 'MarkCacheMisses');", + format: "JSONEachRow", + }) + .then( + async (res) => + (await res.json()) as Array<{ event: string; value: string | number }>, + ), + + client + .query({ + query: ` + SELECT + query_id AS id, + user, + query, + 'running' AS state, + round(elapsed, 2) AS durationSeconds + FROM system.processes + WHERE is_cancelled = 0 AND query NOT LIKE '%system.processes%' + ORDER BY elapsed DESC + LIMIT 25; + `, + format: "JSONEachRow", + }) + .then( + async (res) => + (await res.json()) as Array<{ + id: string; + user: string; + query: string; + state: string; + durationSeconds: number; + }>, + ), + ]); + + let active = 0; + if (metricsSettled.status === "fulfilled" && Array.isArray(metricsSettled.value)) { + for (const row of metricsSettled.value) { + if (row.metric === "TCPConnection" || row.metric === "HTTPConnection") { + active += Number(row.value ?? 0); + } + } + } + active = Math.max(1, active); + + let max = 100; + if ( + settingsSettled.status === "fulfilled" && + Array.isArray(settingsSettled.value) && + settingsSettled.value.length > 0 + ) { + const parsedMax = Number(settingsSettled.value[0]?.value); + if (Number.isFinite(parsedMax) && parsedMax > 0) { + max = parsedMax; + } + } + + let totalQueries = 0; + let markCacheHits = 0; + let markCacheMisses = 0; + let hasMarkCache = false; + + if (eventsSettled.status === "fulfilled" && Array.isArray(eventsSettled.value)) { + for (const row of eventsSettled.value) { + if (row.event === "Query") { + totalQueries = Number(row.value ?? 0); + } else if (row.event === "MarkCacheHits") { + markCacheHits = Number(row.value ?? 0); + hasMarkCache = true; + } else if (row.event === "MarkCacheMisses") { + markCacheMisses = Number(row.value ?? 0); + hasMarkCache = true; + } + } + } + + const totalCacheRequests = markCacheHits + markCacheMisses; + const cacheHitRatio = + hasMarkCache && totalCacheRequests > 0 + ? Number(((markCacheHits / totalCacheRequests) * 100).toFixed(2)) + : null; + + const activeQueries: MonitoringActiveQuery[] = + processesSettled.status === "fulfilled" && Array.isArray(processesSettled.value) + ? processesSettled.value.map((row) => ({ + id: String(row.id ?? ""), + user: String(row.user ?? ""), + query: String(row.query ?? ""), + state: String(row.state ?? "running"), + durationSeconds: Number(row.durationSeconds ?? 0), + })) + : []; + + return { + databaseType: DBType.CLICKHOUSE, + sampledAt: new Date().toISOString(), + health: { ok: true, latencyMs }, + connections: this.mapConnections(active, max), + throughput: { + qps: this.calculateRate(dbId, totalQueries), + totalQueries, + }, + cacheHitRatio, + activeQueries, + }; + } catch (error: any) { + return this.emptySnapshot(DBType.CLICKHOUSE, error?.message || String(error)); + } + } + private mapConnections(active: number, max: number) { return { active, diff --git a/bridge/src/services/monitoringWebSocketServer.ts b/bridge/src/services/monitoringWebSocketServer.ts index 551c72af..3074b038 100644 --- a/bridge/src/services/monitoringWebSocketServer.ts +++ b/bridge/src/services/monitoringWebSocketServer.ts @@ -92,7 +92,7 @@ export class MonitoringWebSocketServer { try { const { conn, dbType } = await this.dbService.getDatabaseConnection(dbId); - if (![DBType.POSTGRES, DBType.MYSQL, DBType.MARIADB].includes(dbType)) { + if (![DBType.POSTGRES, DBType.MYSQL, DBType.MARIADB, DBType.CLICKHOUSE].includes(dbType)) { socket.send( JSON.stringify({ type: "unsupported", diff --git a/src/components/layout/VerticalIconBar.tsx b/src/components/layout/VerticalIconBar.tsx index d9a006a9..2c9413c9 100644 --- a/src/components/layout/VerticalIconBar.tsx +++ b/src/components/layout/VerticalIconBar.tsx @@ -39,7 +39,13 @@ const globalNavigationItems = [{ icon: Home, label: "Dashboard", path: "/" }]; function supportsMonitoring(databaseType?: string) { const type = databaseType?.toLowerCase(); - return type === "postgres" || type === "postgresql" || type === "mysql" || type === "mariadb"; + return ( + type === "postgres" || + type === "postgresql" || + type === "mysql" || + type === "mariadb" || + type === "clickhouse" + ); } export default function VerticalIconBar({ diff --git a/src/features/monitoring/components/MonitoringDashboard.tsx b/src/features/monitoring/components/MonitoringDashboard.tsx index 7b774d96..073b1fe5 100644 --- a/src/features/monitoring/components/MonitoringDashboard.tsx +++ b/src/features/monitoring/components/MonitoringDashboard.tsx @@ -77,9 +77,15 @@ export function MonitoringDashboard({ />
diff --git a/src/features/monitoring/components/MonitoringPanel.tsx b/src/features/monitoring/components/MonitoringPanel.tsx index 714a6561..b51bff18 100644 --- a/src/features/monitoring/components/MonitoringPanel.tsx +++ b/src/features/monitoring/components/MonitoringPanel.tsx @@ -23,7 +23,13 @@ type ThroughputPoint = { function isMonitoringSupported(databaseType?: string) { const type = databaseType?.toLowerCase(); - return type === "postgres" || type === "postgresql" || type === "mysql" || type === "mariadb"; + return ( + type === "postgres" || + type === "postgresql" || + type === "mysql" || + type === "mariadb" || + type === "clickhouse" + ); } export function MonitoringPanel({ dbId, databaseName, databaseType }: MonitoringPanelProps) { From 59a457c56fd64c06c9509e246715229144fcad03 Mon Sep 17 00:00:00 2001 From: Yash Date: Fri, 16 Oct 2026 12:29:00 +0530 Subject: [PATCH 12/17] ci(test): add ClickHouse service container and seed step to test workflow - Add clickhouse service container and health check to .github/workflows/test.yml - Provide REAL_CLICKHOUSE_* environment variables for CI test runner - Add cross-platform seed-clickhouse.cjs runner with connection retry logic - Format files adhering to project Prettier guidelines --- .github/workflows/test.yml | 27 ++++++ bridge/__tests__/clickhouseUrl.test.ts | 22 ++--- .../connectors/clickhouse.batch.test.ts | 12 ++- .../connectors/clickhouse.cache.test.ts | 6 +- bridge/scripts/seed-clickhouse.cjs | 70 +++++++++++++++ bridge/seed-clickhouse.ts | 10 +-- bridge/src/ai/providers/groq.provider.ts | 11 ++- bridge/src/connectors/clickhouse.ts | 79 +++++++++-------- bridge/src/handlers/migrationHandlers.ts | 60 ++++++++++--- bridge/src/services/connectionBuilder.ts | 5 +- bridge/src/services/databaseService.ts | 6 +- bridge/src/services/discoveryService.ts | 4 +- bridge/src/services/monitoringService.ts | 3 +- bridge/src/utils/clickhouseUrl.ts | 3 +- bridge/src/utils/migrationGenerator.ts | 13 ++- src/components/layout/CommandPalette.tsx | 6 +- .../database/hooks/useDatabaseDetails.ts | 8 +- .../home/components/AddConnectionDialog.tsx | 85 +++++++++++-------- .../components/MonitoringDashboard.tsx | 4 +- .../settings/components/AISettings.tsx | 81 +++++++++--------- .../components/SQLWorkspacePanel.tsx | 8 +- src/lib/databases.tsx | 69 ++++++++++++--- src/lib/parseConnectionUrl.ts | 6 +- 23 files changed, 400 insertions(+), 198 deletions(-) create mode 100644 bridge/scripts/seed-clickhouse.cjs diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index df8925be..7cd33e6a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -61,6 +61,22 @@ jobs: --health-timeout=5s --health-retries=5 + clickhouse: + image: clickhouse/clickhouse-server:latest + env: + CLICKHOUSE_USER: testuser + CLICKHOUSE_PASSWORD: testpass + CLICKHOUSE_DB: testdb + CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT: 1 + ports: + - 8123:8123 + - 9000:9000 + options: >- + --health-cmd="wget -q --spider http://localhost:8123/ping || exit 1" + --health-interval=10s + --health-timeout=5s + --health-retries=5 + env: REAL_POSTGRES_HOST: localhost REAL_POSTGRES_PORT: 5432 @@ -93,6 +109,13 @@ jobs: REAL_MARIADB_DATABASE: testdb REAL_MARIADB_SSL: "false" + REAL_CLICKHOUSE_HOST: localhost + REAL_CLICKHOUSE_PORT: 8123 + REAL_CLICKHOUSE_USER: testuser + REAL_CLICKHOUSE_PASSWORD: testpass + REAL_CLICKHOUSE_DATABASE: testdb + REAL_CLICKHOUSE_SSL: "false" + # SQLite — no env vars needed; tests use a temp file steps: @@ -127,6 +150,10 @@ jobs: run: | mysql -h 127.0.0.1 -P 3307 -u root -prootpass testdb < bridge/scripts/seed-mariadb.sql + - name: Seed ClickHouse database + run: | + node bridge/scripts/seed-clickhouse.cjs + - name: Run tests run: | cd bridge diff --git a/bridge/__tests__/clickhouseUrl.test.ts b/bridge/__tests__/clickhouseUrl.test.ts index 49109154..ac157769 100644 --- a/bridge/__tests__/clickhouseUrl.test.ts +++ b/bridge/__tests__/clickhouseUrl.test.ts @@ -11,11 +11,13 @@ describe("ClickHouse URL Utilities", () => { it("should strip markdown links", () => { expect( - cleanClickHouseUrl("[https://demo.clickhouse.cloud:8443](https://demo.clickhouse.cloud:8443)"), - ).toBe("https://demo.clickhouse.cloud:8443"); - expect( - cleanClickHouseUrl("[https://demo.clickhouse.cloud:8443]()"), + cleanClickHouseUrl( + "[https://demo.clickhouse.cloud:8443](https://demo.clickhouse.cloud:8443)", + ), ).toBe("https://demo.clickhouse.cloud:8443"); + expect(cleanClickHouseUrl("[https://demo.clickhouse.cloud:8443]()")).toBe( + "https://demo.clickhouse.cloud:8443", + ); }); it("should strip enclosing single/double quotes and backticks", () => { @@ -40,18 +42,14 @@ describe("ClickHouse URL Utilities", () => { expect(cleanClickHouseUrl("clickhouse://demo.clickhouse.cloud:8443")).toBe( "https://demo.clickhouse.cloud:8443", ); - expect(cleanClickHouseUrl("clickhouse://localhost:8123")).toBe( - "http://localhost:8123", - ); + expect(cleanClickHouseUrl("clickhouse://localhost:8123")).toBe("http://localhost:8123"); }); it("should prepend https:// for clickhouse.cloud or port 8443 if no scheme provided", () => { expect(cleanClickHouseUrl("demo.clickhouse.cloud:8443")).toBe( "https://demo.clickhouse.cloud:8443", ); - expect(cleanClickHouseUrl("demo.clickhouse.cloud")).toBe( - "https://demo.clickhouse.cloud", - ); + expect(cleanClickHouseUrl("demo.clickhouse.cloud")).toBe("https://demo.clickhouse.cloud"); }); it("should prepend http:// for localhost:8123 if no scheme provided", () => { @@ -67,9 +65,7 @@ describe("ClickHouse URL Utilities", () => { describe("parseClickHouseUrl", () => { it("should parse standard cloud url", () => { - const parsed = parseClickHouseUrl( - "https://of8remuy3t.ap-south-1.aws.clickhouse.cloud:8443", - ); + const parsed = parseClickHouseUrl("https://of8remuy3t.ap-south-1.aws.clickhouse.cloud:8443"); expect(parsed.host).toBe("of8remuy3t.ap-south-1.aws.clickhouse.cloud"); expect(parsed.port).toBe(8443); expect(parsed.ssl).toBe(true); diff --git a/bridge/__tests__/connectors/clickhouse.batch.test.ts b/bridge/__tests__/connectors/clickhouse.batch.test.ts index 47639585..2abef9c6 100644 --- a/bridge/__tests__/connectors/clickhouse.batch.test.ts +++ b/bridge/__tests__/connectors/clickhouse.batch.test.ts @@ -207,7 +207,9 @@ describe("ClickHouse getSchemaMetadataBatch unit tests", () => { } // Fallback single table column queries: - if (q.includes("WHERE database = {target_schema: String} AND `table` = {target_table: String}")) { + if ( + q.includes("WHERE database = {target_schema: String} AND `table` = {target_table: String}") + ) { return { json: async () => [ { @@ -273,7 +275,13 @@ describe("ClickHouse getSchemaMetadataBatch unit tests", () => { json: async () => [ { table: "student", name: "id", type: "UInt32", not_nullable: 1, is_in_primary_key: 1 }, { table: "persons", name: "id", type: "UInt32", not_nullable: 1, is_in_primary_key: 1 }, - { table: "TestTable", name: "id", type: "UInt32", not_nullable: 1, is_in_primary_key: 1 }, + { + table: "TestTable", + name: "id", + type: "UInt32", + not_nullable: 1, + is_in_primary_key: 1, + }, ], }; } diff --git a/bridge/__tests__/connectors/clickhouse.cache.test.ts b/bridge/__tests__/connectors/clickhouse.cache.test.ts index 45175c2b..3381c0cd 100644 --- a/bridge/__tests__/connectors/clickhouse.cache.test.ts +++ b/bridge/__tests__/connectors/clickhouse.cache.test.ts @@ -118,7 +118,11 @@ describe("ClickhouseCacheManager", () => { }); it("should clear all caches on clearAll", () => { - clickhouseCache.setTableList(mockConnection, [{ schema: "testdb", name: "users", type: "BASE TABLE" }], "testdb"); + clickhouseCache.setTableList( + mockConnection, + [{ schema: "testdb", name: "users", type: "BASE TABLE" }], + "testdb", + ); clickhouseCache.clearAll(); expect(clickhouseCache.getTableList(mockConnection, "testdb")).toBeNull(); }); diff --git a/bridge/scripts/seed-clickhouse.cjs b/bridge/scripts/seed-clickhouse.cjs new file mode 100644 index 00000000..f1da0f05 --- /dev/null +++ b/bridge/scripts/seed-clickhouse.cjs @@ -0,0 +1,70 @@ +const fs = require("fs"); +const path = require("path"); +const { createClient } = require("@clickhouse/client"); + +function wait(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +async function seed() { + const host = process.env.REAL_CLICKHOUSE_HOST || "127.0.0.1"; + const port = process.env.REAL_CLICKHOUSE_PORT || "8123"; + const user = process.env.REAL_CLICKHOUSE_USER || "testuser"; + const password = process.env.REAL_CLICKHOUSE_PASSWORD || "testpass"; + const url = process.env.REAL_CLICKHOUSE_URL || `http://${host}:${port}`; + + const client = createClient({ + url, + username: user, + password, + database: "default", + request_timeout: 10000, + }); + + let connected = false; + for (let attempt = 1; attempt <= 15; attempt++) { + try { + const ping = await client.ping(); + if (ping.success) { + connected = true; + break; + } + } catch { + // Retry after delay + } + await wait(1000); + } + + if (!connected) { + throw new Error(`ClickHouse at ${url} was not reachable after 15 attempts.`); + } + + const sqlPath = path.join(__dirname, "seed-clickhouse.sql"); + const sql = fs.readFileSync(sqlPath, "utf-8"); + + const cleanSql = sql + .split("\n") + .map((line) => { + const trimmed = line.trim(); + if (trimmed.startsWith("--")) return ""; + return line; + }) + .join("\n"); + + const statements = cleanSql + .split(";") + .map((s) => s.trim()) + .filter((s) => s.length > 0); + + for (const statement of statements) { + await client.command({ query: statement }); + } + + await client.close(); + console.log("ClickHouse test database seeded successfully."); +} + +seed().catch((err) => { + console.error("Failed to seed ClickHouse:", err); + process.exit(1); +}); diff --git a/bridge/seed-clickhouse.ts b/bridge/seed-clickhouse.ts index 8e6f17f4..41bfe9e4 100644 --- a/bridge/seed-clickhouse.ts +++ b/bridge/seed-clickhouse.ts @@ -58,9 +58,7 @@ function parseArgs(): { drop: boolean; } { const args = process.argv.slice(2); - let url = - process.env.CLICKHOUSE_URL || - "https://of8remuy3t.ap-south-1.aws.clickhouse.cloud:8443"; + let url = process.env.CLICKHOUSE_URL || "https://of8remuy3t.ap-south-1.aws.clickhouse.cloud:8443"; let username = process.env.CLICKHOUSE_USER || "default"; let password = process.env.CLICKHOUSE_PASSWORD || "HpD2b9hQ5KW.3"; let database = process.env.CLICKHOUSE_DATABASE || "default"; @@ -125,11 +123,7 @@ async function main() { const { url, username, password, database, scale, drop } = parseArgs(); const config = SCALE_PRESETS[scale] || SCALE_PRESETS.medium; const totalPlanned = - config.users + - config.products + - config.orders + - config.events + - config.metrics; + config.users + config.products + config.orders + config.events + config.metrics; console.log("\n======================================================="); console.log(" RelWave ClickHouse Benchmark & Seed Generator"); diff --git a/bridge/src/ai/providers/groq.provider.ts b/bridge/src/ai/providers/groq.provider.ts index 7f9c2a55..5adc381c 100644 --- a/bridge/src/ai/providers/groq.provider.ts +++ b/bridge/src/ai/providers/groq.provider.ts @@ -13,7 +13,6 @@ import { parseChartRecommendation, } from "../prompts/chart-recommendation"; - const DEFAULT_MODEL = "openai/gpt-oss-20b"; const FALLBACK_MODELS = [ "openai/gpt-oss-20b", @@ -88,7 +87,14 @@ export class GroqProvider implements AIProvider { let availableChatModels: string[] = []; try { const modelsRes = await this.client.models.list(); - const nonChatKeywords = ["whisper", "prompt-guard", "orpheus", "safeguard", "vision", "embed"]; + const nonChatKeywords = [ + "whisper", + "prompt-guard", + "orpheus", + "safeguard", + "vision", + "embed", + ]; availableChatModels = (modelsRes.data ?? []) .map((m) => m.id) .filter((id) => !nonChatKeywords.some((kw) => id.toLowerCase().includes(kw))); @@ -118,4 +124,3 @@ export class GroqProvider implements AIProvider { return this.complete(system, user); } } - diff --git a/bridge/src/connectors/clickhouse.ts b/bridge/src/connectors/clickhouse.ts index 5258719a..3cf10f39 100644 --- a/bridge/src/connectors/clickhouse.ts +++ b/bridge/src/connectors/clickhouse.ts @@ -1,8 +1,5 @@ import { createClient, type ClickHouseClient } from "@clickhouse/client"; -import { - ClickHouseConfig, - ClickHouseSchemaMetadataBatch, -} from "../types/clickhouse"; +import { ClickHouseConfig, ClickHouseSchemaMetadataBatch } from "../types/clickhouse"; export type { ClickHouseConfig, ClickHouseSchemaMetadataBatch }; import { TableInfo, @@ -34,10 +31,7 @@ import { CLICKHOUSE_GET_INDEXES, CLICKHOUSE_BATCH_GET_INDEXES, } from "../queries/clickhouse/constraints"; -import { - CLICKHOUSE_GET_DB_STATS, - CLICKHOUSE_GET_DB_SIZE, -} from "../queries/clickhouse/stats"; +import { CLICKHOUSE_GET_DB_STATS, CLICKHOUSE_GET_DB_SIZE } from "../queries/clickhouse/stats"; import { cleanClickHouseUrl } from "../utils/clickhouseUrl"; import logger from "../services/logger"; @@ -97,7 +91,12 @@ class ClickHouseCacheManager { const entry = this.primaryKeysCache.get(this.getTableKey(cfg, schema, table)); return this.isValid(entry) ? entry!.data : null; } - setPrimaryKeys(cfg: ClickHouseConfig, schema: string, table: string, data: PrimaryKeyInfo[]): void { + setPrimaryKeys( + cfg: ClickHouseConfig, + schema: string, + table: string, + data: PrimaryKeyInfo[], + ): void { this.primaryKeysCache.set(this.getTableKey(cfg, schema, table), { data, timestamp: Date.now(), @@ -133,7 +132,12 @@ class ClickHouseCacheManager { const entry = this.tableDetailsCache.get(this.getTableKey(cfg, schema, table)); return this.isValid(entry) ? entry!.data : null; } - setTableDetails(cfg: ClickHouseConfig, schema: string, table: string, data: ColumnDetail[]): void { + setTableDetails( + cfg: ClickHouseConfig, + schema: string, + table: string, + data: ColumnDetail[], + ): void { this.tableDetailsCache.set(this.getTableKey(cfg, schema, table), { data, timestamp: Date.now(), @@ -281,18 +285,13 @@ export async function testConnection( } } -export async function listTables( - cfg: ClickHouseConfig, - schemaName?: string, -): Promise { +export async function listTables(cfg: ClickHouseConfig, schemaName?: string): Promise { const cached = clickhouseCache.getTableList(cfg, schemaName); if (cached) return cached; const client = getClient(cfg); const query = schemaName ? CLICKHOUSE_LIST_TABLES_BY_SCHEMA : CLICKHOUSE_LIST_TABLES; - const query_params = schemaName - ? { target_schema: schemaName, schema: schemaName } - : undefined; + const query_params = schemaName ? { target_schema: schemaName, schema: schemaName } : undefined; const res = await client.query({ query, @@ -446,7 +445,7 @@ export async function getDBStats(cfg: ClickHouseConfig): Promise { total_tables = Number(statsData[0].total_tables) || 0; total_rows = Number(statsData[0].total_rows) || 0; } - } catch { } + } catch {} try { const sizeRes = await client.query({ @@ -458,7 +457,7 @@ export async function getDBStats(cfg: ClickHouseConfig): Promise { if (sizeData.length > 0) { total_db_size_mb = Number(sizeData[0].total_db_size_mb) || 0; } - } catch { } + } catch {} const stats: DBStats = { total_tables, @@ -595,7 +594,10 @@ export async function getSchemaMetadataBatch( } } } catch (err: any) { - logger.warn({ err: err?.message, schema }, "ClickHouse getSchemaMetadataBatch listTables failed"); + logger.warn( + { err: err?.message, schema }, + "ClickHouse getSchemaMetadataBatch listTables failed", + ); } // Fallback table discovery if empty and no specific table filter was provided @@ -608,7 +610,10 @@ export async function getSchemaMetadataBatch( } } } catch (err: any) { - logger.warn({ err: err?.message, schema }, "ClickHouse getSchemaMetadataBatch allTables fallback failed"); + logger.warn( + { err: err?.message, schema }, + "ClickHouse getSchemaMetadataBatch allTables fallback failed", + ); } } @@ -655,7 +660,10 @@ export async function getSchemaMetadataBatch( entry.primaryKeys.push({ column_name: r.column_name }); } } catch (err: any) { - logger.warn({ err: err?.message, schema }, "ClickHouse CLICKHOUSE_BATCH_GET_PRIMARY_KEYS failed"); + logger.warn( + { err: err?.message, schema }, + "ClickHouse CLICKHOUSE_BATCH_GET_PRIMARY_KEYS failed", + ); } // 4. Batch fetch secondary indexes @@ -681,7 +689,10 @@ export async function getSchemaMetadataBatch( }); } } catch (err: any) { - logger.debug({ err: err?.message, schema }, "ClickHouse CLICKHOUSE_BATCH_GET_INDEXES skipped/failed"); + logger.debug( + { err: err?.message, schema }, + "ClickHouse CLICKHOUSE_BATCH_GET_INDEXES skipped/failed", + ); } // 5. Direct fallback: If tablesMap is still empty after batch queries, query system.tables directly @@ -837,11 +848,7 @@ export async function createTable( }) .join(",\n "); - const pkCols = Array.isArray(primaryKey) - ? primaryKey - : primaryKey - ? [primaryKey] - : []; + const pkCols = Array.isArray(primaryKey) ? primaryKey : primaryKey ? [primaryKey] : []; const orderClause = pkCols.length > 0 ? pkCols.map((c) => `\`${c}\``).join(", ") : "tuple()"; const ifNotExistsClause = ifNotExists ? "IF NOT EXISTS " : ""; @@ -1132,7 +1139,7 @@ export async function rollbackMigration( if (stmts.length === 0) { throw new Error( - `Cannot rollback migration "${migration.name}": no executable SQL found in DOWN section.` + `Cannot rollback migration "${migration.name}": no executable SQL found in DOWN section.`, ); } @@ -1190,7 +1197,11 @@ export async function updateRow( .join(" AND "); } updates = primaryKeyValueOrRow || {}; - } else if (typeof primaryKeyColumnOrObj === "string" && primaryKeyValueOrRow && typeof primaryKeyValueOrRow === "object") { + } else if ( + typeof primaryKeyColumnOrObj === "string" && + primaryKeyValueOrRow && + typeof primaryKeyValueOrRow === "object" + ) { updates = primaryKeyValueOrRow; whereClause = `\`${primaryKeyColumnOrObj}\` = ${formatValue(rowData)}`; } @@ -1203,9 +1214,7 @@ export async function updateRow( throw new Error("Primary key condition required for update"); } - const sets = updateEntries - .map(([col, val]) => `\`${col}\` = ${formatValue(val)}`) - .join(", "); + const sets = updateEntries.map(([col, val]) => `\`${col}\` = ${formatValue(val)}`).join(", "); await client.command({ query: `ALTER TABLE \`${schemaName}\`.\`${tableName}\` UPDATE ${sets} WHERE ${whereClause}`, @@ -1292,9 +1301,7 @@ export async function searchTable( return fetchTableData(cfg, schemaName, tableName, pageSize, page); } - whereConditions = textCols - .map((c) => `\`${c.name}\` ILIKE '%${escapedTerm}%'`) - .join(" OR "); + whereConditions = textCols.map((c) => `\`${c.name}\` ILIKE '%${escapedTerm}%'`).join(" OR "); } const countRes = await client.query({ diff --git a/bridge/src/handlers/migrationHandlers.ts b/bridge/src/handlers/migrationHandlers.ts index 391e0351..e893ee89 100644 --- a/bridge/src/handlers/migrationHandlers.ts +++ b/bridge/src/handlers/migrationHandlers.ts @@ -136,11 +136,17 @@ export class MigrationHandlers { })); } } - if ((!foreignKeys || foreignKeys.length === 0) && typeof connector.listForeignKeys === "function") { + if ( + (!foreignKeys || foreignKeys.length === 0) && + typeof connector.listForeignKeys === "function" + ) { foreignKeys = await connector.listForeignKeys(conn, schemaName, tableName); } } catch (inspectErr) { - this.logger?.warn({ err: inspectErr }, "Could not inspect table before drop migration generation"); + this.logger?.warn( + { err: inspectErr }, + "Could not inspect table before drop migration generation", + ); } } @@ -163,7 +169,10 @@ export class MigrationHandlers { } } } catch (snapErr) { - this.logger?.warn({ err: snapErr }, "Could not load schema snapshot before drop migration generation"); + this.logger?.warn( + { err: snapErr }, + "Could not load schema snapshot before drop migration generation", + ); } } @@ -240,11 +249,20 @@ export class MigrationHandlers { const dbSchema = (await this.queryExecutor.listSchemas(conn, dbType)) as any; const liveSchemas = dbSchema?.schemas || []; const crypto = await import("crypto"); - schemaHash = crypto.createHash("sha256").update(JSON.stringify(liveSchemas)).digest("hex"); + schemaHash = crypto + .createHash("sha256") + .update(JSON.stringify(liveSchemas)) + .digest("hex"); await projectStoreInstance.saveSchema(project.id, liveSchemas, schemaHash, dbType); - this.rpc?.sendNotification?.("project.schema_changed", { projectId: project.id, newHash: schemaHash }); + this.rpc?.sendNotification?.("project.schema_changed", { + projectId: project.id, + newHash: schemaHash, + }); } catch (schemaErr) { - this.logger?.warn({ err: schemaErr }, "Failed to auto-refresh project schema after migration apply"); + this.logger?.warn( + { err: schemaErr }, + "Failed to auto-refresh project schema after migration apply", + ); } // Update Migration Lock first so it gets included in the git sync commit @@ -340,11 +358,20 @@ export class MigrationHandlers { const dbSchema = (await this.queryExecutor.listSchemas(conn, dbType)) as any; const liveSchemas = dbSchema?.schemas || []; const crypto = await import("crypto"); - schemaHash = crypto.createHash("sha256").update(JSON.stringify(liveSchemas)).digest("hex"); + schemaHash = crypto + .createHash("sha256") + .update(JSON.stringify(liveSchemas)) + .digest("hex"); await projectStoreInstance.saveSchema(project.id, liveSchemas, schemaHash, dbType); - this.rpc?.sendNotification?.("project.schema_changed", { projectId: project.id, newHash: schemaHash }); + this.rpc?.sendNotification?.("project.schema_changed", { + projectId: project.id, + newHash: schemaHash, + }); } catch (schemaErr) { - this.logger?.warn({ err: schemaErr }, "Failed to auto-refresh project schema after applyMigrations"); + this.logger?.warn( + { err: schemaErr }, + "Failed to auto-refresh project schema after applyMigrations", + ); } if (!schemaHash) { @@ -490,11 +517,20 @@ export class MigrationHandlers { const dbSchema = (await this.queryExecutor.listSchemas(conn, dbType)) as any; const liveSchemas = dbSchema?.schemas || []; const crypto = await import("crypto"); - schemaHash = crypto.createHash("sha256").update(JSON.stringify(liveSchemas)).digest("hex"); + schemaHash = crypto + .createHash("sha256") + .update(JSON.stringify(liveSchemas)) + .digest("hex"); await projectStoreInstance.saveSchema(project.id, liveSchemas, schemaHash, dbType); - this.rpc?.sendNotification?.("project.schema_changed", { projectId: project.id, newHash: schemaHash }); + this.rpc?.sendNotification?.("project.schema_changed", { + projectId: project.id, + newHash: schemaHash, + }); } catch (schemaErr) { - this.logger?.warn({ err: schemaErr }, "Failed to auto-refresh project schema after migration rollback"); + this.logger?.warn( + { err: schemaErr }, + "Failed to auto-refresh project schema after migration rollback", + ); } // Update Migration Lock first so it gets included in the git sync commit diff --git a/bridge/src/services/connectionBuilder.ts b/bridge/src/services/connectionBuilder.ts index 92b0f77b..3e91e98d 100644 --- a/bridge/src/services/connectionBuilder.ts +++ b/bridge/src/services/connectionBuilder.ts @@ -52,10 +52,7 @@ export class ConnectionBuilder { }; } - const defaultPort = - dbType === DBType.MYSQL || dbType === DBType.MARIADB - ? 3306 - : 5432; + const defaultPort = dbType === DBType.MYSQL || dbType === DBType.MARIADB ? 3306 : 5432; const base: DatabaseConfig = { host: db.host, diff --git a/bridge/src/services/databaseService.ts b/bridge/src/services/databaseService.ts index 321dbae1..e23bf6ba 100644 --- a/bridge/src/services/databaseService.ts +++ b/bridge/src/services/databaseService.ts @@ -62,8 +62,7 @@ export class DatabaseService { const isFileBasedDB = ["sqlite", "duckdb"].includes( (payload.type as string | undefined)?.toLowerCase() || "", ); - const isClickHouse = - (payload.type as string | undefined)?.toLowerCase() === "clickhouse"; + const isClickHouse = (payload.type as string | undefined)?.toLowerCase() === "clickhouse"; const required = isFileBasedDB ? ["name", "database", "type"] : isClickHouse @@ -123,8 +122,7 @@ export class DatabaseService { const isFileBasedDB = ["sqlite", "duckdb"].includes( (payload.type as string | undefined)?.toLowerCase() || "", ); - const isClickHouse = - (payload.type as string | undefined)?.toLowerCase() === "clickhouse"; + const isClickHouse = (payload.type as string | undefined)?.toLowerCase() === "clickhouse"; // Extract SSH credentials const sshPwd = payload.ssh?.password; diff --git a/bridge/src/services/discoveryService.ts b/bridge/src/services/discoveryService.ts index e5a49dc8..4edf15db 100644 --- a/bridge/src/services/discoveryService.ts +++ b/bridge/src/services/discoveryService.ts @@ -297,9 +297,7 @@ export class DiscoveryService { return { user: envVars.get("CLICKHOUSE_USER") || envVars.get("CLICKHOUSE_DEFAULT_USER") || "default", password: - envVars.get("CLICKHOUSE_PASSWORD") || - envVars.get("CLICKHOUSE_DEFAULT_PASSWORD") || - "", + envVars.get("CLICKHOUSE_PASSWORD") || envVars.get("CLICKHOUSE_DEFAULT_PASSWORD") || "", database: envVars.get("CLICKHOUSE_DB") || "default", }; } else { diff --git a/bridge/src/services/monitoringService.ts b/bridge/src/services/monitoringService.ts index 559965f5..80400ee2 100644 --- a/bridge/src/services/monitoringService.ts +++ b/bridge/src/services/monitoringService.ts @@ -267,8 +267,7 @@ export class MonitoringService { format: "JSONEachRow", }) .then( - async (res) => - (await res.json()) as Array<{ event: string; value: string | number }>, + async (res) => (await res.json()) as Array<{ event: string; value: string | number }>, ), client diff --git a/bridge/src/utils/clickhouseUrl.ts b/bridge/src/utils/clickhouseUrl.ts index 0726fa6b..f722ec4c 100644 --- a/bridge/src/utils/clickhouseUrl.ts +++ b/bridge/src/utils/clickhouseUrl.ts @@ -20,7 +20,8 @@ export function cleanClickHouseUrl(raw?: string): string { url = "https://" + url.slice("clickhouses://".length); } else if (url.startsWith("clickhouse://")) { const rest = url.slice("clickhouse://".length); - const isSsl = rest.includes(":8443") || rest.includes(":443") || rest.includes("clickhouse.cloud"); + const isSsl = + rest.includes(":8443") || rest.includes(":443") || rest.includes("clickhouse.cloud"); url = (isSsl ? "https://" : "http://") + rest; } diff --git a/bridge/src/utils/migrationGenerator.ts b/bridge/src/utils/migrationGenerator.ts index 17913dc8..92465ab6 100644 --- a/bridge/src/utils/migrationGenerator.ts +++ b/bridge/src/utils/migrationGenerator.ts @@ -70,9 +70,7 @@ export function generateCreateTableMigration(params: { // Generate UP SQL let upSQL = ""; if (dbType === "clickhouse") { - const pkCols = columns - .filter((c) => c.is_primary_key) - .map((c) => quoteIdent(c.name, dbType)); + const pkCols = columns.filter((c) => c.is_primary_key).map((c) => quoteIdent(c.name, dbType)); const orderClause = pkCols.length > 0 ? pkCols.join(", ") : "tuple()"; upSQL = `CREATE TABLE ${tableRef} ( ${allDefs} @@ -271,7 +269,14 @@ export function generateDropTableMigration(params: { foreignKeys?: any[]; dbType: "mysql" | "postgres" | "mariadb" | "sqlite" | "duckdb" | "clickhouse"; }): MigrationFile { - const { schemaName, tableName, mode = "RESTRICT", columns = [], foreignKeys = [], dbType } = params; + const { + schemaName, + tableName, + mode = "RESTRICT", + columns = [], + foreignKeys = [], + dbType, + } = params; const version = generateMigrationVersion(); const name = `drop_${tableName}_table`; const filename = `${version}_${name}.sql`; diff --git a/src/components/layout/CommandPalette.tsx b/src/components/layout/CommandPalette.tsx index ce25e666..187845b5 100644 --- a/src/components/layout/CommandPalette.tsx +++ b/src/components/layout/CommandPalette.tsx @@ -633,11 +633,7 @@ function CommandItem({ : TYPE_COLORS[type], )} > - {iconNode ? ( - iconNode - ) : Icon ? ( - - ) : null} + {iconNode ? iconNode : Icon ? : null}
{/* Label — IBM Plex Sans 450 (between regular and medium) */} diff --git a/src/features/database/hooks/useDatabaseDetails.ts b/src/features/database/hooks/useDatabaseDetails.ts index 5524792c..a3e40c44 100644 --- a/src/features/database/hooks/useDatabaseDetails.ts +++ b/src/features/database/hooks/useDatabaseDetails.ts @@ -9,7 +9,13 @@ import { useSchemaNames, useFullSchema, } from "@/features/project/hooks/useDbQueries"; -import { QueryProgress, SelectedTable, TableInfo, TableRow, ColumnDetails } from "@/features/database/types"; +import { + QueryProgress, + SelectedTable, + TableInfo, + TableRow, + ColumnDetails, +} from "@/features/database/types"; import { sessionService } from "@/services/bridge/session"; import { queryService } from "@/services/bridge/query"; import { analyticsService } from "@/services/analytics"; diff --git a/src/features/home/components/AddConnectionDialog.tsx b/src/features/home/components/AddConnectionDialog.tsx index cf08e541..5b554b36 100644 --- a/src/features/home/components/AddConnectionDialog.tsx +++ b/src/features/home/components/AddConnectionDialog.tsx @@ -63,8 +63,9 @@ export function AddConnectionDialog({ const reconstructedUrl = initialData.url || (initialData.type === "clickhouse" && initialData.host - ? `${initialData.ssl ? "https" : "http"}://${initialData.host}:${initialData.port || (initialData.ssl ? "8443" : "8123") - }` + ? `${initialData.ssl ? "https" : "http"}://${initialData.host}:${ + initialData.port || (initialData.ssl ? "8443" : "8123") + }` : ""); setFormData((prev) => ({ @@ -304,8 +305,9 @@ export function AddConnectionDialog({ if (isClickHouse && !isUrlMode && !formData.url) { setFormData((prev) => ({ ...prev, - url: `${prev.ssl ? "https" : "http"}://${prev.host || "localhost"}:${prev.port || (prev.ssl ? "8443" : "8123") - }`, + url: `${prev.ssl ? "https" : "http"}://${prev.host || "localhost"}:${ + prev.port || (prev.ssl ? "8443" : "8123") + }`, })); } }} @@ -363,8 +365,9 @@ export function AddConnectionDialog({ value={formData.database} onChange={(e) => handleInputChange("database", e.target.value)} readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono flex-1 ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" - }`} + className={`h-9 text-sm font-mono flex-1 ${ + isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} />
) : ( @@ -439,13 +443,15 @@ export function AddConnectionDialog({ setFormData((prev) => ({ ...prev, host: val, - url: `${prev.ssl ? "https" : "http"}://${val || "localhost"}:${prev.port || (prev.ssl ? "8443" : "8123") - }`, + url: `${prev.ssl ? "https" : "http"}://${val || "localhost"}:${ + prev.port || (prev.ssl ? "8443" : "8123") + }`, })); }} readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" - }`} + className={`h-9 text-sm font-mono ${ + isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} />
@@ -459,13 +465,15 @@ export function AddConnectionDialog({ setFormData((prev) => ({ ...prev, port: val, - url: `${prev.ssl ? "https" : "http"}://${prev.host || "localhost"}:${val || (prev.ssl ? "8443" : "8123") - }`, + url: `${prev.ssl ? "https" : "http"}://${prev.host || "localhost"}:${ + val || (prev.ssl ? "8443" : "8123") + }`, })); }} readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" - }`} + className={`h-9 text-sm font-mono ${ + isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} />
@@ -479,8 +487,9 @@ export function AddConnectionDialog({ value={formData.user} onChange={(e) => handleInputChange("user", e.target.value)} readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" - }`} + className={`h-9 text-sm font-mono ${ + isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} />
@@ -491,8 +500,9 @@ export function AddConnectionDialog({ value={formData.password} onChange={(e) => handleInputChange("password", e.target.value)} readOnly={isDiscoveredMode} - className={`h-9 text-sm ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" - }`} + className={`h-9 text-sm ${ + isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} />
@@ -504,8 +514,9 @@ export function AddConnectionDialog({ value={formData.database} onChange={(e) => handleInputChange("database", e.target.value)} readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" - }`} + className={`h-9 text-sm font-mono ${ + isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} />
@@ -526,8 +537,9 @@ export function AddConnectionDialog({ : prev.port === "8443" ? "8123" : prev.port, - url: `${isSsl ? "https" : "http"}://${prev.host || "localhost"}:${isSsl ? (prev.port === "8123" ? "8443" : prev.port) : prev.port - }`, + url: `${isSsl ? "https" : "http"}://${prev.host || "localhost"}:${ + isSsl ? (prev.port === "8123" ? "8443" : prev.port) : prev.port + }`, })); }} /> @@ -548,8 +560,9 @@ export function AddConnectionDialog({ value={formData.host} onChange={(e) => handleInputChange("host", e.target.value)} readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" - }`} + className={`h-9 text-sm font-mono ${ + isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} />
@@ -559,8 +572,9 @@ export function AddConnectionDialog({ value={formData.port} onChange={(e) => handleInputChange("port", e.target.value)} readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" - }`} + className={`h-9 text-sm font-mono ${ + isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} />
@@ -573,8 +587,9 @@ export function AddConnectionDialog({ value={formData.user} onChange={(e) => handleInputChange("user", e.target.value)} readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" - }`} + className={`h-9 text-sm font-mono ${ + isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} />
@@ -585,8 +600,9 @@ export function AddConnectionDialog({ value={formData.password} onChange={(e) => handleInputChange("password", e.target.value)} readOnly={isDiscoveredMode} - className={`h-9 text-sm ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" - }`} + className={`h-9 text-sm ${ + isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} />
@@ -598,8 +614,9 @@ export function AddConnectionDialog({ value={formData.database} onChange={(e) => handleInputChange("database", e.target.value)} readOnly={isDiscoveredMode} - className={`h-9 text-sm font-mono ${isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" - }`} + className={`h-9 text-sm font-mono ${ + isDiscoveredMode ? "opacity-60 bg-muted cursor-not-allowed" : "" + }`} /> diff --git a/src/features/monitoring/components/MonitoringDashboard.tsx b/src/features/monitoring/components/MonitoringDashboard.tsx index 073b1fe5..2efed4fb 100644 --- a/src/features/monitoring/components/MonitoringDashboard.tsx +++ b/src/features/monitoring/components/MonitoringDashboard.tsx @@ -81,8 +81,8 @@ export function MonitoringDashboard({ data.databaseType === "clickhouse" ? "Mark Cache" : data.databaseType === "postgres" - ? "Cache Hit" - : "Buffer Hit" + ? "Cache Hit" + : "Buffer Hit" } value={data.cacheHitRatio === null ? "N/A" : `${formatNumber(data.cacheHitRatio)}%`} detail={data.databaseType === "clickhouse" ? "Mark cache efficiency" : "Read efficiency"} diff --git a/src/features/settings/components/AISettings.tsx b/src/features/settings/components/AISettings.tsx index f9b0d2de..3ccd482c 100644 --- a/src/features/settings/components/AISettings.tsx +++ b/src/features/settings/components/AISettings.tsx @@ -698,50 +698,49 @@ export default function AISettings() {
{otherProviders.map((provider) => { const modelField = MODEL_FIELD[provider.name]; - const modelOptions = MODEL_OPTIONS[provider.name] ?? []; - const selectedModel = modelField - ? (settings[modelField] as string | undefined) - : undefined; - const selectedModelInfo = - modelOptions.find((o) => o.value === selectedModel) ?? modelOptions[0]; - return ( -
-
-
+ ); + })}
diff --git a/src/features/workspace/components/SQLWorkspacePanel.tsx b/src/features/workspace/components/SQLWorkspacePanel.tsx index 6bb53136..1caa8237 100644 --- a/src/features/workspace/components/SQLWorkspacePanel.tsx +++ b/src/features/workspace/components/SQLWorkspacePanel.tsx @@ -88,9 +88,7 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => { executingTabIdRef.current = targetId; wasExecutingRef.current = true; setTabs((prev) => - prev.map((tab) => - tab.id === targetId ? { ...tab, status: "running", error: null } : tab, - ), + prev.map((tab) => (tab.id === targetId ? { ...tab, status: "running", error: null } : tab)), ); handleExecuteQuery(activeTab.query); }, [activeTab?.query, isExecuting, activeTabId, handleExecuteQuery]); @@ -287,9 +285,7 @@ const SQLWorkspacePanel = ({ dbId }: SQLWorkspacePanelProps) => { onChange={updateActiveTabQuery} onExecute={handleRunActiveQuery} dialect={ - databaseType === "mysql" || databaseType === "mariadb" - ? "mysql" - : "postgresql" + databaseType === "mysql" || databaseType === "mariadb" ? "mysql" : "postgresql" } disabled={isExecuting} minHeight="100%" diff --git a/src/lib/databases.tsx b/src/lib/databases.tsx index 1535f967..723d0a27 100644 --- a/src/lib/databases.tsx +++ b/src/lib/databases.tsx @@ -41,8 +41,14 @@ export const databases: DatabaseConfig[] = [ - - + + ), }, @@ -61,7 +67,10 @@ export const databases: DatabaseConfig[] = [ deviconClass: "devicon-mysql-original colored", icon: ( - + ), }, @@ -80,7 +89,10 @@ export const databases: DatabaseConfig[] = [ deviconClass: "devicon-mariadb-original colored", icon: ( - + ), }, @@ -96,15 +108,32 @@ export const databases: DatabaseConfig[] = [ icon: ( - + - - - + + + ), }, @@ -118,8 +147,14 @@ export const databases: DatabaseConfig[] = [ fileExtensions: ["db", "duckdb", "ddb"], icon: ( - - + + ), }, @@ -138,7 +173,10 @@ export const databases: DatabaseConfig[] = [ icon: ( - + ), }, @@ -178,8 +216,13 @@ export function DatabaseLogo({ } return ( -
svg]:w-full [&>svg]:h-full", className)}> +
svg]:w-full [&>svg]:h-full", + className, + )} + > {meta.icon}
); -} \ No newline at end of file +} diff --git a/src/lib/parseConnectionUrl.ts b/src/lib/parseConnectionUrl.ts index 361d529c..54d951e5 100644 --- a/src/lib/parseConnectionUrl.ts +++ b/src/lib/parseConnectionUrl.ts @@ -59,7 +59,8 @@ export function cleanClickHouseUrl(raw?: string): string { url = "https://" + url.slice("clickhouses://".length); } else if (url.startsWith("clickhouse://")) { const rest = url.slice("clickhouse://".length); - const isSsl = rest.includes(":8443") || rest.includes(":443") || rest.includes("clickhouse.cloud"); + const isSsl = + rest.includes(":8443") || rest.includes(":443") || rest.includes("clickhouse.cloud"); url = (isSsl ? "https://" : "http://") + rest; } @@ -227,8 +228,7 @@ export function buildConnectionUrl(params: { return `${params.type}:///${encodeSqlitePathSegments(params.database)}`; } const isClickHouseSsl = - params.type === "clickhouse" && - (params.sslmode === "require" || params.port === "8443"); + params.type === "clickhouse" && (params.sslmode === "require" || params.port === "8443"); const protocol = params.type === "mysql" ? "mysql" From 1325e4b77893e81c9d802c968777037f0d6491cf Mon Sep 17 00:00:00 2001 From: Yash Date: Fri, 16 Oct 2026 14:29:42 +0530 Subject: [PATCH 13/17] feat(ux): enhance data grid, sql workspace ergonomics, and keyboard navigation --- src/components/layout/TitleBar.tsx | 23 ++- src/components/layout/VerticalIconBar.tsx | 48 +++-- src/components/shared/DataTable.tsx | 189 +++++++++++++----- src/components/shared/ShortcutsHelp.tsx | 9 +- .../components/ContentViewerPanel.tsx | 83 +++++++- .../workspace/components/QueryTabBar.tsx | 48 ++++- .../workspace/components/ResultsPanel.tsx | 100 ++++++++- .../components/SQLWorkspacePanel.tsx | 98 +++++++-- .../workspace/components/SqlEditor.tsx | 9 + .../workspace/components/WorkspaceHeader.tsx | 32 ++- src/main.tsx | 10 +- src/pages/DatabaseDetails.tsx | 35 +++- 12 files changed, 560 insertions(+), 124 deletions(-) diff --git a/src/components/layout/TitleBar.tsx b/src/components/layout/TitleBar.tsx index 79261285..bd73b983 100644 --- a/src/components/layout/TitleBar.tsx +++ b/src/components/layout/TitleBar.tsx @@ -1,8 +1,15 @@ import { Maximize2, Minus, X } from "lucide-react"; import { getCurrentWindow } from "@tauri-apps/api/window"; +import { useLocation, Link } from "react-router-dom"; import BridgeStatus from "./BridgeStatus"; +import { useDatabase } from "@/features/project/hooks/useDbQueries"; +import { DatabaseLogo } from "@/lib/databases"; const TitleBar = () => { + const location = useLocation(); + const dbId = location.pathname !== "/" ? location.pathname.split("/")[1] : undefined; + const { data: database } = useDatabase(dbId); + const handleMinimize = async () => { try { const appWindow = getCurrentWindow(); @@ -44,7 +51,21 @@ const TitleBar = () => { {/* App Title - Left */}
- RelWave + + RelWave + + {database && ( +
+ / +
+ + {database.name} +
+
+ )}
{/* Bridge Status - Center */} diff --git a/src/components/layout/VerticalIconBar.tsx b/src/components/layout/VerticalIconBar.tsx index 2c9413c9..19e28800 100644 --- a/src/components/layout/VerticalIconBar.tsx +++ b/src/components/layout/VerticalIconBar.tsx @@ -48,6 +48,26 @@ function supportsMonitoring(databaseType?: string) { ); } +export function getDatabasePanelItems(databaseType?: string): Array<{ + icon: any; + label: string; + panel: PanelType; +}> { + return [ + { icon: Layers, label: "Data View", panel: "data" }, + { icon: Terminal, label: "SQL Workspace", panel: "sql-workspace" }, + { icon: Sparkles, label: "AI Workspace", panel: "ai-workspace" }, + { icon: Search, label: "Query Builder", panel: "query-builder" }, + { icon: GitBranch, label: "Schema Explorer", panel: "schema-explorer" }, + { icon: Database, label: "ER Diagram", panel: "er-diagram" }, + ...(supportsMonitoring(databaseType) + ? [{ icon: Activity, label: "Monitoring", panel: "monitoring" as PanelType }] + : []), + { icon: History, label: "Migrations", panel: "migrations" }, + { icon: GitCommitHorizontal, label: "Git Status", panel: "git-status" }, + ]; +} + export default function VerticalIconBar({ dbId, databaseType, @@ -65,21 +85,7 @@ export default function VerticalIconBar({ }; // Database-specific panel items (only shown when dbId is provided) - const databasePanelItems: Array<{ icon: any; label: string; panel: PanelType }> = dbId - ? [ - { icon: Layers, label: "Data View", panel: "data" }, - { icon: Terminal, label: "SQL Workspace", panel: "sql-workspace" }, - { icon: Sparkles, label: "AI Workspace", panel: "ai-workspace" }, - { icon: Search, label: "Query Builder", panel: "query-builder" }, - { icon: GitBranch, label: "Schema Explorer", panel: "schema-explorer" }, - { icon: Database, label: "ER Diagram", panel: "er-diagram" }, - ...(supportsMonitoring(databaseType) - ? [{ icon: Activity, label: "Monitoring", panel: "monitoring" as PanelType }] - : []), - { icon: History, label: "Migrations", panel: "migrations" }, - { icon: GitCommitHorizontal, label: "Git Status", panel: "git-status" }, - ] - : []; + const databasePanelItems = dbId ? getDatabasePanelItems(databaseType) : []; return (