Skip to content

Commit 82867c1

Browse files
authored
proper ch migrations (#978)
* proper ch migrations * tmp * removed * fixes
1 parent 7508712 commit 82867c1

File tree

11 files changed

+85
-60
lines changed

11 files changed

+85
-60
lines changed

frontend/instrumentation.ts

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -47,46 +47,23 @@ export async function register() {
4747

4848
const initializeClickHouse = async () => {
4949
try {
50-
const { clickhouseClient } = await import("@/lib/clickhouse/client.ts");
51-
const { readFileSync, readdirSync } = await import("fs");
50+
const { migration } = await import("clickhouse-migrations");
5251
const { join } = await import("path");
5352

54-
for (const file of readdirSync("lib/clickhouse/migrations")) {
55-
if (!file.endsWith(".sql")) {
56-
continue;
57-
}
58-
const schemaSql = readFileSync(join(process.cwd(), "lib/clickhouse/migrations", file), "utf-8");
59-
const statements = schemaSql
60-
.split(";")
61-
.map((s) => s.trim())
62-
.filter((s) => s.length > 0);
53+
const migrationsHome = join(process.cwd(), "lib/clickhouse/migrations");
6354

64-
for (const statement of statements) {
65-
try {
66-
await clickhouseClient.exec({ query: statement });
67-
} catch (error) {
68-
if ((error as { type: string }).type === "DUPLICATE_COLUMN") {
69-
console.warn(
70-
"[WARNING] Failed to apply ClickHouse statement:",
71-
statement,
72-
"because column already exists"
73-
);
74-
continue;
75-
} else if ((error as { type: string }).type === "TABLE_ALREADY_EXISTS") {
76-
console.warn(
77-
"[WARNING] Failed to apply ClickHouse statement:",
78-
statement,
79-
"because table already exists"
80-
);
81-
continue;
82-
} else {
83-
throw error;
84-
}
85-
}
86-
}
87-
}
55+
await migration(
56+
migrationsHome,
57+
process.env.CLICKHOUSE_URL || "http://localhost:8123",
58+
process.env.CLICKHOUSE_USER || "ch_user",
59+
process.env.CLICKHOUSE_PASSWORD || "ch_passwd",
60+
process.env.CLICKHOUSE_DB || "default",
61+
"ENGINE=Atomic", // db_engine
62+
String(Number(process.env.CH_MIGRATIONS_TIMEOUT) || 30000), // timeout as string
63+
);
8864
} catch (error) {
89-
console.error("Failed to apply ClickHouse schema:", error);
65+
console.error("Failed to apply ClickHouse migrations:", error);
66+
throw error;
9067
}
9168
};
9269
// Run Postgres migrations and data initialization

frontend/lib/clickhouse/migrations/0009-labels-to-tags.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.
File renamed without changes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
-- Migration: Rename labels table to tags and label_source column to source
2+
-- This migration is idempotent and handles the case where it's already been applied
3+
4+
-- Step 1: Create tags table if it doesn't exist (in case labels was already renamed)
5+
CREATE TABLE IF NOT EXISTS default.tags
6+
(
7+
`project_id` UUID,
8+
`class_id` UUID,
9+
`created_at` DateTime64(9, 'UTC'),
10+
`id` UUID,
11+
`name` String,
12+
`label_source` UInt8,
13+
`span_id` UUID
14+
)
15+
ENGINE = MergeTree
16+
PRIMARY KEY (project_id, class_id, span_id)
17+
ORDER BY (project_id, class_id, span_id, created_at, id)
18+
SETTINGS index_granularity = 8192;
19+
20+
-- Step 2: Copy data from labels to tags (only if labels exists and tags is empty)
21+
-- This will copy all data from labels if it exists
22+
-- On re-run, if labels doesn't exist, this will fail - but that's okay, it means data was already migrated
23+
INSERT INTO default.tags
24+
SELECT
25+
project_id,
26+
class_id,
27+
created_at,
28+
id,
29+
name,
30+
label_source,
31+
span_id
32+
FROM default.labels
33+
WHERE EXISTS (
34+
SELECT 1
35+
FROM system.tables
36+
WHERE database = 'default' AND name = 'labels'
37+
)
38+
AND NOT EXISTS (
39+
SELECT 1
40+
FROM default.tags
41+
LIMIT 1
42+
);
43+
44+
-- Step 3: Rename column (idempotent with IF EXISTS)
45+
ALTER TABLE default.tags RENAME COLUMN IF EXISTS label_source TO source;
46+
47+
-- Step 4: Drop the old labels table if it still exists
48+
DROP TABLE IF EXISTS default.labels;

frontend/lib/clickhouse/migrations/0010-eval-scores-span-tags.sql renamed to frontend/lib/clickhouse/migrations/3_eval-scores-span-tags.sql

File renamed without changes.
File renamed without changes.

frontend/lib/clickhouse/migrations/0012-trace-summaries.sql renamed to frontend/lib/clickhouse/migrations/5_trace-summaries.sql

File renamed without changes.

frontend/lib/clickhouse/migrations/0013-trace-summary-views.sql renamed to frontend/lib/clickhouse/migrations/6_trace-summary-views.sql

File renamed without changes.

frontend/lib/clickhouse/migrations/README.md

Lines changed: 0 additions & 18 deletions
This file was deleted.

frontend/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
"@ai-sdk/anthropic": "^2.0.13",
2121
"@ai-sdk/azure": "^2.0.26",
2222
"@ai-sdk/google": "^2.0.11",
23-
"@ai-sdk/openai": "^2.0.23",
2423
"@ai-sdk/groq": "^2.0.17",
2524
"@ai-sdk/mistral": "^2.0.13",
25+
"@ai-sdk/openai": "^2.0.23",
2626
"@ai-sdk/provider": "2.0.0",
2727
"@ai-sdk/react": "^2.0.29",
2828
"@aws-sdk/client-s3": "^3.864.0",
@@ -77,9 +77,10 @@
7777
"@uiw/codemirror-themes": "^4.25.1",
7878
"@uiw/react-codemirror": "^4.25.1",
7979
"@vercel/otel": "^1.13.0",
80-
"ai": "5.0.28",
8180
"@xyflow/react": "^12.8.3",
81+
"ai": "5.0.28",
8282
"class-variance-authority": "^0.7.1",
83+
"clickhouse-migrations": "^1.1.1",
8384
"clsx": "^2.1.1",
8485
"cmdk": "^1.1.1",
8586
"dagre": "^0.8.5",
@@ -125,11 +126,11 @@
125126
"shiki": "^3.12.2",
126127
"streamdown": "^1.2.0",
127128
"stripe": "^16.12.0",
128-
"use-stick-to-bottom": "^1.1.1",
129-
"zod": "^4.1.5",
130129
"swr": "^2.3.6",
130+
"use-stick-to-bottom": "^1.1.1",
131131
"uuid": "^9.0.1",
132132
"yaml": "^2.8.1",
133+
"zod": "^4.1.5",
133134
"zustand": "^4.5.7"
134135
},
135136
"devDependencies": {

0 commit comments

Comments
 (0)