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
This commit is contained in:
Félix Malfait
2025-05-13 16:52:07 +02:00
committed by GitHub
parent cec381c4de
commit 3efdbed5d1

View File

@ -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: {