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:
@ -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) {
|
async function ensureMigrationTable(client: ClickHouseClient) {
|
||||||
await client.command({
|
await client.command({
|
||||||
query: `
|
query: `
|
||||||
@ -58,6 +75,8 @@ async function runMigrations() {
|
|||||||
const dir = path.join(__dirname);
|
const dir = path.join(__dirname);
|
||||||
const files = fs.readdirSync(dir).filter((f) => f.endsWith('.sql'));
|
const files = fs.readdirSync(dir).filter((f) => f.endsWith('.sql'));
|
||||||
|
|
||||||
|
await ensureDatabaseExists();
|
||||||
|
|
||||||
const client = createClient({
|
const client = createClient({
|
||||||
url: clickHouseUrl(),
|
url: clickHouseUrl(),
|
||||||
clickhouse_settings: {
|
clickhouse_settings: {
|
||||||
|
|||||||
Reference in New Issue
Block a user