From 3efdbed5d14f040263ec3748ef1d686941454378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Malfait?= Date: Tue, 13 May 2025 16:52:07 +0200 Subject: [PATCH] Fix clickhouse connection 2 (#12010) Revert changes in #12006 as it might still be handy to have the DB auto-created (e.g. for test or self-hosting users), but if there is a permission exception we will just ignore it and assume the database exist in that case --- .../clickHouse/migrations/run-migrations.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packages/twenty-server/src/database/clickHouse/migrations/run-migrations.ts b/packages/twenty-server/src/database/clickHouse/migrations/run-migrations.ts index c57b48b2f..e467971b2 100644 --- a/packages/twenty-server/src/database/clickHouse/migrations/run-migrations.ts +++ b/packages/twenty-server/src/database/clickHouse/migrations/run-migrations.ts @@ -20,6 +20,23 @@ const clickHouseUrl = () => { ); }; +async function ensureDatabaseExists() { + const [url, database] = clickHouseUrl().split(/\/(?=[^/]*$)/); + const client = createClient({ + url, + }); + + try { + await client.command({ + query: `CREATE DATABASE IF NOT EXISTS "${database}"`, + }); + } catch (error) { + // It may fail due to permissions, but the database already exists + } finally { + await client.close(); + } +} + async function ensureMigrationTable(client: ClickHouseClient) { await client.command({ query: ` @@ -58,6 +75,8 @@ async function runMigrations() { const dir = path.join(__dirname); const files = fs.readdirSync(dir).filter((f) => f.endsWith('.sql')); + await ensureDatabaseExists(); + const client = createClient({ url: clickHouseUrl(), clickhouse_settings: {