Skip to content

The open alternative to Salesforce, designed for AI.

Truncate ClickHouse tables on database reset (#26687)

martmullc52ef757c550
diff --git a/packages/twenty-server/project.json b/packages/twenty-server/project.json
index cf63e0ff763..d9297ec6e8c 100644
--- a/packages/twenty-server/project.json
+++ b/packages/twenty-server/project.json
@@ -264,6 +264,7 @@
"no-seed": {
"cwd": "packages/twenty-server",
"commands": [
+ "node dist/database/scripts/truncate-clickhouse.js",
"node dist/database/scripts/truncate-db.js",
"nx database:init",
"nx command-no-deps -- cache:flush"
@@ -273,6 +274,7 @@
"seed": {
"cwd": "packages/twenty-server",
"commands": [
+ "node dist/database/scripts/truncate-clickhouse.js",
"node dist/database/scripts/truncate-db.js",
"nx database:init",
"nx command-no-deps -- cache:flush",
diff --git a/packages/twenty-server/src/database/scripts/truncate-clickhouse.ts b/packages/twenty-server/src/database/scripts/truncate-clickhouse.ts
new file mode 100644
index 00000000000..0cd602fec4b
--- /dev/null
+++ b/packages/twenty-server/src/database/scripts/truncate-clickhouse.ts
@@ -0,0 +1,71 @@
+/* oxlint-disable no-console */
+import { ClickHouseLogLevel, createClient } from '@clickhouse/client';
+import { config } from 'dotenv';
+
+// Read before dotenv overrides it, so an explicit NODE_ENV=production from the caller is not masked by .env
+const callerNodeEnvironment = process.env.NODE_ENV;
+
+config({
+ path: process.env.NODE_ENV === 'test' ? '.env.test' : '.env',
+ override: true,
+});
+
+const ALLOWED_NODE_ENVIRONMENTS = ['development', 'test'];
+
+async function truncateClickHouseTables() {
+ const nodeEnvironment = callerNodeEnvironment ?? process.env.NODE_ENV;
+
+ if (
+ !nodeEnvironment ||
+ !ALLOWED_NODE_ENVIRONMENTS.includes(nodeEnvironment)
+ ) {
+ console.error(
+ `Refusing to truncate ClickHouse tables with NODE_ENV=${nodeEnvironment}, only allowed in development or test.`,
+ );
+ process.exit(1);
+ }
+
+ const clickHouseUrl = process.env.CLICKHOUSE_URL;
+
+ if (!clickHouseUrl) {
+ console.log('CLICKHOUSE_URL is not set, skipping ClickHouse truncation.');
+
+ return;
+ }
+
+ const client = createClient({
+ url: clickHouseUrl,
+ log: { level: ClickHouseLogLevel.OFF },
+ });
+
+ try {
+ const resultSet = await client.query({
+ query: `
+ SELECT name
+ FROM system.tables
+ WHERE database = currentDatabase()
+ AND name != '_migration'
+ AND engine NOT IN ('View', 'MaterializedView')
+ AND is_temporary = 0
+ `,
+ format: 'JSONEachRow',
+ });
+
+ const tables = await resultSet.json<{ name: string }>();
+
+ for (const { name } of tables) {
+ await client.command({
+ query: `TRUNCATE TABLE IF EXISTS \`${name}\``,
+ });
+ console.log(`Truncated ClickHouse table ${name}`);
+ }
+
+ console.log('All ClickHouse tables truncated successfully.');
+ } catch (err) {
+ console.error('Error during ClickHouse truncation:', err);
+ } finally {
+ await client.close();
+ }
+}
+
+void truncateClickHouseTables();