Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions load_queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import * as fs from "fs";
import * as path from "path";
import { createClient } from "@clickhouse/client";

const CLICKHOUSE_URL = process.env.CLICKHOUSE_URL;
const CLICKHOUSE_URL = process.env.CLICKHOUSE_URL || 'https://sql-clickhouse.clickhouse.com';
const CLICKHOUSE_USER = process.env.CLICKHOUSE_USER || 'default';
const CLICKHOUSE_PASSWORD = process.env.CLICKHOUSE_PASSWORD;

if (!CLICKHOUSE_URL || !CLICKHOUSE_USER || !CLICKHOUSE_PASSWORD) {
if (!CLICKHOUSE_URL || !CLICKHOUSE_USER) {
console.error("Environment variables CLICKHOUSE_URL, CLICKHOUSE_USER, and CLICKHOUSE_PASSWORD must be set");
process.exit(1);
}
Expand All @@ -16,7 +16,7 @@ console.log(`Running with CLICKHOUSE_URL: ${CLICKHOUSE_URL} and CLICKHOUSE_USER:
const client = createClient({
url: CLICKHOUSE_URL,
username: CLICKHOUSE_USER,
password: CLICKHOUSE_PASSWORD
password: CLICKHOUSE_PASSWORD,
});

interface Query {
Expand All @@ -32,18 +32,42 @@ interface Query {
params?: string
}

const loadQueries = async () => {
async function loadQueries(validate = false) {
const filePath = path.resolve(__dirname, "queries.json");

fs.readFile(filePath, "utf8", async (err, data) => {
if (err) {
console.error("Error reading file:", err);
return;
}

try {
const queries = JSON.parse(data) as { queries: Query[] };

if (validate) {
const queryErrors: string[] = [];
for (const query of queries.queries) {
if (query.query.startsWith("SHOW") || query.group == "food" || query.query.startsWith("EXPLAIN")) {
continue
}

try {
console.log(`Validating query: ${query.name}`);
await client.exec({ query: `EXPLAIN ${query.query}` });
} catch (error) {
const errorMessage = `Error validating query ${query.name}: ${error.message}`;
queryErrors.push(errorMessage);
console.error(errorMessage);
}
}

for (const e of queryErrors) {
console.error(e);
}

return;
}

// Create the temporary table
await client.exec({
query: `
Expand All @@ -68,15 +92,15 @@ const loadQueries = async () => {
// Insert data into the temporary table
let i = 0;
for (const query of queries.queries) {
const row:Query = {
const row: Query = {
name: query.name,
number: i,
slug: query.slug,
group: query.group,
query: query.comment ? `--${query.comment}\n${query.query}` : query.query,
chart: query.chart,
format: query.format ? true: false,
params: query.params ? query.params: "{}"
format: query.format ? true : false,
params: query.params ? query.params : "{}"
};
if (query.id) {
row.id = query.id;
Expand All @@ -86,7 +110,7 @@ const loadQueries = async () => {
values: [row],
format: 'JSONEachRow',
});
i+=1;
i += 1;
console.log(`Inserted query: ${query.name} into queries_temp`);
}

Expand All @@ -97,13 +121,14 @@ const loadQueries = async () => {
// Drop the temporary table
await client.exec({ query: `DROP TABLE default.queries_temp` });
console.log("Dropped temporary table queries_temp");

} catch (error: unknown) {
console.log(error)
console.log(error)
} finally {
await client.close();
}
});
};

loadQueries();
loadQueries(true);
// await loadQueries();